PR comments

This commit is contained in:
Mihovil Ilakovac 2025-04-16 12:42:17 +02:00
parent a537d00809
commit 85ff1af4e0
3 changed files with 18 additions and 6 deletions

View File

@ -43,7 +43,7 @@ export const lemonSqueezyWebhook: PaymentsWebhook = async (request, response, co
return response.status(200).json({ received: true });
} catch (err) {
if (err instanceof UnhandledWebhookEventError) {
return response.status(200).json({ received: true });
return response.status(422).json({ error: err.message });
}
console.error('Webhook error:', err);

View File

@ -34,6 +34,9 @@ export type SubscriptionData = z.infer<typeof subscriptionDataSchema>;
export type OrderData = z.infer<typeof orderDataSchema>;
/**
* This schema is based on LemonSqueezyResponse type
*/
const genericEventSchema = z.object({
meta: z.object({
event_name: z.string(),
@ -44,8 +47,11 @@ const genericEventSchema = z.object({
data: z.unknown(),
});
// This is a subtype of Order type from "@lemonsqueezy/lemonsqueezy.js"
// specifically Order['data']
/**
* This schema is based on
* @type import('@lemonsqueezy/lemonsqueezy.js').Order
* specifically Order['data'].
*/
const orderDataSchema = z.object({
attributes: z.object({
customer_id: z.number(),
@ -57,8 +63,11 @@ const orderDataSchema = z.object({
}),
});
// This is a subtype of Subscription type from "@lemonsqueezy/lemonsqueezy.js"
// specifically Subscription['data']
/**
* This schema is based on
* @type import('@lemonsqueezy/lemonsqueezy.js').Subscription
* specifically Subscription['data'].
*/
const subscriptionDataSchema = z.object({
attributes: z.object({
customer_id: z.number(),

View File

@ -51,7 +51,7 @@ export const stripeWebhook: PaymentsWebhook = async (request, response, context)
return response.json({ received: true }); // Stripe expects a 200 response to acknowledge receipt of the webhook
} catch (err) {
if (err instanceof UnhandledWebhookEventError) {
return response.status(200).json({ received: true });
return response.status(422).json({ error: err.message });
}
console.error('Webhook error:', err);
@ -211,6 +211,9 @@ const subscriptionItemsSchema = z.object({
});
function extractPriceId(items: SubscsriptionItems): string {
if (items.data.length === 0) {
throw new HttpError(400, 'No items in stripe event object');
}
if (items.data.length > 1) {
throw new HttpError(400, 'More than one item in stripe event object');
}