The design is not safe to build as written, but you can start tomorrow if you rewrite steps 3 to 6 of the flow first. As designed, it can charge a customer more than once and can take money without keeping any record of the order. The module layout, the thin routes and the single Postgres instance are fine for 2,000 orders a day.

## Must change before building

**1. Retrying the charge after a timeout can charge the customer several times (step 4).**
A timeout means the service did not hear back, not that the charge failed.
- **Case:** the provider takes 6 s to process a £50 charge and succeeds. The service gives up at 5 s and retries.
- **Result:** the customer is charged £100, or up to £200 if all four attempts go the same way.
- **Expected:** one £50 charge.
- **Fix:** send an idempotency key (the order id) with every charge attempt, so the provider treats retries as the same charge. Confirm your provider supports this, as most do.

**2. Rolling back on failure can erase the only record of an order that was paid for (steps 3, 5, 6).**
The `PENDING` row lives inside a transaction that is still open while the payment call runs.
- **Case:** the charge succeeds at the provider, but all attempts time out, or the service crashes before commit.
- **Result:** the customer is charged, there is no order row, and nothing to reconcile or refund against.
- **Expected:** the order still exists in a state that says "payment outcome unknown".
- **Fix:** commit the `PENDING` order before calling the provider, then update it to `PAID` or `FAILED` in a second short transaction. Add a small job that asks the provider about orders stuck in `PENDING` and settles them.

This fix also stops the service holding a database transaction and connection open for up to 20 s (four attempts at 5 s each).

```mermaid
flowchart TD
    A[POST /orders] --> B[Check stock]
    B --> C[Insert order as PENDING and commit]
    C --> D[Charge with order id as idempotency key]
    D -->|success| E[Mark PAID and commit]
    D -->|declined| F[Mark FAILED and commit]
    D -->|timeout after retries| G[Leave PENDING]
    E --> H[Send confirmation email]
    G --> I[Reconciliation job asks provider, then marks PAID or FAILED]
```

## Should decide before building

- **Stock is never reserved or decremented.** Two customers buying the last unit within the same 5-minute cache window both pass the check and both get charged. Either decrement a local count when the order is created, or write down what happens on an oversell (refund or backorder).
- **A duplicate `POST /orders` creates two orders.** A double-click or a client retry on a slow response does this. Accept a client-supplied idempotency key on the request and return the existing order for a repeat.
- **The test plan covers only the happy path at integration level.** The risk is all in the failure paths. Add integration tests for a decline, a timeout followed by a successful retry, a timeout on every attempt, and a duplicate request.
- **Declines and timeouts are not distinguished in step 6.** State that only timeouts are retried and that a decline fails immediately with a clear error to the client.

## Can wait

- **Confirmation email:** if sending fails after commit, nothing retries it. A retry or a "resend" path is enough at this scale.
- **Nightly backups:** a disk failure loses up to a day of paid orders. Continuous write-ahead-log archiving closes that gap when you have time.

VERDICT: READY WITH CHANGES
