Almost. The overall design is solid, but four specific points would cause double charges, duplicate orders or overselling stock, so change them in the doc before you start building. Each fix is small, and none of them changes the architecture.

**What's already right:** the payment call is made outside any database transaction, unknown payment results are settled by the reconciliation job instead of being guessed, email failures can never fail an order, each module only reads its own tables, routes stay thin, and there's an alert for orders stuck in `PENDING`. Most first designs miss several of these.

## Must fix before building

**1. Each attempt sends a different payment idempotency key, so retries can charge twice (step 3).**
The key `<order id>-<attempt number>` changes on every retry, so the provider sees each retry as a brand-new charge.
- Case: attempt 1 succeeds at the provider, but the reply arrives after the 5 s timeout. Attempt 2 is sent with key `abc-2`.
- What happens: the customer is charged twice.
- What should happen: one charge.
- Fix: send the order id as the key on every attempt. To trace attempts in the provider's dashboard, put the attempt number in the charge metadata or your own logs instead.

```mermaid
sequenceDiagram
  participant S as order-service
  participant P as Payment provider
  S->>P: charge (key abc-1)
  Note over P: charge succeeds, reply is slow
  S--xS: 5 s timeout
  S->>P: charge (key abc-2)
  Note over P: new key, so a second charge
```

**2. The duplicate-request check only lives in each instance's memory (step 1).**
You run two instances behind a load balancer, and each keeps its own map of keys it has seen.
- Case: the client's first `POST /orders` reaches instance A and times out on the client's side. The client retries with the same `Idempotency-Key`, and the retry lands on instance B.
- What happens: B has never seen the key, so it creates a second order, takes the stock a second time, and charges again.
- What should happen: B returns the first order.
- The same thing happens after any restart or deploy, because the map is wiped.
- Fix: add an `idempotency_key` column on `orders` with a unique constraint and insert it in the step 2 transaction. If the insert hits a conflict, return the existing order.

**3. Reading stock and then writing it back can oversell (step 2).**
Under Postgres's default isolation level, two transactions can both read the same stock value before either writes.
- Case: `available = 1`, and two customers each buy 1 at the same moment.
- What happens: both read 1, both write 0, and two orders are created for one unit.
- What should happen: one order is created and the other gets `409`.
- Fix: do the check and the write in one statement, `UPDATE stock SET available = available - $q WHERE sku = $s AND available >= $q`, and treat "0 rows updated" as out of stock. `SELECT ... FOR UPDATE` before the write also works.

**4. `double precision` is the wrong type for money (data model).**
It can't store most decimal amounts exactly. For example, 0.10 + 0.20 is stored as 0.30000000000000004, so totals can drift from what the provider charges by a fraction of a cent. Use `numeric(12,2)`, or store integer minor units (cents) in `bigint`, for both `total` and `unit_price`.

## Worth adding (small)

- **Reconciliation must give stock back too.** Step 5 restores stock when it marks an order `FAILED`, but step 6 doesn't say the reconciliation job does. It should also treat "provider has no record of this charge" as `FAILED`. That happens if an instance crashes after committing the order but before calling the provider.
- **Only change status from `PENDING`.** Write status changes as `UPDATE orders SET status = ... WHERE id = $1 AND status = 'PENDING'`. Then if the reconciliation job runs on both instances, or overlaps with a slow request, stock still can't be restored twice and a `PAID` order can't be flipped to `FAILED`.
- **The email can be lost.** If an instance crashes between the commit and putting the email on the queue, the email is never sent. If that matters to you, write the email job in the same transaction (a table of pending messages that a worker sends from, often called an outbox).

## Tests to add
Each of these covers one of the fixes above:
- Two concurrent requests for the last unit of stock: one succeeds, one gets `409`.
- The same `Idempotency-Key` sent to two different instances (or after a restart): one order.
- The fake provider charges successfully but replies after the timeout: one charge, and the order ends up `PAID`.
- Reconciliation finds no charge at the provider: order `FAILED` and stock restored, exactly once.

With the four fixes written into the doc, you're in good shape to start tomorrow.

VERDICT: READY WITH CHANGES
