Idempotency & Retries
Platform engineers implementing or reviewing merchantTransactionId dedup logic, retry-count handling, or Stripe idempotency-key forwarding in a v2 payment flow. For the exact rules and database fields, see Idempotency & Retries β Reference. For request and response examples, see How to Retry a Payment Request.
Overviewβ
The v2 payment API uses merchantTransactionId as an idempotency key to prevent duplicate payment processing. This mechanism is critical for reliability in a distributed system where network failures, timeouts, and retries are expected.
How Idempotency Worksβ
Best Practices for Engineersβ
| Practice | Why |
|---|---|
Always use the composite key (merchantTransactionId + merchantId) for dedup lookups | Prevents cross-merchant collisions |
Increment retryCount atomically | Use UPDATE ... SET retryCount = retryCount + 1 WHERE retryCount < 5 to prevent race conditions |
| Return the full current state on retry | Don't return stale data β query the latest payment state |
Forward merchantTransactionId to Stripe as the Stripe idempotency key | Ensures Stripe-level dedup matches our dedup |
| Never re-process a terminal payment | If status is COMPLETED, FAILED, or CANCELLED, return as-is |
| Log retries distinctly | Include retryCount in log entries to distinguish first attempts from retries |
Stripe-Level Idempotencyβ
The Stripe adapter passes merchantTransactionId as Stripe's Idempotency-Key header. This provides a second layer of protection:
| Layer | Key | Protection |
|---|---|---|
| Service layer | merchantTransactionId + merchantId | Database unique constraint |
| Stripe layer | merchantTransactionId as Idempotency-Key | Stripe returns cached response for duplicate calls |
Both layers must agree. If our database shows no record but Stripe has one (e.g., after a partial failure), the Stripe response takes precedence and the service reconciles the state.