**The design is not safe to build as written, but the fix is confined to steps 3–6 of the flow and you can still start tomorrow.** The payment step can charge a customer twice, or charge them and leave no order record. The module structure and the sizing for 200–2,000 orders a day are fine.

## Must change before building

**1. Retrying on timeout can double-charge (step 4).** A timeout means "no answer", not "not charged".
- Case: the provider takes 6 s to process a charge. The service gives up at 5 s and retries, and the retry also succeeds.
- Result: the customer is charged twice for one order. Expected: one charge.
- Fix: send an idempotency key (the order id) on every charge call, so the provider treats retries as the same charge.

**2. A charge can succeed with no order in the database (steps 3–6).** The `PENDING` insert sits in a transaction that stays open during the payment call, so a rollback erases all trace of the order.
- Case: all three attempts time out but the first actually went through; or the process crashes after the charge and before the commit.
- Result: the customer is charged, the order row is gone, and there is nothing to reconcile against. Expected: an order row that says payment is unresolved.
- Fix: commit `PENDING` first, call the provider outside any transaction, then update to `PAID` or `FAILED` in a second short transaction. Add a small job that looks up stale `PENDING` orders with the provider and settles them.
- This also stops a database connection being held for up to 15 s per checkout.

**3. Duplicate `POST /orders` creates duplicate orders (step 1).** The request can take 15 s or more, so clients and users will retry.
- Fix: accept a client-supplied idempotency key, store it with a unique constraint, and return the existing order on a repeat.

```mermaid
flowchart TD
    A[POST /orders with idempotency key] --> B[Check stock]
    B --> C[Insert order as PENDING and commit]
    C --> D[Charge provider, order id as idempotency key]
    D -->|success| E[Mark PAID, commit, send email]
    D -->|declined| F[Mark FAILED, return error]
    D -->|timeout after retries| G[Leave PENDING, reconcile job settles it]
```

## Decide and write down

- **Overselling.** Stock is checked against a cache up to 5 minutes old and nothing reserves or decrements it. Two orders for the last unit both pass and both get charged. Either decrement a local count in the order transaction, or state the policy for an oversold order (refund or backorder).
- **Test plan.** One happy-path integration test misses where the risk is. Add integration tests for decline, timeout then success, timeout on all attempts, duplicate request, and a crash between charge and commit.
- **Backups.** Nightly backups can lose up to a day of paid orders. Turning on continuous WAL archiving (point-in-time recovery) is cheap and closes this.
- **Confirmation email.** If the send fails after commit, the email is lost. Either accept that explicitly or have the reconcile job resend.

VERDICT: READY WITH CHANGES
