The design isn't ready to build exactly as written. The checkout flow (steps 3–6) can charge a customer twice, or charge them and leave no record of the order. The fixes are standard and small, but they change the order of the steps, so update the doc before anyone writes the payment code. The rest of the design (the context, the module split, routes vs. the service class, logging) is fine to start on tomorrow.

## Must fix before building checkout

**1. Retrying the charge can bill the customer twice.** Step 4 retries the charge after a 5-second timeout, but a timeout doesn't mean the charge failed. Example: the provider takes the €50 charge but its reply takes 6 seconds. The service times out, retries, and the customer pays €100 for one order. Up to 3 retries means up to 4 charges.
*Fix:* send an idempotency key with every charge call. The order id works. The provider then treats retries as the same charge. Confirm the provider supports this; most do. If yours doesn't, don't retry automatically: ask the provider for the charge status instead.

**2. Charging inside an open database transaction can take money with no order on record.** The `PENDING` row from step 3 isn't committed, so it disappears on rollback or crash. Example: the charge succeeds, then the process crashes or the commit fails before step 5. The rollback erases the order. The customer has paid, and the database shows nothing. Step 6 has the same problem: after three timeouts it rolls back and returns an error, even though the charge may have gone through. The transaction also stays open for up to 20 s (4 × 5 s) while it waits on a third party.
*Fix:* commit `PENDING` first, then charge outside any transaction, then update the order to `PAID` or `FAILED`. Add a small job that finds orders stuck in `PENDING` and asks the provider for the real status.

```mermaid
flowchart TD
    A[POST /orders with idempotency key] --> B[Check stock in inventory cache]
    B --> C[Insert order PENDING and commit]
    C --> D[Charge provider using order id as idempotency key]
    D -->|success| E[Update order to PAID]
    D -->|declined| F[Update order to FAILED, return error]
    D -->|timeout after retries| G[Leave PENDING, return 'processing']
    E --> H[Queue confirmation email]
    G -.-> R[Reconciliation job checks provider, sets PAID or FAILED]
```

**3. A double-click on Pay creates two orders.** `POST /orders` has no idempotency key, so a resubmitted request is a new order with a new charge. Fix #1 doesn't catch this, because each order gets its own id. Have the client send a key, and have the service return the existing order when it sees a key again.

## Should fix (decide and write down)

- **What happens when we oversell.** Using the cache is a reasonable tradeoff. But two customers can both buy the last unit within the same 5-minute window, and nothing reserves or reduces stock after a sale. The doc should say what happens then: refund, backorder, or manual follow-up. Otherwise someone will decide it under pressure later.
- **Tests.** One happy-path integration test isn't enough for code that takes money. At minimum, test these cases:
  - provider timeout, then success on retry (exactly one charge)
  - decline
  - crash or commit failure after a successful charge
  - duplicate `POST` with the same key
  - the reconciliation job
  - the confirmation email failing (the order must stay `PAID`)
- **Email failure.** Say that the email is sent after the commit and that a failed send never turns a paid order into an error. A queue or outbox table is the simple option.
- **Backups.** Nightly-only backups mean a bad disk can lose up to a day of paid orders. Turn on point-in-time recovery (WAL archiving) or use a managed Postgres that includes it. The single instance is fine for 2,000 orders a day.

## Next steps
1. Rewrite steps 3–6 to match the diagram, and add idempotency keys on both the `POST /orders` request and the charge call.
2. Check that the payment provider supports idempotency keys.
3. Add the oversell policy and the test list to the doc.

Then build. You can start on the module scaffolding, routes and inventory work tomorrow while the payment steps are revised.

VERDICT: READY WITH CHANGES
