**You can start building tomorrow, but fix the design doc first.** The core flow holds up: stock is reserved atomically, the payment call happens outside any transaction, the payment provider gets an idempotency key, and a reconciliation job settles orders whose charge result is unknown. The gaps are in what happens when two parts of the system act on the same order, or when the service crashes between steps. Each one takes a sentence or two in the doc and should be settled before you write the reconciliation job.

## Must fix before coding the payment and reconciliation paths

**1. Status changes aren't guarded, so stock can be restored twice.** The request path and the reconciliation job can both settle the same `PENDING` order. With two instances, the job may also run on both of them.
- Example: a charge times out on every attempt and the client gets `202`. The 5-minute job runs on instance A and instance B at the same moment. Both see the order as `PENDING`, both mark it `FAILED`, and both add the stock back. 1 unit was reserved, but `available` goes up by 2.
- Fix:
  - Every transition must be `UPDATE orders SET status = :new WHERE id = :id AND status = 'PENDING'`. Only restore stock or queue the email if that update changed a row.
  - Run the job on one instance only, using a Postgres advisory lock or `SELECT … FOR UPDATE SKIP LOCKED`.

```mermaid
stateDiagram-v2
    [*] --> PENDING: stock reserved and order inserted (one transaction)
    PENDING --> PAID: charge succeeded (request path or reconciliation job)
    PENDING --> FAILED: declined, or no charge found (also restores stock)
    PAID --> [*]
    FAILED --> [*]
```
Each arrow out of `PENDING` should succeed exactly once, whichever path gets there first.

**2. The reconciliation job doesn't say what it does with stock or email.** Step 5 restores stock when an order fails, but step 6 doesn't. Step 4 queues the confirmation email when an order is paid, but step 6 doesn't. As written, an order the job settles as `FAILED` keeps its stock reserved forever, and an order it settles as `PAID` never gets an email. The doc should say both happen on the job's path too, ideally by calling the same code as steps 4 and 5.

**3. A crash between steps 2 and 3 has no defined outcome.** If the service dies after committing the `PENDING` order but before calling the provider, the provider has no charge for that order id. The job will get back "not found", which is neither `PAID` nor `FAILED`. Define "not found" as `FAILED` plus restoring the stock. That's safe because the job only looks at orders older than 2 minutes, and the longest charge attempt (4 tries × 5 s plus backoff) finishes well inside that.

**4. The flow breaks the module boundary.** Step 2 updates `stock` and `orders` in one transaction, but the code-structure section says "no module reads another module's tables." Pick one approach. My recommendation: the inventory module exposes `reserve(tx, items)` and `release(tx, items)`, which take the caller's open transaction. The orders module then never touches `stock` directly, and the reservation stays atomic.

## Should fix (small, but cheaper now than later)

5. **Concurrent checkouts can deadlock.** If order A reserves SKUs X then Y while order B reserves Y then X, Postgres deadlocks. Fix: update stock rows in SKU-sorted order.
6. **Duplicate POSTs and the idempotency key need more rules.**
   - Scope the unique constraint to `(customer_id, idempotency_key)`.
   - If the same key arrives with a different cart, return `422`. Today it would silently return the first order.
   - When two duplicate POSTs race, the second one's insert fails on the unique constraint. That's caught and the existing order returned. Its rollback also undoes its stock reservation, which is correct, but write it down.
7. **The doc doesn't say where prices come from.** `unit_price` and `total` are stored, but no price source is named. If they come from the client's cart, a client can set their own price. State that the server looks prices up.
8. **The schema is missing constraints.** Add primary keys (`stock.sku`, and `(order_id, sku)` on `order_items`), a foreign key from `order_items` to `orders`, `CHECK (available >= 0)` and `CHECK (quantity > 0)`.
9. **A `202` response has no follow-up.** Add `GET /orders/{id}` (or say that repeating the POST is how clients poll) so they can learn the final status.
10. **The confirmation email can be lost.** If the process dies after the `PAID` commit but before the email is queued, the email is never sent. A transactional outbox fixes this: write a row to an outbox table in the same transaction that marks the order `PAID`, and have a separate worker send from that table. If this gap is acceptable for now, the doc should say so.

## Tests to add to the test plan

- Two concurrent requests for the last unit: exactly one succeeds and the other gets `409`.
- Two concurrent requests with the same idempotency key: one order is created and stock is reserved once.
- The reconciliation job settles an order as `PAID` (email queued) and as `FAILED` (stock restored).
- The provider returns "not found": the order ends `FAILED` and stock is restored.
- The job and a late charge result race on the same order: exactly one transition wins, and stock and email happen once.
- The job runs on both instances at once: each order is processed once.
- Two concurrent multi-SKU orders with overlapping SKUs: no deadlock.

Items 1–4 are a short edit to the doc. Make them before you write the reconciliation job, and get the guarded updates into the first version of `OrderCheckout`. Everything else can be done during the build.

VERDICT: READY WITH CHANGES
