Backend integrations

Designing Payment Integrations: State Machines, Webhooks & Safe Retries

Checkout, capture, webhooks, and retries through the lens of my payment-service experience.

Conceptual flow
  1. 01Created
  2. 02Pending provider
  3. 03Confirmed / failed
  4. 04Reconciled record

More than a gateway request

My Java and Spring work at Prokoders included payment services covering checkout, capture, webhooks, payment links, validation, and resilient error handling. These are connected stages rather than independent API calls. A browser can close after checkout, a provider can reply late, and an event can arrive while another request is still being handled. The application needs a clear account of the payment state throughout those transitions.

The Britrip case study describes my payment integrations with Network International, Stripe, and Cryptomus alongside the platform’s shared identity and support services.

Model uncertainty explicitly

A timeout does not prove that a payment failed. It can mean the application did not receive the result. Distinguish a confirmed failure from an unresolved operation, then define how reconciliation works. Keep provider references alongside internal identifiers so that events and follow-up checks can be linked to the same operation. The names and transitions should follow the actual provider contract and the business workflow.

Make duplicate handling testable

Before relying on webhooks, define how their authenticity is checked and how repeated delivery is handled. Test the same notification twice and verify that it does not create two business actions. Also test a late notification after an earlier timeout. A useful acceptance check inspects the stored transaction and related business record, not only the HTTP response. This keeps integration testing focused on correctness across the whole workflow.

Define states before provider adapters

The flow above is illustrative: the exact states depend on whether the provider separates authorization and capture and on the product’s business rules. Start by naming confirmed states, transitional states, and unresolved states. Then map each provider response into that model. Keep external references and the internal operation identifier together. A provider adapter should translate a contract, while the application owns the business transition it permits.

Verify and record before acting

Stripe’s webhook guidance calls for signature verification using the raw request body and warns that events can be delivered more than once or out of order. That means a handler needs more than JSON parsing. After authenticity checks, decide whether the event has already been accepted and whether its transition is still applicable. Keep heavy processing out of the acknowledgement path when possible, while making the acceptance record durable enough to support recovery.

Reason about concurrent requests

Imagine a customer retrying a checkout while a delayed webhook arrives. Both handlers may observe the same pending record. A read-then-write check alone can allow both to apply a side effect. Review the transaction boundary, uniqueness constraints, and conditional transition used to serialize that decision. The right mechanism depends on the data store, but the invariant is stable: one logical payment should not produce the same business effect twice.

Reconciliation after an ambiguous result

When a request times out, preserve the distinction between no result and a confirmed failure. Reconciliation should use the provider’s supported lookup or event mechanism to resolve the operation. Define who owns that work and how unresolved records are surfaced for investigation. If retries are allowed, reuse the same logical operation identity according to the provider’s idempotency contract instead of creating an unrelated operation for every attempt.

Acceptance criteria that inspect business effects

Test duplicate notifications, invalid signatures, delayed confirmation, a network interruption after provider acceptance, and competing updates. For each case, inspect both the transaction record and the business consequence, such as whether an order becomes eligible for fulfillment. A successful response code is insufficient if a duplicate action occurred. Keep test-provider data separate from real transactions and document the expected final state for every scenario.

Back to all articles →