Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(api): cancel Signed BAA when Pro becomes unpaid

Richie McIlroy6df83ec35c4f
diff --git a/apps/web/__tests__/unit/signed-baa-webhook.test.ts b/apps/web/__tests__/unit/signed-baa-webhook.test.ts
index 0d8365714..66d298749 100644
--- a/apps/web/__tests__/unit/signed-baa-webhook.test.ts
+++ b/apps/web/__tests__/unit/signed-baa-webhook.test.ts
@@ -144,4 +144,92 @@ describe("Stripe webhook — Signed BAA", () => {
});
expect(mockStripe.customers.retrieve).not.toHaveBeenCalled();
});
+
+ it("cancels Signed BAA when Pro becomes unpaid", 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.mockResolvedValue({ id: "sub_baa" });
+
+ const res = await POST(makeWebhookRequest());
+
+ expect(res.status).toBe(200);
+ expect(mockStripe.subscriptions.cancel).toHaveBeenCalledWith("sub_baa");
+ expect(mockDbChain.set).toHaveBeenCalledWith({ status: "canceled" });
+ });
+
+ it("keeps Signed BAA while Pro is past_due", async () => {
+ mockStripe.webhooks.constructEvent.mockReturnValue({
+ type: "customer.subscription.updated",
+ data: {
+ object: {
+ id: "sub_pro",
+ status: "past_due",
+ 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: "past_due",
+ metadata: {},
+ items: { data: [{ quantity: 1 }] },
+ },
+ {
+ id: "sub_baa",
+ status: "active",
+ metadata: { type: "signed_baa" },
+ items: { data: [{ quantity: 1 }] },
+ },
+ ],
+ });
+
+ const res = await POST(makeWebhookRequest());
+
+ expect(res.status).toBe(200);
+ expect(mockStripe.subscriptions.cancel).not.toHaveBeenCalled();
+ });
});
diff --git a/apps/web/app/api/webhooks/stripe/route.ts b/apps/web/app/api/webhooks/stripe/route.ts
index 888ca886d..49d72b4bd 100644
--- a/apps/web/app/api/webhooks/stripe/route.ts
+++ b/apps/web/app/api/webhooks/stripe/route.ts
@@ -92,7 +92,11 @@ async function grantDeveloperCredits(
return NextResponse.json({ received: true });
}
-const BAA_ENTITLED_STATUSES = new Set(["active", "trialing", "past_due"]);
+const ENTITLED_SUBSCRIPTION_STATUSES = new Set([
+ "active",
+ "trialing",
+ "past_due",
+]);
// The Signed BAA add-on lives on its own subscription for the same customer.
// It must never be treated as the Pro subscription: it would otherwise
@@ -101,10 +105,48 @@ function isSignedBaaSubscription(subscription: Stripe.Subscription) {
return subscription.metadata?.type === "signed_baa";
}
+function hasEntitledProSubscription(subscriptions: Stripe.Subscription[]) {
+ return subscriptions.some(
+ (sub) =>
+ !isSignedBaaSubscription(sub) &&
+ ENTITLED_SUBSCRIPTION_STATUSES.has(sub.status),
+ );
+}
+
+async function cancelEntitledBaaSubscriptions(
+ subscriptions: Stripe.Subscription[],
+) {
+ // 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,
+ );
+ }
+ }
+ }
+}
+
async function syncSignedBaaStatus(
subscription: Stripe.Subscription,
): Promise<Response> {
- const status = BAA_ENTITLED_STATUSES.has(subscription.status)
+ const status = ENTITLED_SUBSCRIPTION_STATUSES.has(subscription.status)
? "active"
: "canceled";
const organizationId = subscription.metadata?.organizationId
@@ -484,11 +526,11 @@ export const POST = async (req: Request) => {
// Quota follows entitlement: past_due keeps its seats during the
// dunning window instead of collapsing the org to zero while
// Stripe retries the card.
- const entitledStatuses = new Set(["active", "trialing", "past_due"]);
const inviteQuota = subscriptions.data
.filter(
(sub) =>
- entitledStatuses.has(sub.status) && !isSignedBaaSubscription(sub),
+ ENTITLED_SUBSCRIPTION_STATUSES.has(sub.status) &&
+ !isSignedBaaSubscription(sub),
)
.reduce((total, sub) => {
return (
@@ -521,6 +563,10 @@ export const POST = async (req: Request) => {
"Successfully updated user in database with new invite quota:",
inviteQuota,
);
+
+ if (!hasEntitledProSubscription(subscriptions.data)) {
+ await cancelEntitledBaaSubscriptions(subscriptions.data);
+ }
}
if (event.type === "invoice.payment_failed") {
@@ -678,46 +724,13 @@ export const POST = async (req: Request) => {
inviteQuota: 1,
});
- // The Signed BAA add-on cannot outlive Cap Pro: its terms tie
- // termination to the services agreement. When the last Pro
- // subscription is deleted, cancel any active BAA subscription so
- // the customer isn't billed $99/mo for a service they no longer
- // have. Skipped when another entitled Pro subscription remains
- // (e.g. a plan switch that deletes and recreates).
const remainingSubscriptions = await stripe().subscriptions.list({
customer: customer.id,
status: "all",
limit: 100,
});
- const stillEntitled = new Set(["active", "trialing", "past_due"]);
- const hasOtherProSubscription = remainingSubscriptions.data.some(
- (sub) =>
- !isSignedBaaSubscription(sub) && stillEntitled.has(sub.status),
- );
- if (!hasOtherProSubscription) {
- for (const sub of remainingSubscriptions.data) {
- if (isSignedBaaSubscription(sub) && stillEntitled.has(sub.status)) {
- try {
- await stripe().subscriptions.cancel(sub.id);
- // The cancellation fires its own deleted webhook which
- // also syncs the row; update eagerly in case that
- // delivery fails or arrives late.
- 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 (!hasEntitledProSubscription(remainingSubscriptions.data)) {
+ await cancelEntitledBaaSubscriptions(remainingSubscriptions.data);
}
}