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

The design is not ready. The module layout is sound, but steps 3 to 6, the part that takes money, can charge customers twice and can charge them while leaving no order on record. Both problems are known and fixable, but the fix changes how the flow is shaped, so redo steps 3 to 6 before you write `OrderCheckout`. You can still start tomorrow on the module scaffolding, which is fine as written.

## Blocking problems

**1. Retrying a timed-out charge can charge twice.** A timeout doesn't mean the charge failed. It means you don't know.
- Case: the provider charges the card, but its response takes 6 s. Your 5 s timeout fires and you retry. The customer is charged twice for one order.
- Expected: one charge.
- Fix: send an idempotency key with every charge (e.g. the order id). Most payment providers support this. Retries then return the original result instead of charging again.

**2. The payment call runs inside an open database transaction, so a rollback can erase a real charge.**
- Case: the charge succeeds, then the commit fails (connection drop, deploy, crash). The `PENDING` row is rolled back and disappears. The customer has paid and you have no order.
- Expected: an order row that shows a charge was attempted, so someone can reconcile it.
- Same case for step 6: after 3 timeouts, "roll back and return an error" deletes the only record of attempts that may have gone through.
- This also holds a transaction open for up to about 20 s (4 attempts × 5 s).
- Fix: commit `PENDING` before calling the provider. Then update to `PAID`, `FAILED`, or `PAYMENT_UNKNOWN` in a second transaction. Add a reconciliation job that checks `PAYMENT_UNKNOWN` and stale `PENDING` orders against the provider.

**3. `POST /orders` has no idempotency key, so a double-click creates two orders and two charges.** Have the client send a key and store it with a unique constraint.

```mermaid
flowchart TD
    A[POST /orders with idempotency key] --> B{Key already seen?}
    B -- yes --> R[Return the existing order]
    B -- no --> C[Check stock]
    C --> D[Insert order as PENDING and commit]
    D --> E[Charge with idempotency key = order id]
    E -- success --> F[Mark PAID]
    E -- declined --> G[Mark FAILED]
    E -- timeouts used up --> H[Mark PAYMENT_UNKNOWN]
    H --> J[Reconciliation job asks provider]
    F --> K[Queue confirmation email]
```

## Decisions the doc needs to make

**4. Overselling from the 5-minute stock cache.** Nothing reserves or decrements stock when an order is placed. Case: one unit left, two customers check out within the same 5 minutes, both pass the check, both are charged, and one order can't be filled. That may be acceptable at your volume, but the doc should say so and say what happens next (refund, backorder, or customer contact). If it isn't acceptable, decrement a local stock count inside the order transaction.

**5. Nightly backups mean you can lose up to a day of paid orders.** With this approach, a disk failure at 23:00 loses every order since the last backup, while the payment provider still has the charges. Turn on point-in-time recovery (continuous WAL archiving) or a managed equivalent. It's cheap, and payment records are the data you can least afford to lose.

## Smaller changes

- **Email:** sending it inline means a slow or failing mail server delays or fails a paid checkout. Queue it after commit (an outbox table works) and retry separately.
- **Test plan:** one happy-path integration test misses the risky cases. Add integration tests for: charge timeout followed by a successful retry (exactly one charge), commit failure after a successful charge, a duplicate `POST` with the same key, a decline, and retries running out (→ `PAYMENT_UNKNOWN`). Write these first; they define the redesigned flow.

## What's fine as written

- **Scale:** 2,000 orders a day is about 1.4 a minute, well within a single Postgres instance.
- **Code structure:** module boundaries with no reads across modules' tables, thin routes, logs keyed by order id. All good.

## Next steps

Rewrite steps 3 to 6 to match the diagram, decide on overselling, enable point-in-time recovery, and expand the test plan. That's probably half a day of design work, and after it the doc would be ready.

VERDICT: NOT READY
