## Is design.md ready to build from?

No. The code structure section is fine. The checkout flow in steps 3–6 has two problems that will charge customers money with no order behind it, and a few more gaps. None of them takes long to fix on paper, but they decide how `OrderCheckout` is built, so fix them before you write it.

### Blockers

**1. Retrying a timed-out charge will double-charge people (step 4).** A timeout doesn't mean the charge failed. It means you don't know. If the provider charged the card and the response was slow, each of your 3 retries can charge it again. The design says nothing about an idempotency key.
- **Fix:** send an idempotency key with every charge (the order id works). Retry only with that same key. Check that your provider supports this; most do.

**2. The payment call runs inside an open database transaction (steps 3–6).** This causes three problems:
- **Charged but no order.** The charge times out, or Postgres fails to commit after a successful charge. The transaction rolls back, and the `PENDING` row vanishes with it. The customer has paid, and you have no record that they did.
- **The `PENDING` status is useless.** It's never committed, so nothing outside the transaction can see it. No support tool or cleanup job can ever find a stuck order.
- **Connections are held too long.** One transaction can stay open for over 20 seconds (4 attempts × 5 s). At your volume that's survivable, but it's still the wrong shape.
- **Fix:** split it into separate steps:
  - Commit the order as `PENDING`.
  - Charge the card outside any transaction.
  - In a second transaction, mark the order `PAID` or `FAILED`.
  - Keep failed orders as `FAILED` rather than deleting them.

**3. There's no plan for "we don't know if they paid".** After the retries run out, the design returns an error. But the customer may well have been charged.
- **Fix:** add an outcome such as `PAYMENT_UNKNOWN`. Resolve it with the provider's webhooks, plus a scheduled job that checks old `PENDING` orders against the provider. This is the piece teams most often skip and most often regret.

**4. A client retry creates a duplicate order.** `POST /orders` has no idempotency key. The client can easily give up during that 20-second wait and resend, which creates a second order and a second charge.
- **Fix:** require an `Idempotency-Key` header and store it with a unique constraint.

### Should fix before building

**5. Stock is checked against a cache but never reserved (step 2).** The cache can be 5 minutes out of date, and nothing holds the stock once it's checked. Two orders can both pass for the last unit. The design doesn't say what happens when you've taken payment for something you can't ship.
- **I disagree with the reasoning here.** At 2,000 orders a day you get about one order a minute, so an 800 ms warehouse call at checkout is perfectly affordable. The cache is avoiding a cost you don't really pay. Either call the warehouse directly, or keep the cache and reserve stock (decrement or hold it) inside the order transaction. Also write down what happens when you oversell anyway: refund, backorder, or something else.

**6. Prices.** "Sends the cart" doesn't say whether the server trusts prices sent by the client. State that the server works out prices and totals itself.

**7. Confirmation email (step 5).** The design doesn't say what happens if sending fails, or if the process crashes between the commit and the send.
- **Fix:** in the same transaction that marks the order `PAID`, write a row to an outbox table. A background worker then sends the email from that row.

**8. Nightly backups could lose a day of paid orders.** If the database dies at 6 pm, everything since last night is gone, including records of customers you charged. Turn on point-in-time recovery (WAL archiving, or the managed version). It's cheap, and you should decide on it now.

**9. Tests cover the wrong path.** One happy-path integration test misses where the risk is. Add integration tests for these cases:
- charge times out, then succeeds on retry
- charge times out and stays unknown
- commit fails after a successful charge
- the same `Idempotency-Key` sent twice
- stock runs out between the check and the charge

### Fine as is
- Separate modules that don't read each other's tables, and thin routes: good.
- Structured logging with the order id: good. Also add a metric and an alert for the number of orders stuck in `PENDING` or `PAYMENT_UNKNOWN`.
- Scale: at about one order a minute, don't add queues, sagas or microservices beyond the outbox. One Postgres instance is enough.

### Revised flow
1. The client sends `POST /orders` with an `Idempotency-Key`. The server works out the prices.
2. Check stock and reserve it, either through a direct warehouse call or a reservation in the order transaction.
3. Insert the order as `PENDING` and commit.
4. Call the charge API with the order id as the idempotency key. Retry only with that same key. No transaction is open during this step.
5. In one transaction, mark the order `PAID` and write the email to the outbox. On failure, mark it `FAILED`. If the result is still unknown, mark it `PAYMENT_UNKNOWN`.
6. Webhooks and the scheduled check settle `PENDING` and `PAYMENT_UNKNOWN` orders. A worker sends the emails from the outbox.

If you rewrite steps 2–6 along these lines and make decisions on points 5–8, I'd be comfortable with you starting tomorrow.

VERDICT: NOT READY
