I wouldn't start building from this tomorrow. The module layout, thin routes and logging are fine, and so is the scale: 2,000 orders a day is about 1.4 a minute, which one Postgres instance handles easily. The problem is the payment part of the flow (steps 3–6). As written, it can charge a customer twice, or take their money and keep no record of the order. Rewriting those four steps should take an hour or two, so the fix is small.

## Blocking: fix these before writing checkout code

**1. Retrying after a timeout without an idempotency key will double-charge customers (step 4).**
A timeout does not mean the charge failed. Often the provider charged the card and the reply was simply slow.
- Case: the provider charges $50, but its reply takes 6 s. The service gives up at 5 s and retries, and the provider charges another $50. Expected: one $50 charge.
- Fix: send an idempotency key with every charge, using the order id. Reuse the same key on every retry. Most payment providers support this; check that yours does before you build.

**2. Calling the payment provider while the database transaction is open can charge a customer with no order saved (steps 3–6).**
- Case: the charge succeeds, then the commit fails (connection drop, deploy, crash). The transaction rolls back, so the `PENDING` row never existed. The customer paid $50 and you have no order. Expected: a saved order whose payment you can look up and fix.
- The open transaction is also held for up to about 20 s (four attempts × 5 s) and locks rows while it waits on an outside service.
- Fix: commit `PENDING` first in its own transaction. Then call the provider. Then record the result in a second transaction.

**3. The design doesn't say what happens when every retry times out.**
After three timeouts you don't know whether the customer was charged. "Roll back and return an error" is wrong here: the customer may have paid. You need a third outcome, such as `PAYMENT_UNKNOWN`, plus a small reconciliation job that asks the provider what happened.

**4. A client retry on `POST /orders` creates a duplicate order.**
The client can wait up to about 20 s, and a browser or mobile client that waits that long will often retry. Each retry creates a new order and a new charge. Fix: the client sends an `Idempotency-Key` header, and the orders table has a unique constraint on it.

## 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[Transaction 1: insert order PENDING, commit]
    D --> E[Charge provider, idempotency key = order id, retry on timeout]
    E -- success --> F[Transaction 2: mark PAID]
    E -- declined --> G[Transaction 2: mark FAILED]
    E -- all retries timed out --> H[Mark PAYMENT_UNKNOWN]
    H --> J[Reconciliation job asks provider, sets PAID or FAILED]
    F --> K[Queue confirmation email]
```

## Decide before building, but these don't block

- **Overselling (step 2).** A cache that can be 5 minutes old, with no stock reservation, means two customers can both buy the last unit. At this volume, accepting that and handling it afterwards (refund and apologise) is a reasonable call. Write the decision down so nobody finds out from a customer.
- **Confirmation email (step 5).** If sending the email throws an error, it must not fail an order that is already paid. Send it after the commit, from a queue or an "outbox" table (a table of messages still to be sent, which a background job works through), with retries.
- **Nightly backups.** Losing the database mid-day loses up to 24 hours of paid orders. Orders are financial records, so turn on point-in-time recovery, which restores the database to any moment instead of only to last night.
- **Test plan.** One happy-path integration test misses where the real risk is. Add integration tests for: payment timeout followed by a successful retry (exactly one charge), card declined, all retries timing out (order ends as `PAYMENT_UNKNOWN`), a crash after the charge but before marking the order `PAID`, a duplicate `POST /orders`, and the email failing after payment.

## What you can start tomorrow

The module skeletons (orders, payments, inventory), the routes and the logging don't depend on any of this. You can build those while steps 3–6 are rewritten.

VERDICT: NOT READY
