# Review of design.md (checkout flow, revision 3)

You can start building tomorrow if you first add about a dozen lines to the doc. Taking an order is a sound plan: stock is reserved in one transaction, the payment call happens outside any transaction, the provider gets an idempotency key, and a job settles orders whose payment result is unknown. The traffic estimate (2,000 orders a day) is no strain for this setup. But three gaps would lead to wrong stock counts, lost emails or wrong prices, and each is cheap to fix now and expensive to fix later.

## Fix before building

**1. Two instances can restore the same stock twice.** You run two instances, so the reconciliation job runs twice every 5 minutes, and the doc doesn't stop both copies from settling the same order.
- **Example:** order A has 2 units of SKU X and is still `PENDING`. Both job copies ask the provider, and both hear "declined". Both run step 5 (mark `FAILED`, restore stock), so `available` goes up by 4 when it should go up by 2. You have now sold stock you don't have.
- The same race exists between a late request finishing step 4 and the job finishing step 6. It's unlikely there because of the 2-minute gap, but nothing in the doc prevents it.
- **Fix:** write it into the doc that every status change is conditional, as `UPDATE orders SET status = :new WHERE id = :id AND status = 'PENDING'`. Restore stock only when that update changed exactly one row, in the same transaction. Optionally, have the job select orders with `FOR UPDATE SKIP LOCKED` so the two copies don't fetch the same rows.

```mermaid
stateDiagram-v2
    [*] --> PENDING: POST /orders (stock reserved)
    PENDING --> PAID: charge succeeded (request or reconciliation job)
    PENDING --> FAILED: declined or not found at provider (stock restored)
    PAID --> [*]
    FAILED --> [*]
```
Each arrow must fire at most once per order, and only from `PENDING`.

**2. Some customers never get a confirmation email.**
- **Example A:** the charge times out on every try, and 5 minutes later the job marks the order `PAID`. Step 6 never queues an email, so the customer has paid but gets no confirmation.
- **Example B:** in step 4 the instance crashes after the `PAID` commit and before the email is queued. That email is lost too.
- **Fix:** add an `outbox` table. Insert the email row in the same transaction that sets `PAID`, whichever code path does it, and have a worker send rows from the outbox. This also answers a question the doc leaves open: where the queue lives.

**3. The doc doesn't say where prices come from.** Step 1 says the client sends "the cart", and `order_items.unit_price` gets stored. If that price comes from the request body, a client can send `unit_price: 0.01` and get charged a cent. Name the source the server reads prices and currency from (a catalog table or another service), and add a test that ignores or rejects a price sent by the client.

## Small changes worth adding to the doc

- **Reusing an idempotency key with a different cart:** today it returns the old order silently. Return `422` when the request body doesn't match the stored order.
- **Two identical requests at the same moment:** a check-then-insert lets both through. Say that you rely on the unique constraint, catch the violation (which also rolls back the stock decrement) and return the existing order. Also decide whether keys are unique per customer or across all customers.
- **Client after a `202`:** there is no `GET /orders/{id}`, so the client has no way to learn the final status. Add the endpoint and the polling contract.
- **Provider says "no such charge":** step 6 only covers paid and failed. If the first request never reached the provider, "not found" should mean `FAILED` plus stock restored. Write that down.
- **What gets retried:** retry only timeouts, 5xx responses and network errors. Never retry a decline.
- **Module rule vs. step 2:** step 2 updates `stock` and inserts into `orders` in one transaction, which looks like it breaks "no module reads another module's tables". It doesn't have to: the inventory module can expose `reserve(tx, items)` and accept the caller's transaction. That keeps the boundary and the atomic commit. Record this as a deliberate decision.

## Missing tests

The current list covers the request path well but not the reconciliation job. Add:
- reconciliation settles as `PAID` (and the email goes out)
- reconciliation settles as `FAILED` (and the stock comes back)
- reconciliation gets "not found"
- two job copies settle the same order, and stock is restored only once
- two identical requests arrive at the same moment
- the same key arrives with a different cart
- the client sends a tampered price
- the instance crashes after the `PAID` commit, and the email still goes out

None of this changes the overall design. It's all specification and tests you can add tonight, then build to.

VERDICT: READY WITH CHANGES
