The core design is sound. You could start building tomorrow, but first fix two bugs on paper and decide where prices come from. Those changes take about an hour of design edits and don't touch the overall structure.

**What already works well:**
- Reserving stock with a conditional `UPDATE` stops overselling.
- The payment call happens outside the database transaction.
- The provider gets an idempotency key, so retries can't charge twice.
- Emails are queued after commit and can't fail an order.
- A reconciliation job settles unknown results, with an alert as a backstop.

## Problems, most severe first

### High: fix before writing the checkout code

**1. An order can be settled twice, which restores stock twice.** Both service instances run the reconciliation job, and nothing stops two writers from settling the same order. The request path can also overlap with the job.
- *Case:* order X reserved 1 unit of SKU `A`, so `available` is 0, and X is still `PENDING` after 3 minutes. Both instances' jobs pick X up and the provider says "declined." Each runs "mark `FAILED` and restore stock," so `available` becomes 2. It should be 1.
- *Reverse case:* reconciliation marks X `FAILED` and restores the stock, then the slow request path gets "success" and marks it `PAID`. Now the customer has paid and the unit can be sold to someone else.
- *Fix:* every status change is a guarded update, `UPDATE orders SET status = :new WHERE id = :id AND status = 'PENDING'`. Only restore stock or queue the email if that update changed a row. Optionally, have the job claim rows with `FOR UPDATE SKIP LOCKED`.

```mermaid
stateDiagram-v2
    [*] --> PENDING: stock reserved, order inserted
    PENDING --> PAID: charge succeeded (only if still PENDING)
    PENDING --> FAILED: declined or no charge found (only if still PENDING), stock restored once
    PAID --> [*]
    FAILED --> [*]
```

**2. A negative quantity adds stock.** With `:qty = -5`, the check `available >= -5` passes, `available - (-5)` adds 5 units, and the client gets an order. The expected result is a `400`.
- *Fix:* validate `quantity > 0` at the route.
- Also add `CHECK (quantity > 0)` on `order_items` and `CHECK (available >= 0)` on `stock`.

**3. The design doesn't say where prices come from.** The client sends the cart, but there's no price or product table and no owning module. If `unit_price` comes from the client, anyone can set their own price.
- *Fix:* state that the server looks up prices and calculates `total`, and name the module or service that owns prices.

### Medium: settle these in the design, build them in the first pass

**4. Same idempotency key, different cart.** A retry with the same key but different items silently returns the old order.
- Store a hash of the request body next to the key.
- Return `422` when the key matches but the body doesn't.

**5. Idempotency keys are unique across all customers, not per customer.** A key sent by customer B that matches customer A's returns A's order to B.
- Make the unique constraint `(customer_id, idempotency_key)`.

**6. Two simultaneous requests with the same key aren't handled.** The second insert hits the unique constraint and its transaction rolls back, which is correct. But the design doesn't say to catch that unique-constraint error and return the existing order. Without that, the client gets a `500`.

**7. "No module reads another module's tables" conflicts with the single transaction.** Step 2 and step 5 change `stock` (inventory) and `orders` (orders) in one transaction.
- Fine, but say how: `OrderCheckout` opens the transaction and passes it to `inventory.reserve(tx, items)` and `orders.create(tx, …)`.
- Otherwise someone will "fix" the rule by splitting the transaction.

**8. Reconciliation doesn't cover "no charge found."** If the instance crashed before calling the provider, the provider has no record of the charge. The design only covers `PAID` and `FAILED`.
- State that "not found after 2 minutes" means `FAILED` and the stock is restored.

**9. Orders settled by reconciliation never get a confirmation email.** Step 4 queues the email only on the request path. The reconciliation job needs to do the same.

**10. The email can be lost if the instance crashes between commit and queueing.**
- Write the email job to an outbox table in the same transaction that marks the order `PAID`.
- Or accept the risk and say so.

**11. A client that gets `202` has no way to find out the result.** Add `GET /orders/{id}` or a webhook, and say which.

**12. Requests can take about 30 seconds.** The worst case is 4 attempts × 5 s plus backoff, which may run into your load balancer's idle timeout.
- Check that timeout.
- Consider fewer retries on the request path and let reconciliation handle the rest.

**13. The test list misses the riskiest cases.** Add tests for:
- two concurrent buyers of the last unit
- concurrent duplicate `POST`s with the same key
- the same key with a different body
- reconciliation settling `PAID`, `FAILED`, and "not found"
- two reconciliation runs on the same order (issue 1)
- `PAID` arriving after `FAILED`
- a decline restoring stock
- a negative or zero quantity

### Low: tidy-ups

14. **Possible deadlocks.** Two orders that reserve SKUs `A, B` and `B, A` at the same time can deadlock each other. Reserve items in `sku` order.
15. **Missing keys and constraints:**
    - `stock.sku` has no primary key.
    - `order_items` has no primary key and no foreign key to `orders`.
    - `status` has no `CHECK` or enum.
    - `idempotency_key` isn't declared `NOT NULL`, and the header isn't stated as required.
16. **No index for the reconciliation query.** Add one on `orders (status, created_at)`.
17. **The provider's charge id isn't stored.** You'll need it for refunds, support, and audits.
18. **Retry rules aren't stated.** Say that retries happen only on timeouts, network errors, and 5xx responses, never on a decline.
19. **Response codes for success and decline aren't stated.** For example, `201` for success and `402` for a decline. Also, the `409` should say which SKUs are out of stock.
20. **Orders can stay `PENDING` forever during a long provider outage, holding stock.** Define a cutoff age after which the order is failed or escalated.
21. **No non-goals section.** Say that refunds, cancellations, and partial payment are out of scope, so nobody assumes they're covered.
22. **The 2-minute reconciliation threshold isn't explained.** State that it's deliberately longer than the longest possible request.

Issues 1–3 are about ten lines of design text. Issues 4–13 can be built in as you go, as long as their tests are on the list from day one.

VERDICT: READY WITH CHANGES
