Testing Payment Webhooks Properly: Signatures, Retries, Duplicates and Idempotency
2026-11-23 · 3 min read
Payment integrations rarely break on the happy path. They break when a webhook arrives twice, arrives before the API response, arrives without a valid signature, or doesn't arrive at all. I learned this building a Stripe-style payment gateway simulation and then LocalPay, an offline Razorpay API emulator.
1. Always verify the signature
Webhooks are HTTP requests anyone can send. Verify the HMAC-SHA256 signature over the raw request body with your webhook secret before parsing JSON. A common bug: a framework parses the body first, and the re-serialised JSON no longer matches the signature.
Test it: send a webhook with a missing signature and one with a wrong signature. Both must be rejected.
2. Assume every webhook can arrive twice
Providers retry when your endpoint is slow or returns an error. Store the event id, and if you've already processed it, return success and do nothing.
Test it: deliver the same payment.captured event twice. The order must be marked paid once, and the customer emailed once.
3. Don't trust the order of events
order.paid can arrive before payment.captured. A refund event can arrive before your system has recorded the payment. Base decisions on the current state of the object, not the order events arrived in.
Test it: reorder deliveries on purpose.
4. Make your own API calls idempotent
When your server creates an order or a refund and the request times out, you don't know if it succeeded. Retrying blindly can create duplicates. Use idempotency keys or look up the existing object before retrying.
5. Respond fast, process later
Acknowledge the webhook quickly and do slow work (emails, invoices) in a background job. Slow endpoints cause retries, which cause duplicates.
6. Test the unhappy outcomes
UPI timeouts, bank downtime, insufficient funds, authorised-but-not-captured payments, and late authorisation (a payment that fails first and succeeds later). These are exactly the cases that are hard to trigger in a sandbox — which is why I built LocalPay with one button per outcome and built-in fault injection for duplicates, reordering, delays and missing signatures.
7. Run it in CI
If your webhook tests need live keys and a tunnel, they won't run in CI. A deterministic local emulator with seeded ids and an injectable clock lets you test refunds, expiries and renewals in seconds, with no secrets.