(Note · · 6 min read)

The payment succeeded and your system never heard

Webhooks look like the easy half of an integration — an endpoint that receives a message. They are a public entry point carrying claims about money, delivered more than once, sometimes out of order, sometimes while your server is restarting. Each of those needs handling, and the gaps show up as orders nobody paid for.

By Md. Habibur Rahman Shohel, full-stack & AI product engineer in Dhaka

Verify the signature before anything else

The endpoint is public, so anyone can post to it. Without verification, a crafted request claiming a payment succeeded will be believed. Every serious provider signs its webhooks; verify against the raw request body, before parsing, using a constant-time comparison.

Parsing first and then verifying the re-serialised body is a common mistake that makes verification unreliable, because the bytes you check are no longer the bytes that were signed. Check the timestamp the provider includes too, so an old captured request cannot be replayed.

Assume it arrives more than once

At-least-once delivery means duplicates are normal: a retry after a timeout, a provider resending, a network hiccup. Record each event's identifier and ignore one you have already processed.

Use a unique constraint on that identifier rather than checking before inserting, so two simultaneous deliveries cannot both pass the check. This is the difference between a customer being charged once and twice.

Acknowledge fast, process after

Providers expect a quick response and will retry if you are slow, which means doing the work inline produces timeouts and then duplicates. Verify, store the event, return success, and process from a queue.

That also makes failures recoverable. The event is already recorded, so a bug in processing can be fixed and the stored events replayed — rather than being lost because the only copy was in a request that already timed out.

Order is not guaranteed

A cancellation can arrive before the creation it refers to. Handle each event against current state rather than assuming a sequence: ignore an update describing a state you have already moved past, and use the event's own timestamp rather than arrival time to decide.

Where an event refers to something you have never seen, record it rather than discarding it. The alternative is losing a legitimate event because two messages crossed.

Do not trust the payload's figures

Treat the webhook as a notification that something changed, not as the authoritative description of what it changed to. Read the current state back from the provider's API using the identifier before acting on an amount.

This matters most where the amount decides what a customer receives. On a hotel booking system I took over, the previous implementation's gap left a booking recorded as unpaid after a refused payment — it now asks the gateway what the truth is rather than inferring it from what arrived.

Make the endpoint observable

Log every delivery with its event identifier, type and outcome, including rejections for a bad signature — those are both a debugging aid and a sign someone is probing. Keep the raw payload long enough to replay it.

Alert on absence as well as failure. A webhook that stops arriving produces no errors at all, and that silence is how an integration can be broken for a week without anybody noticing.

Common questions

How do I secure a webhook endpoint?

Verify the provider's signature against the raw request body before parsing, with a constant-time comparison, and check the included timestamp so an old request cannot be replayed. The endpoint is public, so without verification a crafted request claiming a payment succeeded will be believed.

Why do I receive the same webhook twice?

Because delivery is at-least-once: retries after timeouts, provider resends and network hiccups are all normal. Record each event identifier with a unique constraint and ignore repeats — a check-then-insert lets two simultaneous deliveries both pass.

Should webhook processing happen in the request?

No. Providers retry if you are slow, so inline work produces timeouts and duplicates. Verify, store the event, return success, then process from a queue — which also makes failures replayable instead of lost.

Can I trust the amounts in a webhook payload?

Treat it as notification that something changed, not as the authoritative description. Read current state back from the provider's API using the identifier before acting on an amount, especially where the amount decides what a customer receives.

Need something like this built?