Refactor and improve billing module
The billing module has been refined and enhanced to include deeper validation and detailing of billing plans and products. The checkout session creation process was revised to handle more complex scenarios, incorporating better parsing and validation. Additional validations were added for the plan and product schemas, improving product details extraction, and rearranging of module exports was made for better organization. The code refactor allows easier future modifications and upgrades for recurring and one-time payments with nuanced product configurations.
This commit is contained in:
@@ -26,19 +26,18 @@ export async function createStripeCheckout(
|
||||
const mode: Stripe.Checkout.SessionCreateParams.Mode =
|
||||
params.paymentType === 'recurring' ? 'subscription' : 'payment';
|
||||
|
||||
// TODO: support multiple line items and per-seat pricing
|
||||
const lineItem: Stripe.Checkout.SessionCreateParams.LineItem = {
|
||||
quantity: 1,
|
||||
price: params.planId,
|
||||
};
|
||||
|
||||
const subscriptionData: Stripe.Checkout.SessionCreateParams.SubscriptionData =
|
||||
{
|
||||
trial_period_days: params.trialPeriodDays,
|
||||
metadata: {
|
||||
accountId: params.accountId,
|
||||
},
|
||||
};
|
||||
// this should only be set if the mode is 'subscription'
|
||||
const subscriptionData:
|
||||
| Stripe.Checkout.SessionCreateParams.SubscriptionData
|
||||
| undefined =
|
||||
mode === 'subscription'
|
||||
? {
|
||||
trial_period_days: params.trialDays,
|
||||
metadata: {
|
||||
accountId: params.accountId,
|
||||
},
|
||||
}
|
||||
: undefined;
|
||||
|
||||
const urls = getUrls({
|
||||
returnUrl: params.returnUrl,
|
||||
@@ -55,10 +54,23 @@ export async function createStripeCheckout(
|
||||
customer_email: params.customerEmail,
|
||||
};
|
||||
|
||||
const lineItems = params.lineItems.map((item) => {
|
||||
if (item.usageType === 'metered') {
|
||||
return {
|
||||
price: item.id,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
price: item.id,
|
||||
quantity: item.quantity,
|
||||
};
|
||||
});
|
||||
|
||||
return stripe.checkout.sessions.create({
|
||||
mode,
|
||||
ui_mode: uiMode,
|
||||
line_items: [lineItem],
|
||||
line_items: lineItems,
|
||||
client_reference_id: clientReferenceId,
|
||||
subscription_data: subscriptionData,
|
||||
...customerData,
|
||||
|
||||
Reference in New Issue
Block a user