{
  "info": {
    "name": "Firux Pay: Conectar con Firux Pay",
    "description": "Para plataformas: el permiso del comercio (OAuth 2.0 con PKCE), el acceso, la cuenta, un cobro y su aviso de prueba. Llena client_id, client_secret y redirect_uri. Para el code: abre en el navegador https://firux.co/oauth/authorize con tu code_challenge (S256), da el permiso con tu cuenta de prueba y copia el code de la vuelta junto a tu code_verifier. Guía completa: https://developers.firux.co/#plataformas",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
  },
  "variable": [
    { "key": "api", "value": "https://api.firux.co/v1" },
    { "key": "client_id", "value": "" },
    { "key": "client_secret", "value": "" },
    { "key": "redirect_uri", "value": "https://tu-plataforma.com/firux/vuelta" },
    { "key": "code", "value": "" },
    { "key": "code_verifier", "value": "" },
    { "key": "access_token", "value": "" },
    { "key": "refresh_token", "value": "" },
    { "key": "charge_id", "value": "" }
  ],
  "item": [
    {
      "name": "1. Cambiar el código por el acceso",
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "var r = pm.response.json();",
              "if (r.access_token) {",
              "  pm.collectionVariables.set('access_token', r.access_token);",
              "  pm.collectionVariables.set('refresh_token', r.refresh_token);",
              "}"
            ]
          }
        }
      ],
      "request": {
        "method": "POST",
        "auth": {
          "type": "basic",
          "basic": [
            { "key": "username", "value": "{{client_id}}", "type": "string" },
            { "key": "password", "value": "{{client_secret}}", "type": "string" }
          ]
        },
        "body": {
          "mode": "urlencoded",
          "urlencoded": [
            { "key": "grant_type", "value": "authorization_code" },
            { "key": "code", "value": "{{code}}" },
            { "key": "redirect_uri", "value": "{{redirect_uri}}" },
            { "key": "code_verifier", "value": "{{code_verifier}}" }
          ]
        },
        "url": "{{api}}/oauth/token",
        "description": "El código sirve una sola vez y vence a los 10 minutos."
      }
    },
    {
      "name": "2. La cuenta del comercio",
      "request": {
        "method": "GET",
        "auth": { "type": "bearer", "bearer": [{ "key": "token", "value": "{{access_token}}", "type": "string" }] },
        "url": "{{api}}/account",
        "description": "Permiso account:read."
      }
    },
    {
      "name": "3. Crear un cobro",
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "var r = pm.response.json();",
              "if (r.id) { pm.collectionVariables.set('charge_id', r.id); }"
            ]
          }
        }
      ],
      "request": {
        "method": "POST",
        "auth": { "type": "bearer", "bearer": [{ "key": "token", "value": "{{access_token}}", "type": "string" }] },
        "header": [
          { "key": "Idempotency-Key", "value": "{{$guid}}" },
          { "key": "Content-Type", "value": "application/json" }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"amount\": 50000,\n  \"external_reference\": \"PEDIDO-1001\",\n  \"description\": \"Pedido 1001\"\n}"
        },
        "url": "{{api}}/charges",
        "description": "Permiso charges:write. Montos en pesos enteros."
      }
    },
    {
      "name": "4. Consultar el cobro",
      "request": {
        "method": "GET",
        "auth": { "type": "bearer", "bearer": [{ "key": "token", "value": "{{access_token}}", "type": "string" }] },
        "url": "{{api}}/charges/{{charge_id}}",
        "description": "Permiso charges:read."
      }
    },
    {
      "name": "5. Simular el pago (solo prueba)",
      "request": {
        "method": "POST",
        "auth": { "type": "bearer", "bearer": [{ "key": "token", "value": "{{access_token}}", "type": "string" }] },
        "url": "{{api}}/charges/{{charge_id}}/simulate_payment",
        "description": "Con un acceso firux_oat_test_...: manda charge.paid firmado a la URL de avisos de tu aplicación, sin mover dinero real."
      }
    },
    {
      "name": "6. Renovar el acceso",
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "var r = pm.response.json();",
              "if (r.access_token) {",
              "  pm.collectionVariables.set('access_token', r.access_token);",
              "  pm.collectionVariables.set('refresh_token', r.refresh_token);",
              "}"
            ]
          }
        }
      ],
      "request": {
        "method": "POST",
        "auth": {
          "type": "basic",
          "basic": [
            { "key": "username", "value": "{{client_id}}", "type": "string" },
            { "key": "password", "value": "{{client_secret}}", "type": "string" }
          ]
        },
        "body": {
          "mode": "urlencoded",
          "urlencoded": [
            { "key": "grant_type", "value": "refresh_token" },
            { "key": "refresh_token", "value": "{{refresh_token}}" }
          ]
        },
        "url": "{{api}}/oauth/token",
        "description": "El refresh_token nuevo reemplaza al anterior: si se vuelve a usar uno viejo, se revoca la conexión."
      }
    },
    {
      "name": "7. Revocar la conexión",
      "request": {
        "method": "POST",
        "auth": {
          "type": "basic",
          "basic": [
            { "key": "username", "value": "{{client_id}}", "type": "string" },
            { "key": "password", "value": "{{client_secret}}", "type": "string" }
          ]
        },
        "body": {
          "mode": "urlencoded",
          "urlencoded": [{ "key": "token", "value": "{{refresh_token}}" }]
        },
        "url": "{{api}}/oauth/revoke",
        "description": "Deja la conexión desconectada y llega connection.revoked. No toca cobros pagados ni saldo."
      }
    }
  ]
}
