From 3edf0cb471d3605d60e30c83f1ec4de403160b5b Mon Sep 17 00:00:00 2001 From: m5r Date: Sun, 24 Oct 2021 16:55:07 +0200 Subject: [PATCH] respond 200 to valid but unsupported paddle webhooks --- app/settings/api/webhook/subscription.ts | 29 ++++++++++++++++-------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/app/settings/api/webhook/subscription.ts b/app/settings/api/webhook/subscription.ts index 63cf1ca..221387f 100644 --- a/app/settings/api/webhook/subscription.ts +++ b/app/settings/api/webhook/subscription.ts @@ -6,7 +6,7 @@ import type { PaddleSdkSubscriptionPaymentSucceededEvent, PaddleSdkSubscriptionUpdatedEvent, } from "@devoxa/paddle-sdk"; -import { PaddleSdkWebhookEventType } from "@devoxa/paddle-sdk"; +import { PaddleSdkException, PaddleSdkWebhookEventType } from "@devoxa/paddle-sdk"; import type { ApiError } from "app/core/types"; import subscriptionCreatedQueue from "../queue/subscription-created"; @@ -47,17 +47,26 @@ const webhook: BlitzApiHandler = async (req, res) => { return; } - const event = paddleSdk.parseWebhookEvent(req.body); - const alertName = event.eventType; - logger.info(`Received ${alertName} webhook`); - logger.info(event); - if (isSupportedWebhook(alertName)) { - await queues[alertName].enqueue({ event: event as Events }, { id: event.eventId.toString() }); + try { + const event = paddleSdk.parseWebhookEvent(req.body); + const alertName = event.eventType; + logger.info(`Received ${alertName} webhook`); + logger.info(event); + if (isSupportedWebhook(alertName)) { + await queues[alertName].enqueue({ event: event as Events }, { id: event.eventId.toString() }); - return res.status(200).end(); + return res.status(200).end(); + } + + return res.status(400).end(); + } catch (error) { + if (error instanceof PaddleSdkException && error.message.includes(req.body.alert_name)) { + // event implementation is missing such as `subscription_payment_refunded` + return res.status(200).end(); + } + + throw error; } - - return res.status(400).end(); }; export default webhook;