## Callback: contact.consent

`POST <your callback URL>`

A contact opted in or out of a category - by replying STOP or START (`source: keyword`),
or through the API. After a STOP, business-initiated marketing and notification
templates to them are refused; update your own records too.

### Headers

| Name | Description |
|---|---|
| `X-PaalChat-Event` |  |
| `X-PaalChat-Delivery` | Unique per event (equals body `id`). De-duplicate on it. |
| `X-PaalChat-Signature` | `t=<unix seconds>,v1=<hex HMAC-SHA256(secret, "<t>.<raw body>")>`. Reject if invalid or older than 300 seconds. |

### Body

| Field | Type | Description |
|---|---|---|
| `id` | string | Equals X-PaalChat-Delivery. |
| `sandbox` | boolean | true for sandbox businesses (test keys) - nothing reached WhatsApp. |
| `event` | any |  |
| `occurred_at` | string |  |
| `business` | object |  |
| `business.external_id` | string |  |
| `data` | object |  |
| `data.contact_id` | integer |  |
| `data.contact_external_id` | string | null |  |
| `data.phone` | string | null |  |
| `data.category` | string |  |
| `data.status` | string |  |
| `data.source` | string | keyword (the contact replied STOP or START), api, inbox, or the source you sent. |

### Example

```json
{
  "id": "6a7b8c9d-0e1f-4a2b-9c3d-4e5f6a7b8c9d",
  "event": "contact.consent",
  "occurred_at": "2026-10-02T19:04:11+00:00",
  "business": {"external_id": "presec"},
  "sandbox": false,
  "data": {
    "contact_id": 41,
    "contact_external_id": "parent-77",
    "phone": "233241234567",
    "category": "marketing",
    "status": "revoked",
    "source": "keyword"
  }
}
```

### Verify and handle (PHP)

```php
// routes/api.php: Route::post('/paalchat/callback', PaalChatCallbackController::class);
public function __invoke(Request $request)
{
    $header = (string) $request->header('X-PaalChat-Signature');
    $raw = $request->getContent();

    if (! preg_match('/^t=(\d+),v1=([a-f0-9]{64})$/', $header, $m)
        || abs(time() - (int) $m[1]) > 300
        || ! hash_equals(hash_hmac('sha256', $m[1].'.'.$raw, config('services.paalchat.callback_secret')), $m[2])) {
        abort(401);
    }

    // Process each delivery once.
    if (! Cache::add('paalchat:'.$request->header('X-PaalChat-Delivery'), true, now()->addDays(2))) {
        return response()->noContent();
    }

    $event = json_decode($raw, true);

    if ($event['event'] === 'contact.consent') {
        Guardian::where('phone', $event['data']['phone'])->update(['whatsapp_'.$event['data']['category'] => $event['data']['status'] === 'granted']);
    }

    return response()->noContent();
}
```
