From ee90137ed5ea5e5259eb557d50867f9c1eead1ec Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Mon, 30 Nov 2026 20:03:40 -0800 Subject: [PATCH 1/2] fix(billing): answer stripe's refusals to end a trial early with a conflict A declined card already maps to a conflict, but Stripe can also refuse the first charge itself, such as an amount past the card currency's limit or a subscription past its daily invoice limit. Those surfaced as server errors; they now return a clear conflict, keep the trial running, and log Stripe's type and code. --- apps/sim/lib/billing/application/pro-trial.ts | 22 +++++++++++++++ apps/sim/lib/billing/free-plan.integration.ts | 28 +++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/apps/sim/lib/billing/application/pro-trial.ts b/apps/sim/lib/billing/application/pro-trial.ts index a12d7a4738e..c6d7d7ec94b 100644 --- a/apps/sim/lib/billing/application/pro-trial.ts +++ b/apps/sim/lib/billing/application/pro-trial.ts @@ -1,3 +1,4 @@ +import { createLogger } from '@sim/logger' import Stripe from 'stripe' import { getHighestPriorityPersonalSubscription } from '@/lib/billing/core/plan' import { requireStripeClient } from '@/lib/billing/stripe-client' @@ -7,6 +8,8 @@ import { OrchestrationError } from '@/lib/core/orchestration/types' import type { UserAccountOperation } from '@/lib/users/application/operations' import { authorizeAccountPreferences } from '@/lib/users/application/preferences-authorization' +const logger = createLogger('ProTrial') + /** Session-only: ending a trial charges the account's own card, so no delegation may do it. */ function defineProTrialOperation(id: Id) { const operation = { @@ -64,6 +67,25 @@ export const endProTrial: OperationUseCase< 'Your card could not be charged. Update your payment method, then try again.' ) } + // Stripe can also refuse the first charge itself, such as an amount past the card + // currency's limit or a subscription past its daily invoice limit. The trial keeps running + // either way, so the payer gets a clear answer instead of a server error. + if ( + error instanceof Stripe.errors.StripeInvalidRequestError || + error instanceof Stripe.errors.StripeRateLimitError + ) { + logger.warn('Stripe refused to end a trial early', { + subscriptionId: subscription.id, + type: error.type, + code: error.code, + error: error.message, + }) + throw new OrchestrationError( + 'conflict', + "We couldn't end your trial early right now. Your trial keeps running, so you can try again later or let it end on its own.", + { cause: error } + ) + } throw error } return { success: true } diff --git a/apps/sim/lib/billing/free-plan.integration.ts b/apps/sim/lib/billing/free-plan.integration.ts index 1fcf366dd77..fd6a6b5356c 100644 --- a/apps/sim/lib/billing/free-plan.integration.ts +++ b/apps/sim/lib/billing/free-plan.integration.ts @@ -451,6 +451,34 @@ describe('endProTrial', () => { ) }) + it.each([ + { + label: 'a charge past the card currency limit', + refusal: new Stripe.errors.StripeInvalidRequestError({ + type: 'invalid_request_error', + message: 'Amount must be no more than Rp999,999.99', + }), + }, + { + label: "a subscription past Stripe's daily invoice limit", + refusal: new Stripe.errors.StripeRateLimitError({ + type: 'rate_limit_error', + message: 'You have exceeded the daily invoice limit for this subscription.', + }), + }, + ])('reports $label as a payer error and keeps the trial', async ({ refusal }) => { + const userId = await createUser() + await addSubscription(userId, 'trialing') + stripeStub.subscriptions.update.mockRejectedValueOnce(refusal) + + await expect( + endProTrial.execute({ principal: asSession(userId), input: {} }) + ).rejects.toMatchObject({ + code: 'conflict', + message: expect.stringContaining('trial keeps running'), + }) + }) + it('refuses an account whose plan is already paid', async () => { const userId = await createUser() await addSubscription(userId, 'active') From 7467b147ce18af2872b69c88d80026e72e598168 Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Mon, 30 Nov 2026 20:15:56 -0800 Subject: [PATCH 2/2] fix(billing): match only stripe's confirmed charge refusals when ending a trial --- apps/sim/lib/billing/application/pro-trial.ts | 27 ++++++++++++------ apps/sim/lib/billing/free-plan.integration.ts | 28 +++++++++++++++++-- 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/apps/sim/lib/billing/application/pro-trial.ts b/apps/sim/lib/billing/application/pro-trial.ts index c6d7d7ec94b..0ad21ed1773 100644 --- a/apps/sim/lib/billing/application/pro-trial.ts +++ b/apps/sim/lib/billing/application/pro-trial.ts @@ -10,6 +10,23 @@ import { authorizeAccountPreferences } from '@/lib/users/application/preferences const logger = createLogger('ProTrial') +/** + * Stripe invalid-request codes that refuse the first charge itself rather than reporting a fault + * in the request: an amount past the card currency's maximum, or a subscription past Stripe's + * per-subscription invoice limit. + */ +const STRIPE_CHARGE_REFUSAL_CODES: ReadonlySet = new Set(['amount_too_large', 'rate_limit']) + +/** Whether Stripe refused to charge now, which leaves the trial running and is worth a retry later. */ +function isStripeChargeRefusal(error: unknown): error is Stripe.errors.StripeError { + if (error instanceof Stripe.errors.StripeRateLimitError) return true + return ( + error instanceof Stripe.errors.StripeInvalidRequestError && + error.code !== undefined && + STRIPE_CHARGE_REFUSAL_CODES.has(error.code) + ) +} + /** Session-only: ending a trial charges the account's own card, so no delegation may do it. */ function defineProTrialOperation(id: Id) { const operation = { @@ -67,15 +84,9 @@ export const endProTrial: OperationUseCase< 'Your card could not be charged. Update your payment method, then try again.' ) } - // Stripe can also refuse the first charge itself, such as an amount past the card - // currency's limit or a subscription past its daily invoice limit. The trial keeps running - // either way, so the payer gets a clear answer instead of a server error. - if ( - error instanceof Stripe.errors.StripeInvalidRequestError || - error instanceof Stripe.errors.StripeRateLimitError - ) { + if (isStripeChargeRefusal(error)) { logger.warn('Stripe refused to end a trial early', { - subscriptionId: subscription.id, + stripeSubscriptionId: subscription.stripeSubscriptionId, type: error.type, code: error.code, error: error.message, diff --git a/apps/sim/lib/billing/free-plan.integration.ts b/apps/sim/lib/billing/free-plan.integration.ts index fd6a6b5356c..0ead08a6c60 100644 --- a/apps/sim/lib/billing/free-plan.integration.ts +++ b/apps/sim/lib/billing/free-plan.integration.ts @@ -456,14 +456,23 @@ describe('endProTrial', () => { label: 'a charge past the card currency limit', refusal: new Stripe.errors.StripeInvalidRequestError({ type: 'invalid_request_error', - message: 'Amount must be no more than Rp999,999.99', + code: 'amount_too_large', + message: 'Amount must be no more than the currency maximum.', }), }, { label: "a subscription past Stripe's daily invoice limit", + refusal: new Stripe.errors.StripeInvalidRequestError({ + type: 'invalid_request_error', + code: 'rate_limit', + message: 'You have exceeded the daily invoice limit for this subscription.', + }), + }, + { + label: 'a Stripe rate limit', refusal: new Stripe.errors.StripeRateLimitError({ type: 'rate_limit_error', - message: 'You have exceeded the daily invoice limit for this subscription.', + message: 'Too many requests hit the API too quickly.', }), }, ])('reports $label as a payer error and keeps the trial', async ({ refusal }) => { @@ -479,6 +488,21 @@ describe('endProTrial', () => { }) }) + it('still fails on a Stripe request fault that is not a refusal', async () => { + const userId = await createUser() + await addSubscription(userId, 'trialing') + const fault = new Stripe.errors.StripeInvalidRequestError({ + type: 'invalid_request_error', + code: 'resource_missing', + message: 'No such subscription.', + }) + stripeStub.subscriptions.update.mockRejectedValueOnce(fault) + + await expect(endProTrial.execute({ principal: asSession(userId), input: {} })).rejects.toBe( + fault + ) + }) + it('refuses an account whose plan is already paid', async () => { const userId = await createUser() await addSubscription(userId, 'active')