Implement updateSubscription feature and refactor billing services

This commit introduces the updateSubscription method to the BillingStrategyProviderService, ensuring that subscriptions can be updated within the billing core. Additionally, a refactor has been applied to the BillingGatewayFactoryService and stripe-billing-strategy.service to improve error handling and the robustness of subscription updates. Logging in the webhook route has been adjusted for clarity and the data model has been enhanced.
This commit is contained in:
giancarlo
2024-04-04 20:15:12 +08:00
parent 4a122ee5df
commit 220a23e185
26 changed files with 1499 additions and 993 deletions

View File

@@ -8,29 +8,31 @@ import billingConfig from '~/config/billing.config';
* @description Handle the webhooks from Stripe related to checkouts
*/
export async function POST(request: Request) {
// we can infer the provider from the billing config or the request
// for simplicity, we'll use the billing config for now
// TODO: use dynamic provider from request?
const provider = billingConfig.provider;
Logger.info(
{
name: 'billing',
name: 'billing.webhook',
provider,
},
`Received billing webhook. Processing...`,
);
const clientProvider = () => getSupabaseRouteHandlerClient({ admin: true });
const supabaseClientProvider = () =>
getSupabaseRouteHandlerClient({ admin: true });
const service = await getBillingEventHandlerService(clientProvider, provider);
const service = await getBillingEventHandlerService(
supabaseClientProvider,
provider,
billingConfig,
);
try {
await service.handleWebhookEvent(request);
Logger.info(
{
name: 'billing',
name: 'billing.webhook',
},
`Successfully processed billing webhook`,
);

View File

@@ -11,10 +11,21 @@ const webhooksSecret = z
const service = new DatabaseWebhookHandlerService();
export async function POST(request: Request) {
await service.handleWebhook(request, webhooksSecret);
const response = (status: number) => new Response(null, { status });
return new Response(null, {
status: 200,
});
/**
* @name POST
* @description POST handler for the webhook route that handles the webhook event
* @param request
* @constructor
*/
export async function POST(request: Request) {
try {
// handle the webhook event
await service.handleWebhook(request, webhooksSecret);
return response(200);
} catch {
return response(500);
}
}