Skip to content

Commit 1115a56

Browse files
authored
fix(billing): send the trial reminder only for a trial still running (#10503)
* 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. * test(billing): cover a trial reminder for an end date the trial no longer has
1 parent 4e67e08 commit 1115a56

2 files changed

Lines changed: 48 additions & 6 deletions

File tree

‎apps/sim/lib/billing/free-plan.integration.ts‎

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ import { executeSearchTool } from '@/lib/internal/search/execute-tool'
2121
import { applyScheduleRunFailureUpdate } from '@/background/schedule-execution'
2222

2323
const { stripeStub, mockSendEmail, mockNotifyScheduleAutoDisabled } = vi.hoisted(() => ({
24-
stripeStub: { subscriptions: { update: vi.fn() }, invoices: { createPreview: vi.fn() } },
24+
stripeStub: {
25+
subscriptions: { update: vi.fn(), retrieve: vi.fn() },
26+
invoices: { createPreview: vi.fn() },
27+
},
2528
mockSendEmail: vi.fn(),
2629
mockNotifyScheduleAutoDisabled: vi.fn(),
2730
}))
@@ -110,15 +113,21 @@ function search(userId: string, workspaceId: string) {
110113
})
111114
}
112115

116+
/** The subscriptions as Stripe holds them now, which the reminder reads back. */
117+
const liveSubscriptions = new Map<string, TrialEndingSubscription>()
118+
119+
/** A `trial_will_end` payload, which Stripe also holds as the live subscription. */
113120
function trialWillEnd(stripeSubscriptionId: string, status = 'trialing'): TrialEndingSubscription {
114-
return {
121+
const subscription = {
115122
id: stripeSubscriptionId,
116123
status,
117124
trial_end: Math.floor((Date.now() + 3 * DAY_MS) / 1000),
118125
items: {
119126
data: [{ quantity: 1, price: { unit_amount: 2500, recurring: { interval: 'month' } } }],
120127
},
121128
}
129+
liveSubscriptions.set(stripeSubscriptionId, subscription)
130+
return subscription
122131
}
123132

124133
async function addClaimedSchedule() {
@@ -152,6 +161,10 @@ beforeEach(() => {
152161
mockSendEmail.mockResolvedValue({ success: true })
153162
stripeStub.invoices.createPreview.mockReset()
154163
stripeStub.invoices.createPreview.mockResolvedValue({ amount_due: 2500 })
164+
stripeStub.subscriptions.retrieve.mockReset()
165+
stripeStub.subscriptions.retrieve.mockImplementation(async (id: string) =>
166+
liveSubscriptions.get(id)
167+
)
155168
mockNotifyScheduleAutoDisabled.mockReset()
156169
})
157170

@@ -280,6 +293,32 @@ describe('handleTrialWillEnd', () => {
280293

281294
expect(mockSendEmail).not.toHaveBeenCalled()
282295
})
296+
297+
it('sends nothing when the trial ended before the reminder arrived', async () => {
298+
const userId = await createUser()
299+
const stripeSubscriptionId = await addSubscription(userId, 'trialing')
300+
const event = trialWillEnd(stripeSubscriptionId)
301+
// A card that already had a trial ends the new one at once; the event still says trialing.
302+
liveSubscriptions.set(stripeSubscriptionId, { ...event, status: 'past_due', trial_end: null })
303+
304+
await handleTrialWillEnd(event)
305+
306+
expect(mockSendEmail).not.toHaveBeenCalled()
307+
})
308+
it('sends nothing for a reminder about an end date the trial no longer has', async () => {
309+
const userId = await createUser()
310+
const stripeSubscriptionId = await addSubscription(userId, 'trialing')
311+
const event = trialWillEnd(stripeSubscriptionId)
312+
// Still trialing, but its end moved after this event was sent.
313+
liveSubscriptions.set(stripeSubscriptionId, {
314+
...event,
315+
trial_end: (event.trial_end ?? 0) + 7 * 86_400,
316+
})
317+
318+
await handleTrialWillEnd(event)
319+
320+
expect(mockSendEmail).not.toHaveBeenCalled()
321+
})
283322
})
284323

285324
describe('endProTrial', () => {

‎apps/sim/lib/billing/webhooks/plan-notices.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,15 @@ async function readRenewalCharge(
6868
* Handles Stripe's `customer.subscription.trial_will_end`, which arrives three days before a trial
6969
* converts: emails the trialing account the date, the charge, and how to cancel. Sent once per
7070
* subscription and trial end, however often Stripe redelivers the event.
71+
*
72+
* Reads the subscription as Stripe holds it now: the event's copy can predate a trial ended early
73+
* (a card that already had a trial is charged at once), and Stripe does not order its webhooks.
7174
*/
72-
export async function handleTrialWillEnd(
73-
stripeSubscription: TrialEndingSubscription
74-
): Promise<void> {
75+
export async function handleTrialWillEnd(event: TrialEndingSubscription): Promise<void> {
76+
const stripeSubscription: TrialEndingSubscription =
77+
await requireStripeClient().subscriptions.retrieve(event.id)
7578
const trialEnd = stripeSubscription.trial_end
76-
if (stripeSubscription.status !== 'trialing' || !trialEnd) return
79+
if (stripeSubscription.status !== 'trialing' || !trialEnd || trialEnd !== event.trial_end) return
7780

7881
const [recorded] = await db
7982
.select({ plan: subscription.plan, referenceId: subscription.referenceId })

0 commit comments

Comments
 (0)