Refactor billing system and enhance logging
Updated the billing system's schema to change 'storeId' to a string type, and improved the cleanliness and readability of the code. Enhanced the logging system within the billing service for better tracking and debugging. In line with these changes, added corresponding error pages in the client side to handle any errors.
This commit is contained in:
@@ -5,10 +5,10 @@ export const getLemonSqueezyEnv = () =>
|
||||
.object({
|
||||
secretKey: z.string().min(1),
|
||||
webhooksSecret: z.string().min(1),
|
||||
storeId: z.number().positive(),
|
||||
storeId: z.string(),
|
||||
})
|
||||
.parse({
|
||||
secretKey: process.env.LEMON_SQUEEZY_SECRET_KEY,
|
||||
webhooksSecret: process.env.LEMON_SQUEEZY_WEBHOOK_SECRET,
|
||||
webhooksSecret: process.env.LEMON_SQUEEZY_SIGNING_SECRET,
|
||||
storeId: process.env.LEMON_SQUEEZY_STORE_ID,
|
||||
});
|
||||
|
||||
@@ -9,19 +9,16 @@ import { initializeLemonSqueezyClient } from './lemon-squeezy-sdk';
|
||||
* Creates a LemonSqueezy billing portal session for the given parameters.
|
||||
*
|
||||
* @param {object} params - The parameters required to create the billing portal session.
|
||||
* @return {Promise<string>} - A promise that resolves to the URL of the customer portal.
|
||||
* @throws {Error} - If no customer is found with the given customerId.
|
||||
*/
|
||||
export async function createLemonSqueezyBillingPortalSession(
|
||||
params: z.infer<typeof CreateBillingPortalSessionSchema>,
|
||||
) {
|
||||
await initializeLemonSqueezyClient();
|
||||
|
||||
const customer = await getCustomer(params.customerId);
|
||||
const { data, error } = await getCustomer(params.customerId);
|
||||
|
||||
if (!customer?.data) {
|
||||
throw new Error('No customer found');
|
||||
}
|
||||
|
||||
return customer.data.data.attributes.urls.customer_portal;
|
||||
return {
|
||||
data: data?.data.attributes.urls.customer_portal,
|
||||
error,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import {
|
||||
ReportBillingUsageSchema,
|
||||
RetrieveCheckoutSessionSchema,
|
||||
} from '@kit/billing/schema';
|
||||
import { Logger } from '@kit/shared/logger';
|
||||
|
||||
import { createLemonSqueezyBillingPortalSession } from './create-lemon-squeezy-billing-portal-session';
|
||||
import { createLemonSqueezyCheckout } from './create-lemon-squeezy-checkout';
|
||||
@@ -24,66 +25,206 @@ export class LemonSqueezyBillingStrategyService
|
||||
async createCheckoutSession(
|
||||
params: z.infer<typeof CreateBillingCheckoutSchema>,
|
||||
) {
|
||||
const { data: response } = await createLemonSqueezyCheckout(params);
|
||||
Logger.info(
|
||||
{
|
||||
name: 'billing.lemon-squeezy',
|
||||
customerId: params.customerId,
|
||||
accountId: params.accountId,
|
||||
returnUrl: params.returnUrl,
|
||||
trialDays: params.trialDays,
|
||||
planId: params.plan.id,
|
||||
},
|
||||
'Creating checkout session...',
|
||||
);
|
||||
|
||||
const { data: response, error } = await createLemonSqueezyCheckout(params);
|
||||
|
||||
if (error ?? !response?.data.id) {
|
||||
Logger.error(
|
||||
{
|
||||
name: 'billing.lemon-squeezy',
|
||||
customerId: params.customerId,
|
||||
accountId: params.accountId,
|
||||
error: error?.message,
|
||||
},
|
||||
'Failed to create checkout session',
|
||||
);
|
||||
|
||||
if (!response?.data.id) {
|
||||
throw new Error('Failed to create checkout session');
|
||||
}
|
||||
|
||||
Logger.info(
|
||||
{
|
||||
name: 'billing.lemon-squeezy',
|
||||
customerId: params.customerId,
|
||||
accountId: params.accountId,
|
||||
},
|
||||
'Checkout session created successfully',
|
||||
);
|
||||
|
||||
return { checkoutToken: response.data.id };
|
||||
}
|
||||
|
||||
async createBillingPortalSession(
|
||||
params: z.infer<typeof CreateBillingPortalSessionSchema>,
|
||||
) {
|
||||
const url = await createLemonSqueezyBillingPortalSession(params);
|
||||
Logger.info(
|
||||
{
|
||||
name: 'billing.lemon-squeezy',
|
||||
customerId: params.customerId,
|
||||
},
|
||||
'Creating billing portal session...',
|
||||
);
|
||||
|
||||
const { data, error } =
|
||||
await createLemonSqueezyBillingPortalSession(params);
|
||||
|
||||
if (error ?? !data) {
|
||||
Logger.error(
|
||||
{
|
||||
name: 'billing.lemon-squeezy',
|
||||
customerId: params.customerId,
|
||||
error: error?.message,
|
||||
},
|
||||
'Failed to create billing portal session',
|
||||
);
|
||||
|
||||
if (!url) {
|
||||
throw new Error('Failed to create billing portal session');
|
||||
}
|
||||
|
||||
return { url };
|
||||
Logger.info(
|
||||
{
|
||||
name: 'billing.lemon-squeezy',
|
||||
customerId: params.customerId,
|
||||
},
|
||||
'Billing portal session created successfully',
|
||||
);
|
||||
|
||||
return { url: data };
|
||||
}
|
||||
|
||||
async cancelSubscription(
|
||||
params: z.infer<typeof CancelSubscriptionParamsSchema>,
|
||||
) {
|
||||
await cancelSubscription(params.subscriptionId);
|
||||
Logger.info(
|
||||
{
|
||||
name: 'billing.lemon-squeezy',
|
||||
subscriptionId: params.subscriptionId,
|
||||
},
|
||||
'Cancelling subscription...',
|
||||
);
|
||||
|
||||
return { success: true };
|
||||
try {
|
||||
const { error } = await cancelSubscription(params.subscriptionId);
|
||||
|
||||
if (error) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
Logger.info(
|
||||
{
|
||||
name: 'billing.lemon-squeezy',
|
||||
subscriptionId: params.subscriptionId,
|
||||
},
|
||||
'Subscription cancelled successfully',
|
||||
);
|
||||
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
Logger.error(
|
||||
{
|
||||
name: 'billing.lemon-squeezy',
|
||||
subscriptionId: params.subscriptionId,
|
||||
error: (error as Error)?.message,
|
||||
},
|
||||
'Failed to cancel subscription',
|
||||
);
|
||||
|
||||
throw new Error('Failed to cancel subscription');
|
||||
}
|
||||
}
|
||||
|
||||
async retrieveCheckoutSession(
|
||||
params: z.infer<typeof RetrieveCheckoutSessionSchema>,
|
||||
) {
|
||||
const session = await getCheckout(params.sessionId);
|
||||
Logger.info(
|
||||
{
|
||||
name: 'billing.lemon-squeezy',
|
||||
sessionId: params.sessionId,
|
||||
},
|
||||
'Retrieving checkout session...',
|
||||
);
|
||||
|
||||
const { data: session, error } = await getCheckout(params.sessionId);
|
||||
|
||||
if (error ?? !session?.data) {
|
||||
Logger.error(
|
||||
{
|
||||
name: 'billing.lemon-squeezy',
|
||||
sessionId: params.sessionId,
|
||||
error: error?.message,
|
||||
},
|
||||
'Failed to retrieve checkout session',
|
||||
);
|
||||
|
||||
if (!session.data) {
|
||||
throw new Error('Failed to retrieve checkout session');
|
||||
}
|
||||
|
||||
const data = session.data.data;
|
||||
Logger.info(
|
||||
{
|
||||
name: 'billing.lemon-squeezy',
|
||||
sessionId: params.sessionId,
|
||||
},
|
||||
'Checkout session retrieved successfully',
|
||||
);
|
||||
|
||||
const { id, attributes } = session.data;
|
||||
|
||||
return {
|
||||
checkoutToken: data.id,
|
||||
checkoutToken: id,
|
||||
isSessionOpen: false,
|
||||
status: 'complete' as const,
|
||||
customer: {
|
||||
email: data.attributes.checkout_data.email,
|
||||
email: attributes.checkout_data.email,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async reportUsage(params: z.infer<typeof ReportBillingUsageSchema>) {
|
||||
Logger.info(
|
||||
{
|
||||
name: 'billing.lemon-squeezy',
|
||||
subscriptionItemId: params.subscriptionId,
|
||||
},
|
||||
'Reporting usage...',
|
||||
);
|
||||
|
||||
const { error } = await createUsageRecord({
|
||||
quantity: params.usage.quantity,
|
||||
subscriptionItemId: params.subscriptionId,
|
||||
});
|
||||
|
||||
if (error) {
|
||||
Logger.error(
|
||||
{
|
||||
name: 'billing.lemon-squeezy',
|
||||
subscriptionItemId: params.subscriptionId,
|
||||
error: error.message,
|
||||
},
|
||||
'Failed to report usage',
|
||||
);
|
||||
|
||||
throw new Error('Failed to report usage');
|
||||
}
|
||||
|
||||
Logger.info(
|
||||
{
|
||||
name: 'billing.lemon-squeezy',
|
||||
subscriptionItemId: params.subscriptionId,
|
||||
},
|
||||
'Usage reported successfully',
|
||||
);
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ export async function initializeLemonSqueezyClient() {
|
||||
name: `billing.lemon-squeezy`,
|
||||
error: error.message,
|
||||
},
|
||||
'Error in Lemon Squeezy SDK',
|
||||
'Encountered an error using the Lemon Squeezy SDK',
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user