Sub topic 11
Webhooks and asynchronous status updates
This chapter assumes you know HTTP basics, idempotent APIs, and the payment state machine from earlier chapters. It covers how a payment system learns about, and reports, status changes that happen after the original API call returns.
Why status updates are asynchronous
Many payment events finish after the request that started them. A bank may take seconds or days to confirm a transfer. A 3-D Secure challenge waits for the customer. A chargeback arrives weeks later. Polling for each of these wastes calls and adds delay. Instead, the PSP sends a webhook: an HTTP POST to a URL you register, carrying an event such as payment.captured.
Your platform plays both roles. It receives webhooks from PSPs, and it sends webhooks to merchants.
Receiving webhooks safely
Follow these rules in the receiver:
- Verify the signature. Anyone can POST to a public URL. The PSP signs the raw body with a shared secret, and you recompute and compare it.
- Store first, process later. Persist the event and return
200quickly. If you do slow work inline, the PSP times out and retries. - Deduplicate. Delivery is at-least-once, so the same event can arrive more than once. Record the event ID under a unique constraint.
- Expect disorder. Events can arrive out of order. A
capturedevent may come beforeauthorized.
import hashlib
import hmac
def verify_signature(secret: bytes, raw_body: bytes, header_signature: str) -> bool:
expected = hmac.new(secret, raw_body, hashlib.sha256).hexdigest()
return hmac.compare_digest(expected, header_signature)Use hmac.compare_digest rather than ==. It takes the same time whatever the input, so it does not leak information about the correct signature.
Applying events to the state machine
Run each event through the same transition table you use for API calls. If an event describes a move that is not allowed from the current state, do not crash and do not apply it blindly. Handle two cases:
- Stale or duplicate event: the payment is already at or past that state. Ignore it and return success.
- Early event: the payment has not reached the prerequisite state yet. Keep the event for a later retry.
When the event's order is in doubt, fetch the payment from the PSP's API and trust that result over the webhook payload. Treat the webhook as a hint to look, not as the final word.
Sending webhooks to merchants
Merchants need the same guarantees from you. Write the event to an outbox table in the same database transaction as the state change. A separate dispatcher then delivers it. If the merchant's server returns an error or times out, retry with exponential backoff and jitter, and stop after a fixed window such as 72 hours. Include a stable event ID and a timestamp, and sign each request. Offer a way to replay missed events.
Common errors
| Mistake | Result |
|---|---|
| Skipping signature checks | Forged "payment succeeded" events |
| Processing inline before replying | PSP timeouts and duplicate deliveries |
| Assuming ordered delivery | Illegal transitions or lost updates |
| Sending the webhook before the commit | Merchant sees a state your database does not have |
Exercise
A PSP delivers the same payment.captured event twice, and the second copy arrives after the payment has already moved to refunded. What should the receiver do?
Show answer
Deduplicate by event ID and ignore the stale event. The unique constraint on event ID rejects the exact duplicate. Even if it were a new ID, captured is not allowed from refunded, so the receiver should treat it as stale, take no action, and still return 200 so the PSP stops retrying.