Preventing Duplicate Charges: Idempotent Payment Webhooks with Node.js and PostgreSQL
Payment providers deliver webhooks at least once, so retries or simultaneous deliveries can cause a handler to extend a subscription or charge a customer multiple times. The author, building the payment-recovery engine for the RailSwitch billing system on Nigerian payment rails, needed each customer charged exactly once per billing cycle regardless of duplicate or concurrent deliveries. The article covers three kinds of problems: plain duplicates, concurrent duplicates that slip past check-then-insert logic, and out-of-order events. The proposed fix combines a stable idempotency key per event, a processed-events table with a unique constraint as the true correctness guarantee, a row lock on the subscription, and all steps inside one database transaction.
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