**Mostly ready.** The core checkout flow is sound, but seven gaps need fixing first. Most need one sentence in the doc; a few need a small schema or SQL change. Three of them let money or stock go wrong without anyone noticing, so fix those before you write the checkout code. Together they're about half a day of design work.

## What's already solid
- The stock reservation is a single atomic statement (`UPDATE … WHERE available >= :qty`), so two buyers can't both get the last unit.
- The payment call happens outside any transaction, and the order id is sent as the provider's idempotency key, so retries can't double-charge.
- The email is sent after the commit and never fails the order. Reconciliation plus the 30-minute alert give you a safety net.
- The module split, thin routes and the `OrderCheckout` service match how you like to structure code.

## Must fix before building

**1. Stock can be put back twice.** Nothing stops an order from being settled twice. Both service instances will run the 5-minute reconciliation job unless you say otherwise. Example: order A is `PENDING` and the provider says "declined". Instance 1 and instance 2 both mark it `FAILED` and both put the stock back, so 1 reserved unit becomes 2 available. The same thing can happen if the request path settles late while reconciliation is also settling.
- **Fix:** only allow the change out of `PENDING` with a guarded update: `UPDATE orders SET status = :new WHERE id = :id AND status = 'PENDING'`. Put the stock back only if that update changed exactly one row.
- Also make the job run on one instance at a time, using a Postgres advisory lock or `FOR UPDATE SKIP LOCKED`.

**2. A negative quantity creates stock.** If `quantity = -5`, the update becomes `available - (-5) WHERE available >= -5`. That condition is always true, so stock goes up by 5 and the order succeeds.
- **Fix:** reject `quantity <= 0` when validating the request.
- Also add `CHECK (quantity > 0)` on `order_items` and `CHECK (available >= 0)` on `stock`.

**3. The design doesn't say where prices come from.** No table in the data model holds prices, so `unit_price` and `total` can only come from the client's cart. Example: a client posts `unit_price: 0.01` for a $200 item and gets charged $0.01.
- **Fix:** state the source of prices (a product or price table, or a catalog service) and compute `total` on the server.

## Should fix (each is a short addition to the doc)

**4. Idempotency keys are global, not per customer.** Example: customers X and Y both send `Idempotency-Key: abc`. Y gets back X's order, which leaks X's data.
- Make the unique constraint `(customer_id, idempotency_key)`.
- Return `422` when a key is reused with a different cart.
- Say what a duplicate request gets while the first one is still being charged (presumably `202` with the `PENDING` order).

**5. Reconciliation doesn't say what to do when the provider has never seen the charge.** This happens if the instance crashes after step 2 commits but before step 3 calls the provider. The order stays `PENDING` with stock reserved, and the job has no rule for a "not found" answer.
- **Fix:** define it: "not found" after 2 minutes means `FAILED` and the stock goes back. "Still processing" means leave it alone.
- Also state that when reconciliation settles an order as `PAID`, it queues the confirmation email too.

**6. Clients that get `202` have no way to learn the final status.** Add `GET /orders/{id}`, or a webhook if clients need to be notified.

**7. The one-transaction design breaks the module rule.** Step 2 updates `stock` (inventory) and `orders` (orders) in one transaction, which contradicts "no module reads another module's tables". Say how this works. The usual way is for each module to expose a function that takes the caller's transaction, so `OrderCheckout` owns the transaction and each module writes only its own tables.

## Small changes
- **Deadlocks:** two carts containing the same SKUs, locked in different orders, can deadlock each other. Update stock rows in SKU order to prevent this.
- **Which failures are retried:** say that only timeouts and 5xx responses are retried, and declines never are.
- **Load balancer timeout:** in the worst case one request takes about 30 seconds (4 attempts × 5 s plus backoff). Check that the load balancer's idle timeout is longer than that.
- **Lost emails:** if the instance crashes between the `PAID` commit and queuing the email, the email is lost. A transactional outbox (writing the email to a table in the same transaction) fixes this. If you accept the risk for now, write it down as known debt.
- **Missing keys:** add primary keys to `order_items` (`order_id, sku`) and `stock` (`sku`).

## Order lifecycle with guarded status changes

```mermaid
stateDiagram-v2
    [*] --> PENDING: POST /orders (stock reserved)
    PENDING --> PAID: charge succeeded (only if still PENDING)
    PENDING --> FAILED: declined, or provider has no record (only if still PENDING, then put stock back)
    PENDING --> PENDING: result unknown, return 202, reconciliation checks later
    PAID --> [*]
    FAILED --> [*]
```

## Tests to add to the plan
- Two concurrent orders for the last unit: exactly one succeeds.
- Reconciliation and a late request both settling the same order: stock is restored exactly once.
- Two reconciliation runs at the same time.
- A crash after step 2, before the charge call: reconciliation marks the order `FAILED` and restores the stock.
- Rejected input: `quantity` of 0 or less, and a client-supplied price is ignored.
- The same idempotency key with a different cart, and the same key from a different customer.

VERDICT: READY WITH CHANGES
