mirror of
https://github.com/jcreek/SvelteKitSaasBoilerplate.git
synced 2026-07-13 19:13:48 +00:00
feat(#29): Add additional stripe webhook event handlers
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import type { RequestHandler } from '@sveltejs/kit';
|
import type { RequestHandler } from '@sveltejs/kit';
|
||||||
import stripe from 'stripe';
|
import stripe from 'stripe';
|
||||||
import { PUBLIC_STRIPE_SECRET_KEY } from '$env/static/public';
|
import { STRIPE_SECRET_KEY } from '$env/static/private';
|
||||||
import { PUBLIC_STRIPE_ENDPOINT_SECRET } from '$env/static/public';
|
import { STRIPE_ENDPOINT_SECRET } from '$env/static/private';
|
||||||
import {
|
import {
|
||||||
deletePriceRecord,
|
deletePriceRecord,
|
||||||
deleteProductRecord,
|
deleteProductRecord,
|
||||||
@@ -10,7 +10,7 @@ import {
|
|||||||
upsertProductRecord
|
upsertProductRecord
|
||||||
} from '$lib/utils/supabase/admin';
|
} from '$lib/utils/supabase/admin';
|
||||||
|
|
||||||
const stripeClient = new stripe(PUBLIC_STRIPE_SECRET_KEY);
|
const stripeClient = new stripe(STRIPE_SECRET_KEY);
|
||||||
|
|
||||||
export const POST: RequestHandler = async ({ request }) => {
|
export const POST: RequestHandler = async ({ request }) => {
|
||||||
const body = await request.text();
|
const body = await request.text();
|
||||||
@@ -23,13 +23,9 @@ export const POST: RequestHandler = async ({ request }) => {
|
|||||||
|
|
||||||
// Only verify the event if you have an endpoint secret defined.
|
// Only verify the event if you have an endpoint secret defined.
|
||||||
// Otherwise use the basic event deserialized with JSON.parse
|
// Otherwise use the basic event deserialized with JSON.parse
|
||||||
if (PUBLIC_STRIPE_ENDPOINT_SECRET && signature) {
|
if (STRIPE_ENDPOINT_SECRET && signature) {
|
||||||
try {
|
try {
|
||||||
event = stripeClient.webhooks.constructEvent(
|
event = stripeClient.webhooks.constructEvent(body, signature, STRIPE_ENDPOINT_SECRET);
|
||||||
body,
|
|
||||||
signature,
|
|
||||||
PUBLIC_STRIPE_ENDPOINT_SECRET
|
|
||||||
);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(`⚠️ Webhook signature verification failed.`, err.message);
|
console.error(`⚠️ Webhook signature verification failed.`, err.message);
|
||||||
return {
|
return {
|
||||||
@@ -44,6 +40,9 @@ export const POST: RequestHandler = async ({ request }) => {
|
|||||||
case 'product.updated':
|
case 'product.updated':
|
||||||
await upsertProductRecord(event.data.object as stripe.Product);
|
await upsertProductRecord(event.data.object as stripe.Product);
|
||||||
break;
|
break;
|
||||||
|
case 'product.deleted':
|
||||||
|
await deleteProductRecord(event.data.object as stripe.Product);
|
||||||
|
break;
|
||||||
case 'price.created':
|
case 'price.created':
|
||||||
case 'price.updated':
|
case 'price.updated':
|
||||||
await upsertPriceRecord(event.data.object as stripe.Price);
|
await upsertPriceRecord(event.data.object as stripe.Price);
|
||||||
@@ -51,12 +50,11 @@ export const POST: RequestHandler = async ({ request }) => {
|
|||||||
case 'price.deleted':
|
case 'price.deleted':
|
||||||
await deletePriceRecord(event.data.object as stripe.Price);
|
await deletePriceRecord(event.data.object as stripe.Price);
|
||||||
break;
|
break;
|
||||||
case 'product.deleted':
|
|
||||||
await deleteProductRecord(event.data.object as stripe.Product);
|
|
||||||
break;
|
|
||||||
case 'customer.subscription.created':
|
case 'customer.subscription.created':
|
||||||
case 'customer.subscription.updated':
|
case 'customer.subscription.updated':
|
||||||
case 'customer.subscription.deleted': {
|
case 'customer.subscription.deleted':
|
||||||
|
case 'customer.subscription.paused':
|
||||||
|
case 'customer.subscription.resumed': {
|
||||||
const subscription = event.data.object as stripe.Subscription;
|
const subscription = event.data.object as stripe.Subscription;
|
||||||
await manageSubscriptionStatusChange(
|
await manageSubscriptionStatusChange(
|
||||||
subscription.id,
|
subscription.id,
|
||||||
@@ -86,10 +84,7 @@ export const POST: RequestHandler = async ({ request }) => {
|
|||||||
status: 200
|
status: 200
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(`Webhook Error: ${err.message}`);
|
const message = err instanceof Error ? err.message : 'An unknown error has occurred';
|
||||||
return {
|
return new Response(`Webhook Error: ${message}`, { status: 500 });
|
||||||
status: 400,
|
|
||||||
body: `Webhook Error: ${err.message}`
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user