diff --git a/template/app/src/payment/lemonSqueezy/webhook.ts b/template/app/src/payment/lemonSqueezy/webhook.ts index 99e4d0a..53779d4 100644 --- a/template/app/src/payment/lemonSqueezy/webhook.ts +++ b/template/app/src/payment/lemonSqueezy/webhook.ts @@ -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); diff --git a/template/app/src/payment/lemonSqueezy/webhookPayload.ts b/template/app/src/payment/lemonSqueezy/webhookPayload.ts index a3d7915..1b2cf24 100644 --- a/template/app/src/payment/lemonSqueezy/webhookPayload.ts +++ b/template/app/src/payment/lemonSqueezy/webhookPayload.ts @@ -34,6 +34,9 @@ export type SubscriptionData = z.infer; export type OrderData = z.infer; +/** + * 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(), diff --git a/template/app/src/payment/stripe/webhook.ts b/template/app/src/payment/stripe/webhook.ts index 6769d52..798bbaa 100644 --- a/template/app/src/payment/stripe/webhook.ts +++ b/template/app/src/payment/stripe/webhook.ts @@ -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'); }