0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-04-08 02:52:39 -05:00

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)
This commit is contained in:
Fabien O'Carroll 2019-10-02 10:36:54 +07:00
parent 48cb8d14da
commit 80f1155590

View file

@ -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,