
Summary
Key points of this document
- Where are the design challenges?
- How to define boundaries?
- Details that make a difference in implementation
Overview
In payment integrations, relying solely on successful screen transitions can lead to state inconsistencies. It's necessary to design processes, centered around Stripe Webhooks, that yield the same result even with duplication or retries. The starting point for webhook processing design is to abandon the assumption that events will only be delivered once.
Where are the design challenges?
If orders or contracts are finalized solely by redirection after payment completion, the state can become inconsistent if the browser is closed or communication fails. On the other hand, webhooks may receive the same event multiple times, or event order may not be as expected.
Payments directly impact amounts, access rights, and billing status. Double execution of processes or missed cancellations become major issues. Furthermore, synchronously executing external API calls or email sending upon webhook reception makes error handling difficult.
Stripe Webhooks are a crucial entry point for payment integration, but the same event may arrive multiple times, out of order, or the application side might fail midway through processing. Simply receiving an event and immediately updating the DB can lead to duplicate processing or state rollbacks.
How to define boundaries?
Upon webhook reception, verify the signature, save the event ID, and if unprocessed, queue it as an internal event. Payment state updates only execute transitions allowed by the current state and the received event. Side effects are separated into jobs.
In design, webhooks are treated not as business processes themselves, but as records of external event reception. First, save them uniquely by event_id, verify the signature and event type, and then pass them to an idempotent use case.
Details that make a difference in implementation
The DB should have separate tables for external events and internal states. Place a unique constraint on the event ID, and return success on duplicate reception. For state updates, explicitly define transitions such as pending payment, confirmed, failed, refunded, or canceled.
In PostgreSQL,`webhook_events`table, storing eventid, type, payloadhash, receivedat, processedat, status, and error. On the business logic side, check the current state for each payment ID or subscription ID, and do nothing if already reflected.
- Place a unique constraint on
event_idso that duplicate receptions can be treated as success. - Return a short HTTP response, and pass heavy processing to a queue for retries.
- When updating state, check the current state with an external API and do not roll back with old events.
Observe how it breaks
For verification, test multiple transmissions of the same webhook, out-of-order delivery, invalid signatures, mid-processing failures, and job retries. Even when using the Stripe CLI, prepare test data that anticipates retries occurring in actual operation.
For verification, reproduce duplicate transmission of the same event, out-of-order delivery, exceptions during processing, and external API timeouts. Having a reprocessing command that uses saved payloads, not just test events, makes recovery during failures more realistic.
Discarded options and tradeoffs
Strictly handling idempotency increases tables and state transitions. It might seem heavy for simple, one-off payments. However, when subscriptions or permission grants are involved, it's safer to think in an event-driven manner from the start.
Synchronous processing for everything simplifies implementation but makes it vulnerable to timeouts and retries. Queuing increases state management but facilitates retries and investigations. In payments, a structure that prevents double reflection should be prioritized over implementation brevity.
Decision-making criteria for the field
Stripe Webhooks are not mere notifications, but crucial inputs for finalizing payment states. Designing idempotent processing that accounts for duplication and out-of-order delivery is fundamental to payment integration.
Webhook idempotency is not about avoiding duplication with if statements. It's about separating received events from business states and designing the system to converge to the same result no matter how many times it's executed.


