**Not ready as written: steps 3 to 6 of the flow can charge a customer twice, or charge them and leave no order behind.** The code structure and the single Postgres instance are fine for 2,000 orders a day; the payment sequence is what needs rewriting.

## Must fix before building

1. **Retrying on timeout can double-charge.** A timeout means "unknown", not "failed".
   - Case: the charge succeeds at the provider in 5.2 s, the service times out at 5 s and retries, and the customer is charged twice for one order.
   - Expected: one charge.
   - Fix: send an idempotency key (the order id) with every charge attempt, so retries are safe.

2. **Rolling back on failure erases the only record of a charge that may have gone through.**
   - Case: all three attempts time out, but the first one actually succeeded. Step 6 rolls back the `PENDING` row and returns an error, so the customer is charged and no order exists.
   - The same happens if the process crashes or the commit fails after a successful charge.
   - Expected: a durable order row that says payment is unresolved.
   - Fix: commit `PENDING` before calling the provider, and add a reconciliation job that resolves stale `PENDING` orders against the provider.

3. **The database transaction stays open across the payment call.** With retries this is 15 s or more per order, holding a connection and row locks. A slow provider then exhausts the connection pool and takes down checkout. Fix 2 removes this as well.

4. **`POST /orders` has no protection against client retries.**
   - Case: the user double-clicks, or the mobile client retries after a dropped response, producing two orders and two charges.
   - Expected: the second request returns the first order.
   - Fix: a client-supplied idempotency key with a unique constraint.

5. **Stock is checked but never reserved.**
   - Case: two customers order the last unit within the same 5-minute cache window. Both pass the check, both are charged, and one order cannot ship.
   - At your volume the stale cache is an acceptable trade, but only with a written rule for the oversold order: either reserve stock locally at checkout, or auto-refund and notify.

6. **The test plan covers only the happy path.** Every risk above is in the failure paths. Add integration tests with a fake provider for: timeout then success, timeout on all attempts, hard decline, duplicate `POST`, and a crash between charge and status update.

## Proposed flow

```mermaid
sequenceDiagram
    Client->>Service: POST /orders (idempotency key)
    Service->>DB: insert order PENDING, commit
    Service->>Provider: charge (key = order id), retry on timeout
    alt charged
        Service->>DB: mark PAID, queue email (same transaction)
    else declined
        Service->>DB: mark FAILED
    else still unknown
        Service->>Client: 202 "processing"
        Reconciler->>Provider: look up charge, then mark PAID or FAILED
    end
```

## Smaller points

- **Confirmation email:** if it is sent after commit and the send fails, it is lost. Writing an "email to send" row in the same transaction as `PAID`, with a worker that sends it, fixes this.
- **Nightly backups:** a disk failure can lose up to a day of paid orders. The reconciliation job can rebuild them from the provider, but say so explicitly, or turn on WAL archiving.

## Decisions for you

- **Provider idempotency:** does your payment provider support idempotency keys? Most do; if not, the retry has to become "look up, then charge".
- **Oversell rule:** reserve locally or auto-refund?

Revising the flow section is a couple of hours of design work. Module scaffolding and the fake payment provider for tests can start tomorrow in parallel, since neither depends on these decisions.

VERDICT: NOT READY
