From 67d21041905244cb6df9e33e3e11462f642f91b6 Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Tue, 7 Sep 2021 17:18:23 +0200 Subject: [PATCH] Deleted webhooks when disconnecting from Stripe refs https://github.com/TryGhost/Team/issues/1006 When disconnecting from Stripe, we currently do not remove the webhooks, this will result in the webhooks from Stripe failing, and tending toward a 100% error rate, which will ultimately result in emails from Stripe about the failing webhook. In order to stop all of that from happening, we should make sure that we actively remove the webhook from Stripe when disconnecting. --- ghost/members-api/lib/MembersAPI.js | 4 ++++ ghost/members-api/lib/services/stripe-webhook.js | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/ghost/members-api/lib/MembersAPI.js b/ghost/members-api/lib/MembersAPI.js index 0270503663..093e363d61 100644 --- a/ghost/members-api/lib/MembersAPI.js +++ b/ghost/members-api/lib/MembersAPI.js @@ -169,6 +169,10 @@ module.exports = function MembersAPI({ }); async function disconnectStripe() { + if (stripeConfig && stripeConfig.webhook && stripeConfig.webhook.id) { + await stripeWebhookService.removeWebhook(stripeConfig.webhook.id); + } + await Product.forge().query().update({ monthly_price_id: null, yearly_price_id: null diff --git a/ghost/members-api/lib/services/stripe-webhook.js b/ghost/members-api/lib/services/stripe-webhook.js index 7483fa418e..3224c2f6af 100644 --- a/ghost/members-api/lib/services/stripe-webhook.js +++ b/ghost/members-api/lib/services/stripe-webhook.js @@ -100,6 +100,20 @@ module.exports = class StripeWebhookService { this._webhookSecret = webhook.secret; } + /** + * @param {string} id - WebhookEndpoint Stripe ID + * + * @returns {Promise} + */ + async removeWebhook(id) { + try { + await this._stripeAPIService.deleteWebhookEndpoint(id); + return true; + } catch (err) { + return false; + } + } + /** * @param {string} body * @param {string} signature