{
  "info": {
    "name": "Payment Link QA",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
  },
  "variable": [
    { "key": "baseUrl", "value": "https://be-midtrans-cifo.winteraccess.id" },
    { "key": "paymentLinkBase", "value": "https://midtrans-cifo.winteraccess.id/pay" },
    { "key": "externalApiKey", "value": "" },
    { "key": "token", "value": "" },
    { "key": "order_id", "value": "" }
  ],
  "item": [
    {
      "name": "1) Create Transaction",
      "request": {
        "method": "POST",
        "header": [
          { "key": "Content-Type", "value": "application/json" },
          { "key": "X-API-KEY", "value": "{{externalApiKey}}" }
        ],
        "url": { "raw": "{{baseUrl}}/createtransaksi", "host": ["{{baseUrl}}"], "path": ["createtransaksi"] },
        "body": {
          "mode": "raw",
          "raw": "{\n  \"mercant_id\": \"REFNO-001\",\n  \"timestamp\": 1731300000000,\n  \"deskripsi\": \"Bayar Internet\",\n  \"nominal\": 200000,\n  \"nama\": \"Demo User\",\n  \"no_telepon\": \"081234567890\",\n  \"email\": \"demo@example.com\",\n  \"item\": [\n    { \"item_id\": \"TKG-2511101\", \"nama\": \"Internet\", \"harga\": 200000, \"qty\": 1 }\n  ]\n}"
        }
      },
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "let res = {};",
              "try { res = pm.response.json(); } catch(e) { res = {}; }",
              "const url = (res && res.data && res.data.url) ? res.data.url : (res && res.url ? res.url : '');",
              "if (url) {",
              "  pm.collectionVariables.set('paymentLinkUrl', url);",
              "  const trimmed = url.replace(/\\\/$/, '');",
              "  const parts = trimmed.split('/');",
              "  const tok = parts[parts.length - 1];",
              "  pm.collectionVariables.set('token', tok);",
              "}",
              "pm.test('Create Transaction returns data.url and token', function () {",
              "  pm.expect(url, 'data.url exists').to.be.a('string');",
              "});"
            ]
          }
        }
      ],
      "response": []
    },
    {
      "name": "2) Resolve Token",
      "request": {
        "method": "GET",
        "header": [],
        "url": { "raw": "{{baseUrl}}/api/payment-links/{{token}}", "host": ["{{baseUrl}}"], "path": ["api","payment-links","{{token}}"] }
      },
      "event": [
        {
          "listen": "prerequest",
          "script": {
            "type": "text/javascript",
            "exec": [
              "const t = pm.variables.get('token');",
              "if (!t) { throw new Error(\"Missing token. Run '1) Create Transaction' first.\"); }"
            ]
          }
        },
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "let res = {};",
              "try { res = pm.response.json(); } catch(e) { res = {}; }",
              "if (res && res.order_id) { pm.collectionVariables.set('order_id', res.order_id); }",
              "pm.test('Resolve Token payload has order_id and nominal', function () {",
              "  pm.expect(res.order_id, 'order_id').to.be.a('string');",
              "  pm.expect(res.nominal, 'nominal').to.exist;",
              "});"
            ]
          }
        }
      ],
      "response": []
    },
    {
      "name": "3) Charge - Bank Transfer (BCA)",
      "request": {
        "method": "POST",
        "header": [ { "key": "Content-Type", "value": "application/json" } ],
        "url": { "raw": "{{baseUrl}}/api/payments/charge", "host": ["{{baseUrl}}"], "path": ["api","payments","charge"] },
        "body": {
          "mode": "raw",
          "raw": "{\n  \"payment_type\": \"bank_transfer\",\n  \"transaction_details\": { \"order_id\": \"{{order_id}}\", \"gross_amount\": 150000 },\n  \"bank_transfer\": { \"bank\": \"bca\" }\n}"
        }
      },
      "response": []
    },
    {
      "name": "3) Charge - CStore (Indomaret)",
      "request": {
        "method": "POST",
        "header": [ { "key": "Content-Type", "value": "application/json" } ],
        "url": { "raw": "{{baseUrl}}/api/payments/charge", "host": ["{{baseUrl}}"], "path": ["api","payments","charge"] },
        "body": {
          "mode": "raw",
          "raw": "{\n  \"payment_type\": \"cstore\",\n  \"transaction_details\": { \"order_id\": \"{{order_id}}\", \"gross_amount\": 150000 },\n  \"cstore\": { \"store\": \"indomaret\" }\n}"
        }
      },
      "response": []
    },
    {
      "name": "4) Payment Status",
      "request": {
        "method": "GET",
        "header": [],
        "url": { "raw": "{{baseUrl}}/api/payments/{{order_id}}/status", "host": ["{{baseUrl}}"], "path": ["api","payments","{{order_id}}","status"] }
      },
      "event": [
        {
          "listen": "prerequest",
          "script": {
            "type": "text/javascript",
            "exec": [
              "const oid = pm.variables.get('order_id');",
              "if (!oid) { throw new Error(\"Missing order_id. Run '1) Create Transaction' first.\"); }"
            ]
          }
        }
      ],
      "response": []
    }
  ]
}