How idempotency prevents duplicate payments when webhooks are resent
Payment gateways like Stripe, Mercado Pago, and PagSeguro automatically resend webhook events when they do not receive a 2xx response in time, which can happen due to timeouts, deploys, or slow processing. If a backend system treats every incoming webhook as a new event, it can create duplicate payments, release access twice, and send multiple confirmation emails to the same customer. The root cause is non-idempotent code that performs a fresh database insert on every request, regardless of whether the event was already processed. The fix involves storing each unique event ID in a dedicated database table with a unique constraint, then attempting an INSERT rather than a SELECT check to safely handle race conditions. If the insert fails due to a duplicate, the system returns a 200 response immediately, signalling the gateway to stop retrying without reprocessing the event.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in