# Review of design.md (order-service checkout, revision 3)

You can start building tomorrow. The core of the design holds up: stock is reserved atomically, the payment call sits outside the database transaction, the provider dedupes retries by order id, the email can't fail an order, and stuck orders trigger an alert. There are four gaps to close first. Each needs about a sentence in the doc. Three of them affect only the reconciliation job and the endpoints, so they don't block you from starting on steps 1–2.

## Must fix before building those parts

**1. Reconciliation has no rule for "the provider has never heard of this charge."**
Case: the service commits the `PENDING` order in step 2, then crashes before step 3 sends any charge. Five minutes later reconciliation asks the provider about the order and gets "not found." The design only describes settling as `PAID` or `FAILED`, so this order stays `PENDING` forever. Its stock stays reserved, and after 30 minutes it fires the alert. The fix is to add the rule: when the provider returns not found, mark the order `FAILED` and restore its stock, or retry the charge with the same order id. Either works because the provider dedupes on the order id. Pick one and write it down.

**2. An order can be settled twice, which restores its stock twice.**
Case: both instances run the reconciliation job (nothing says it runs on only one). Both pick up order X, both get "declined," and both run step 5. One unit of stock was reserved but two come back, so `available` is now 1 higher than it should be. The same race can happen between a slow request and the job. The fix is to make every status change conditional: `UPDATE orders SET status = 'FAILED' ... WHERE id = :id AND status = 'PENDING'`, and restore stock only if that update changed a row. Also say whether the job runs on one instance or on both behind a lock.

**3. The idempotency key's uniqueness scope isn't stated.**
Case: if the unique constraint covers `idempotency_key` alone, customer B sending a key that customer A already used gets back A's order. That leaks A's data. The fix:
- Make the constraint unique on `(customer_id, idempotency_key)`.
- If a request reuses a key with a different cart, return `422` rather than silently returning the old order.

**4. After a `202` the client has no way to find out what happened.**
The design returns `202` when the payment outcome is unknown, but it defines no endpoint for checking later. The fix is to add `GET /orders/{id}` and say whether the client polls it or gets a webhook.

## Should fix (small)

- **Where prices come from:** say that `unit_price` and `total` are calculated on the server from the catalog, never taken from the client's cart.
- **What gets retried:** retry only timeouts and 5xx errors from the provider, never declines.
- **Email can be lost:** a crash between committing `PAID` and queuing the email loses the email, and nothing ever notices, because reconciliation only looks at `PENDING` orders. At 2,000 orders a day you may accept that risk. If you don't, write the email into a table in the same transaction that marks the order `PAID`, and send it from there (the "transactional outbox" pattern).
- **Shared transaction across modules:** step 2 updates the inventory module's `stock` table and inserts into the orders module's `orders` table in one transaction. Say how that works without breaking the rule that no module reads another's tables. For example, the inventory module could expose a `reserve(tx, items)` function that takes the open transaction.
- **Keys not shown in the data model:** `stock.sku` should be the primary key, `order_items` needs a primary key, and the table should show the unique constraint on the idempotency key.

## Missing tests

The test list covers the request path but not the reconciliation job. Add tests for:
- reconciliation finding a charge that was paid
- reconciliation finding a charge that was declined
- reconciliation getting "not found" from the provider
- two reconciliation runs settling the same order at once (stock restored exactly once)
- two simultaneous `POST /orders` requests with the same key
- two buyers going for the last unit at the same moment
- a decline restoring stock correctly

## Order states after the fixes

The two dashed transitions are the ones the design is currently missing.

```mermaid
stateDiagram-v2
    [*] --> PENDING: POST /orders (stock reserved)
    PENDING --> PAID: charge succeeded (request or reconciliation)
    PENDING --> FAILED: declined, stock restored
    PENDING --> FAILED: provider has no charge (not in design yet)
    note right of PENDING: every transition runs only WHERE status = 'PENDING' (not in design yet)
```

VERDICT: READY WITH CHANGES
