**Not quite as written: the structure is sound, but the payment step (steps 3–6) can charge a customer twice, or charge them and leave no order record.** Fix that on paper before you build that part; the rest can start tomorrow.

## What must change before building the payment step

**1. Retrying on timeout can double-charge.**
- **Case:** the charge request reaches the provider, the card is charged, but the response takes 6 s. The service gives up at 5 s and retries, and the provider charges again.
- **Result:** one order, up to four charges. It should be one.
- **Fix:** send an idempotency key (the order id works) with every charge attempt, so the provider treats retries as the same charge.

**2. Rolling back on failure can erase the only record of a charged order.**
- **Case:** all attempts time out, but one of them did go through at the provider. Step 6 rolls back, so the `PENDING` row disappears.
- **Result:** the customer is charged and the database has no order to refund or reconcile against. It should leave an order you can resolve.
- **Same outcome** if the process crashes between the charge and the commit.
- **Fix:** commit the `PENDING` order first, call the provider outside any transaction, then record the result in a second short transaction.
- **Unknown outcomes:** an order whose charge result is unknown stays `PENDING`, and a provider webhook or a small reconciliation job settles it.
- **Side benefit:** you no longer hold a database transaction open for up to 20 s while waiting on the provider.

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

## Decisions to write down (not blockers for starting)

- **Overselling:** nothing in the flow reserves or decrements stock. If the cache says 1 left and two customers order within the same 5 minutes, both orders are accepted. Accepting the cache is reasonable at this volume, but either decrement the cached count on each order or state what happens to the second customer (backorder or refund).
- **Duplicate submissions:** if the client retries `POST /orders` after a dropped connection, you get two orders. A client-supplied idempotency key on the request covers this.
- **Tests:** one happy-path integration test leaves the riskiest code untested. Add integration tests for payment timeout then success, decline, and unknown outcome.

## What is already right

- Module boundaries with no cross-module table reads.
- Thin routes with the flow in `OrderCheckout`.
- Order id on every log line.
- A single Postgres instance with no queues or extra services, which fits 2,000 orders a day.

VERDICT: READY WITH CHANGES
