From a31d6fc689d1d7b472af45c0a083d15b38c7d3d2 Mon Sep 17 00:00:00 2001 From: vincanger <70215737+vincanger@users.noreply.github.com> Date: Thu, 13 Apr 2023 13:13:53 +0200 Subject: [PATCH] add stripe comments --- src/server/webhooks.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/server/webhooks.ts b/src/server/webhooks.ts index 9dac34b..ba8c107 100644 --- a/src/server/webhooks.ts +++ b/src/server/webhooks.ts @@ -85,7 +85,15 @@ export const stripeWebhook: StripeWebhook = async (request, response, context) = const subscription = event.data.object as Stripe.Subscription; userStripeId = subscription.customer as string; + /** + * Stripe will send a subscription.updated event when a subscription is canceled + * but the subscription is still active until the end of the period. + * So we check if cancel_at_period_end is true and send an email to the customer. + * https://stripe.com/docs/billing/subscriptions/cancel#events + */ if (subscription.cancel_at_period_end) { + console.log('Subscription canceled at period end'); + const customerEmail = await context.entities.User.findFirst({ where: { stripeId: userStripeId, @@ -108,7 +116,11 @@ export const stripeWebhook: StripeWebhook = async (request, response, context) = const subscription = event.data.object as Stripe.Subscription; userStripeId = subscription.customer as string; - console.log('Subscription canceled'); + /** + * Stripe will send then finally send a subscription.deleted event when subscription period ends + * https://stripe.com/docs/billing/subscriptions/cancel#events + */ + console.log('Subscription deleted/ended'); await context.entities.User.updateMany({ where: { stripeId: userStripeId,