# Webhooks

Instead of asking again and again whether an invoice is paid, be told.

## Events

| Event | When |
|---|---|
| `invoice.posted` | An invoice was issued, by you or in the office. |
| `invoice.paid` | An invoice is fully paid **and the money is in the bank** (a card confirmation alone is not enough). |
| `invoice.unpaid` | An invoice that was paid is no longer (a payment was undone). |
| `invoice.credited` | A credit note was applied to an invoice. |
| `credit.raised` | A credit note was issued. |
| `quote.accepted` | A customer accepted a quotation. |

Add the URL and the events on the connection in **Connected systems**. The signing secret is shown once, when you add it. The page shows each webhook's health and its recent deliveries.

## What arrives

A `POST` with a JSON body:

```json
{
  "event": "invoice.paid",
  "at": "2026-12-03T09:14:07+02:00",
  "document_id": "9a3c...",
  "doc_no": "INV-00042",
  "doc_type": "SALES_INVOICE",
  "external_ref": "ORD-5001",
  "source_system": "shop",
  "amount": 1150.00,
  "ccy": "ZAR",
  "doc_date": "2026-12-01",
  "due_date": "2026-12-31",
  "customer": "Northwind Trading (Pty) Ltd"
}
```

Your own `external_ref` is in it, so you need no mapping table. Headers: `ReconSmith-Event`, `ReconSmith-Delivery` (unique per delivery: use it to ignore a repeat) and `ReconSmith-Signature`.

## Checking the signature

`ReconSmith-Signature: t=1764745247,v1=5257a8...`

1. Take `t` and the raw body exactly as received.
2. Compute HMAC-SHA256 of `t + "." + body` with your webhook secret.
3. Compare it with `v1` in constant time.
4. Refuse a `t` more than five minutes old, so a captured delivery cannot be replayed.

```python
import hmac, hashlib, time

def verify(secret: str, header: str, body: bytes) -> bool:
    parts = dict(p.split("=", 1) for p in header.split(","))
    if abs(time.time() - int(parts["t"])) > 300:
        return False
    mac = hmac.new(secret.encode(), parts["t"].encode() + b"." + body, hashlib.sha256).hexdigest()
    return hmac.compare_digest(mac, parts["v1"])
```

## Answering

Answer `2xx` quickly and do the work afterwards. Anything else is retried, up to eight attempts, waiting longer each time (30 seconds, then 1.5 minutes, 4.5 minutes, and so on). A `4xx` other than `429` means "do not send me this" and is not retried. An endpoint that fails 20 deliveries in 24 hours is switched off; add it again in **Connected systems** once it is fixed.

Deliveries can arrive more than once and out of order. Treat them as a nudge: when one arrives, read the current state (`GET /v1/documents`) if order matters.
