Files
myeasycms-v2/apps/web/app/api/billing/webhook/route.ts
giancarlo dbce7e38ae Refactor route handlers and CMS clients
Refactored the route handlers to use a new `enhanceRouteHandler` function for better control over request handlers and user authentication. CMS clients are now created using factory functions for better encapsulation and control over instance creation. Renamed `client.ts` in 'keystatic' to `keystatic-client.ts`.
2024-05-02 12:37:58 +07:00

50 lines
1.3 KiB
TypeScript

import { getBillingEventHandlerService } from '@kit/billing-gateway';
import { enhanceRouteHandler } from '@kit/next/routes';
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 const POST = enhanceRouteHandler(
async ({ request }) => {
const provider = billingConfig.provider;
const logger = await getLogger();
const ctx = {
name: 'billing.webhook',
provider,
};
logger.info(ctx, `Received billing webhook. Processing...`);
const supabaseClientProvider = () =>
getSupabaseRouteHandlerClient({ admin: true });
const service = await getBillingEventHandlerService(
supabaseClientProvider,
provider,
billingConfig,
);
try {
await service.handleWebhookEvent(request);
logger.info(ctx, `Successfully processed billing webhook`);
return new Response('OK', { status: 200 });
} catch (error) {
logger.error(ctx, `Failed to process billing webhook`, error);
return new Response('Failed to process billing webhook', {
status: 500,
});
}
},
{
auth: false,
},
);