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

**Not ready to build as written.** The payment part of the flow (steps 3–6) can charge a customer twice, or charge them and leave no order on record. Both come from three decisions about how the payment call is wrapped. The fixes are standard and probably take half a day to write into the doc. The rest of the doc is sound, and you can start on the module skeleton, routes and inventory module tomorrow while you rewrite steps 3–6.

## Must fix before building the payment flow

**1. Retrying a timed-out charge can charge the customer twice (step 4).**
A timeout does not mean the charge failed. It only means you don't know the result.
- *Case:* the provider charges the card for $50, but the response takes longer than 5 s. The service retries and the provider charges $50 again.
- *Wrong result:* $100 charged for one order.
- *Expected:* $50.

**Fix:** send an idempotency key with every charge call, derived from the order id. Most providers support this, and a retry with the same key returns the original result instead of charging again. Confirm your provider supports it before you write any code.

**2. Holding the database transaction open across the payment call loses paid orders (steps 3–6).**
The `PENDING` insert and the charge sit inside one transaction.
- *Case:* the charge succeeds, then the commit fails, or the process restarts before it commits.
- *Wrong result:* the transaction rolls back, so the `PENDING` row disappears. The customer is charged and you have no record of the order.
- *Expected:* an order row exists for every charge.

There is a second cost. With 4 attempts × 5 s, one checkout can hold a connection and row locks for 20 s or more. A few slow checkouts at once could use up every database connection.

**Fix:** commit `PENDING` in its own short transaction *before* calling the provider. Then update the status in a second transaction.

**3. "On failure, roll back and return an error" treats "unknown" as "failed" (step 6).**
- *Case:* all 4 attempts time out, but the provider actually charged the card on attempt 1.
- *Wrong result:* the client is told the order failed and probably tries again.
- *Expected:* the order is marked as having an unknown payment result and gets resolved.

**Fix:** add a `PAYMENT_UNKNOWN` status. Resolve those orders with the provider's webhooks, or a small job that looks up the charge by its idempotency key. That same job also catches orders stuck in `PENDING` after a crash.

**4. The `POST /orders` endpoint itself has no protection against duplicate requests.**
- *Case:* a double-click, or the client retrying after a 20 s wait.
- *Wrong result:* two orders and two charges with two different idempotency keys, so the fix in item 1 does not help.
- *Expected:* one order.

**Fix:** require a client-supplied `Idempotency-Key` header and store it with a unique constraint.

Proposed flow:

```mermaid
flowchart TD
    A[POST /orders + Idempotency-Key] --> B{Key seen before?}
    B -- yes --> R[Return existing order]
    B -- no --> C[Check stock in inventory cache]
    C --> D[Txn 1: insert order PENDING, commit]
    D --> E[Charge provider, idempotency key = order id]
    E -- success --> F[Txn 2: mark PAID]
    E -- declined --> G[Txn 2: mark FAILED, return error]
    E -- timed out after retries --> H[Mark PAYMENT_UNKNOWN, return 'processing']
    H --> J[Reconciliation job / provider webhook]
    J --> F
    J --> G
    F --> K[Queue confirmation email]
```

## Should decide, but these needn't block you

- **Overselling (step 2).** The stock check reads a cache that can be up to 5 minutes old, and nothing reserves or decrements stock. Two customers can both buy the last unit. At about 2,000 orders a day that is acceptable *if you choose it on purpose*. Either write down the policy (for example "oversold orders are refunded or backordered") or add a reservation step. The doc also never says what happens when the cache shows zero stock, or which component tells the warehouse system an item was sold.
- **Confirmation email (step 5).** A failed email must not fail the order. Queue it, for example with an outbox table written in Txn 2, rather than sending it inside the request.
- **Tests.** One integration test for the happy path is not enough for a payment flow. Add tests for: a declined charge, a timeout followed by a successful retry (exactly one charge), a timeout on every attempt (ends as `PAYMENT_UNKNOWN`), a duplicate `POST` with the same key, and a crash between the charge and the commit. A fake payment provider that can be told to time out makes all of these cheap to write.
- **Backups.** With nightly backups only, a database failure can lose up to 24 hours of orders. You can rebuild charges from the provider's records, but not order contents. Point-in-time recovery (continuous WAL archiving) costs little and is worth turning on before launch.

## What's already good

- Postgres on a single instance is plenty for 2,000 orders a day.
- Separate modules, a ban on reading another module's tables, thin routes, and one `OrderCheckout` service class are the right structure.
- Logging the order id on every line will make the reconciliation work above much easier to debug.

VERDICT: NOT READY
