From b0793548963c042c77556dd8de71b7b89960736a Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Sat, 28 Nov 2026 15:19:48 -0800 Subject: [PATCH 1/2] fix(billing): send the trial reminder only for a trial still running The reminder trusted the webhook's copy of the subscription, which can predate a trial ended early when the card already had one, so the account was told its trial ends in two weeks after being charged. It now reads the subscription back from Stripe and sends only while that trial is still running to the same end. --- apps/sim/lib/billing/free-plan.integration.ts | 29 +++++++++++++++++-- apps/sim/lib/billing/webhooks/plan-notices.ts | 11 ++++--- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/apps/sim/lib/billing/free-plan.integration.ts b/apps/sim/lib/billing/free-plan.integration.ts index 6fb202a9ac0..5cb5152bd53 100644 --- a/apps/sim/lib/billing/free-plan.integration.ts +++ b/apps/sim/lib/billing/free-plan.integration.ts @@ -21,7 +21,10 @@ import { executeSearchTool } from '@/lib/internal/search/execute-tool' import { applyScheduleRunFailureUpdate } from '@/background/schedule-execution' const { stripeStub, mockSendEmail, mockNotifyScheduleAutoDisabled } = vi.hoisted(() => ({ - stripeStub: { subscriptions: { update: vi.fn() }, invoices: { createPreview: vi.fn() } }, + stripeStub: { + subscriptions: { update: vi.fn(), retrieve: vi.fn() }, + invoices: { createPreview: vi.fn() }, + }, mockSendEmail: vi.fn(), mockNotifyScheduleAutoDisabled: vi.fn(), })) @@ -110,8 +113,12 @@ function search(userId: string, workspaceId: string) { }) } +/** The subscriptions as Stripe holds them now, which the reminder reads back. */ +const liveSubscriptions = new Map() + +/** A `trial_will_end` payload, which Stripe also holds as the live subscription. */ function trialWillEnd(stripeSubscriptionId: string, status = 'trialing'): TrialEndingSubscription { - return { + const subscription = { id: stripeSubscriptionId, status, trial_end: Math.floor((Date.now() + 3 * DAY_MS) / 1000), @@ -119,6 +126,8 @@ function trialWillEnd(stripeSubscriptionId: string, status = 'trialing'): TrialE data: [{ quantity: 1, price: { unit_amount: 2500, recurring: { interval: 'month' } } }], }, } + liveSubscriptions.set(stripeSubscriptionId, subscription) + return subscription } async function addClaimedSchedule() { @@ -152,6 +161,10 @@ beforeEach(() => { mockSendEmail.mockResolvedValue({ success: true }) stripeStub.invoices.createPreview.mockReset() stripeStub.invoices.createPreview.mockResolvedValue({ amount_due: 2500 }) + stripeStub.subscriptions.retrieve.mockReset() + stripeStub.subscriptions.retrieve.mockImplementation(async (id: string) => + liveSubscriptions.get(id) + ) mockNotifyScheduleAutoDisabled.mockReset() }) @@ -280,6 +293,18 @@ describe('handleTrialWillEnd', () => { expect(mockSendEmail).not.toHaveBeenCalled() }) + + it('sends nothing when the trial ended before the reminder arrived', async () => { + const userId = await createUser() + const stripeSubscriptionId = await addSubscription(userId, 'trialing') + const event = trialWillEnd(stripeSubscriptionId) + // A card that already had a trial ends the new one at once; the event still says trialing. + liveSubscriptions.set(stripeSubscriptionId, { ...event, status: 'past_due', trial_end: null }) + + await handleTrialWillEnd(event) + + expect(mockSendEmail).not.toHaveBeenCalled() + }) }) describe('endProTrial', () => { diff --git a/apps/sim/lib/billing/webhooks/plan-notices.ts b/apps/sim/lib/billing/webhooks/plan-notices.ts index ea3ea9e2231..1f52d2d88c8 100644 --- a/apps/sim/lib/billing/webhooks/plan-notices.ts +++ b/apps/sim/lib/billing/webhooks/plan-notices.ts @@ -68,12 +68,15 @@ async function readRenewalCharge( * Handles Stripe's `customer.subscription.trial_will_end`, which arrives three days before a trial * converts: emails the trialing account the date, the charge, and how to cancel. Sent once per * subscription and trial end, however often Stripe redelivers the event. + * + * Reads the subscription as Stripe holds it now: the event's copy can predate a trial ended early + * (a card that already had a trial is charged at once), and Stripe does not order its webhooks. */ -export async function handleTrialWillEnd( - stripeSubscription: TrialEndingSubscription -): Promise { +export async function handleTrialWillEnd(event: TrialEndingSubscription): Promise { + const stripeSubscription: TrialEndingSubscription = + await requireStripeClient().subscriptions.retrieve(event.id) const trialEnd = stripeSubscription.trial_end - if (stripeSubscription.status !== 'trialing' || !trialEnd) return + if (stripeSubscription.status !== 'trialing' || !trialEnd || trialEnd !== event.trial_end) return const [recorded] = await db .select({ plan: subscription.plan, referenceId: subscription.referenceId }) From 30bea55e2547cfa3a124612cc9de7f758fbac1dd Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Sat, 28 Nov 2026 15:30:44 -0800 Subject: [PATCH 2/2] test(billing): cover a trial reminder for an end date the trial no longer has --- apps/sim/lib/billing/free-plan.integration.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/apps/sim/lib/billing/free-plan.integration.ts b/apps/sim/lib/billing/free-plan.integration.ts index 5cb5152bd53..1a5dcb51eec 100644 --- a/apps/sim/lib/billing/free-plan.integration.ts +++ b/apps/sim/lib/billing/free-plan.integration.ts @@ -303,6 +303,20 @@ describe('handleTrialWillEnd', () => { await handleTrialWillEnd(event) + expect(mockSendEmail).not.toHaveBeenCalled() + }) + it('sends nothing for a reminder about an end date the trial no longer has', async () => { + const userId = await createUser() + const stripeSubscriptionId = await addSubscription(userId, 'trialing') + const event = trialWillEnd(stripeSubscriptionId) + // Still trialing, but its end moved after this event was sent. + liveSubscriptions.set(stripeSubscriptionId, { + ...event, + trial_end: (event.trial_end ?? 0) + 7 * 86_400, + }) + + await handleTrialWillEnd(event) + expect(mockSendEmail).not.toHaveBeenCalled() }) })