**Not ready yet.** The structure section is fine, but the payment part of the flow (steps 3–6) can charge customers twice, or take their money and leave no order on record. Both need a design change, not just care during coding. The fix is a few hours of redesign, and you can still start tomorrow on the parts that don't depend on it (listed at the end).

## Must fix before building checkout

**1. Retrying the charge can bill the customer twice.** Step 4 retries on a 5 s timeout, but a timeout doesn't mean the charge failed. It means you don't know whether it went through.
- *Case:* the provider charges $80 and its reply arrives after 6 s. The service gives up at 5 s and retries, and the retry also succeeds.
- *Result:* the customer pays $160, or up to $320 if all 3 retries succeed.
- *Expected:* one charge of $80.
- *Fix:* send an idempotency key (the order id) on every charge call, so the provider treats a retry as the same charge. First confirm the provider supports this. If it doesn't, you have to check the payment's status before each retry instead of retrying blindly.

**2. The database transaction is held open during the payment call, so a failure can lose a paid order.** Step 3 opens the transaction and the commit doesn't happen until step 5, after the charge.
- *Case:* the charge succeeds, then the commit fails (connection drop, deploy, crash).
- *Result:* the `PENDING` row is rolled back. The customer has been charged, and there's no order or any record that the payment was attempted.
- *Expected:* an order row exists showing the payment, so someone can fulfil or refund it.
- The same structure has two more effects:
  - **`PENDING` is never visible.** It's never committed, so nothing can ever see it.
  - **Connections stay tied up.** Each checkout can hold a database connection for 20 s or more (4 attempts × 5 s).
- *Fix:* commit `PENDING` before calling the provider, then update the status in a second transaction afterwards.

**3. There's no outcome for "still unknown after all retries."** Step 6 treats every non-success as a failure: it rolls back and returns an error. After a final timeout, though, the money may already have been taken. You need a state for this, for example `PAYMENT_UNKNOWN`, plus a job that asks the provider for the real result and then marks the order `PAID` or `FAILED`.

**4. `POST /orders` itself has no protection against duplicates.**
- *Case:* the user double-clicks, or the client times out during a 20 s retry loop and resends.
- *Result:* two orders, and two charges.
- *Expected:* one order.
- *Fix:* the client sends an `Idempotency-Key` header. The service stores it with a unique constraint and returns the existing order if the key repeats.

Proposed flow:

```mermaid
flowchart TD
    A["POST /orders + Idempotency-Key"] --> B{Key seen before?}
    B -- yes --> R[Return existing order]
    B -- no --> C[Check stock in inventory cache]
    C --> D["Tx 1: insert order PENDING, commit"]
    D --> E["Charge API, idempotency key = order id<br/>retry on timeout with same key"]
    E -- success --> F["Tx 2: mark PAID + add email to outbox, commit"]
    E -- declined --> G["Mark FAILED, return error"]
    E -- still timing out --> H["Mark PAYMENT_UNKNOWN<br/>reconciliation job resolves it later"]
    F --> I[Email worker sends confirmation]
```

## Should decide, not blocking

5. **Overselling.** The stock check reads a cache that can be up to 5 minutes old, and nothing reserves or decrements stock.
   - *Case:* two customers buy the last unit within the same 5-minute window. Both are charged, and only one item exists.
   - That may be acceptable at your volume, but the design should say what happens next: refund, backorder, or a reservation step.
6. **The confirmation email is sent inline.** If the process dies right after the commit, the email never goes out. And it's unclear whether a failed send should fail the request. Writing the email to an "outbox" table in the same transaction as `PAID`, with a worker sending from there (as in the diagram), covers both.
7. **Backups.** Nightly backups mean you could lose up to 24 hours of paid orders. Point-in-time recovery (WAL archiving, or the managed Postgres equivalent) is cheap, and it matters once real money is involved.
8. **Test plan.** "One integration test for the happy path" doesn't test the dangerous paths. Add integration tests for:
   - a timeout followed by a successful retry (exactly one charge)
   - a final timeout (order ends as `PAYMENT_UNKNOWN`)
   - a commit failure after the charge
   - a duplicate request with the same `Idempotency-Key`
   - a declined charge

   Use a fake payment provider so you can force each case.

## What's fine as written
- **Scale:** 2,000 orders a day is about one every 40 seconds, so a single Postgres instance and a 5-minute cache are comfortably enough.
- **Code structure:** the module boundaries (no reading other modules' tables), thin routes with an `OrderCheckout` service, and logging with the order id on every line are all sound.

## What you can start tomorrow
You can start the module scaffolding, the inventory cache and its refresh job, the orders schema (add the status values above and the idempotency-key column), and the fake payment provider for tests. Hold off on the `OrderCheckout` flow until you've settled items 1–4 and confirmed the provider supports idempotency keys.

VERDICT: NOT READY
