The design is close: the structure section is solid and can be built as written, but the payment steps (3–6) can charge a customer twice or take money without leaving an order record. Fix those two before you write payment code. Both are changes to the flow on paper, not a rethink, so tomorrow is still realistic.

## What must change

**1. Retrying on timeout can charge the customer twice.**
A timeout means "we don't know", not "it failed". Suppose the charge for a $80 order succeeds at the provider in 6 s. The service gave up at 5 s and retries, and the customer is charged $160 when they should be charged $80.

Fix: send an idempotency key (the order id works) with every charge attempt, so the provider treats retries as the same charge. Most providers support this directly.

**2. Rolling back can erase the only record of an order that was paid.**
The `PENDING` insert and the `PAID` update sit in one transaction that stays open across the payment call. Suppose the charge succeeds and then the commit fails, the process crashes, or all three retries time out while the first charge actually went through. The rollback deletes the order row, so the customer is charged $80 and you have no record of it. They should either have a `PAID` order or a refund.

Fix: commit the `PENDING` order first, call the provider outside any transaction, then update to `PAID` or `FAILED` in a second short transaction. Add a small job that asks the provider about orders stuck in `PENDING` and settles them. This also stops a database connection being held for up to 15–20 s per checkout.

```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]
    D -->|success| E[Mark PAID, commit, send email]
    D -->|declined| F[Mark FAILED, return error]
    D -->|still unknown after retries| G[Leave PENDING, job checks provider later]
```

## Decide before building

- **Stock is checked but never reserved or decremented.** With one unit left, two customers who check out in the same 5-minute cache window both pass the check and both pay. At 200 orders a day, accepting this and handling oversells manually may be fine. If so, write in the design that it is accepted and what happens to the second customer.
- **The test plan covers only the happy path.** The money risk is all in the failure paths. Add integration tests against a fake provider for: timeout then success (one charge), a decline (order `FAILED`), and a crash after the charge (the job settles the order).

## What is good as is

- The module boundaries, with no module reading another's tables.
- Thin routes with the flow in `OrderCheckout`.
- Order id on every log line.
- Accepting the 5-minute cache over an 800 ms warehouse call.
- Single Postgres instance, which is plenty for 2,000 orders a day.

VERDICT: READY WITH CHANGES
