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:
@@ -34,8 +34,15 @@ export async function createStripeCheckout(
|
|||||||
|
|
||||||
const isSubscription = mode === 'subscription';
|
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 =
|
const trialSettings =
|
||||||
params.plan.trialDays && enableTrialWithoutCreditCard
|
trialDays && enableTrialWithoutCreditCard
|
||||||
? {
|
? {
|
||||||
trial_settings: {
|
trial_settings: {
|
||||||
end_behavior: {
|
end_behavior: {
|
||||||
@@ -50,7 +57,7 @@ export async function createStripeCheckout(
|
|||||||
| Stripe.Checkout.SessionCreateParams.SubscriptionData
|
| Stripe.Checkout.SessionCreateParams.SubscriptionData
|
||||||
| undefined = isSubscription
|
| undefined = isSubscription
|
||||||
? {
|
? {
|
||||||
trial_period_days: params.plan.trialDays,
|
trial_period_days: trialDays,
|
||||||
metadata: {
|
metadata: {
|
||||||
accountId: params.accountId,
|
accountId: params.accountId,
|
||||||
...(params.metadata ?? {}),
|
...(params.metadata ?? {}),
|
||||||
@@ -80,6 +87,12 @@ export async function createStripeCheckout(
|
|||||||
: { customer_creation: 'always' };
|
: { customer_creation: 'always' };
|
||||||
|
|
||||||
const lineItems = params.plan.lineItems.map((item) => {
|
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
|
// if we pass a custom quantity for the item ID
|
||||||
// we use that - otherwise we set it to 1 by default
|
// we use that - otherwise we set it to 1 by default
|
||||||
const quantity =
|
const quantity =
|
||||||
|
|||||||
Reference in New Issue
Block a user