Files
website/server/routes/account/stripe/webhook.post.ts
2026-08-09 21:28:45 +02:00

41 lines
1.2 KiB
TypeScript

import { handleStripeEvent } from '~~/server/utils/handleStripeWebhook';
import type { Stripe } from 'stripe';
export default defineEventHandler(async (event): Promise<{ success: boolean; message?: string }> => {
const config = useRuntimeConfig(event);
const stripe = useStripe(event);
if (!stripe || !config.stripeWebhookSecret) {
throw new Error('Stripe not configured on this instance');
}
let webhookEvent: Stripe.Event;
try {
const signatureHeader = getHeader(event, 'stripe-signature');
if (!signatureHeader) {
throw new Error('No signature header on webhook event');
}
const rawBody = await readRawBody(event) ?? '';
if (!rawBody) {
throw new Error('No body on webhook event');
}
webhookEvent = stripe.webhooks.constructEvent(rawBody, signatureHeader, config.stripeWebhookSecret);
} catch (error) {
console.error(error);
setResponseStatus(event, 400);
return {
success: false,
message: 'Invalid webhook'
};
}
const notificationEmails: string[] = [];
if (config.stripeNotificationEmail) {
notificationEmails.push(config.stripeNotificationEmail);
}
await handleStripeEvent(event, stripe, webhookEvent, notificationEmails);
return {
success: true
};
});