{
  "info": {
    "name": "SPIDI · API Productos (simulador)",
    "description": "Flujo verificado contra el simulador hosteado de SPIDI: registro, acuerdo, sesion de Boton, forzar desenlace (plano de control) y consultar estado. Ajusta la variable base_url para tu entorno.",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
  },
  "variable": [
    { "key": "base_url", "value": "https://sim-productos.abiertolab.com", "type": "string" },
    { "key": "token", "value": "", "type": "string" },
    { "key": "agreement_id", "value": "", "type": "string" },
    { "key": "session_id", "value": "", "type": "string" }
  ],
  "item": [
    {
      "name": "0 · Registro (cuenta de prueba)",
      "event": [{ "listen": "test", "script": { "type": "text/javascript", "exec": [
        "const b = pm.response.json();",
        "if (b.token) pm.collectionVariables.set('token', b.token);",
        "pm.test('201 con token', () => pm.expect(b.token).to.be.a('string'));"
      ] } }],
      "request": {
        "method": "POST",
        "header": [{ "key": "Content-Type", "value": "application/json" }],
        "url": { "raw": "{{base_url}}/register", "host": ["{{base_url}}"], "path": ["register"] },
        "body": { "mode": "raw", "raw": "{\n  \"short_name\": \"mi-tienda\",\n  \"password\": \"demo\"\n}" }
      }
    },
    {
      "name": "1 · Crear acuerdo de pago",
      "event": [{ "listen": "test", "script": { "type": "text/javascript", "exec": [
        "const b = pm.response.json();",
        "if (b.agreement_id) pm.collectionVariables.set('agreement_id', b.agreement_id);"
      ] } }],
      "request": {
        "method": "POST",
        "header": [
          { "key": "Authorization", "value": "Bearer {{token}}" },
          { "key": "Content-Type", "value": "application/json" }
        ],
        "url": { "raw": "{{base_url}}/api/v1/ext/agreements", "host": ["{{base_url}}"], "path": ["api", "v1", "ext", "agreements"] },
        "body": { "mode": "raw", "raw": "{\n  \"title\": \"Acuerdo sin Split\",\n  \"description\": \"Sin distribucion de fondos\",\n  \"split\": false,\n  \"payment_methods\": { \"immediate_debit\": true, \"crypto\": false, \"mobile_payment\": true },\n  \"default_bank_account_id\": \"uuid_sofitasa_001\",\n  \"rules\": [{ \"origin_bank_code\": \"0105\", \"destination_bank_account_id\": \"uuid_mercantil_007\" }]\n}" }
      }
    },
    {
      "name": "2 · Crear sesion de Boton",
      "event": [{ "listen": "test", "script": { "type": "text/javascript", "exec": [
        "const b = pm.response.json();",
        "if (b.data && b.data.id) pm.collectionVariables.set('session_id', b.data.id);",
        "pm.test('201 + payment_url', () => pm.expect(b.payment_url).to.include('/pay/'));"
      ] } }],
      "request": {
        "method": "POST",
        "header": [
          { "key": "Authorization", "value": "Bearer {{token}}" },
          { "key": "Content-Type", "value": "application/json" }
        ],
        "url": { "raw": "{{base_url}}/api/v1/ext/payment-sessions/buttons", "host": ["{{base_url}}"], "path": ["api", "v1", "ext", "payment-sessions", "buttons"] },
        "body": { "mode": "raw", "raw": "{\n  \"amount_reference\": 50,\n  \"currency_reference\": \"USD\",\n  \"identifier_label\": \"Pedido\",\n  \"identifier\": \"ORD-0001\",\n  \"description\": \"Mi primer pago de prueba\",\n  \"success_url\": \"https://miapp.com/pago-exitoso\",\n  \"failure_url\": \"https://miapp.com/pago-fallido\",\n  \"agreement_id\": \"{{agreement_id}}\"\n}" }
      }
    },
    {
      "name": "3 · Forzar desenlace (plano de control)",
      "request": {
        "method": "POST",
        "header": [
          { "key": "Authorization", "value": "Bearer {{token}}" },
          { "key": "Content-Type", "value": "application/json" }
        ],
        "url": { "raw": "{{base_url}}/control/sessions/{{session_id}}/outcome", "host": ["{{base_url}}"], "path": ["control", "sessions", "{{session_id}}", "outcome"] },
        "body": { "mode": "raw", "raw": "{\n  \"leg\": 1,\n  \"outcome\": \"paid\"\n}" }
      }
    },
    {
      "name": "4 · Consultar estado",
      "request": {
        "method": "GET",
        "header": [{ "key": "Authorization", "value": "Bearer {{token}}" }],
        "url": { "raw": "{{base_url}}/api/v1/ext/payment-sessions/status/{{session_id}}", "host": ["{{base_url}}"], "path": ["api", "v1", "ext", "payment-sessions", "status", "{{session_id}}"] }
      }
    }
  ]
}
