{
  "info": {
    "name": "Blinkerbot booking contract",
    "description": "Calls your booking endpoint the way Blinkerbot does. Set the collection variables `endpoint_url` and `signing_secret` (whsec_..., from manual setup in the Blinkerbot panel, never share it). The collection pre-request script signs every request with Standard Webhooks v1 (webhook-id, webhook-timestamp, webhook-signature). Run the requests in order: list_services fills service_id, find_slots fills the slot, create_booking fills booking_id. The create request uses test:true, so nothing is kept.",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
  },
  "event": [
    {
      "listen": "prerequest",
      "script": {
        "type": "text/javascript",
        "exec": [
          "// Standard Webhooks v1 signature over the exact body that is sent.",
          "const cryptoJs = require('crypto-js');",
          "const now = new Date();",
          "pm.variables.set('from_iso', now.toISOString());",
          "pm.variables.set('to_iso', new Date(now.getTime() + 7 * 86400000).toISOString());",
          "const secret = String(pm.variables.get('signing_secret') || '').trim();",
          "if (!secret.startsWith('whsec_')) { throw new Error('Set the signing_secret variable (whsec_...)'); }",
          "const raw = pm.variables.replaceIn(pm.request.body.raw);",
          "pm.request.body.update({ mode: 'raw', raw: raw, options: { raw: { language: 'json' } } });",
          "const id = 'msg_' + cryptoJs.lib.WordArray.random(12).toString();",
          "const ts = String(Math.floor(Date.now() / 1000));",
          "const key = cryptoJs.enc.Base64.parse(secret.slice('whsec_'.length));",
          "const sig = cryptoJs.HmacSHA256(id + '.' + ts + '.' + raw, key).toString(cryptoJs.enc.Base64);",
          "pm.request.headers.upsert({ key: 'webhook-id', value: id });",
          "pm.request.headers.upsert({ key: 'webhook-timestamp', value: ts });",
          "pm.request.headers.upsert({ key: 'webhook-signature', value: 'v1,' + sig });",
          "pm.request.headers.upsert({ key: 'Content-Type', value: 'application/json' });"
        ]
      }
    },
    {
      "listen": "test",
      "script": {
        "type": "text/javascript",
        "exec": [
          "pm.test('HTTP 200 with JSON', function () {",
          "  pm.response.to.have.status(200);",
          "  pm.expect(pm.response.headers.get('Content-Type') || '').to.include('application/json');",
          "});"
        ]
      }
    }
  ],
  "variable": [
    { "key": "endpoint_url", "value": "https://www.example.com/blinkerbot-endpoint.php" },
    { "key": "signing_secret", "value": "" },
    { "key": "service_id", "value": "" },
    { "key": "staff_id", "value": "" },
    { "key": "slot_start", "value": "" },
    { "key": "slot_end", "value": "" },
    { "key": "booking_id", "value": "" },
    { "key": "idempotency_key", "value": "bk_postman_1" }
  ],
  "item": [
    {
      "name": "1 ping",
      "request": {
        "method": "POST",
        "header": [],
        "url": "{{endpoint_url}}",
        "body": {
          "mode": "raw",
          "raw": "{\"operation\":\"ping\",\"request_id\":\"req_{{$guid}}\",\"idempotency_key\":null,\"account\":null,\"test\":false,\"data\":{}}",
          "options": { "raw": { "language": "json" } }
        }
      },
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": ["pm.test('ok', function () { pm.expect(pm.response.json().ok).to.eql(true); });"]
          }
        }
      ]
    },
    {
      "name": "2 list_services",
      "request": {
        "method": "POST",
        "header": [],
        "url": "{{endpoint_url}}",
        "body": {
          "mode": "raw",
          "raw": "{\"operation\":\"list_services\",\"request_id\":\"req_{{$guid}}\",\"idempotency_key\":null,\"account\":null,\"test\":false,\"data\":{}}",
          "options": { "raw": { "language": "json" } }
        }
      },
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "const body = pm.response.json();",
              "pm.test('services listed', function () { pm.expect(body.ok).to.eql(true); pm.expect(body.data.services).to.be.an('array').that.is.not.empty; });",
              "pm.collectionVariables.set('service_id', String(body.data.services[0].id));"
            ]
          }
        }
      ]
    },
    {
      "name": "3 find_slots",
      "request": {
        "method": "POST",
        "header": [],
        "url": "{{endpoint_url}}",
        "body": {
          "mode": "raw",
          "raw": "{\"operation\":\"find_slots\",\"request_id\":\"req_{{$guid}}\",\"idempotency_key\":null,\"account\":null,\"test\":false,\"data\":{\"service_id\":\"{{service_id}}\",\"from\":\"{{from_iso}}\",\"to\":\"{{to_iso}}\",\"timezone\":\"Europe/Istanbul\"}}",
          "options": { "raw": { "language": "json" } }
        }
      },
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "const body = pm.response.json();",
              "pm.test('slots listed', function () { pm.expect(body.ok).to.eql(true); pm.expect(body.data.slots).to.be.an('array').that.is.not.empty; });",
              "const s = body.data.slots[0];",
              "pm.collectionVariables.set('slot_start', s.start);",
              "pm.collectionVariables.set('slot_end', s.end);",
              "pm.collectionVariables.set('staff_id', s.staff_id || '');"
            ]
          }
        }
      ]
    },
    {
      "name": "4 create_booking (test: true)",
      "request": {
        "method": "POST",
        "header": [],
        "url": "{{endpoint_url}}",
        "body": {
          "mode": "raw",
          "raw": "{\"operation\":\"create_booking\",\"request_id\":\"req_{{$guid}}\",\"idempotency_key\":\"{{idempotency_key}}\",\"account\":null,\"test\":true,\"data\":{\"service_id\":\"{{service_id}}\",\"staff_id\":\"{{staff_id}}\",\"start\":\"{{slot_start}}\",\"end\":\"{{slot_end}}\",\"timezone\":\"Europe/Istanbul\",\"customer\":{\"name\":\"Postman Test\",\"phone\":\"+905550000000\"},\"channel\":\"webchat\",\"note\":\"Postman test\"}}",
          "options": { "raw": { "language": "json" } }
        }
      },
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "const body = pm.response.json();",
              "pm.test('booking answered', function () { pm.expect(body.ok).to.eql(true); pm.expect(body.data.booking_id).to.be.a('string'); });",
              "pm.collectionVariables.set('booking_id', body.data.booking_id);"
            ]
          }
        }
      ]
    },
    {
      "name": "5 create_booking again (same key -> same booking_id)",
      "request": {
        "method": "POST",
        "header": [],
        "url": "{{endpoint_url}}",
        "body": {
          "mode": "raw",
          "raw": "{\"operation\":\"create_booking\",\"request_id\":\"req_{{$guid}}\",\"idempotency_key\":\"{{idempotency_key}}\",\"account\":null,\"test\":true,\"data\":{\"service_id\":\"{{service_id}}\",\"staff_id\":\"{{staff_id}}\",\"start\":\"{{slot_start}}\",\"end\":\"{{slot_end}}\",\"timezone\":\"Europe/Istanbul\",\"customer\":{\"name\":\"Postman Test\",\"phone\":\"+905550000000\"},\"channel\":\"webchat\",\"note\":\"Postman test\"}}",
          "options": { "raw": { "language": "json" } }
        }
      },
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test('same booking_id', function () { pm.expect(pm.response.json().data.booking_id).to.eql(pm.collectionVariables.get('booking_id')); });"
            ]
          }
        }
      ]
    },
    {
      "name": "6 create_booking other content (idempotency_mismatch)",
      "request": {
        "method": "POST",
        "header": [],
        "url": "{{endpoint_url}}",
        "body": {
          "mode": "raw",
          "raw": "{\"operation\":\"create_booking\",\"request_id\":\"req_{{$guid}}\",\"idempotency_key\":\"{{idempotency_key}}\",\"account\":null,\"test\":true,\"data\":{\"service_id\":\"{{service_id}}\",\"staff_id\":\"{{staff_id}}\",\"start\":\"{{slot_start}}\",\"end\":\"{{slot_end}}\",\"timezone\":\"Europe/Istanbul\",\"customer\":{\"name\":\"Someone Else\",\"phone\":\"+905550000001\"},\"channel\":\"webchat\"}}",
          "options": { "raw": { "language": "json" } }
        }
      },
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test('idempotency_mismatch', function () { pm.expect(pm.response.json().error.code).to.eql('idempotency_mismatch'); });"
            ]
          }
        }
      ]
    },
    {
      "name": "7 get_booking",
      "request": {
        "method": "POST",
        "header": [],
        "url": "{{endpoint_url}}",
        "body": {
          "mode": "raw",
          "raw": "{\"operation\":\"get_booking\",\"request_id\":\"req_{{$guid}}\",\"idempotency_key\":null,\"account\":null,\"test\":true,\"data\":{\"booking_id\":\"{{booking_id}}\"}}",
          "options": { "raw": { "language": "json" } }
        }
      },
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "const body = pm.response.json();",
              "pm.test('answered (a test booking is not kept, so booking_not_found is correct)', function () {",
              "  pm.expect(body.ok === true || body.error.code === 'booking_not_found').to.eql(true);",
              "});"
            ]
          }
        }
      ]
    },
    {
      "name": "8 cancel_booking",
      "request": {
        "method": "POST",
        "header": [],
        "url": "{{endpoint_url}}",
        "body": {
          "mode": "raw",
          "raw": "{\"operation\":\"cancel_booking\",\"request_id\":\"req_{{$guid}}\",\"idempotency_key\":\"cx_{{$guid}}\",\"account\":null,\"test\":true,\"data\":{\"booking_id\":\"{{booking_id}}\"}}",
          "options": { "raw": { "language": "json" } }
        }
      },
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "const body = pm.response.json();",
              "pm.test('answered', function () { pm.expect(body.ok === true || body.error.code === 'booking_not_found').to.eql(true); });"
            ]
          }
        }
      ]
    }
  ]
}
