From 804fb7fbe3ac896dcf28b2e6af084917c70743b2 Mon Sep 17 00:00:00 2001 From: gbuomprisco Date: Thu, 15 May 2025 23:42:39 +0800 Subject: [PATCH] Handle trial periods for existing customers in Stripe checkout Updated logic to skip trial periods for existing customers by setting `trialDays` to undefined when a customer is already provided. Modified subscription data and trial settings to use the adjusted `trialDays`. Additionally, added handling for metered billing line items and included a sample trial period configuration in the billing sample config. --- .../src/services/create-stripe-checkout.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/billing/stripe/src/services/create-stripe-checkout.ts b/packages/billing/stripe/src/services/create-stripe-checkout.ts index fbaacd505..da46e1626 100644 --- a/packages/billing/stripe/src/services/create-stripe-checkout.ts +++ b/packages/billing/stripe/src/services/create-stripe-checkout.ts @@ -34,8 +34,15 @@ export async function createStripeCheckout( const isSubscription = mode === 'subscription'; + let trialDays: number | null | undefined = params.plan.trialDays; + + // if the customer already exists, we do not set a trial period + if (customer) { + trialDays = undefined; + } + const trialSettings = - params.plan.trialDays && enableTrialWithoutCreditCard + trialDays && enableTrialWithoutCreditCard ? { trial_settings: { end_behavior: { @@ -50,7 +57,7 @@ export async function createStripeCheckout( | Stripe.Checkout.SessionCreateParams.SubscriptionData | undefined = isSubscription ? { - trial_period_days: params.plan.trialDays, + trial_period_days: trialDays, metadata: { accountId: params.accountId, ...(params.metadata ?? {}), @@ -80,6 +87,12 @@ export async function createStripeCheckout( : { customer_creation: 'always' }; const lineItems = params.plan.lineItems.map((item) => { + if (item.type === 'metered') { + return { + price: item.id, + }; + } + // if we pass a custom quantity for the item ID // we use that - otherwise we set it to 1 by default const quantity =