# Review of design.md (order-service checkout flow)

**It isn't ready to build from yet.** The way steps 3–6 handle payment can charge a customer twice, or charge them and keep no record of the order. Both lose real money, and both sit in the core flow, not at the edges. The fix is a well-known pattern and fairly small (sketch below), so this is about half a day of redesign, not a restart.

## Blocking problems

**1. Retrying a timed-out charge can charge the customer twice (step 4).**
A timeout doesn't mean the charge failed. It means you don't know whether it went through.
- Case: the provider charges $50, but its reply arrives after 5 s. The service retries and the provider charges $50 again.
- Expected: one $50 charge.
- Fix: send an idempotency key with every charge attempt, for example the order id, so the provider treats retries as the same charge. **Check that your payment provider supports idempotency keys before you build.** If it doesn't, the design has to change more than this.

**2. The database transaction stays open across the payment call (steps 3–6).**
This causes two problems:
- **A charge with no order.** Case: the charge succeeds, then the commit fails or the process crashes. The `PENDING` row is rolled back, so the customer has paid and you have no record of it. Expected: an order row that shows the charge happened.
- **Long-held connections.** With 4 attempts × 5 s, one checkout can hold a database connection for 20 s or more. At 2,000 orders a day that's survivable, but it's the wrong default.

There's also a smaller point: the `PENDING` status does nothing today. The row never commits while it is `PENDING`, so nothing else can ever see that state.

**3. A timeout on every attempt is treated as a failure (step 6).**
After 4 timeouts the service rolls back and tells the client "error", but the customer may already have been charged. You need a third outcome, "unknown", and a job that settles it later by asking the provider about that order id.

**4. A client retry of `POST /orders` creates a duplicate order.**
- Case: the client gives up after 10 s while the payment retries are still running, then sends the request again. That creates a second order and a second charge.
- Expected: one order.
- Fix: require an `Idempotency-Key` header on `POST /orders` and return the existing order when the same key arrives again.

## Revised flow

```mermaid
flowchart TD
    A[POST /orders with Idempotency-Key] --> B{Key seen before?}
    B -- yes --> R[Return existing order]
    B -- no --> C[Check stock against inventory cache]
    C --> D[Transaction 1: insert order PENDING, commit]
    D --> E[Charge provider, idempotency key = order id<br/>retry timeouts with the same key]
    E -- success --> F[Transaction 2: mark PAID]
    E -- declined --> G[Transaction 2: mark FAILED]
    E -- still timing out --> H[Leave PENDING<br/>reconciliation job asks provider later]
    F --> I[Queue confirmation email<br/>outbox row in the same transaction]
```

## Should fix before launch, but not blocking

- **Overselling.** The stock check reads a cache that can be up to 5 minutes old, and nothing reserves the stock. Two customers can both buy the last item. That may be acceptable at your volume, but decide what happens when it does: refund automatically, backorder, or handle it by hand. Write that decision into the doc.
- **Confirmation email.** The doc doesn't say what happens when sending fails after commit. An outbox row (a database row that a worker turns into the email and retries until it sends) avoids silently lost emails.
- **Tests.** One happy-path integration test isn't enough for a payment flow. You also need integration tests for:
  - a declined card
  - a timeout followed by a successful retry, asserting there is exactly one charge
  - a timeout on every attempt, leaving the order `PENDING`
  - a crash between the charge and Transaction 2
  - a duplicate `Idempotency-Key`

  Write these first. They are the specification for problems 1–4.

## What's fine as written

- The context section: one Postgres instance comfortably handles 2,000 orders a day, so scale isn't a concern here.
- The code structure: separate modules that don't read each other's tables, thin routes, the `OrderCheckout` service class, and logging with the order id on every line. Keep the order id in the payment-call logs too; you'll need it for reconciliation.

## Next steps

1. Confirm that your payment provider supports idempotency keys.
2. Rewrite steps 3–6 to match the revised flow.
3. Add the "unknown" outcome and the reconciliation job.
4. Write down the overselling policy.
5. Expand the test plan.

Once those are in the doc, you can start building.

VERDICT: NOT READY
