{
  "info": {
    "name": "Hanshop Partner API | TEST",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
    "description": "Uji Partner API Hanshop. Isi partner_api_key dan partner_api_secret di environment aktif. Jalankan cek saldo, katalog, pesanan, lalu cek status."
  },
  "auth": {
    "type": "noauth"
  },
  "event": [
    {
      "listen": "prerequest",
      "script": {
        "type": "text/javascript",
        "exec": [
          "const credential = (name) => String(pm.environment.get(name) || \"\").trim();",
          "const currentKey = credential(\"partner_api_key\");",
          "const currentSecret = credential(\"partner_api_secret\");",
          "const useCurrent = Boolean(currentKey || currentSecret);",
          "const apiKey = useCurrent ? currentKey : credential(\"hanshop_api_key\");",
          "const secret = useCurrent ? currentSecret : credential(\"hanshop_api_secret\");",
          "if (!apiKey.startsWith(\"hsp_test_\") || !secret) {",
          "  throw new Error(\"Isi partner_api_key (hsp_test_...) dan partner_api_secret di environment aktif.\");",
          "}",
          "const url = new URL(pm.variables.replaceIn(pm.request.url.toString()));",
          "if (url.origin !== \"https://api2.hansdigi.biz.id\" || !url.pathname.startsWith(\"/api/v1/\")) {",
          "  throw new Error(\"Collection ini hanya mengirim ke Partner API Hanshop.\");",
          "}",
          "const encode = (value) => encodeURIComponent(value)",
          "  .replace(/[!'()*]/g, (char) => \"%\" + char.charCodeAt(0).toString(16).toUpperCase());",
          "const path = url.pathname.split(\"/\").map(part => encode(decodeURIComponent(part))).join(\"/\");",
          "const pairs = Array.from(url.searchParams, ([key, value]) => [encode(key), encode(value)]);",
          "pairs.sort((a, b) => a[0].localeCompare(b[0]) || a[1].localeCompare(b[1]));",
          "const query = pairs.map(([key, value]) => key + \"=\" + value).join(\"&\");",
          "const target = query ? path + \"?\" + query : path;",
          "",
          "const method = pm.request.method.toUpperCase();",
          "let body = \"\";",
          "if (method === \"POST\") {",
          "  const externalId = pm.variables.get(\"external_id\");",
          "  const productCode = pm.variables.get(\"product_code\");",
          "  const destination = pm.variables.get(\"destination\");",
          "  if (!externalId || !productCode || productCode === \"ISI_KODE_PRODUK\" || !destination || destination === \"ISI_TUJUAN_UJI\") {",
          "    throw new Error(\"Isi external_id, product_code, dan destination pada Variables collection.\");",
          "  }",
          "  body = JSON.stringify({external_id: externalId, product_code: productCode, destination});",
          "  pm.variables.set(\"hanshop_request_body\", body);",
          "  pm.request.headers.upsert({key: \"Content-Type\", value: \"application/json\"});",
            "}",
          "",
          "const bytes = new TextEncoder();",
          "const hex = (value) => Array.from(new Uint8Array(value), byte => byte.toString(16).padStart(2, \"0\")).join(\"\");",
          "const timestamp = String(Math.floor(Date.now() / 1000));",
          "const nonce = hex(crypto.getRandomValues(new Uint8Array(16)));",
          "const bodyHash = hex(await crypto.subtle.digest(\"SHA-256\", bytes.encode(body)));",
          "const canonical = [method, target, bodyHash, timestamp, nonce].join(\"\\n\");",
          "const key = await crypto.subtle.importKey(\"raw\", bytes.encode(secret), {name:\"HMAC\", hash:\"SHA-256\"}, false, [\"sign\"]);",
          "const signature = hex(await crypto.subtle.sign(\"HMAC\", key, bytes.encode(canonical)));",
          "for (const [name, value] of Object.entries({",
          "  \"X-API-Key\": apiKey, \"X-Timestamp\": timestamp, \"X-Nonce\": nonce, \"X-Signature\": \"sha256=\" + signature,",
          "  \"Accept\": \"application/json\"",
          "})) pm.request.headers.upsert({key:name, value});"
        ]
      }
    }
  ],
  "variable": [
    {
      "key": "product_code",
      "value": "TEST-PREMIUM"
    },
    {
      "key": "destination",
      "value": "081234567001"
    },
    {
      "key": "external_id",
      "value": "TEST-POSTMAN-001"
    }
  ],
  "item": [
    {
      "name": "1. Cek saldo TEST",
      "request": {
        "method": "GET",
        "header": [],
        "url": "https://api2.hansdigi.biz.id/api/v1/balance",
        "description": "Perlu scope balance:read."
      }
    },
    {
      "name": "2. Lihat katalog",
      "request": {
        "method": "GET",
        "header": [],
        "url": "https://api2.hansdigi.biz.id/api/v1/products?limit=20",
        "description": "Perlu scope catalog:read. Di tab Params, tambahkan service_code=PULSA dan/atau category_code=AXIS untuk menyaring katalog. Pilih product_code yang AVAILABLE."
      }
    },
    {
      "name": "2b. Filter katalog",
      "request": {
        "method": "GET",
        "header": [],
        "url": "https://api2.hansdigi.biz.id/api/v1/products?limit=20&service_code=PULSA&category_code=AXIS",
        "description": "Ubah service_code atau category_code di tab Params. Keduanya opsional."
      }
    },
    {
      "name": "3. Buat pesanan TEST",
      "request": {
        "method": "POST",
        "header": [],
        "url": "https://api2.hansdigi.biz.id/api/v1/transactions",
        "description": "Perlu scope transaction:create. Ubah external_id untuk pesanan baru.",
        "body": {
          "mode": "raw",
          "raw": "{{hanshop_request_body}}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        }
      }
    },
    {
      "name": "4. Cek status pesanan",
      "request": {
        "method": "GET",
        "header": [],
        "url": "https://api2.hansdigi.biz.id/api/v1/transactions/external/{{external_id}}",
        "description": "Perlu scope transaction:read. Gunakan external_id yang sama dengan pesanan."
      }
    }
  ]
}
