A storefront platform handles the part customers see. Behind it, every order turns into work: stock to allocate, items to pick, parcels to label, exceptions to resolve. Most problems in commerce operations come from treating that work as a single status field.
Model the order lifecycle#
Payment, confirmation, allocation, packing and dispatch are related events with different owners. Write down which system owns each decision and how a person resolves an exception.
- 01Paid
- 02Allocated
- 03Picked
- 04Packed
- 05Dispatched
| Step | Owner | Typical exception | Who resolves it |
|---|---|---|---|
| Paid | Storefront | Payment review | Store operator |
| Allocated | Inventory system | Not enough stock | Operations |
| Picked | Warehouse | Item damaged or missing | Warehouse lead |
| Dispatched | Carrier integration | Label rejected, address invalid | Customer service |
Once ownership is explicit, it becomes clear which system should hold which state, and where synchronisation is actually needed.
Assume events can repeat#
Webhooks are delivered at least once. Treat each one as an input to a durable workflow: verify it, record its identity, then process it. A repeated delivery must not create a second shipment or decrement stock twice.
import { createHmac, timingSafeEqual } from "node:crypto";
export async function POST(request: Request) {
const raw = await request.text(); // verify the exact bytes received
const signature = request.headers.get("x-shopify-hmac-sha256") ?? "";
const expected = createHmac("sha256", process.env.SHOPIFY_WEBHOOK_SECRET!)
.update(raw, "utf8")
.digest("base64");
const valid =
signature.length === expected.length &&
timingSafeEqual(Buffer.from(signature), Buffer.from(expected));
if (!valid) return new Response("Invalid signature", { status: 401 });
const deliveryId = request.headers.get("x-shopify-webhook-id");
if (!deliveryId) return new Response("Missing delivery id", { status: 400 });
// Record first, process asynchronously. Duplicates hit the unique constraint.
const isNew = await inbox.insertIfAbsent(deliveryId, "orders/create", raw);
if (isNew) await queue.publish("order.received", { deliveryId });
return new Response(null, { status: 200 });
}Respond quickly and do the real work in a worker. Providers retry slow endpoints, which multiplies duplicates exactly when your system is already under load.
Reconcile deliberately#
Events give a timely view. Reconciliation checks whether that view still agrees with the source. Webhooks get dropped, edits happen in two places, and a manual stock correction in the warehouse system will never arrive as a storefront event.
Make the work visible#
An operations interface should help someone find the orders that need attention, understand why, and take the next permitted action. Filters by exception type and age matter more than revenue charts. If an order has been allocated but not picked for six hours, that should be the first thing the warehouse lead sees.
Release one connected path#
Begin with a narrow path from a storefront order to a confirmed operational record. Before expanding, test the difficult cases on purpose:
- A cancellation after allocation.
- A partial fulfilment across two parcels.
- A return of one item from a multi-item order.
- A stock level that changed in the warehouse system mid-order.
A connected system earns trust through predictable handling of these cases, not through the happy path.