**Not ready to build tomorrow.** The module layout is fine. The payment steps (3 to 6) can charge a customer several times and then leave no record of the order. You'd be building that part first, so fix it on paper before writing code. It's probably a few hours of design work.

## Blocking: fix before building

**1. Retrying the charge after a timeout can charge the customer more than once.**
A timeout doesn't mean the charge failed. It means you don't know. The design retries without an idempotency key (a unique ID sent with each charge, so the provider treats repeat requests as the same charge).
- Case: a $50 cart. The provider charges the card, but its reply takes 6 s. The service times out and retries. The same thing happens on all 4 attempts (the first try plus 3 retries).
- Wrong result: the customer is charged $200, gets an error, and no order exists.
- Expected result: one $50 charge and one order.
- Fix: send the same idempotency key on every attempt, for example the order id. **Open question:** confirm your payment provider supports idempotency keys. If it doesn't, you can't safely retry at all.

**2. The PENDING order is written inside the same transaction that gets rolled back, so it records nothing.**
The order is inserted as PENDING (step 3) but not committed until after payment (step 5). Any failure or crash rolls it back, so the PENDING row never survives.
- Case: payment succeeds, then the commit fails or the process restarts.
- Result: the customer has been charged and the database has no trace of the order. Nothing exists to reconcile or refund against.
- Fix: commit PENDING in its own transaction before calling payment, then update the status in a second transaction. This also stops a database connection being held open for up to 20 s of payment calls.

**3. The design has no outcome for "we don't know if payment worked."**
Step 6 treats every failure as a clean failure. When all retries time out, the honest state is "payment unknown." You need:
- a status for it, such as `PAYMENT_UNKNOWN`
- a reconciliation job that asks the provider for the charge's status by its key and moves the order to PAID or FAILED.

**4. The client can create duplicate orders.**
The worst case is a 20 s wait (4 × 5 s), and the client may give up and resend `POST /orders`. Nothing removes the duplicate. Fix: have the client send an `Idempotency-Key` header, store it on the order with a unique constraint, and return the existing order when a key repeats.

## Proposed flow

```mermaid
flowchart TD
    A[POST /orders with Idempotency-Key] --> B{Key already seen?}
    B -- yes --> R[Return existing order]
    B -- no --> C[Check stock in inventory cache]
    C --> D[Commit order as PENDING]
    D --> E[Charge payment, key = order id, same key on retries]
    E -- success --> F[Mark PAID, commit, send email]
    E -- declined --> G[Mark FAILED, return error]
    E -- all retries timed out --> H[Mark PAYMENT_UNKNOWN, tell client it is processing]
    H --> J[Reconciliation job checks provider by key]
    J --> F
    J --> G
```

## Should change, but doesn't block the start

- **The design never says what happens when you oversell.** The 5-minute cache is a reasonable tradeoff at this volume, and the 800 ms warehouse call is a fair reason for it. But stock is only checked, never reserved or reduced. When two customers buy the last unit inside one 5-minute window, what happens: refund, backorder, or cancel? Write that rule down now, even if it's a manual refund.
- **Nightly backups can lose up to a day of paid orders.** At 2,000 orders a day, a failure late in the day could lose around 2,000 records of real charges. Turn on point-in-time recovery (continuous WAL archiving). It's cheap.
- **One happy-path integration test isn't enough for a payment flow.** Your own standards ask for failure cases too. Add tests for:
  - a timeout followed by a successful retry (exactly one charge)
  - all retries timing out (order ends up `PAYMENT_UNKNOWN`)
  - a decline
  - a repeated client `Idempotency-Key`
  - the commit failing after a successful charge
- **Decide what happens when the confirmation email fails.** It runs after the commit, which is right. Either log it and accept the occasional lost email, or retry it from a queue.

## Fine as written

- Separate modules that don't read each other's tables, thin routes, and an `OrderCheckout` service class.
- A single Postgres instance at 2,000 orders a day (about 1.4 a minute on average). Don't add scaling work.
- Structured logs with the order id on every line. Also log the payment idempotency key so you can match logs against the provider's records.

## Next steps

Rewrite steps 3 to 6 to match the flow above, add the `PAYMENT_UNKNOWN` status and the reconciliation job, confirm your provider supports idempotency keys, and write down the oversell rule. After that, it's ready to build.

VERDICT: NOT READY
