{
  "info": {
    "name": "PartnerGateway API",
    "description": "The partner-facing PartnerGateway API. Set the collection variables baseUrl, appId and appKey (use a Sandbox credential first). Requests authenticate with HTTP Basic (AppId:AppKey). Money-out requests (withdraw, crypto sell) are signed automatically by a pre-request script: X-Signature = lowercase hex HMAC-SHA256(AppKey, \"{X-Timestamp}.{raw body}\"). In Sandbox, only the test numbers from GET /api/v1/sandbox/test-scenarios are accepted.",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
  },
  "auth": {
    "type": "basic",
    "basic": [
      {
        "key": "username",
        "value": "{{appId}}",
        "type": "string"
      },
      {
        "key": "password",
        "value": "{{appKey}}",
        "type": "string"
      }
    ]
  },
  "variable": [
    {
      "key": "baseUrl",
      "value": "https://gateway.example.com"
    },
    {
      "key": "appId",
      "value": ""
    },
    {
      "key": "appKey",
      "value": ""
    },
    {
      "key": "testMsisdn",
      "value": "+237670000001"
    },
    {
      "key": "lastOrderId",
      "value": ""
    }
  ],
  "event": [
    {
      "listen": "prerequest",
      "script": {
        "type": "text/javascript",
        "exec": [
          "// Fresh order ids per send, so replays don't hit idempotency by accident.",
          "pm.variables.set('orderId', 'postman-' + Date.now());"
        ]
      }
    },
    {
      "listen": "test",
      "script": {
        "type": "text/javascript",
        "exec": [
          "// Remember the order id each create request used, for the status and replay requests.",
          "const raw = pm.request.body && pm.request.body.raw;",
          "if (raw && raw.includes(\"partnerOrderId\")) {",
          "  try { pm.collectionVariables.set(\"lastOrderId\", JSON.parse(pm.variables.replaceIn(raw)).partnerOrderId); } catch (e) {}",
          "}"
        ]
      }
    }
  ],
  "item": [
    {
      "name": "Start here",
      "item": [
        {
          "name": "List services",
          "request": {
            "method": "GET",
            "url": "{{baseUrl}}/api/v1/services"
          }
        },
        {
          "name": "Sandbox test numbers",
          "request": {
            "method": "GET",
            "url": "{{baseUrl}}/api/v1/sandbox/test-scenarios"
          }
        }
      ]
    },
    {
      "name": "Mobile Money",
      "item": [
        {
          "name": "Deposit (collect from a customer)",
          "request": {
            "method": "POST",
            "header": [
              {
                "key": "Content-Type",
                "value": "application/json"
              }
            ],
            "url": "{{baseUrl}}/api/v1/momo/deposit",
            "body": {
              "mode": "raw",
              "raw": "{\"partnerOrderId\":\"{{orderId}}\",\"msisdn\":\"{{testMsisdn}}\",\"amount\":1000,\"currency\":\"XAF\"}"
            }
          }
        },
        {
          "name": "Withdraw (pay a customer) - signed",
          "event": [
            {
              "listen": "prerequest",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "// Sign the exact bytes Postman will send (variables already substituted).",
                  "const body = pm.variables.replaceIn(pm.request.body.raw);",
                  "const timestamp = new Date().toISOString();",
                  "const signature = CryptoJS.HmacSHA256(timestamp + '.' + body, pm.variables.get('appKey')).toString(CryptoJS.enc.Hex);",
                  "pm.request.body.raw = body;",
                  "pm.request.headers.upsert({ key: 'X-Timestamp', value: timestamp });",
                  "pm.request.headers.upsert({ key: 'X-Signature', value: signature });"
                ]
              }
            }
          ],
          "request": {
            "method": "POST",
            "header": [
              {
                "key": "Content-Type",
                "value": "application/json"
              }
            ],
            "url": "{{baseUrl}}/api/v1/momo/withdraw",
            "body": {
              "mode": "raw",
              "raw": "{\"partnerOrderId\":\"{{orderId}}\",\"msisdn\":\"{{testMsisdn}}\",\"amount\":500,\"currency\":\"XAF\"}"
            }
          }
        },
        {
          "name": "KYC lookup",
          "request": {
            "method": "GET",
            "url": "{{baseUrl}}/api/v1/kyc/momo/{{testMsisdn}}"
          }
        }
      ]
    },
    {
      "name": "Crypto",
      "item": [
        {
          "name": "Quote",
          "request": {
            "method": "GET",
            "url": {
              "raw": "{{baseUrl}}/api/v1/crypto/quote?IsBuy=true&Amount=10000&TokenAddress=&Currency=XAF",
              "host": [
                "{{baseUrl}}"
              ],
              "path": [
                "api",
                "v1",
                "crypto",
                "quote"
              ],
              "query": [
                {
                  "key": "IsBuy",
                  "value": "true"
                },
                {
                  "key": "Amount",
                  "value": "10000"
                },
                {
                  "key": "TokenAddress",
                  "value": ""
                },
                {
                  "key": "Currency",
                  "value": "XAF"
                }
              ]
            }
          }
        },
        {
          "name": "Buy",
          "request": {
            "method": "POST",
            "header": [
              {
                "key": "Content-Type",
                "value": "application/json"
              }
            ],
            "url": "{{baseUrl}}/api/v1/crypto/buy",
            "body": {
              "mode": "raw",
              "raw": "{\"partnerOrderId\":\"{{orderId}}\",\"phoneNumber\":\"{{testMsisdn}}\",\"tokenAddress\":\"\",\"userAddress\":\"\",\"amountIn\":10000,\"amountOut\":0,\"avgPrice\":0,\"totalLocal\":10000,\"totalUsd\":0,\"currency\":\"XAF\"}"
            }
          }
        },
        {
          "name": "Sell - signed",
          "event": [
            {
              "listen": "prerequest",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "const body = pm.variables.replaceIn(pm.request.body.raw);",
                  "const timestamp = new Date().toISOString();",
                  "const signature = CryptoJS.HmacSHA256(timestamp + '.' + body, pm.variables.get('appKey')).toString(CryptoJS.enc.Hex);",
                  "pm.request.body.raw = body;",
                  "pm.request.headers.upsert({ key: 'X-Timestamp', value: timestamp });",
                  "pm.request.headers.upsert({ key: 'X-Signature', value: signature });"
                ]
              }
            }
          ],
          "request": {
            "method": "POST",
            "header": [
              {
                "key": "Content-Type",
                "value": "application/json"
              }
            ],
            "url": "{{baseUrl}}/api/v1/crypto/sell",
            "body": {
              "mode": "raw",
              "raw": "{\"partnerOrderId\":\"{{orderId}}\",\"phoneNumber\":\"{{testMsisdn}}\",\"tokenAddress\":\"\",\"userAddress\":\"\",\"amountIn\":0,\"amountOut\":5000,\"avgPrice\":0,\"totalLocal\":5000,\"totalUsd\":0,\"currency\":\"XAF\"}"
            }
          }
        }
      ]
    },
    {
      "name": "Status & ledger",
      "item": [
        {
          "name": "Transaction status by your order id",
          "request": {
            "method": "GET",
            "url": "{{baseUrl}}/api/v1/transactions/{{lastOrderId}}"
          }
        },
        {
          "name": "Ledger balance",
          "request": {
            "method": "GET",
            "url": "{{baseUrl}}/api/v1/ledger/balance"
          }
        },
        {
          "name": "Ledger history",
          "request": {
            "method": "GET",
            "url": {
              "raw": "{{baseUrl}}/api/v1/ledger/history?page=1&pageSize=50",
              "host": [
                "{{baseUrl}}"
              ],
              "path": [
                "api",
                "v1",
                "ledger",
                "history"
              ],
              "query": [
                {
                  "key": "page",
                  "value": "1"
                },
                {
                  "key": "pageSize",
                  "value": "50"
                }
              ]
            }
          }
        }
      ]
    },
    {
      "name": "Sandbox",
      "item": [
        {
          "name": "Replay a callback",
          "request": {
            "method": "POST",
            "url": "{{baseUrl}}/api/v1/sandbox/callbacks/{{lastOrderId}}/trigger"
          }
        }
      ]
    }
  ]
}
