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.
This commit is contained in:
gbuomprisco
2025-05-15 23:42:39 +08:00
parent b55b02e2f9
commit 804fb7fbe3

View File

@@ -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 =