B2B-88: add starter kit structure and elements
This commit is contained in:
49
app/api/billing/webhook/route.ts
Normal file
49
app/api/billing/webhook/route.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { getPlanTypesMap } from '@kit/billing';
|
||||
import { getBillingEventHandlerService } from '@kit/billing-gateway';
|
||||
import { enhanceRouteHandler } from '@kit/next/routes';
|
||||
import { getLogger } from '@kit/shared/logger';
|
||||
import { getSupabaseServerAdminClient } from '@kit/supabase/server-admin-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 = () => getSupabaseServerAdminClient();
|
||||
|
||||
const service = await getBillingEventHandlerService(
|
||||
supabaseClientProvider,
|
||||
provider,
|
||||
getPlanTypesMap(billingConfig),
|
||||
);
|
||||
|
||||
try {
|
||||
await service.handleWebhookEvent(request);
|
||||
|
||||
logger.info(ctx, `Successfully processed billing webhook`);
|
||||
|
||||
return new Response('OK', { status: 200 });
|
||||
} catch (error) {
|
||||
logger.error({ ...ctx, error }, `Failed to process billing webhook`);
|
||||
|
||||
return new Response('Failed to process billing webhook', {
|
||||
status: 500,
|
||||
});
|
||||
}
|
||||
},
|
||||
{
|
||||
auth: false,
|
||||
},
|
||||
);
|
||||
43
app/api/db/webhook/route.ts
Normal file
43
app/api/db/webhook/route.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { getDatabaseWebhookHandlerService } from '@kit/database-webhooks';
|
||||
import { getServerMonitoringService } from '@kit/monitoring/server';
|
||||
import { enhanceRouteHandler } from '@kit/next/routes';
|
||||
|
||||
/**
|
||||
* @name POST
|
||||
* @description POST handler for the webhook route that handles the webhook event
|
||||
*/
|
||||
export const POST = enhanceRouteHandler(
|
||||
async ({ request }) => {
|
||||
const service = getDatabaseWebhookHandlerService();
|
||||
|
||||
try {
|
||||
const signature = request.headers.get('X-Supabase-Event-Signature');
|
||||
|
||||
if (!signature) {
|
||||
return new Response('Missing signature', { status: 400 });
|
||||
}
|
||||
|
||||
const body = await request.clone().json();
|
||||
|
||||
// handle the webhook event
|
||||
await service.handleWebhook({
|
||||
body,
|
||||
signature,
|
||||
});
|
||||
|
||||
// return a successful response
|
||||
return new Response(null, { status: 200 });
|
||||
} catch (error) {
|
||||
const service = await getServerMonitoringService();
|
||||
|
||||
await service.ready();
|
||||
await service.captureException(error as Error);
|
||||
|
||||
// return an error response
|
||||
return new Response(null, { status: 500 });
|
||||
}
|
||||
},
|
||||
{
|
||||
auth: false,
|
||||
},
|
||||
);
|
||||
Reference in New Issue
Block a user