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

**The code structure is fine, but the payment steps (3 to 6) will charge some customers twice, or charge them with no order saved.** Fix those steps in the doc before you write checkout code. It's roughly an hour of design work, and you can still scaffold the modules tomorrow.

## Must fix before building

**1. Retrying the charge after a timeout can charge the customer twice (step 4).**
A timeout doesn't mean the charge failed. It means you don't know whether it went through.
- Case: the charge call times out at 5 s, but the provider did charge the card. The service retries and the provider charges again.
- What happens: the customer pays twice for one order.
- What should happen: one charge.
- Fix: send an idempotency key with every charge call, derived from the order id. Payment providers use this key to recognise a repeat and return the first result instead of charging again.

**2. The charge happens inside an open database transaction (steps 3 to 6). If anything fails after the charge, the order record is lost.**
- Case: the charge succeeds, then the commit fails or the process crashes before step 5.
- What happens: the transaction rolls back, so the `PENDING` row disappears. The customer is charged and you have no record of the order.
- What should happen: an order row you can match to the charge.
- Same thing when all retries time out: step 6 rolls back and tells the client "error", but the charge may have gone through. The client retries and you're back at problem 1.
- Holding the transaction open for up to about 20 s (4 attempts × 5 s) also ties up a database connection and any locks for that whole time.
- Fix: commit `PENDING` first in its own short transaction. Call the provider outside any transaction. Then update the row in a second transaction.

**3. Add a "result unknown" state and a way to resolve it.**
When retries run out, set the order to something like `PAYMENT_UNKNOWN`, not deleted. A reconciliation job (a periodic check that asks the provider what actually happened, using the idempotency key) then moves it to `PAID` or `FAILED`. Tell the client the order is processing, not that it failed.

**4. A double-click or client retry on `POST /orders` creates two orders.**
Have the client send an idempotency key with `POST /orders`. If the same key comes in again, return the existing order.

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 inventory cache]
    C --> D[Insert order PENDING and commit]
    D --> E[Charge provider with key = order id, retry on timeout]
    E -- success --> F[Mark PAID, commit]
    E -- declined --> G[Mark FAILED, commit, return error]
    E -- retries exhausted --> H[Mark PAYMENT_UNKNOWN, return 'processing']
    H --> J[Reconciliation job asks provider, sets PAID or FAILED]
    F --> K[Queue confirmation email]
```

## Should decide, not blocking

- **Overselling from the 5-minute cache (step 2).** Two customers can each buy the last unit, and nothing reserves or decrements stock. Accepting the stale cache is a reasonable call at this volume, but the doc needs to say what happens to a paid order that turns out to have no stock: refund, backorder, or manual follow-up.
- **The confirmation email (step 5).** The doc doesn't say whether a failed email send fails the request. Send it after commit through a queue or an outbox table (a table of messages to send, written in the same transaction as the order) so the email can never undo a paid order.
- **Nightly backups mean losing up to 24 hours of paid orders.** For payment records, turn on point-in-time recovery (Postgres WAL archiving, or your managed provider's equivalent). Load isn't a concern: 2,000 orders a day is under 2 a minute, so one Postgres instance is plenty.
- **Client latency.** Retries can make a request hang for about 20 s, and the client may give up and retry before then. Use fewer retries or shorter timeouts, or rely on the `PAYMENT_UNKNOWN` path.

## Test plan gaps

One happy-path integration test isn't enough for code that moves money. Add tests for:
- a timeout followed by a successful retry, checking the provider sees exactly one charge
- retries exhausted, leading to `PAYMENT_UNKNOWN` and then reconciliation
- a declined charge
- a crash between the charge and the commit
- a duplicate `POST /orders` with the same key
- a paid order that turns out to have no stock

Use a fake payment provider that can time out after it has already charged. That's the case that catches problem 1.

## What's good

The module boundaries, thin routes, the `OrderCheckout` service class, and order id on every log line are all sound. Keep them.

VERDICT: NOT READY
