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

56 lines
1.3 KiB
TypeScript

import { getBillingEventHandlerService } from '@kit/billing-gateway';
import { getLogger } 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;
const logger = await getLogger();
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 (error) {
console.error(error);
logger.error(
{
name: 'billing',
error: JSON.stringify(error),
},
`Failed to process billing webhook`,
);
return new Response('Error', { status: 500 });
}
}