Allow customers to subscribe for a free trial without the need to enter their CC details (#68)
This commit is contained in:
committed by
GitHub
parent
25adc2d1e3
commit
8f5690b959
@@ -3,6 +3,12 @@ import { z } from 'zod';
|
|||||||
|
|
||||||
import type { CreateBillingCheckoutSchema } from '@kit/billing/schema';
|
import type { CreateBillingCheckoutSchema } from '@kit/billing/schema';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description If set to true, users can start a trial without entering their credit card details
|
||||||
|
*/
|
||||||
|
const enableTrialWithoutCreditCard =
|
||||||
|
process.env.STRIPE_ENABLE_TRIAL_WITHOUT_CC === 'true';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @name createStripeCheckout
|
* @name createStripeCheckout
|
||||||
* @description Creates a Stripe Checkout session, and returns an Object
|
* @description Creates a Stripe Checkout session, and returns an Object
|
||||||
@@ -28,6 +34,14 @@ export async function createStripeCheckout(
|
|||||||
|
|
||||||
const isSubscription = mode === 'subscription';
|
const isSubscription = mode === 'subscription';
|
||||||
|
|
||||||
|
const trialSettings = params.plan.trialDays && enableTrialWithoutCreditCard ? {
|
||||||
|
trial_settings: {
|
||||||
|
end_behavior: {
|
||||||
|
missing_payment_method: 'cancel' as const,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} : {};
|
||||||
|
|
||||||
// this should only be set if the mode is 'subscription'
|
// this should only be set if the mode is 'subscription'
|
||||||
const subscriptionData:
|
const subscriptionData:
|
||||||
| Stripe.Checkout.SessionCreateParams.SubscriptionData
|
| Stripe.Checkout.SessionCreateParams.SubscriptionData
|
||||||
@@ -38,6 +52,7 @@ export async function createStripeCheckout(
|
|||||||
accountId: params.accountId,
|
accountId: params.accountId,
|
||||||
...(params.metadata ?? {}),
|
...(params.metadata ?? {}),
|
||||||
},
|
},
|
||||||
|
...trialSettings,
|
||||||
}
|
}
|
||||||
: {};
|
: {};
|
||||||
|
|
||||||
@@ -81,6 +96,12 @@ export async function createStripeCheckout(
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const paymentCollectionMethod = enableTrialWithoutCreditCard && params.plan.trialDays
|
||||||
|
? {
|
||||||
|
payment_method_collection: 'if_required' as const,
|
||||||
|
}
|
||||||
|
: {};
|
||||||
|
|
||||||
return stripe.checkout.sessions.create({
|
return stripe.checkout.sessions.create({
|
||||||
mode,
|
mode,
|
||||||
allow_promotion_codes: params.enableDiscountField,
|
allow_promotion_codes: params.enableDiscountField,
|
||||||
@@ -91,6 +112,7 @@ export async function createStripeCheckout(
|
|||||||
...customerCreation,
|
...customerCreation,
|
||||||
...customerData,
|
...customerData,
|
||||||
...urls,
|
...urls,
|
||||||
|
...paymentCollectionMethod,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user