Skip to content

Webhook handled twice

A service receives an order.confirmed webhook and charges the card. The sender retries when it does not hear back in time, so the same order can arrive twice. Two workers take the two deliveries at the same time, both read unpaid, and both charge the card. A test that runs the retry after the first delivery passes.

src/webhook.ts
export interface Deps {
getStatus: (orderId: string) => Promise<OrderStatus>;
chargeCard: (orderId: string) => Promise<void>;
setStatus: (orderId: string, status: OrderStatus) => Promise<void>;
}
export async function handleOrderConfirmed(orderId: string, deps: Deps): Promise<void> {
const status = await deps.getStatus(orderId);
if (status === 'unpaid') {
await deps.chargeCard(orderId);
await deps.setStatus(orderId, 'paid');
}
}

See how fast-check finds and fixes it.

Last updated: