Refactor logger usage and add discount field to checkout
This commit refactors the logger usage in various files to make it more streamlined and consistent. It also introduces a new 'enableDiscountField' feature for the checkout that can be toggled on specific products. This allows customers to apply discounts at checkout if the product or subscription plan has the 'enableDiscountField' set to true.
This commit is contained in:
@@ -17,6 +17,8 @@ import billingConfig from '~/config/billing.config';
|
||||
import pathsConfig from '~/config/paths.config';
|
||||
|
||||
export class UserBillingService {
|
||||
private readonly namespace = 'billing.personal-account';
|
||||
|
||||
constructor(private readonly client: SupabaseClient<Database>) {}
|
||||
|
||||
async createCheckoutSession({
|
||||
@@ -73,6 +75,7 @@ export class UserBillingService {
|
||||
customerId,
|
||||
plan,
|
||||
variantQuantities: [],
|
||||
enableDiscountField: product.enableDiscountField,
|
||||
});
|
||||
|
||||
logger.info(
|
||||
@@ -111,6 +114,7 @@ export class UserBillingService {
|
||||
}
|
||||
|
||||
const service = await getBillingGatewayProvider(this.client);
|
||||
const logger = await getLogger();
|
||||
|
||||
const accountId = data.id;
|
||||
const customerId = await getCustomerIdFromAccountId(accountId);
|
||||
@@ -120,14 +124,14 @@ export class UserBillingService {
|
||||
throw new Error('Customer not found');
|
||||
}
|
||||
|
||||
const logger = await getLogger();
|
||||
const ctx = {
|
||||
name: this.namespace,
|
||||
customerId,
|
||||
accountId,
|
||||
};
|
||||
|
||||
logger.info(
|
||||
{
|
||||
name: `billing.personal-account`,
|
||||
customerId,
|
||||
accountId,
|
||||
},
|
||||
ctx,
|
||||
`User requested a Billing Portal session. Contacting provider...`,
|
||||
);
|
||||
|
||||
@@ -144,8 +148,7 @@ export class UserBillingService {
|
||||
logger.error(
|
||||
{
|
||||
error,
|
||||
customerId,
|
||||
accountId,
|
||||
...ctx,
|
||||
},
|
||||
`Failed to create a Billing Portal session`,
|
||||
);
|
||||
@@ -155,14 +158,7 @@ export class UserBillingService {
|
||||
);
|
||||
}
|
||||
|
||||
logger.info(
|
||||
{
|
||||
name: `billing.personal-account`,
|
||||
customerId,
|
||||
accountId,
|
||||
},
|
||||
`Session successfully created.`,
|
||||
);
|
||||
logger.info(ctx, `Session successfully created.`);
|
||||
|
||||
// redirect user to billing portal
|
||||
return url;
|
||||
|
||||
@@ -38,14 +38,13 @@ export class TeamBillingService {
|
||||
const accountId = params.accountId;
|
||||
const logger = await getLogger();
|
||||
|
||||
logger.info(
|
||||
{
|
||||
userId,
|
||||
accountId,
|
||||
name: this.namespace,
|
||||
},
|
||||
`Requested checkout session. Processing...`,
|
||||
);
|
||||
const ctx = {
|
||||
userId,
|
||||
accountId,
|
||||
name: this.namespace,
|
||||
};
|
||||
|
||||
logger.info(ctx, `Requested checkout session. Processing...`);
|
||||
|
||||
// verify permissions to manage billing
|
||||
const hasPermission = await getBillingPermissionsForAccountId(
|
||||
@@ -57,11 +56,7 @@ export class TeamBillingService {
|
||||
// then we should not proceed
|
||||
if (!hasPermission) {
|
||||
logger.warn(
|
||||
{
|
||||
userId,
|
||||
accountId,
|
||||
name: this.namespace,
|
||||
},
|
||||
ctx,
|
||||
`User without permissions attempted to create checkout.`,
|
||||
);
|
||||
|
||||
@@ -74,7 +69,7 @@ export class TeamBillingService {
|
||||
|
||||
// retrieve the plan from the configuration
|
||||
// so we can assign the correct checkout data
|
||||
const plan = getPlan(params.productId, params.planId);
|
||||
const { plan, product } = getPlanDetails(params.productId, params.planId);
|
||||
|
||||
// find the customer ID for the account if it exists
|
||||
// (eg. if the account has been billed before)
|
||||
@@ -94,10 +89,8 @@ export class TeamBillingService {
|
||||
|
||||
logger.info(
|
||||
{
|
||||
userId,
|
||||
accountId,
|
||||
...ctx,
|
||||
planId: plan.id,
|
||||
namespace: this.namespace,
|
||||
},
|
||||
`Creating checkout session...`,
|
||||
);
|
||||
@@ -111,6 +104,7 @@ export class TeamBillingService {
|
||||
customerEmail,
|
||||
customerId,
|
||||
variantQuantities,
|
||||
enableDiscountField: product.enableDiscountField,
|
||||
});
|
||||
|
||||
// return the checkout token to the client
|
||||
@@ -121,10 +115,8 @@ export class TeamBillingService {
|
||||
} catch (error) {
|
||||
logger.error(
|
||||
{
|
||||
name: this.namespace,
|
||||
...ctx,
|
||||
error,
|
||||
accountId,
|
||||
planId: plan.id,
|
||||
},
|
||||
`Error creating the checkout session`,
|
||||
);
|
||||
@@ -338,7 +330,7 @@ async function getCustomerIdFromAccountId(
|
||||
return data?.customer_id;
|
||||
}
|
||||
|
||||
function getPlan(productId: string, planId: string) {
|
||||
function getPlanDetails(productId: string, planId: string) {
|
||||
const product = billingConfig.products.find(
|
||||
(product) => product.id === productId,
|
||||
);
|
||||
@@ -353,5 +345,5 @@ function getPlan(productId: string, planId: string) {
|
||||
throw new Error('Plan not found');
|
||||
}
|
||||
|
||||
return plan;
|
||||
return { plan, product };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user