The code structure is ready to build on, but the payment steps (3 to 6) will charge customers twice or charge them without recording an order, so those steps need to change before you write the payment code. The fix is a reordering of the steps plus one key sent to the payment provider.

## What must change before building

**1. Retrying a timed-out charge can charge the customer twice.**
- **Case:** the provider processes the charge but takes 6 s to answer. The service gives up at 5 s and retries, and the provider charges again.
- **Expected:** one charge.
- **Fix:** send an idempotency key (the order id) with every charge call, so the provider treats retries as the same charge.

**2. Rolling back after a failure can erase the only record of a charged order.**
- **Case:** all attempts time out, but one actually succeeded at the provider. Step 6 rolls back, the order row disappears, and the customer has paid for an order you have no trace of. The same happens if the service crashes between the charge and the commit.
- **Expected:** a stored order you can reconcile or refund.
- **Fix:** commit the `PENDING` order first, call the provider outside any transaction, then update to `PAID` or `FAILED` in a second short transaction. As written, `PENDING` is never visible to anyone because it lives inside the uncommitted transaction.

**3. The database transaction stays open during the payment call.**
With a 5 s timeout and 3 retries, one checkout can hold a connection and row locks for up to 20 s. The fix for item 2 removes this too.

**4. A client retry of `POST /orders` creates a second order and a second charge.**
- **Case:** a double-click, or a mobile retry on a flaky network.
- **Fix:** accept a client-supplied idempotency key and put a unique constraint on it.

```mermaid
sequenceDiagram
    participant C as Client
    participant S as OrderCheckout
    participant DB as Postgres
    participant P as Payment provider
    C->>S: POST /orders (idempotency key)
    S->>DB: insert order PENDING, commit
    S->>P: charge (key = order id), retry on timeout
    P-->>S: success / declined / still unknown
    S->>DB: update to PAID or FAILED, commit
    S-->>C: result
    S->>C: confirmation email (after commit)
```

Orders still `PENDING` after the retries need a small reconciliation job that asks the provider what happened to the charge. An email failure should not fail the request.

## What needs a decision, not a redesign

- **Overselling:** the stock cache can be 5 minutes stale, and the flow never reserves or decrements stock. Two customers can both buy the last unit. At 200 orders a day, accepting this may be fine, but the design should say what happens then (refund, backorder) and where stock gets decremented.
- **Tests:** one happy-path integration test leaves the risky paths uncovered. Add integration tests for timeout-then-success, declined card, duplicate `POST`, and a crash after the charge.
- **Backups:** nightly backups mean a bad day loses up to 24 hours of paid orders. Continuous WAL archiving (point-in-time recovery) closes that gap; it can follow launch, but write the risk down.

## What is already right

- The module boundaries, with no module reading another's tables.
- Thin routes, with the flow in one `OrderCheckout` class.
- The order id on every log line.
- Accepting the cache over an 800 ms warehouse call is a reasonable trade at this volume.
- Single-instance Postgres is the right size for 2,000 orders a day.

You can start tomorrow on the module skeletons, the order model and the tests while you update steps 3 to 6 in the doc.

VERDICT: READY WITH CHANGES
