Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(billing): keep committed Stripe seat change on local save failure

Richie McIlroy34c83bdea727
diff --git a/apps/web/__tests__/unit/update-seat-quantity.test.ts b/apps/web/__tests__/unit/update-seat-quantity.test.ts
index 778244ff0..99d149519 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("restores scheduled cancellation when the local quota write fails", async () => {
+ it("does not undo a committed Stripe change when the local write fails", async () => {
mockSeatLookup({ currentQuantity: 1 });
mockStripe.subscriptions.retrieve.mockResolvedValue({
id: "sub_1",
@@ -284,9 +284,9 @@ describe("updateSeatQuantity", () => {
);
await expect(updateSeatQuantity("org-1" as never, 2)).rejects.toThrow(
- "scheduled cancellation is still in effect",
+ "Billing was updated to 2 seats",
);
- expect(mockStripe.subscriptions.update).toHaveBeenCalledWith("sub_1", {
+ expect(mockStripe.subscriptions.update).not.toHaveBeenCalledWith("sub_1", {
cancel_at_period_end: true,
});
});
diff --git a/apps/web/actions/organization/update-seat-quantity.ts b/apps/web/actions/organization/update-seat-quantity.ts
index a2a0a6cd7..6cce3279d 100644
--- a/apps/web/actions/organization/update-seat-quantity.ts
+++ b/apps/web/actions/organization/update-seat-quantity.ts
@@ -196,7 +196,9 @@ 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 if the quantity update or local persistence fails.
+ // 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 () => {
try {
await stripe().subscriptions.update(subscription.id, {
@@ -258,9 +260,9 @@ export async function updateSeatQuantity(
.set({ inviteQuota: newQuantity })
.where(eq(users.id, user.id));
} catch (dbError) {
- if (wasCanceling) {
- await restoreScheduledCancellation();
- }
+ // 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,
@@ -269,9 +271,7 @@ export async function updateSeatQuantity(
dbError,
);
throw new Error(
- wasCanceling
- ? "We couldn't save the seat change. Your scheduled cancellation is still in effect. Please try again."
- : "Billing update succeeded but local state could not be saved. Please contact support.",
+ `Billing was updated to ${newQuantity} seats, but saving it locally failed. It will sync automatically shortly; refresh to confirm before retrying.`,
);
}