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

**Not ready.** The payment steps (3 to 6) can charge a customer twice, or charge them and keep no record of the order. Both come from how the payment call sits inside the database transaction. This is a few hours of design rework, not a rethink. The context, module boundaries and code structure are sound, and you could start building those tomorrow.

## Blockers (fix before building)

**1. Retrying a timed-out charge can charge the customer twice.**
A timeout doesn't mean the charge failed. It means you don't know whether it went through.
- Case: the provider charges $50 but its reply takes 6 s. You time out at 5 s and retry, and the retry succeeds.
- What you get: two $50 charges. What you expect: one.
- Fix: send an idempotency key on every charge attempt, such as the order id, and use the same key on every retry. Check that your provider supports this; most do.

**2. If a charge succeeds and the commit fails, the customer is charged but you have no order.**
The `PENDING` row is inside the uncommitted transaction. If the commit fails or the process crashes after the charge, the row rolls back and disappears. Step 6 has the same problem: after 4 timeouts you roll back and return an error, but one of those attempts may have charged the card.
- Case: the charge succeeds, then the database connection drops before commit.
- What you get: a charged card, no order row, and nothing to reconcile against. What you expect: an order row you can complete or refund.
- Fix: commit `PENDING` *before* calling the payment provider. Make the charge outside any transaction, then update the status in a second, short transaction. When the outcome is unknown, store it as `PAYMENT_UNKNOWN` instead of failing. Then settle it from the provider's webhook or a reconciliation job that queries the provider by idempotency key.

**3. The transaction stays open across a slow external call.**
4 attempts × 5 s means one checkout can hold a database connection and transaction open for about 20 s. At 2,000 orders a day the volume is fine. But a single slow period at the provider can use up your connection pool and stall every checkout. Fix #2 removes this problem too.

Proposed 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->>DB: insert order PENDING, commit
    S->>P: charge (idempotency key = order id), retry on timeout
    alt success
        S->>DB: mark PAID, commit
        S-->>C: 201
        S->>S: queue confirmation email
    else declined
        S->>DB: mark FAILED, commit
        S-->>C: 402
    else still unknown after retries
        S->>DB: mark PAYMENT_UNKNOWN, commit
        S-->>C: 202 (processing)
        Note over S,P: webhook or reconciliation job settles it later
    end
```

## Should fix (decide now, can build in parallel)

**4. Duplicate orders from client retries.** A double-click, or a mobile client retrying `POST /orders`, creates two orders and two charges with different order ids, so the fix in #1 doesn't catch it. Accept an `Idempotency-Key` header on `POST /orders` and return the existing order when you see a repeat key.

**5. Overselling is possible, and the design doesn't say what happens next.** With a cache up to 5 minutes old and no reservation, two buyers can both get the last unit. Going without the 800 ms warehouse call is a reasonable tradeoff. But write down what happens when an order is oversold (auto-refund? backorder? who gets told?) so it's a deliberate decision rather than a surprise.

**6. Nightly backups mean you could lose up to 24 hours of paid orders.** Losing a day of records for charges that really happened is a support and accounting problem. Turn on point-in-time recovery (WAL archiving). Most managed Postgres services include it.

**7. The test plan covers only the happy path.** By your own standard that isn't enough, and the failure paths are where this design is riskiest. Add integration tests for:
- a charge that times out and then succeeds on retry (exactly one charge)
- a charge whose outcome is still unknown after all retries
- a crash or commit failure after a successful charge
- a declined card
- a repeated idempotency key
- the email failing (the order still ends up `PAID`)

**8. Email is sent in the request path.** If the email call fails or hangs, it shouldn't affect the order. Queue the email after commit, or use an outbox table.

## What's already right
- Postgres on a single instance is plenty for 2,000 orders a day, which averages about 1.4 a minute. Don't add infrastructure.
- The module boundaries (no module reads another's tables), thin routes, the `OrderCheckout` service class, and logging the order id on every line are all good choices. Keep them.

## Next step
Rewrite steps 3 to 6 to match the diagram, add the `PAYMENT_UNKNOWN` status and a plan for settling it, and extend the test list. Then the design is ready to build.

VERDICT: NOT READY
