-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds Zod validation for webhook payloads #377
base: main
Are you sure you want to change the base?
Changes from 1 commit
a8f52f2
20ee20a
ad41d60
b7de406
ef496af
a969544
77ad7a4
e40c1e4
40afc5e
e628733
98047b7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,14 @@ export async function parseWebhookPayload(rawStripeEvent: Stripe.Event) { | |
throw new Error('Error parsing Stripe event object'); | ||
}); | ||
return { eventName: event.type, data: invoice }; | ||
case 'payment_intent.succeeded': | ||
const paymentIntent = await paymentIntentSucceededDataSchema | ||
.parseAsync(event.data.object) | ||
.catch((e) => { | ||
console.error(e); | ||
throw new Error('Error parsing Stripe event object'); | ||
}); | ||
return { eventName: event.type, data: paymentIntent }; | ||
case 'customer.subscription.updated': | ||
const updatedSubscription = await subscriptionUpdatedDataSchema | ||
.parseAsync(event.data.object) | ||
|
@@ -61,6 +69,16 @@ const invoicePaidDataSchema = z.object({ | |
period_start: z.number(), | ||
}); | ||
|
||
// This is a subtype of Stripe.PaymentIntent from "stripe" | ||
const paymentIntentSucceededDataSchema = z.object({ | ||
invoice: z.unknown().optional(), | ||
created: z.number(), | ||
metadata: z.object({ | ||
priceId: z.string(), | ||
}), | ||
customer: z.string(), | ||
}); | ||
|
||
// This is a subtype of Stripe.Subscription from "stripe" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Applies to all these comments. I think there's some special syntax you can use when referencing other symbols to enable users to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Screen.Recording.2025-02-28.at.13.16.56.movGreat idea! |
||
const subscriptionUpdatedDataSchema = z.object({ | ||
customer: z.string(), | ||
|
@@ -86,6 +104,8 @@ export type SessionCompletedData = z.infer<typeof sessionCompletedDataSchema>; | |
|
||
export type InvoicePaidData = z.infer<typeof invoicePaidDataSchema>; | ||
|
||
export type PaymentIntentSucceededData = z.infer<typeof paymentIntentSucceededDataSchema>; | ||
|
||
export type SubscriptionUpdatedData = z.infer<typeof subscriptionUpdatedDataSchema>; | ||
|
||
export type SubscriptionDeletedData = z.infer<typeof subscriptionDeletedDataSchema>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we separate and import types at the type of the file, as we've been doing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean import the types at the top of the file? I don't see that we did that in this file e.g.
is on top.
I've added the
import type
bit for the types.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah true, this won't apply for zod types as they're runtime specific, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not following sorry :) What do mean exactly that won't apply to Zod types?