At most one successful authorization and one capture per order attempt. Refunds never exceed captures. A payment that succeeded at the provider is never lost from your records.Payment processing is external, asynchronous, and retried by design. The system calls a provider, and the call may time out after the money has moved. The provider sends webhooks that are at-least-once, can arrive out of order, and can be delayed by minutes. The mechanisms on this page exist to make this correct.
The boundary: who is authoritative for what
Define this boundary explicitly to avoid double charges.- The payment provider is authoritative for money movement. Whether a card was actually charged is their truth, not yours.
- CockroachDB is authoritative for your intent and your recorded outcome. What you meant to charge, and what you believe happened, lives in your database.
Record intent before you call out
Create the payment record before calling the provider, keyed by an idempotency key that the application generates and sends to the provider. If the call times out and is retried, the provider deduplicates on that key, which prevents a double charge.UNIQUE constraint on idempotency_key guarantees that one logical payment attempt yields one row. The CHECK (refunded_amount <= amount) constraint makes over-refunding impossible.
[!WARNING] Never hold a transaction open across the provider call The external call can take seconds and can hang. Record intent, commit, then call the provider. Holding a CockroachDB transaction open while waiting on a third party converts the provider’s latency into contention in the cluster.
Process callbacks through an inbox
Provider webhooks are at-least-once and may be reordered. Treat every callback as potentially duplicate and potentially stale. An inbox table keyed by the provider’s event id makes duplicate processing a no-op.ON CONFLICT eliminates duplicates, and the FOR UPDATE lock combined with the compare-and-set eliminates races and out-of-order events. SELECT ... FOR UPDATE serializes concurrent callbacks for the same payment, and the transaction runs under a 40001 retry loop.
[!NOTE]
Always ack duplicates with success
If the application returns an error to a duplicate webhook, the provider continues to redeliver it. A replay should commit, performing no change, and return 200. Under idempotent processing, a duplicate is a successful no-op, not a failure.
Refunds
A refund is a forward transition bounded by the captured amount. TheCHECK (refunded_amount <= amount) constraint is the backstop; the guarded update is the gate.
Reconciliation: detect drift and repair it
Regardless of how carefully the write path is implemented, an ambiguous case remains: the capture call succeeded at the provider but the local commit was lost. The provider charged the customer, but the database recordsinitiated. This case cannot be prevented; it must be detected and repaired.
Run a periodic job that compares the provider’s ledger against payment_transactions and resolves discrepancies, such as marking the local record captured or requesting a refund for a charge with no matching order. Use follower reads (AS OF SYSTEM TIME follower_read_timestamp()) so these scans do not contend with the live write path.
Payment-attempt tables get big
The payment-attempt and payment-event tables are among the highest-volume tables in the system. They can reach tens or hundreds of millions of rows, because every retry, callback, and status change is a row. Design for this volume from the start:- Partition by time and/or merchant so queries and retention touch a bounded slice.
- Index for the queries you actually run (by order, by provider reference, by status + time), not speculatively.
- Keyset pagination, not
OFFSET, for scanning history. - Row-Level TTL to age out resolved attempts you no longer need online.
Driving the order forward
Payment outcomes advance the order lifecycle. When a capture completes, transition the ordercreated → paid and commit the inventory reservation in the same transaction as the payment-state change, then enqueue downstream work through the outbox. See Order lifecycle & idempotency and Inventory & reservations.
Tradeoffs
[!CAUTION] Anti-patterns
- Calling the provider inside an open database transaction.
- Treating a duplicate webhook as an error (causes infinite redelivery).
- Applying callbacks in arrival order without a monotonic guard: a late “authorized” after a “captured” corrupts state.
- Relying on the write path alone and skipping reconciliation: the charged-but-not-recorded case will happen.

