Add end-to-end tests for user and team billing features

This commit introduces end-to-end tests for the user and team billing features. It also enhances existing billing configurations, logging, and error handling mechanisms. Refactoring has been done to simplify the code and make it more readable. Adjustments have also been made in the visual aspects of some components. The addition of these tests will help ensure the reliability of the billing features.
This commit is contained in:
giancarlo
2024-04-14 17:15:04 +08:00
parent 1321b31ae4
commit d078e0021c
20 changed files with 330 additions and 140 deletions

View File

@@ -26,18 +26,19 @@ export async function createStripeCheckout(
const mode: Stripe.Checkout.SessionCreateParams.Mode =
params.plan.paymentType === 'recurring' ? 'subscription' : 'payment';
const isSubscription = mode === 'subscription';
// 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;
| undefined = isSubscription
? {
trial_period_days: params.trialDays,
metadata: {
accountId: params.accountId,
},
}
: {};
const urls = getUrls({
returnUrl: params.returnUrl,
@@ -54,6 +55,10 @@ export async function createStripeCheckout(
customer_email: params.customerEmail,
};
const customerCreation = isSubscription
? ({} as Record<string, string>)
: { customer_creation: 'always' };
const lineItems = params.plan.lineItems.map((item) => {
if (item.type === 'metered') {
return {
@@ -81,7 +86,7 @@ export async function createStripeCheckout(
line_items: lineItems,
client_reference_id: clientReferenceId,
subscription_data: subscriptionData,
customer_creation: 'always',
...customerCreation,
...customerData,
...urls,
});

View File

@@ -282,17 +282,18 @@ export class StripeWebhookHandlerService
const lineItems = params.lineItems.map((item) => {
const quantity = item.quantity ?? 1;
const variantId = item.price?.id as string;
return {
id: item.id,
quantity,
subscription_id: params.id,
product_id: item.price?.product as string,
variant_id: item.price?.id,
variant_id: variantId,
price_amount: item.price?.unit_amount,
interval: item.price?.recurring?.interval as string,
interval_count: item.price?.recurring?.interval_count as number,
type: getLineItemTypeById(this.config, item.id),
type: getLineItemTypeById(this.config, variantId),
};
});