I wouldn't build from this flow tomorrow. As written, steps 3–6 can charge a customer twice, or take their money and keep no order. The fix is small, an hour or two of redesign, but it changes the order of the steps, so it has to happen before any code. The rest is fine: the context, the module layout, thin routes, the `OrderCheckout` class and logging by order id.

## Must fix before building

**1. Retrying a timed-out charge can charge the customer twice** (step 4)
- **Case:** the provider takes the charge, but its reply takes longer than 5 s. The service retries, and the provider charges again.
- **Wrong result:** the customer pays twice for one order. With 3 retries they can pay up to 4 times.
- **Expected:** one charge.
- **Fix:** send an idempotency key (for example, the order id) with every charge attempt. Most payment providers support this. Retry only when the provider confirms the earlier attempt didn't go through, or when the same key protects the retry.

**2. The charge happens inside an open database transaction** (steps 3–6)
- **Case:** the charge succeeds, then the commit in step 5 fails (connection drop, deploy, crash).
- **Wrong result:** the customer is charged but no order row exists, so there is nothing to reconcile against. The same thing happens if the process dies mid-retry.
- **Expected:** a saved record of every charge attempt.
- **Two more problems with the current order of steps:**
  - The `PENDING` row is never committed, so no one ever sees it. The status does nothing.
  - Each checkout holds a transaction and connection open for up to about 20 s (4 tries × 5 s).
- **Fix:** commit `PENDING` first, outside any transaction that covers the charge. Then charge, then update to `PAID` or `FAILED`. Add a reconciliation job that checks orders stuck in `PENDING` against the provider.

**3. Nothing reserves stock, so popular items will be oversold** (step 2)
- **Case:** one unit is left, and two customers check out within the same 5-minute cache window.
- **Wrong result:** both pass the stock check, both are charged, and one order can't be fulfilled.
- **Expected:** the second customer is turned away, or the doc explicitly accepts overselling.
- **Also undecided:**
  - Nothing ever decrements stock.
  - It doesn't say what happens to a paid order that turns out to have no stock.
- **Decide one of:**
  - Reserve stock with the warehouse system after payment. 800 ms is acceptable at this volume.
  - Accept overselling, and write down the refund or backorder path.

**4. A double-submitted `POST /orders` creates two orders**
- **Case:** a double-click, or a client retry after a slow (up to 20 s) response.
- **Wrong result:** two orders and two charges.
- **Fix:** have the client send an idempotency key, and return the existing order when the key repeats.

## Proposed flow

```mermaid
flowchart TD
    A[POST /orders with idempotency key] --> B{Key seen before?}
    B -- yes --> R[Return existing order]
    B -- no --> C[Check stock in inventory cache]
    C --> D[Insert order as PENDING and commit]
    D --> E[Charge payment provider with order id as idempotency key]
    E -- success --> F[Mark PAID and commit]
    E -- failure --> G[Mark FAILED and commit]
    F --> H[Queue confirmation email]
    E -- unknown / crash --> J[Reconciliation job checks PENDING orders with provider]
```

## Should fix, not blocking

- **Test plan:** one happy-path integration test isn't enough for a payment flow. Add tests for:
  - a charge timeout followed by a successful retry, with exactly one charge
  - a commit failure after a successful charge
  - a duplicate `POST`
  - the out-of-stock path
  - an order stuck in `PENDING` being reconciled
- **Confirmation email:** queue it, or write it to the database in the same commit as the order and send it from there, instead of sending it inline. An email failure shouldn't affect a paid order, and the doc doesn't say what happens when one fails.
- **Backups:** nightly backups mean a bad failure could lose up to a day of paid orders. Turn on point-in-time recovery.
- **Scale is fine:** 2,000 orders a day is about 1.4 a minute on average. A single Postgres instance handles that easily, so nothing needs to change here.

**Next step:** rewrite steps 3–6 as in the diagram, write down the stock decision from issue 3, and expand the test plan. Then it's ready to build.

VERDICT: NOT READY
