From 4359517e75d22d7d1635e38b9fcc5fe64f201291 Mon Sep 17 00:00:00 2001 From: Matt Hanley <3798302+matthanley@users.noreply.github.com> Date: Thu, 15 Oct 2020 11:55:36 +0100 Subject: [PATCH] Added Stripe webhook listener for subscription created event (#208) no-issue Subscription created events are required for migrating Stripe subscriptions from alternative platforms, which involves creating a new subscription for a customer (outside of Ghost) before cancelling the original subscription. --- ghost/members-api/index.js | 4 ++++ ghost/members-api/lib/stripe/index.js | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/ghost/members-api/index.js b/ghost/members-api/index.js index 52b25d69b4..40217192e9 100644 --- a/ghost/members-api/index.js +++ b/ghost/members-api/index.js @@ -346,6 +346,10 @@ module.exports = function MembersApi({ await stripe.handleCustomerSubscriptionUpdatedWebhook(event.data.object); } + if (event.type === 'customer.subscription.created') { + await stripe.handleCustomerSubscriptionCreatedWebhook(event.data.object); + } + if (event.type === 'invoice.payment_succeeded') { await stripe.handleInvoicePaymentSucceededWebhook(event.data.object); } diff --git a/ghost/members-api/lib/stripe/index.js b/ghost/members-api/lib/stripe/index.js index 97930deaa1..7f45e57798 100644 --- a/ghost/members-api/lib/stripe/index.js +++ b/ghost/members-api/lib/stripe/index.js @@ -70,6 +70,7 @@ module.exports = class StripePaymentProcessor { 'checkout.session.completed', 'customer.subscription.deleted', 'customer.subscription.updated', + 'customer.subscription.created', 'invoice.payment_succeeded', 'invoice.payment_failed' ] @@ -386,6 +387,10 @@ module.exports = class StripePaymentProcessor { await this._updateSubscription(subscription); } + async handleCustomerSubscriptionCreatedWebhook(subscription) { + await this._updateSubscription(subscription); + } + async handleInvoicePaymentSucceededWebhook(invoice) { const subscription = await retrieve(this._stripe, 'subscriptions', invoice.subscription, { expand: ['default_payment_method']