Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
fix(api): retry webhook when Signed BAA cancel fails
Richie McIlroyf0ec6d73a2b5
diff --git a/apps/web/__tests__/unit/signed-baa-webhook.test.ts b/apps/web/__tests__/unit/signed-baa-webhook.test.tsindex 66d298749..8115e4253 100644--- a/apps/web/__tests__/unit/signed-baa-webhook.test.ts+++ b/apps/web/__tests__/unit/signed-baa-webhook.test.ts@@ -232,4 +232,49 @@ describe("Stripe webhook — Signed BAA", () => {expect(res.status).toBe(200);expect(mockStripe.subscriptions.cancel).not.toHaveBeenCalled();});++ it("fails the webhook when Signed BAA cancellation is rejected", async () => {+ mockStripe.webhooks.constructEvent.mockReturnValue({+ type: "customer.subscription.updated",+ data: {+ object: {+ id: "sub_pro",+ status: "unpaid",+ customer: "cus_1",+ metadata: {},+ },+ },+ });+ mockStripe.customers.retrieve.mockResolvedValue({+ id: "cus_1",+ email: "[email protected]",+ metadata: { userId: "user-1" },+ });+ mockDbChain.limit.mockResolvedValueOnce([+ { id: "user-1", email: "[email protected]" },+ ]);+ mockStripe.subscriptions.list.mockResolvedValue({+ data: [+ {+ id: "sub_pro",+ status: "unpaid",+ metadata: {},+ items: { data: [{ quantity: 1 }] },+ },+ {+ id: "sub_baa",+ status: "active",+ metadata: { type: "signed_baa" },+ items: { data: [{ quantity: 1 }] },+ },+ ],+ });+ mockStripe.subscriptions.cancel.mockRejectedValue(+ new Error("stripe unavailable"),+ );++ const res = await POST(makeWebhookRequest());++ expect(res.status).toBe(400);+ });});diff --git a/apps/web/app/api/webhooks/stripe/route.ts b/apps/web/app/api/webhooks/stripe/route.tsindex 49d72b4bd..8c4acc70a 100644--- a/apps/web/app/api/webhooks/stripe/route.ts+++ b/apps/web/app/api/webhooks/stripe/route.ts@@ -119,27 +119,17 @@ async function cancelEntitledBaaSubscriptions(// BAA terms end with the services agreement, so the add-on must stop// billing when no entitled Pro subscription remains.for (const sub of subscriptions) {- if (- isSignedBaaSubscription(sub) &&- ENTITLED_SUBSCRIPTION_STATUSES.has(sub.status)- ) {- try {- await stripe().subscriptions.cancel(sub.id);- await db()- .update(signedBaas)- .set({ status: "canceled" })- .where(eq(signedBaas.stripeSubscriptionId, sub.id));- console.log("Signed BAA subscription canceled alongside Pro", {- subscriptionId: sub.id,- });- } catch (cancelError) {- console.error(- "Failed to cancel Signed BAA subscription",- sub.id,- cancelError,- );- }+ if (!isSignedBaaSubscription(sub)) continue;+ if (ENTITLED_SUBSCRIPTION_STATUSES.has(sub.status)) {+ await stripe().subscriptions.cancel(sub.id);}+ await db()+ .update(signedBaas)+ .set({ status: "canceled" })+ .where(eq(signedBaas.stripeSubscriptionId, sub.id));+ console.log("Signed BAA subscription canceled alongside Pro", {+ subscriptionId: sub.id,+ });}}