{
  "info": {
    "name": "MEGOGO Terminals API (phone number)",
    "description": "Requests of the MEGOGO Terminals API for partners that activate MEGOGO subscriptions by the phone number of the user.\n\nPut your values (partner_id, public_key, private_key, phone, service_id) in a Postman environment. Do not put keys in the collection and do not share an environment with keys. Send the requests from a computer with an IP address of your list.\n\nGuide: https://megogo-phone-subscriptions.pages.dev/reference/postman/",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
  },
  "event": [
    {
      "listen": "prerequest",
      "script": {
        "type": "text/javascript",
        "exec": [
          "// MEGOGO Terminals API: signature of each request.",
          "// text = private_key + \"GET\" + path + \"name=value\" pairs sorted by name",
          "// sign = Base64(SHA-256 hex of text), without \"=\", \"/\" and \"+\" replaced by \"-\", + \"_\" + public_key",
          "const cryptoJs = require('crypto-js');",
          "",
          "const url = pm.request.url;",
          "url.query.remove(q => q.key === 'sign');",
          "const path = pm.variables.replaceIn(url.getPath());",
          "const params = url.query.all()",
          "    .filter(q => !q.disabled)",
          "    .map(q => [q.key, pm.variables.replaceIn(q.value || '')]);",
          "params.sort((a, b) => (a[0] < b[0] ? -1 : (a[0] > b[0] ? 1 : 0)));",
          "const text = pm.variables.get('private_key') + 'GET' + path",
          "    + params.map(p => p[0] + '=' + p[1]).join('');",
          "const hex = cryptoJs.SHA256(text).toString(cryptoJs.enc.Hex);",
          "const sign = cryptoJs.enc.Base64.stringify(cryptoJs.enc.Utf8.parse(hex))",
          "    .replace(/=/g, '').replace(/[\\/+]/g, '-') + '_' + pm.variables.get('public_key');",
          "url.query.add({ key: 'sign', value: sign });"
        ]
      }
    }
  ],
  "variable": [
    {
      "key": "terminals_api",
      "value": "https://billing.megogo.net",
      "description": "Base URL of the Terminals API"
    },
    {
      "key": "partner_id",
      "value": "",
      "description": "Your partner ID. Put it in your environment"
    },
    {
      "key": "public_key",
      "value": "",
      "description": "Your public key. Put it in your environment"
    },
    {
      "key": "private_key",
      "value": "",
      "description": "Your private key. Put it only in your environment, as a secret"
    },
    {
      "key": "phone",
      "value": "",
      "description": "Phone number of your test SIM card: digits only, international format, no +"
    },
    {
      "key": "service_id",
      "value": "",
      "description": "Service ID of a test subscription"
    }
  ],
  "item": [
    {
      "name": "Your services",
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test('list of services', () => pm.expect(pm.response.json()).to.be.an('array'));"
            ]
          }
        }
      ],
      "request": {
        "method": "GET",
        "description": "The services that MEGOGO configured for your partner ID.",
        "url": {
          "raw": "{{terminals_api}}/terminals/{{partner_id}}/subscriptions",
          "host": [
            "{{terminals_api}}"
          ],
          "path": [
            "terminals",
            "{{partner_id}}",
            "subscriptions"
          ],
          "query": []
        }
      }
    },
    {
      "name": "Check the phone number",
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "const body = pm.response.json();",
              "pm.test('answer', () => pm.expect(body).to.have.property('successful'));",
              "console.log(body.successful ? 'The phone number is registered' : 'Not registered: ' + body.message);"
            ]
          }
        }
      ],
      "request": {
        "method": "GET",
        "description": "Tells if the phone number is registered in MEGOGO. Does not create a user.",
        "url": {
          "raw": "{{terminals_api}}/terminals/{{partner_id}}/user/phone?phone={{phone}}",
          "host": [
            "{{terminals_api}}"
          ],
          "path": [
            "terminals",
            "{{partner_id}}",
            "user",
            "phone"
          ],
          "query": [
            {
              "key": "phone",
              "value": "{{phone}}"
            }
          ]
        }
      }
    },
    {
      "name": "Subscribe",
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test('successful', () => pm.expect(pm.response.json().successful).to.eql(true));"
            ]
          }
        }
      ],
      "request": {
        "method": "GET",
        "description": "Starts the service `service_id`. Creates the user if the number is not registered.",
        "url": {
          "raw": "{{terminals_api}}/terminals/{{partner_id}}/subscription/subscribe?phone={{phone}}&serviceId={{service_id}}",
          "host": [
            "{{terminals_api}}"
          ],
          "path": [
            "terminals",
            "{{partner_id}}",
            "subscription",
            "subscribe"
          ],
          "query": [
            {
              "key": "phone",
              "value": "{{phone}}"
            },
            {
              "key": "serviceId",
              "value": "{{service_id}}"
            }
          ]
        }
      }
    },
    {
      "name": "User services",
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test('list of services', () => pm.expect(pm.response.json()).to.be.an('array'));"
            ]
          }
        }
      ],
      "request": {
        "method": "GET",
        "description": "Active services of the user. Creates the user if the number is not registered.",
        "url": {
          "raw": "{{terminals_api}}/terminals/{{partner_id}}/subscriptions?phone={{phone}}",
          "host": [
            "{{terminals_api}}"
          ],
          "path": [
            "terminals",
            "{{partner_id}}",
            "subscriptions"
          ],
          "query": [
            {
              "key": "phone",
              "value": "{{phone}}"
            }
          ]
        }
      }
    },
    {
      "name": "Suspend",
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test('successful', () => pm.expect(pm.response.json().successful).to.eql(true));"
            ]
          }
        }
      ],
      "request": {
        "method": "GET",
        "description": "Stops the access until `resume`. The time that is left stays.",
        "url": {
          "raw": "{{terminals_api}}/terminals/{{partner_id}}/subscription/suspend?phone={{phone}}&serviceId={{service_id}}",
          "host": [
            "{{terminals_api}}"
          ],
          "path": [
            "terminals",
            "{{partner_id}}",
            "subscription",
            "suspend"
          ],
          "query": [
            {
              "key": "phone",
              "value": "{{phone}}"
            },
            {
              "key": "serviceId",
              "value": "{{service_id}}"
            }
          ]
        }
      }
    },
    {
      "name": "Resume",
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test('successful', () => pm.expect(pm.response.json().successful).to.eql(true));"
            ]
          }
        }
      ],
      "request": {
        "method": "GET",
        "description": "Starts a suspended service again. Send it only after your own `suspend`.",
        "url": {
          "raw": "{{terminals_api}}/terminals/{{partner_id}}/subscription/resume?phone={{phone}}&serviceId={{service_id}}",
          "host": [
            "{{terminals_api}}"
          ],
          "path": [
            "terminals",
            "{{partner_id}}",
            "subscription",
            "resume"
          ],
          "query": [
            {
              "key": "phone",
              "value": "{{phone}}"
            },
            {
              "key": "serviceId",
              "value": "{{service_id}}"
            }
          ]
        }
      }
    },
    {
      "name": "Unsubscribe",
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test('successful', () => pm.expect(pm.response.json().successful).to.eql(true));"
            ]
          }
        }
      ],
      "request": {
        "method": "GET",
        "description": "Stops the service immediately.",
        "url": {
          "raw": "{{terminals_api}}/terminals/{{partner_id}}/subscription/unsubscribe?phone={{phone}}&serviceId={{service_id}}",
          "host": [
            "{{terminals_api}}"
          ],
          "path": [
            "terminals",
            "{{partner_id}}",
            "subscription",
            "unsubscribe"
          ],
          "query": [
            {
              "key": "phone",
              "value": "{{phone}}"
            },
            {
              "key": "serviceId",
              "value": "{{service_id}}"
            }
          ]
        }
      }
    }
  ]
}
