From 5a17327a93cc4a765e9dba2dc16a00e82e7eba7a Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Tue, 8 Oct 2019 18:14:59 +0700 Subject: [PATCH] Improved error logging for webhook handling no-issue --- ghost/members-api/index.js | 12 +++++++++--- ghost/members-api/lib/stripe/index.js | 4 +++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/ghost/members-api/index.js b/ghost/members-api/index.js index 5d4eef53d4..5fa330cbcb 100644 --- a/ghost/members-api/index.js +++ b/ghost/members-api/index.js @@ -192,9 +192,15 @@ module.exports = function MembersApi({ }); middleware.handleStripeWebhook.use(ensureStripe, body.raw({type: 'application/json'}), async function (req, res) { + let event; + try { + event = await stripe.parseWebhook(req.body, req.headers['stripe-signature']); + } catch (err) { + common.logging.error(err); + res.writeHead(401); + return res.end(); + } try { - const event = await stripe.parseWebhook(req.body, req.headers['stripe-signature']); - if (event.type === 'customer.subscription.deleted') { await stripe.handleCustomerSubscriptionDeletedWebhook(event.data.object); } @@ -226,7 +232,7 @@ module.exports = function MembersApi({ res.writeHead(200); res.end(); } catch (err) { - common.logging.error(err); + common.logging.error(`Error handling webhook ${event.type}`, err); res.writeHead(400); res.end(); } diff --git a/ghost/members-api/lib/stripe/index.js b/ghost/members-api/lib/stripe/index.js index ae0259b33e..4a762cac31 100644 --- a/ghost/members-api/lib/stripe/index.js +++ b/ghost/members-api/lib/stripe/index.js @@ -80,7 +80,9 @@ module.exports = class StripePaymentProcessor { } async parseWebhook(body, signature) { - return this._stripe.webhooks.constructEvent(body, signature, this._webhookSecret); + const event = await this._stripe.webhooks.constructEvent(body, signature, this._webhookSecret); + debug(`Parsed webhook event: ${event.type}`); + return event; } async createCheckoutSession(member, planName, options) {