I wouldn't build the checkout flow as it's written. Two parts of it can lose customers' money. The rest is sound: the module boundaries, the thin routes with an `OrderCheckout` class, and logging with the order id on every line are all good. The volume (about 2,000 orders a day) is easy for a single Postgres instance. The fixes are standard and you can make them in the doc today.

## Must fix before building the payment path

**1. Retrying on timeout can charge customers twice (step 4).** A timeout doesn't mean the charge failed; it may have gone through and only the response was lost. Retrying up to 3 times can charge a customer as many as 4 times.
- Send an idempotency key with every charge (the order id works), and reuse the same key on every retry. Check that your payment provider supports this. Most do.
- Decide what happens when all retries time out. The real result is unknown, so it isn't a failure. Mark the order `PAYMENT_UNKNOWN` and settle it later by asking the provider or waiting for its webhook. Don't tell the client the payment failed.

**2. The payment call runs inside an open database transaction (steps 3–6).** This causes three problems:
- **Charged but no order.** If the charge succeeds and then the commit fails, or the process crashes, the rollback deletes the order. The customer has paid and you have no record of it. Step 6 has the same problem when a "failure" comes after a charge that actually went through.
- **The `PENDING` order is invisible.** It isn't committed, so nothing else can see it or recover it, which makes the status pointless.
- **Long-held connections.** With 4 attempts of 5 s each, a transaction can stay open for about 20 s, tying up a pool connection and locks.

  **Fix:** split it into separate steps:
  1. Insert the order as `PENDING` and commit.
  2. Call the payment provider with no transaction open.
  3. Update the order to `PAID` or `FAILED` in a second short transaction.
  4. Add a reconciliation job that sweeps orders left in `PENDING` or `PAYMENT_UNKNOWN` against the provider.

**3. A client retry creates a duplicate order (step 1).** If the client's `POST /orders` times out and it tries again, you get a second order and possibly a second charge. Accept an `Idempotency-Key` header and enforce it with a unique constraint.

## Should fix (decide now, build soon)

**4. Overselling (step 2).** A cache that's up to 5 minutes old, with nothing reserving or decrementing stock, means you will sell items you don't have. Using the cache is a reasonable call. What's missing is a policy for when it's wrong: reserve stock at order time, or cancel and refund automatically when the warehouse rejects the order.

**5. Email failures (step 5).** If the email is sent inline and fails, don't turn a paid order into an error. Send it after commit through an outbox table or a job queue, with retries.

**6. Tests.** One happy-path integration test misses the paths that matter most. Add tests for:
- a timeout followed by a retry
- every retry timing out
- the charge succeeding and then the database write failing
- a duplicate request with the same idempotency key
- the email failing

**7. Backups.** With nightly backups only, you could lose up to a day of paid orders. Turn on point-in-time recovery (WAL archiving), which is cheap.

**8. Client timeout.** The worst-case request takes about 20 s or more, which is probably longer than the client waits. Either cap the total time or return `202` with the `PENDING` order and let the client poll.

## For tomorrow

You can start on the module skeletons, the inventory cache, the order schema (add the extra statuses and the idempotency-key column) and the logging straight away. Rewrite Flow steps 3–6 using fixes 1–3 before anyone writes `OrderCheckout`.

VERDICT: READY WITH CHANGES
