From 343fcecfff0f4d692082fbc6ab41288f5d8221ef Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Tue, 24 Sep 2019 17:20:59 +0700 Subject: [PATCH] Updated stripe to create webhook on boot configure no-issue This will allow us to a) have an endpoint to receive webhooks and b) get hold of the webhook secret to validate the signature. --- ghost/members-api/lib/stripe/index.js | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/ghost/members-api/lib/stripe/index.js b/ghost/members-api/lib/stripe/index.js index 1fe590c083..8908ddfa4a 100644 --- a/ghost/members-api/lib/stripe/index.js +++ b/ghost/members-api/lib/stripe/index.js @@ -19,21 +19,30 @@ module.exports = class StripePaymentProcessor { this._public_token = config.publicKey; this._checkoutSuccessUrl = config.checkoutSuccessUrl; this._checkoutCancelUrl = config.checkoutCancelUrl; + this._webhookHandlerUrl = config.webhookHandlerUrl; try { this._product = await api.products.ensure(this._stripe, config.product); - } catch (err) { - return this._rejectReady(err); - } - this._plans = []; - for (const planSpec of config.plans) { - try { + this._plans = []; + for (const planSpec of config.plans) { const plan = await api.plans.ensure(this._stripe, planSpec, this._product); this._plans.push(plan); - } catch (err) { - return this._rejectReady(err); } + + try { + // @TODO Need to somehow not duplicate this every time we boot + const webhook = await create(this._stripe, 'webhookEndpoints', { + url: this._webhookHandlerUrl, + enabled_events: ['checkout.session.completed'] + }); + this._webhookSecret = webhook.secret; + } catch (err) { + console.log(err); + this._webhookSecret = process.env.WEBHOOK_SECRET; + } + } catch (err) { + return this._rejectReady(err); } return this._resolveReady({