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

**Not ready to build as written.** The payment steps (3 to 6) can charge a customer twice, and they can take a customer's money without leaving an order behind. The code structure section is solid. The fix is a well-known pattern and should take a few hours of design work, not a rewrite, so you could probably still start tomorrow if you revise steps 3 to 6 first.

## Must fix before building

**1. Retrying on timeout can double-charge (step 4).** A timeout doesn't mean the charge failed. It only means you didn't get the answer.
- *Case:* the first charge call succeeds at the provider, but the response arrives after 5 s. The service retries and the provider charges again. Result: one order, two charges. Expected: one charge.
- *Fix:* send an idempotency key (for example, the order id) with every charge attempt, including retries. Most payment providers support this. Confirm that yours does before committing to the design.

**2. The database transaction stays open across the payment call (steps 3 to 6).** The order insert isn't committed until after payment, so a charge can succeed with no order on record.
- *Case:* the charge succeeds, then the commit fails or the process crashes before step 5. The `PENDING` row is rolled back. Result: the customer is charged and no order exists, so there's nothing to reconcile against. Expected: an order row that shows the charge happened.
- It also holds a database connection and row locks for up to about 20 s (4 attempts × 5 s). At 2,000 orders a day that probably won't exhaust the connection pool, but it's fragile.
- *Fix:* commit `PENDING` in its own short transaction **before** calling the payment provider, then update the status in a second transaction.

**3. There's no state for "we don't know if the charge happened" (step 6).** If all retries time out, the design rolls back and returns an error. The customer may well have been charged, and they will likely retry, which leads back to problem 1.
- *Fix:* add a `PAYMENT_UNKNOWN` status, and a reconciliation job that asks the provider for the real outcome and moves the order to `PAID` or `FAILED`.

**4. A double-submitted `POST /orders` creates two orders.** A double-click, or a client retry after a slow response, creates a second order and a second charge. The idempotency key from fix 1 doesn't help here, because each order gets its own id.
- *Fix:* have the client send an idempotency key with `POST /orders`, and return the existing order if the same key arrives again.

The flow with fixes 1 to 3 applied looks like this. Each status change is committed on its own, so the database always shows how far the order got:

```mermaid
flowchart TD
    A[POST /orders with client idempotency key] --> B[Check stock in inventory cache]
    B --> C[Commit order as PENDING]
    C --> D[Charge payment provider, idempotency key = order id, retry on timeout]
    D -->|success| E[Commit PAID, then send confirmation email]
    D -->|declined| F[Commit FAILED, return error]
    D -->|all retries timed out| G[Commit PAYMENT_UNKNOWN]
    G --> H[Reconciliation job asks provider, sets PAID or FAILED]
```

## Should decide before building (smaller, but don't leave them implicit)

- **Overselling from the 5-minute stock cache (step 2).** The design accepts the cache, which is a reasonable choice, but it doesn't say what happens when it's wrong. *Case:* one unit is left, and two customers check out within the same 5-minute window. Both pass the stock check and both get charged. Decide what happens then: refund the second customer, backorder, or reserve stock at checkout. The answer affects the inventory module's interface.
- **Email failure (step 5).** If the email send fails after the commit, does the client get an error even though the order is paid? It should be sent asynchronously or on a best-effort basis, and an email failure should never fail the order.
- **End-to-end timeout.** In the worst case the request blocks for 20 s or more. Check the client's own timeout. If it's shorter, clients will retry mid-charge, which makes problem 4 more likely.
- **Nightly backups mean up to 24 hours of paid orders could be lost.** That may be acceptable if the payment provider's records can be used to rebuild orders, but say so explicitly in the design.

## Test plan gap

"One integration test for the happy path" isn't enough for a payment flow. Add integration tests for:
- a timeout followed by a successful retry (exactly one charge)
- all retries timing out (order ends up `PAYMENT_UNKNOWN`)
- a crash or failed commit after a successful charge
- a duplicate `POST /orders` (one order, one charge)
- a declined payment

Write these first. They are the tests that would have caught problems 1 to 4.

## What's already good

- Modules have clear boundaries, and no module reads another module's tables.
- Routes stay thin, and the flow lives in the `OrderCheckout` service class.
- Every log line carries the order id, which you'll need when debugging the reconciliation job.
- A single Postgres instance is the right size for 200 to 2,000 orders a day.

VERDICT: NOT READY
