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

**The design isn't ready as written: the payment steps can charge a customer twice, or take their money and leave no order behind.** Both problems sit in steps 3–6 and can be fixed by rewriting those steps, which should take about an hour. The rest of the design is fine for your scale. Fix the payment steps before you write any payment code tomorrow. The other modules can start now.

## Must fix before building

**1. Retrying the charge without an idempotency key can charge twice (step 4).** A timeout doesn't tell you the charge failed. It only tells you that you didn't get an answer.
- Case: you send a $50 charge. The provider processes it, but the response takes 6 s, so your 5 s timeout fires. You retry, and the retry succeeds.
- Wrong result: the customer pays $100 for one order.
- Expected: one $50 charge.
- Fix: send an idempotency key with every charge (the order id works) so the provider treats retries as the same charge. Most providers support this; confirm yours does. If it doesn't, don't retry. Mark the order as unknown and check its status with the provider instead.

**2. The charge happens inside an open database transaction (steps 3–5).** This causes two problems:
- **Money taken, no order.** The charge succeeds, then the commit fails (connection drop, deploy, crash). The customer is charged and there's no order row to show for it. Nobody finds out until the customer complains.
- **Long-held transactions.** With 4 attempts at 5 s each, a transaction can stay open for about 20 s while waiting on a third party. At 2,000 orders a day this won't break Postgres, but it's the wrong default and it gets worse during a provider outage.
- Fix: commit `PENDING` in its own short transaction first. Then call the provider with no transaction open. Then update the order to `PAID` or `FAILED` in a second short transaction.

**3. Rolling back on failure deletes the evidence (step 6).** If the order row disappears on failure, you can't match it against the provider's records, support has nothing to look up, and issue 2 can't be detected. Keep the row and mark it `FAILED`. Also add a status for "timed out, outcome unknown" (for example `PAYMENT_UNKNOWN`). A small scheduled job should resolve those orders by asking the provider and then mark them `PAID` or `FAILED`.

**4. A client retrying `POST /orders` creates a duplicate order.** For example, a phone loses signal after tapping "Pay", the app retries, and two orders get created and charged. Have the client send an `Idempotency-Key` header. Store it with a unique constraint and return the existing order when the same key comes in again.

Here's the order flow after these fixes:

```mermaid
stateDiagram-v2
    [*] --> PENDING: insert + commit (short txn)
    PENDING --> PAID: charge succeeded
    PENDING --> FAILED: charge declined
    PENDING --> PAYMENT_UNKNOWN: timed out after retries (same idempotency key)
    PAYMENT_UNKNOWN --> PAID: scheduled job checks provider
    PAYMENT_UNKNOWN --> FAILED: scheduled job checks provider
    PAID --> [*]: send confirmation email
```

## Should decide (these won't block you, but write the answer down)

- **Overselling (step 2).** Checking stock against a cache that can be 5 minutes old is a reasonable tradeoff against the 800 ms warehouse call. But with no reservation step, two customers can buy the last unit. The design should say what happens next: cancel and refund, backorder, or something else. It should also say which module owns that decision.
- **Confirmation email failure (step 5).** If the email fails after `PAID`, the order should still succeed. Send the email asynchronously or retry it later. Don't let an email failure turn into an error for the client.
- **Payment timeout and retry details.** Is a 5 s timeout what your provider recommends? Use backoff between retries rather than retrying immediately.

## Tests

One happy-path integration test isn't enough for the parts that handle money. Your own standard asks for failure cases, so add tests for:
- a timeout followed by a successful retry, asserting only one charge happens
- a decline, asserting the order is marked `FAILED` and kept
- a timeout on every attempt, asserting the order ends up `PAYMENT_UNKNOWN`
- the scheduled job resolving an unknown order
- a duplicate `Idempotency-Key`
- a commit failure after a successful charge

A fake payment provider that can be scripted to time out, decline, or succeed on the Nth call will cover all of these.

## What's already sound

- A single Postgres instance and a team of four are fine for 2,000 orders a day, which averages under 2 per minute.
- The module boundaries (no module reads another's tables), thin routes, the `OrderCheckout` service class, and logging the order id on every line all match good practice. Keep them.
- The cache tradeoff in step 2 explains its reasoning, which is what a design doc should do.

VERDICT: READY WITH CHANGES
