Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
fix(billing): re-schedule cancellation when local seat save fails
Richie McIlroyadbe83d7ab77
diff --git a/apps/web/__tests__/unit/update-seat-quantity.test.ts b/apps/web/__tests__/unit/update-seat-quantity.test.tsindex 99d149519..233826a96 100644--- a/apps/web/__tests__/unit/update-seat-quantity.test.ts+++ b/apps/web/__tests__/unit/update-seat-quantity.test.ts@@ -267,7 +267,7 @@ describe("updateSeatQuantity", () => {);});- it("does not undo a committed Stripe change when the local write fails", async () => {+ it("restores scheduled cancellation without undoing the committed Stripe change when the local write fails", async () => {mockSeatLookup({ currentQuantity: 1 });mockStripe.subscriptions.retrieve.mockResolvedValue({id: "sub_1",@@ -284,11 +284,41 @@ describe("updateSeatQuantity", () => {);await expect(updateSeatQuantity("org-1" as never, 2)).rejects.toThrow(- "Billing was updated to 2 seats",+ "your scheduled cancellation was kept",);- expect(mockStripe.subscriptions.update).not.toHaveBeenCalledWith("sub_1", {+ expect(mockStripe.subscriptions.update).toHaveBeenCalledTimes(3);+ expect(mockStripe.subscriptions.update).toHaveBeenNthCalledWith(+ 3,+ "sub_1",+ {+ cancel_at_period_end: true,+ },+ );+ });++ it("reports when neither the local save nor the cancellation restore succeeded", async () => {+ mockSeatLookup({ currentQuantity: 1 });+ mockStripe.subscriptions.retrieve.mockResolvedValue({+ id: "sub_1",cancel_at_period_end: true,+ items: {+ data: [{ id: "si_1", quantity: 1 }],+ },+ });+ mockStripe.subscriptions.update+ .mockResolvedValueOnce({ id: "sub_1", pending_update: null })+ .mockResolvedValueOnce({ id: "sub_1", pending_update: null })+ .mockRejectedValueOnce(new Error("restore failed"));+ mockDb.set.mockImplementation(() => {+ throw new Error("db down");});+ const { updateSeatQuantity } = await import(+ "@/actions/organization/update-seat-quantity"+ );++ await expect(updateSeatQuantity("org-1" as never, 2)).rejects.toThrow(+ "couldn't save it locally or keep your scheduled cancellation",+ );});it("does not claim cancellation remains when restore fails", async () => {diff --git a/apps/web/actions/organization/update-seat-quantity.ts b/apps/web/actions/organization/update-seat-quantity.tsindex 6cce3279d..4abeec566 100644--- a/apps/web/actions/organization/update-seat-quantity.ts+++ b/apps/web/actions/organization/update-seat-quantity.ts@@ -196,10 +196,11 @@ export async function updateSeatQuantity(const wasCanceling = subscription.cancel_at_period_end;// Stripe rejects cancel_at_period_end combined with// payment_behavior=pending_if_incomplete, so increases clear cancellation- // first. Restore it only when the quantity update itself failed; once- // Stripe commits the new quantity the requested change succeeded and- // re-scheduling cancellation would undo it.- const restoreScheduledCancellation = async () => {+ // first. Every failure path afterwards must re-schedule it: the committed+ // quantity self-heals locally via the customer.subscription.updated+ // webhook, but nothing re-schedules a dropped cancellation, which would+ // silently resume renewal.+ const restoreScheduledCancellation = async (failureMessage: string) => {try {await stripe().subscriptions.update(subscription.id, {cancel_at_period_end: true,@@ -210,9 +211,7 @@ export async function updateSeatQuantity(subscription.id,restoreError,);- throw new Error(- "We couldn't complete the seat change or keep your scheduled cancellation. Please contact support.",- );+ throw new Error(failureMessage);}};@@ -249,7 +248,9 @@ export async function updateSeatQuantity(}} catch (error) {if (wasCanceling) {- await restoreScheduledCancellation();+ await restoreScheduledCancellation(+ "We couldn't complete the seat change or keep your scheduled cancellation. Please contact support.",+ );}throw error;}@@ -260,9 +261,6 @@ export async function updateSeatQuantity(.set({ inviteQuota: newQuantity }).where(eq(users.id, user.id));} catch (dbError) {- // Stripe already committed the change; the customer.subscription.updated- // webhook recomputes inviteQuota from Stripe, so local state self-heals- // without compensating in Stripe.console.error("CRITICAL: Stripe updated to quantity",newQuantity,@@ -270,6 +268,17 @@ export async function updateSeatQuantity(user.id,dbError,);+ // The committed quantity stays: the customer.subscription.updated+ // webhook recomputes inviteQuota from Stripe, so local state+ // self-heals. Only the cleared cancellation needs compensating here.+ if (wasCanceling) {+ await restoreScheduledCancellation(+ `Billing was updated to ${newQuantity} seats, but we couldn't save it locally or keep your scheduled cancellation. Please contact support.`,+ );+ throw new Error(+ `Billing was updated to ${newQuantity} seats and your scheduled cancellation was kept. Saving it locally failed; it will sync automatically shortly.`,+ );+ }throw new Error(`Billing was updated to ${newQuantity} seats, but saving it locally failed. It will sync automatically shortly; refresh to confirm before retrying.`,);