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

**You can start tomorrow, but not on the payment steps as written.** Steps 3–6 can charge a customer more than once, and they can take a customer's money without keeping the order. Both fixes are well known and small, and you can make them in the doc tonight. The context, the module layout and the scale assumptions are fine. At 2,000 orders a day, one Postgres instance is more than enough.

## Must change before building

**1. Retrying the charge can bill the customer up to four times (step 4).**
- What happens: the provider charges the card but its reply takes longer than 5 s. The service treats that as a failure and retries.
- Wrong result: up to 4 charges for one order (the first try plus 3 retries).
- Expected: exactly one charge.
- Fix: send an idempotency key on every charge call, for example one derived from the order id. Most payment providers support this. Retries are only safe with a key. Also say what happens when all retries time out: the payment state is *unknown*, not *failed*. The order should stay `PENDING` so a reconciliation job can check it with the provider. It should not be rolled back.

**2. Calling the payment provider inside an open database transaction (steps 3–6).**
- Lost charge: the charge succeeds, then the commit fails or the process crashes. The rollback erases the `PENDING` order, so the customer has paid and you have no record of the order.
- Wasted connections: each checkout can hold a transaction and a DB connection for up to about 20 s (4 tries × 5 s).
- `PENDING` means nothing as designed: no other process can see the row until commit, so nothing can ever act on it.
- Fix: commit `PENDING` first, charge outside the transaction, then run a second short transaction to set `PAID` (or `PAYMENT_FAILED`).

**3. Two submissions of the same cart create two orders (step 1).**
- What happens: a double-click or a client retry sends `POST /orders` twice.
- Wrong result: two orders and two charges. The fix in item 1 doesn't help, because each order gets its own key.
- Fix: accept a client-supplied `Idempotency-Key` header on `POST /orders` and return the existing order when you see the same key again.

**4. Only the happy path has an integration test (Code structure).** Fixes 1–3 are where the money risk is, so each needs a test:
- the charge times out, then succeeds on retry, and you see exactly one charge
- all retries time out, and the order stays `PENDING`
- the charge succeeds but the `PAID` update fails, and reconciliation fixes it
- the same `POST /orders` is sent twice, and you get one order back

Revised flow:

```mermaid
sequenceDiagram
    participant C as Client
    participant S as OrderCheckout
    participant DB as Postgres
    participant P as Payment provider
    C->>S: POST /orders (Idempotency-Key)
    S->>S: Check stock (inventory cache)
    S->>DB: Insert order PENDING, commit
    S->>P: Charge (key = order id), retry on timeout
    alt charged
        S->>DB: Set PAID, commit
        S-->>C: 201 + order
        S->>S: Queue confirmation email
    else declined
        S->>DB: Set PAYMENT_FAILED, commit
        S-->>C: Error
    else unknown after retries
        S-->>C: 202 "processing"
        Note over S,DB: Reconciliation job checks PENDING orders with the provider
    end
```

## Decide, but these don't block day one

- **Overselling (step 2).** The cache can be up to 5 minutes old and nothing reserves stock, so two customers can both buy the last unit. That may be acceptable, but write down what happens when it occurs (refund? backorder?). Alternatively, call the warehouse system directly only when stock is low.
- **Confirmation email (step 5).** If sending fails after commit, the doc doesn't say whether the customer still gets a success response. Send the email asynchronously, for example from a queue or a table of messages waiting to be sent, so a mail outage can't fail checkout.
- **Nightly backups.** These can lose up to 24 hours of paid orders. Point-in-time recovery (continuous WAL archiving) is cheap insurance for payment data.

VERDICT: READY WITH CHANGES
