{
  "info": {
    "_postman_id": "9feee6b0-4bb4-4d50-a77c-f3c85ea00001",
    "name": "Calypso Static Invoice Quick Start",
    "description": "Ready-to-use Calypso Public API collection for creating and checking a SINGLE static invoice. Fill the Calypso Static Invoice environment variables before running requests.",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
  },
  "auth": {
    "type": "noauth"
  },
  "event": [
    {
      "listen": "prerequest",
      "script": {
        "type": "text/javascript",
        "exec": [
          "const secretKey = pm.environment.get('secretApiKey');",
          "const publicKey = pm.environment.get('publicApiKey');",
          "",
          "if (!publicKey || publicKey.includes('YOUR_')) {",
          "  throw new Error('Set publicApiKey in the selected Postman environment.');",
          "}",
          "",
          "if (!secretKey || secretKey.includes('YOUR_')) {",
          "  throw new Error('Set secretApiKey in the selected Postman environment.');",
          "}",
          "",
          "pm.environment.set('timestamp', Date.now().toString());",
          "",
          "if (!pm.request.body || !pm.request.body.raw) {",
          "  return;",
          "}",
          "",
          "const rawBody = pm.variables.replaceIn(pm.request.body.raw);",
          "const sign = CryptoJS.HmacSHA512(rawBody, secretKey).toString(CryptoJS.enc.Hex);",
          "",
          "pm.request.headers.upsert({ key: 'Key', value: publicKey });",
          "pm.request.headers.upsert({ key: 'Sign', value: sign });",
          "pm.request.headers.upsert({ key: 'Content-Type', value: 'application/json' });"
        ]
      }
    }
  ],
  "variable": [
    {
      "key": "baseUrl",
      "value": "https://api.calypso.money"
    }
  ],
  "item": [
    {
      "name": "01. Setup",
      "item": [
        {
          "name": "Get current user",
          "event": [
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "pm.test('Response is successful', function () {",
                  "  pm.expect(pm.response.code).to.be.oneOf([200]);",
                  "});",
                  "",
                  "const json = pm.response.json();",
                  "const text = JSON.stringify(json);",
                  "const accountMatch = text.match(/0x[a-fA-F0-9]{40}/);",
                  "",
                  "if (accountMatch && !pm.environment.get('account')) {",
                  "  pm.environment.set('account', accountMatch[0]);",
                  "  console.log('Saved account from response:', accountMatch[0]);",
                  "}",
                  "",
                  "pm.test('Account is available or manually configured', function () {",
                  "  pm.expect(pm.environment.get('account') || accountMatch).to.exist;",
                  "});"
                ]
              }
            }
          ],
          "request": {
            "method": "POST",
            "header": [],
            "body": {
              "mode": "raw",
              "raw": "{\n  \"timestamp\": {{timestamp}}\n}",
              "options": {
                "raw": {
                  "language": "json"
                }
              }
            },
            "url": {
              "raw": "{{baseUrl}}/api/v1/user/current",
              "host": [
                "{{baseUrl}}"
              ],
              "path": [
                "api",
                "v1",
                "user",
                "current"
              ]
            },
            "description": "Checks that the API keys are valid. If an account ID is found in the response and the environment account is empty, the script saves it automatically."
          }
        },
        {
          "name": "Get available currencies",
          "event": [
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "pm.test('Response is successful', function () {",
                  "  pm.expect(pm.response.code).to.be.oneOf([200]);",
                  "});",
                  "",
                  "const text = pm.response.text();",
                  "pm.test('Response contains configured currency or currency list is returned', function () {",
                  "  const currency = pm.environment.get('currency');",
                  "  pm.expect(text.length).to.be.greaterThan(2);",
                  "  if (currency) {",
                  "    console.log('Configured invoice currency:', currency);",
                  "  }",
                  "});"
                ]
              }
            }
          ],
          "request": {
            "method": "POST",
            "header": [],
            "body": {
              "mode": "raw",
              "raw": "{\n  \"timestamp\": {{timestamp}}\n}",
              "options": {
                "raw": {
                  "language": "json"
                }
              }
            },
            "url": {
              "raw": "{{baseUrl}}/api/v1/currency",
              "host": [
                "{{baseUrl}}"
              ],
              "path": [
                "api",
                "v1",
                "currency"
              ]
            },
            "description": "Returns currencies available in Calypso Public API. Use a currency supported for the target merchant account."
          }
        }
      ]
    },
    {
      "name": "02. Static invoice",
      "item": [
        {
          "name": "Create SINGLE invoice",
          "event": [
            {
              "listen": "prerequest",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "if (!pm.environment.get('account') || pm.environment.get('account').includes('YOUR_')) {",
                  "  throw new Error('Set account in the selected Postman environment.');",
                  "}",
                  "",
                  "pm.environment.set('invoiceIdempotencyKey', pm.variables.replaceIn('{{$guid}}'));",
                  "",
                  "if (!pm.environment.get('externalId')) {",
                  "  pm.environment.set('externalId', 'order-' + Date.now());",
                  "}"
                ]
              }
            },
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "pm.test('Invoice is created', function () {",
                  "  pm.expect(pm.response.code).to.be.oneOf([200]);",
                  "});",
                  "",
                  "const json = pm.response.json();",
                  "",
                  "if (json.id) pm.environment.set('invoiceId', json.id);",
                  "if (json.idempotencyKey) pm.environment.set('invoiceIdempotencyKey', json.idempotencyKey);",
                  "if (json.invoiceAddress) pm.environment.set('invoiceAddress', json.invoiceAddress);",
                  "if (json.state) pm.environment.set('invoiceState', json.state);",
                  "",
                  "pm.test('Response contains invoice payment details', function () {",
                  "  pm.expect(json.id).to.exist;",
                  "  pm.expect(json.invoiceAddress).to.exist;",
                  "  pm.expect(json.type).to.eql('SINGLE');",
                  "});",
                  "",
                  "console.log('Invoice ID:', json.id);",
                  "console.log('Invoice address:', json.invoiceAddress);",
                  "console.log('Invoice state:', json.state);"
                ]
              }
            }
          ],
          "request": {
            "method": "POST",
            "header": [],
            "body": {
              "mode": "raw",
              "raw": "{\n  \"account\": \"{{account}}\",\n  \"timestamp\": {{timestamp}},\n  \"payload\": {\n    \"amount\": {{invoiceAmount}},\n    \"currency\": \"{{currency}}\",\n    \"description\": \"{{invoiceDescription}}\",\n    \"idempotencyKey\": \"{{invoiceIdempotencyKey}}\",\n    \"returnUrl\": \"{{returnUrl}}\",\n    \"supportUrl\": \"{{supportUrl}}\",\n    \"externalId\": \"{{externalId}}\",\n    \"fiatAvailable\": {{fiatAvailable}}\n  }\n}",
              "options": {
                "raw": {
                  "language": "json"
                }
              }
            },
            "url": {
              "raw": "{{baseUrl}}/api/v1/invoice/single/create",
              "host": [
                "{{baseUrl}}"
              ],
              "path": [
                "api",
                "v1",
                "invoice",
                "single",
                "create"
              ]
            },
            "description": "Creates a SINGLE static invoice. The test script saves invoiceId, invoiceAddress, invoiceState, and invoiceIdempotencyKey to the environment."
          }
        },
        {
          "name": "Get invoice by ID",
          "event": [
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "pm.test('Invoice is returned', function () {",
                  "  pm.expect(pm.response.code).to.be.oneOf([200]);",
                  "});",
                  "",
                  "const json = pm.response.json();",
                  "if (json.state) pm.environment.set('invoiceState', json.state);",
                  "if (json.invoiceAddress) pm.environment.set('invoiceAddress', json.invoiceAddress);",
                  "",
                  "console.log('Current invoice state:', json.state);",
                  "",
                  "pm.test('Response contains the invoice state', function () {",
                  "  pm.expect(json.state).to.exist;",
                  "});"
                ]
              }
            }
          ],
          "request": {
            "method": "POST",
            "header": [],
            "body": {
              "mode": "raw",
              "raw": "{\n  \"account\": \"{{account}}\",\n  \"timestamp\": {{timestamp}},\n  \"payload\": {\n    \"id\": \"{{invoiceId}}\"\n  }\n}",
              "options": {
                "raw": {
                  "language": "json"
                }
              }
            },
            "url": {
              "raw": "{{baseUrl}}/api/v1/invoice",
              "host": [
                "{{baseUrl}}"
              ],
              "path": [
                "api",
                "v1",
                "invoice"
              ]
            },
            "description": "Gets the current invoice state by the saved invoiceId."
          }
        },
        {
          "name": "Get invoice by idempotency key",
          "event": [
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "pm.test('Invoice is returned', function () {",
                  "  pm.expect(pm.response.code).to.be.oneOf([200]);",
                  "});",
                  "",
                  "const json = pm.response.json();",
                  "if (json.id) pm.environment.set('invoiceId', json.id);",
                  "if (json.state) pm.environment.set('invoiceState', json.state);",
                  "",
                  "console.log('Current invoice state:', json.state);"
                ]
              }
            }
          ],
          "request": {
            "method": "POST",
            "header": [],
            "body": {
              "mode": "raw",
              "raw": "{\n  \"account\": \"{{account}}\",\n  \"timestamp\": {{timestamp}},\n  \"payload\": {\n    \"idempotencyKey\": \"{{invoiceIdempotencyKey}}\"\n  }\n}",
              "options": {
                "raw": {
                  "language": "json"
                }
              }
            },
            "url": {
              "raw": "{{baseUrl}}/api/v1/invoice",
              "host": [
                "{{baseUrl}}"
              ],
              "path": [
                "api",
                "v1",
                "invoice"
              ]
            },
            "description": "Gets the current invoice state by the idempotency key used when the invoice was created."
          }
        }
      ]
    }
  ]
}
