Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 5 additions & 21 deletions ee/pages/api/integrations/stripepayment/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,25 +110,6 @@ type WebhookHandler = (event: Stripe.Event) => Promise<void>;

const webhookHandlers: Record<string, WebhookHandler | undefined> = {
"payment_intent.succeeded": handlePaymentSuccess,
"customer.subscription.deleted": async (event) => {
const object = event.data.object as Stripe.Subscription;

const customerId = typeof object.customer === "string" ? object.customer : object.customer.id;

const customer = (await stripe.customers.retrieve(customerId)) as Stripe.Customer;
if (typeof customer.email !== "string") {
throw new Error(`Couldn't find customer email for ${customerId}`);
}

await prisma.user.update({
where: {
email: customer.email,
},
data: {
plan: "FREE",
},
});
},
};

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
Expand All @@ -146,15 +127,18 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}
const requestBuffer = await buffer(req);
const payload = requestBuffer.toString();
// console.log("payload", payload);

const event = stripe.webhooks.constructEvent(payload, sig, process.env.STRIPE_WEBHOOK_SECRET);

const handler = webhookHandlers[event.type];
if (handler) {
await handler(event);
} else {
console.warn(`Unhandled Stripe Webhook event type ${event.type}`);
/** Not really an error, just letting Stripe know that the webhook was received but unhandled */
throw new HttpError({
statusCode: 202,
message: `Unhandled Stripe Webhook event type ${event.type}`,
});
}
} catch (_err) {
const err = getErrorFromUnknown(_err);
Expand Down