# Review of design.md (checkout flow, order-service)

**It isn't ready to build from yet.** The way money moves through the flow (steps 3–6) can charge a customer more than once, and it can take a customer's money while leaving no record of the order. The fixes are well known and probably take an evening to write into the doc. They change the core flow, though, so fix the design before writing code against it.

The rest holds up. Your scale numbers fit a single Postgres instance: 2,000 orders a day is about 1.4 a minute. The module boundaries, the thin routes, the `OrderCheckout` class and logging the order id on every line are all sound.

## Blocking problems

**1. Retrying the charge on timeout can charge the customer up to 4 times.**
A timeout doesn't mean the charge failed. It means you don't know whether it went through.
- *Case:* a customer checks out a $50 cart. The provider charges the card but the reply takes 6 s, so your call times out and you retry. The same thing happens three more times.
- *What happens:* up to 4 charges of $50, with one order.
- *What should happen:* one charge of $50.
- *Fix:* send an idempotency key with every charge attempt, using the order id. Most payment providers support this, so check that yours does. All retries must reuse the same key.

**2. Calling the payment provider inside the database transaction can lose paid orders.**
Because the `PENDING` insert is in the same transaction as the charge, a rollback or crash erases the only record that a charge was attempted.
- *Case:* the charge succeeds, then the database commit fails or the process restarts.
- *What happens:* the customer is charged and there's no order row. You have nothing to refund from or reconcile against.
- *What should happen:* a `PENDING` row exists, so you can see the charge and finish or refund it.
- *Fix:* commit `PENDING` in its own transaction first. Then call the provider outside any transaction, then update the status in a second transaction. A secondary benefit: you stop holding a transaction open for up to 20 s (4 attempts × 5 s).

**3. "All retries timed out" is treated as "failed", but the payment state is unknown.**
- *Case:* all four attempts time out, but the provider did charge the card.
- *What happens:* the order is rolled back and the client sees an error, so the customer pays and is told it failed.
- *What should happen:* the order is held in an explicit unknown state until it's checked.
- *Fix:* add a `PAYMENT_UNKNOWN` status, plus a small reconciliation job that asks the provider for the charge by idempotency key and moves the order to `PAID` or `FAILED`.

**4. A client retrying `POST /orders` creates duplicate orders.**
- *Case:* a mobile client loses its connection after sending the request and retries.
- *What happens:* two orders and two charges. Each has its own order id, so the fix for problem 1 won't catch it.
- *What should happen:* the second request returns the first order.
- *Fix:* require a client-supplied `Idempotency-Key` header on `POST /orders` and store it with a unique constraint.

Corrected order lifecycle:

```mermaid
stateDiagram-v2
    [*] --> PENDING: insert and commit
    PENDING --> PAID: charge succeeds
    PENDING --> FAILED: charge declined
    PENDING --> PAYMENT_UNKNOWN: all retries timed out
    PAYMENT_UNKNOWN --> PAID: reconciliation finds the charge
    PAYMENT_UNKNOWN --> FAILED: reconciliation finds no charge
    PAID --> [*]: send confirmation email
```

## Should fix, but they don't block building

**5. Stock is checked but never reserved or reduced.** Two customers can both buy the last unit, and the 5-minute-old cache makes this more likely. Overselling may be acceptable at your volume, but the doc should say so and say what happens when it occurs: a refund, a backorder, or an apology email. Otherwise it will be handled ad hoc in production.

**6. Nightly backups can lose up to a day of paid orders.** A disk failure at 11 pm loses every order since the last backup, and those customers have already been charged. Point-in-time recovery (continuous WAL archiving, or the equivalent on a managed Postgres) is cheap insurance.

**7. Email failure is unspecified.** If sending fails after the commit, is the email retried, or is it lost? The usual pattern is an outbox table: write the email job in the same transaction that marks the order `PAID`, and have a worker send it.

**8. The test plan covers only the case that's least likely to break.** One happy-path integration test leaves untested the cases above that cost money. Add integration tests for:
- a charge that times out and then succeeds on retry, which must produce one charge
- every retry timing out, which must end in `PAYMENT_UNKNOWN`
- a crash after the charge and before the status update
- a duplicate `POST /orders` with the same key
- a declined card

A fake payment provider that can be set to delay or fail makes all of these straightforward.

## What to do tonight

Rewrite the Flow section with the three-transaction structure from problem 2, the two idempotency keys from problems 1 and 4, and the `PAYMENT_UNKNOWN` status with reconciliation from problem 3. Add the failure-path tests from problem 8 to the test plan. With those in, you can start tomorrow, and items 5–7 can be decided while you build.

VERDICT: NOT READY
