From 80f1155590331a1d48dc0341d4bef56823070244 Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Wed, 2 Oct 2019 10:36:54 +0700 Subject: [PATCH] Ensured we do not create multiple webhooks on boot no-issue This updates the initialisation logic to fetch all webhooks (we use limit: 100, and there are currently a max of 16 webhooks in stripe) and find one with the corrct url. Once found, delete that webhook. We then attempt to create a new one, and log out any errors (this is to allow for local development, creating a webhook with a local url is expected to fail) --- ghost/members-api/lib/stripe/index.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/ghost/members-api/lib/stripe/index.js b/ghost/members-api/lib/stripe/index.js index 4cf7a1bf21..244329a036 100644 --- a/ghost/members-api/lib/stripe/index.js +++ b/ghost/members-api/lib/stripe/index.js @@ -1,4 +1,5 @@ -const {retrieve, create} = require('./api/stripeRequests'); +const debug = require('ghost-ignition').debug('stripe'); +const {retrieve, list, create, del} = require('./api/stripeRequests'); const api = require('./api'); const STRIPE_API_VERSION = '2019-09-09'; @@ -36,8 +37,19 @@ module.exports = class StripePaymentProcessor { this._plans.push(plan); } + const webhooks = await list(this._stripe, 'webhookEndpoints', { + limit: 100 + }); + + const webhookToDelete = webhooks.data.find((webhook) => { + return webhook.url === this._webhookHandlerUrl; + }); + + if (webhookToDelete) { + await del(this._stripe, 'webhookEndpoints', webhookToDelete.id); + } + try { - // @TODO Need to somehow not duplicate this every time we boot const webhook = await create(this._stripe, 'webhookEndpoints', { url: this._webhookHandlerUrl, api_version: STRIPE_API_VERSION,