Files
myeasycms-v2/apps/web/app/api/billing/webhook/route.ts
giancarlo 220a23e185 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.
2024-04-04 20:15:12 +08:00

53 lines
1.2 KiB
TypeScript

import { getBillingEventHandlerService } from '@kit/billing-gateway';
import { Logger } from '@kit/shared/logger';
import { getSupabaseRouteHandlerClient } from '@kit/supabase/route-handler-client';
import billingConfig from '~/config/billing.config';
/**
* @description Handle the webhooks from Stripe related to checkouts
*/
export async function POST(request: Request) {
const provider = billingConfig.provider;
Logger.info(
{
name: 'billing.webhook',
provider,
},
`Received billing webhook. Processing...`,
);
const supabaseClientProvider = () =>
getSupabaseRouteHandlerClient({ admin: true });
const service = await getBillingEventHandlerService(
supabaseClientProvider,
provider,
billingConfig,
);
try {
await service.handleWebhookEvent(request);
Logger.info(
{
name: 'billing.webhook',
},
`Successfully processed billing webhook`,
);
return new Response('OK', { status: 200 });
} catch (e) {
Logger.error(
{
name: 'billing',
error: e,
},
`Failed to process billing webhook`,
);
return new Response('Error', { status: 500 });
}
}