StoicBuilder
Later missionMission 49: Paid, and still locked outYou can practise it now. It counts once you reach mission 49, after “Draw the lines before the servers”.Back to HQ

DrillsPaid, but the event never left

Paid, but the event never left

XV the PantheonHarbor, built with Claude CodeTrickyAbout 10 minPro

What happened

When Stripe confirms a payment, Harbor's webhook calls markPaid. It marks the invoice paid, then publishes InvoicePaid, which lifts the team's overdue lock and sends the receipt. During Thursday's deploy at 10:14, web-3 was killed mid-request. Three teams who paid in that minute have invoices marked Paid, but they are still locked out of uploads and got no receipt.

Priya, engineering managerThree tickets from Thursday: paid, the invoice says Paid, the red overdue banner is still there and uploads are blocked. Stripe shows the payments and retried the webhook. Billing looks fine. What happened to the event?
What you seeServer log
Dec 03 10:14:07 webhook evt_DEMO_5521 payment_intent.succeeded for inv_2291 Dec 03 10:14:07 inv_2291 marked paid (pi_DEMO_8812) Dec 03 10:14:07 deploy: web-3 did not exit within 10 s, killed (SIGKILL) Dec 03 10:14:19 web-3 started Dec 03 10:15:12 webhook evt_DEMO_5521 (retry) for inv_2291: already paid, skipped

The MagisterRun it first and read what comes back. Then change the code if you like: your edits go to the review too.

Run it: Stripe sends the webhook, a deploy kills the server right after the commit, the server starts again, and Stripe retries.

// Stripe's webhook calls this when the payment for an invoice succeeds.
// Billing owns invoices. Accounts (lifts the overdue lock) and notifications
// (sends the receipt) react to InvoicePaid.
async function markPaid(invoiceId, paymentId) {
  const [current] = await db.select("invoices", (r) => r.id === invoiceId);
  // Stripe retries webhooks, so this can run twice for one payment.
  if (current.status === "paid") return current;

  const invoice = await db.update("invoices", invoiceId, { status: "paid", paymentId, paidAt: NOW });

  await queue.publish({
    id: newId("evt"),
    type: "InvoicePaid",
    invoiceId: invoice.id,
    teamId: invoice.teamId,
    total: invoice.total,
  });
  return invoice;
}

// ---- Simulation, not part of Harbor. db, queue, server, NOW and newId are test
// doubles. Stripe sends the webhook, and a deploy kills the server right after
// the database commit. The server starts again, then Stripe retries the webhook.
await server.handle(() => markPaid("inv_2291", "pi_DEMO_8812"));
await server.restart();
await server.handle(() => markPaid("inv_2291", "pi_DEMO_8812"));

console.log("invoice:", invoices[0].status);
console.log("InvoicePaid events published:", published.filter((e) => e.type === "InvoicePaid").length);

The code runs in your browser against stand-in data. Nothing is sent anywhere.

Your answer

Reviewed by AI against what a good fix needs. About 10 seconds.

Sign in to get your answer reviewed

It's free. Anything you've typed here stays in this browser and is waiting when you come back.

Sign in with email