**`design.md` is not safe to build as written, because steps 3–6 can charge a customer twice or charge them and keep no record of the order.** The fixes are standard and small, and the code structure section can stand, so you can start tomorrow if you rewrite the flow first.

## Must fix before coding the payment step

**1. Retrying on timeout can charge the customer more than once (step 4).**
A timeout means "no answer", not "charge failed". Example: the provider completes the charge at 5.2 s, the service gave up at 5 s and retries, and the customer is charged twice (up to four times with three retries). Expected: one charge.
- Fix: send the provider an idempotency key (the order id) on every attempt, so a repeat is recognised as the same charge.
- Confirm your provider supports this; nearly all do.

**2. A customer can be charged with no order on record (steps 3, 5, 6).**
The `PENDING` insert sits in an open transaction during the payment call, so it is not visible or durable until commit. Example: the charge succeeds, then the process crashes, the commit fails, or all four attempts time out. The rollback erases the order, and nothing in your database says a charge was attempted. Expected: a row that can be reconciled.
- Fix: commit the `PENDING` order before calling the provider, then update it to `PAID` or `FAILED` in a second short transaction.
- Never delete the row.
- This also stops a database connection being held for up to 20 s per checkout.

**3. Nothing resolves an order whose payment outcome is unknown.**
After fix 2, an order can stay `PENDING` when every attempt timed out.
- Fix: return "payment pending" to the client rather than an error.
- Add a scheduled job that asks the provider for the status of old `PENDING` orders and settles them to `PAID` or `FAILED`.

**4. A repeated `POST /orders` creates a second order and a second charge (step 1).**
The request can take 20 s or more, so clients and impatient users will retry.
- Fix: the client sends a unique key per checkout attempt, stored with a unique constraint.
- A repeat returns the existing order.

**5. Stock is checked but never reserved (step 2).**
Example: one unit left, two customers check out within the same 5 minutes, both pass the check and both are charged. This also happens with a perfectly fresh cache, because the flow never decrements anything.

The cache itself is a reasonable choice. What is missing is two decisions:
- Is the warehouse system or your database the source of truth for available stock?
- What happens to an oversold order: refund, or backorder?

At 200 orders a day, "accept rare oversell and refund" may be a fine answer, but it has to be written down and built.

**6. The test plan covers only the happy path.**
Every defect above lives in a failure path. Add integration tests, against a fake payment provider, for:
- timeout then success;
- all attempts time out;
- a decline;
- a crash between charge and commit;
- a duplicate `POST`.

## Fix before launch, not before building

- **Nightly backups can lose up to a day of paid orders.** Turn on continuous Postgres archiving (point-in-time recovery).
- **The email in step 5 should not affect the response.** If sending fails after commit, the order is still paid; log it and retry later.

## What is fine

- The module boundaries, the thin routes and the `OrderCheckout` class.
- A synchronous charge on a single Postgres instance, which is ample for 2,000 orders a day. Do not add queues or a saga framework.

## Revised flow

```mermaid
flowchart TD
    A[POST /orders with checkout key] --> B{Key seen before?}
    B -- yes --> C[Return existing order]
    B -- no --> D[Check stock against inventory cache]
    D --> E[Insert order as PENDING and commit]
    E --> F[Charge provider, order id as idempotency key, retry on timeout]
    F -- success --> G[Mark PAID, commit, then send email]
    F -- declined --> H[Mark FAILED, return error]
    F -- still no answer --> I[Leave PENDING, return payment pending]
    I --> J[Scheduled job asks provider and settles to PAID or FAILED]
```

VERDICT: READY WITH CHANGES
