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

**Not ready as written.** The checkout flow in steps 3 to 6 can charge a customer twice, or charge them and keep no order record. Both problems come from how the payment call is placed relative to the database transaction. The module layout and code structure are fine. The flow needs restructuring before you build it, but that's probably a few hours of design work, not a rethink.

## Must fix before building

**1. Retrying the charge on timeout can double-charge (step 4).**
A timeout doesn't mean the charge failed, only that you didn't hear back in time.
- *Case:* the charge for order 123 succeeds at the provider, but the response takes 6 s. You time out at 5 s and retry. The provider sees a new request and charges again. The customer pays twice for one order.
- *Fix:* send an idempotency key with every charge request, for example the order id (Stripe, Adyen and Braintree all support this). Then a retry returns the original result instead of creating a second charge. If your provider has no idempotency key, you can't retry blindly. You must query the charge status before trying again.

**2. The payment call happens inside an open database transaction (steps 3 to 6).**
This causes three separate problems:
- **Charged, but no order.** If retries run out or the commit fails after the provider took the money, step 6 rolls back the `PENDING` row. You've taken the money and have nothing to reconcile against.
  *Case:* the provider charges the card, the 3rd retry times out, you roll back, and the client gets an error. The customer sees a charge on their statement and support has no order to find.
- **No record of attempts.** An uncommitted `PENDING` row is invisible to everything else, so there's no trail of what was tried.
- **Long-held connections.** One request can hold a connection and transaction open for about 20 s (4 attempts × 5 s). At 2,000 orders a day this won't break you, but it's the wrong shape.

*Fix:* use separate short transactions with the external call in between:

```mermaid
flowchart TD
    A[POST /orders with idempotency key] --> B[Tx 1: insert order PENDING, commit]
    B --> C[Charge payment provider, order id as idempotency key]
    C -->|success| D[Tx 2: mark PAID, write email to outbox table, commit]
    C -->|definite decline| E[Tx 2: mark FAILED, commit, return error]
    C -->|timeout after retries| F[Leave PENDING, return 'processing' to client]
    F --> G[Reconciliation job: query provider status for stale PENDING orders]
    G --> D
    G --> E
```

The design also needs to say what the client receives in the "unknown outcome" case.

**3. A client retry of `POST /orders` creates a duplicate order.**
- *Case:* the user double-clicks, or the mobile app retries after a network drop. You get two orders and two charges, and the payment idempotency key from item 1 doesn't help because each order has its own id.
- *Fix:* have the client send an idempotency key with the request, and add a unique constraint on that key in the orders table.

**4. The test plan only covers the happy path (Code structure section).**
The risky behaviour in this design is all on the failure paths. At minimum, add integration tests for:
- a payment timeout followed by a successful retry, asserting exactly one charge
- retries running out, asserting the order stays `PENDING` and gets reconciled
- a definite decline
- a duplicate `POST` with the same idempotency key
- a commit failing after a successful charge

Use a fake payment provider that can simulate these responses.

## Should decide, but not blocking

**5. Overselling is possible and unhandled (step 2).**
The stock cache can be up to 5 minutes stale, and nothing is reserved. Two customers can both buy the last unit. At your volume, accepting that is a reasonable trade-off, but the design should say what happens when it occurs: refund, backorder, or a manual step. Right now the design is silent on it.

**6. Sending the email "after commit" isn't specified (step 5).**
If the process crashes between the commit and the send, the email is lost. If the send happens before the commit, you can email about an order that doesn't exist. The outbox table in the diagram above fixes both cases cheaply.

**7. Nightly backups can lose up to a day of paid orders (Context section).**
That's a full day of records showing who paid for what. Check whether that's acceptable. Point-in-time recovery (WAL archiving, or the setting your managed Postgres offers) is usually a small config change.

## What's good
- Separate modules for orders, payments and inventory, with no cross-module table reads.
- Thin routes, with the flow in `OrderCheckout`.
- Logging the order id on every line.
- Accepting the inventory cache because the warehouse call takes 800 ms is a sound, clearly stated trade-off.

## Next step
Rewrite the Flow section around the diagram above. Add the two idempotency keys, the `PENDING` reconciliation job and the failure-path tests. Then the design is ready to build.

VERDICT: NOT READY
