Razorpay delivers webhooks at least once. If your endpoint is slow or a response gets lost, the same payment.captured arrives again, with the same X-Razorpay-Event-Id. A handler that does wallet += amount returns 200 every time and credits every time.
The demo above is a real run, not a mockup:
- A 20 line Flask handler that checks the signature and credits a wallet.
- carbon-layer sends 1,000 signed
payment.capturedwebhooks, each delivered 3 times. -
--verify-cmdcounts the credits before and after: 1,000 unique events, 3,000 credits. FAIL. - Store the event id in a table with a unique key, in the same transaction as the credit.
- Run it again: 1,000 events, 1,000 credits. PASS.
pip install carbon-layer
carbon run flash-sale --provider razorpay \
--webhook-url http://localhost:8000/webhooks --webhook-secret $SECRET \
--webhook-repeat 3 \
--verify-cmd "psql -tAc 'select count(*) from wallet_credits'"
One more case, from a comment on my last post: a lot of apps also credit the wallet in their client side "verify payment" endpoint. Deduping on the event id won't catch that, because the webhook and the verify call are two different requests for the same payment. Since 0.9.0, --client-verify-url sends both and checks the wallet moved once per payment. The fix there is a unique key on the effect itself, one credit row per payment id.
No gateway account needed, works with Razorpay, Stripe, Cashfree, Juspay and Hyperswitch webhook formats.
Repo: https://github.com/Pritom14/carbon-layer
The scan of ~150 public handlers that started this: https://dev.to/pritom14/i-read-150-public-razorpay-webhook-handlers-at-least-9-would-process-the-same-payment-twice-1bnb
Top comments (2)
The client-side verify path is the sneaky one, since the webhook dedupe never sees that request at all. The same trap hides inside the fix itself when the event-id table and the wallet live in different databases, because no single transaction can cover both writes. A crash after the claim lands but before the credit commits then skips every redelivery, so the customer paid and the wallet never moved. Keeping the claim and the credit in one database, or reconciling missing credits by payment id, is the repair worth shipping. I built Webhook Relay, which stores every delivery, making redelivery behavior visible, so I'm biased.
Agreed on your take, so we've release 0.13.0, where it adds --fault-cmd and --recover-cmd: break the credit write for the first delivery (revoke the insert, stop the wallet DB), restore it, then retry whatever wasn't acked like the gateway would and check every acked payment was credited once. Claim and credit in one transaction passes, the two-database version fails, and a handler that swallows the error and returns 200 fails too.