The design is close, but the payment step (steps 3 to 6) can charge a customer twice or charge them with no order on record. Fix that on paper before building; the rest is sound enough to start tomorrow.

## What must change: the payment step

**1. Retrying on timeout can double-charge.**
A timeout does not mean the charge failed. For example, the provider takes 6 s to process a charge and succeeds. The service gave up at 5 s and retries, and the second charge also succeeds. The customer is charged twice for one order.

Fix: send an idempotency key (the order id) with every charge call, so the provider treats retries as the same charge.

**2. Rolling back on failure can erase the only record of a charged order.**
Suppose all three attempts time out but one actually went through at the provider. Step 6 rolls back, so the `PENDING` row is gone. The customer has been charged, has an error on screen, and you have no row to reconcile or refund against. The same happens if the charge succeeds and the commit in step 5 then fails.

Fix: commit the `PENDING` order before calling the provider. Then record the result in a second short transaction as `PAID`, `FAILED` or, after timeouts, `PAYMENT_UNKNOWN`. A small reconciliation job, or the provider's webhook, resolves the unknown ones.

**3. The database transaction is held open during the provider call.**
With three 5 s attempts, that is up to 15 s per checkout holding a connection and locks. It is tolerable at 200 orders a day, but a slow provider at 2,000 a day can exhaust the connection pool. The fix for item 2 removes this as well.

The corrected flow:

```mermaid
flowchart TD
    A[POST /orders] --> B[Check stock]
    B --> C[Insert order as PENDING and commit]
    C --> D[Charge with idempotency key = order id, retry on timeout]
    D -->|success| E[Mark PAID, commit, send email]
    D -->|declined| F[Mark FAILED, return error]
    D -->|still timing out| G[Mark PAYMENT_UNKNOWN, reconcile later]
```

## Smaller additions to the doc

- **Overselling:** nothing in the flow reserves or decrements stock. Two customers can both buy the last unit within the same 5-minute cache window. Accepting the cache is reasonable at this volume, but write down what happens when it occurs: either a refund and apology process, or a local decrement at order time.
- **Duplicate submissions:** a double-click or a client retry on `POST /orders` creates two orders. Accept a client-supplied idempotency key on the request.
- **Test plan:** one happy-path integration test leaves out the cases that cost money. Add integration tests for timeout then success, a declined charge, a commit failure after the charge, and a duplicate POST.

## What is already good

- The module boundaries, with no module reading another's tables.
- Thin routes with the flow in `OrderCheckout`.
- Logging the order id on every line.
- A single Postgres instance is right for this scale.
- Sending the confirmation email after the commit is the correct order.

VERDICT: READY WITH CHANGES
