{
  "info": {
    "name": "TopupRelay Partner API",
    "description": "Ready-to-run requests for the TopupRelay partner API. Fill in client_key and api_secret in the environment, then run requests 1-4 in order, no code required. Every request signs itself automatically using the collection's Pre-request Script below.",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
  },
  "variable": [
    {
      "key": "base_url",
      "value": "http://localhost:8000"
    },
    {
      "key": "client_key",
      "value": ""
    },
    {
      "key": "api_secret",
      "value": ""
    },
    {
      "key": "access_token",
      "value": ""
    },
    {
      "key": "refresh_token",
      "value": ""
    }
  ],
  "event": [
    {
      "listen": "prerequest",
      "script": {
        "type": "text/javascript",
        "exec": [
          "// Signs every request in this collection the same way, so you never have to write this by hand.",
          "const timestamp = Math.floor(Date.now() / 1000).toString();",
          "const method = pm.request.method.toUpperCase();",
          "const rawPath = pm.request.url.getPath();",
          "const path = '/' + rawPath.replace(/^\\//, '');",
          "const body = (pm.request.body && pm.request.body.mode === 'raw') ? pm.request.body.raw : '';",
          "const bodyHash = CryptoJS.SHA256(body).toString(CryptoJS.enc.Hex);",
          "",
          "const clientKey = pm.variables.get('client_key');",
          "const apiSecret = pm.variables.get('api_secret');",
          "",
          "if (!clientKey || !apiSecret) {",
          "    console.warn('TopupRelay: client_key / api_secret are not set. Open the environment (top right dropdown) and fill them in.');",
          "}",
          "",
          "const signingString = [clientKey, timestamp, method, path, bodyHash].join('|');",
          "const signature = CryptoJS.HmacSHA256(signingString, apiSecret).toString(CryptoJS.enc.Hex);",
          "",
          "pm.request.headers.upsert({ key: 'X-Client-Key', value: clientKey });",
          "pm.request.headers.upsert({ key: 'X-Timestamp', value: timestamp });",
          "pm.request.headers.upsert({ key: 'X-Signature', value: signature });",
          "",
          "const accessToken = pm.variables.get('access_token');",
          "if (accessToken) {",
          "    pm.request.headers.upsert({ key: 'Authorization', value: 'Bearer ' + accessToken });",
          "}"
        ]
      }
    }
  ],
  "item": [
    {
      "name": "1. Get an access token",
      "request": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": ""
        },
        "url": {
          "raw": "{{base_url}}/api/v1/partner/auth/token",
          "host": [
            "{{base_url}}"
          ],
          "path": [
            "api",
            "v1",
            "partner",
            "auth",
            "token"
          ]
        },
        "description": "Run this first. On success, the script below saves the token into this collection's variables automatically, every other request picks it up on its own."
      },
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test('Got a token (status 200)', function () {",
              "    pm.response.to.have.status(200);",
              "});",
              "",
              "if (pm.response.code === 200) {",
              "    const body = pm.response.json();",
              "    pm.environment.set('access_token', body.data.tokens.access_token);",
              "    pm.environment.set('refresh_token', body.data.tokens.refresh_token);",
              "    console.log('Saved an access token, valid for ' + body.data.tokens.expires_in + ' seconds. Run request 2 next.');",
              "} else {",
              "    console.log('Did not get a token. Response: ' + pm.response.text());",
              "    console.log('Most common cause: client_key or api_secret in the environment is wrong, or your system clock is off by more than 5 minutes.');",
              "}"
            ]
          }
        }
      ]
    },
    {
      "name": "2. Check wallet balance",
      "request": {
        "method": "GET",
        "header": [],
        "url": {
          "raw": "{{base_url}}/api/v1/partner/wallet",
          "host": [
            "{{base_url}}"
          ],
          "path": [
            "api",
            "v1",
            "partner",
            "wallet"
          ]
        },
        "description": "Your partner wallet's current balance. If this is your first time, it will be 0, fund it from the TopupRelay dashboard before trying a real purchase."
      },
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test('Got the wallet (status 200)', function () {",
              "    pm.response.to.have.status(200);",
              "});",
              "if (pm.response.code === 401) {",
              "    console.log('401 means your access token is missing or expired, run request 1 again.');",
              "}"
            ]
          }
        }
      ]
    },
    {
      "name": "3. Buy airtime",
      "request": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"network\": \"mtn\",\n  \"phone\": \"08011112222\",\n  \"amount\": \"200.00\"\n}"
        },
        "url": {
          "raw": "{{base_url}}/api/v1/partner/vtu/airtime",
          "host": [
            "{{base_url}}"
          ],
          "path": [
            "api",
            "v1",
            "partner",
            "vtu",
            "airtime"
          ]
        },
        "description": "Edit the JSON body above to change network (mtn, airtel, glo, 9mobile), phone, or amount. In test mode this is served by a mock, no real airtime moves and no real money is spent."
      },
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test('Purchase accepted (status 201)', function () {",
              "    pm.response.to.have.status(201);",
              "});",
              "if (pm.response.code === 402) {",
              "    console.log('402 INSUFFICIENT_BALANCE: fund your partner wallet first, nothing was charged.');",
              "}"
            ]
          }
        }
      ]
    },
    {
      "name": "4. List recent purchases",
      "request": {
        "method": "GET",
        "header": [],
        "url": {
          "raw": "{{base_url}}/api/v1/partner/vtu/orders?limit=10",
          "host": [
            "{{base_url}}"
          ],
          "path": [
            "api",
            "v1",
            "partner",
            "vtu",
            "orders"
          ],
          "query": [
            {
              "key": "limit",
              "value": "10"
            }
          ]
        },
        "description": "Confirms request 3 actually went through, and shows its status (success/failed)."
      },
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test('Got the order list (status 200)', function () {",
              "    pm.response.to.have.status(200);",
              "});"
            ]
          }
        }
      ]
    }
  ]
}