From 11e2732d5003574d3be38d1cf4871aab97076013 Mon Sep 17 00:00:00 2001 From: Rish Date: Wed, 10 Jun 2020 16:29:27 +0530 Subject: [PATCH] Handled error for stripe checkout rejection refs https://github.com/TryGhost/members.js/issues/38 - In case of incomplete Stripe setup like Account name, checkout session creation fails and throws error, which was not being handled and 200 returned after long timeout - This change catches the error and returns correct status along with message for clients to handle it downstream --- ghost/members-api/index.js | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/ghost/members-api/index.js b/ghost/members-api/index.js index 7167b1d080..ae050b670b 100644 --- a/ghost/members-api/index.js +++ b/ghost/members-api/index.js @@ -237,18 +237,24 @@ module.exports = function MembersApi({ return res.end('No permission'); } - const sessionInfo = await stripe.createCheckoutSession(member, plan, { - successUrl: req.body.successUrl, - cancelUrl: req.body.cancelUrl, - customerEmail: req.body.customerEmail, - metadata: req.body.metadata - }); + try { + const sessionInfo = await stripe.createCheckoutSession(member, plan, { + successUrl: req.body.successUrl, + cancelUrl: req.body.cancelUrl, + customerEmail: req.body.customerEmail, + metadata: req.body.metadata + }); - res.writeHead(200, { - 'Content-Type': 'application/json' - }); + res.writeHead(200, { + 'Content-Type': 'application/json' + }); - res.end(JSON.stringify(sessionInfo)); + res.end(JSON.stringify(sessionInfo)); + } catch (e) { + const error = e.message || 'Unable to initiate checkout session'; + res.writeHead(400); + return res.end(error); + } }); middleware.createCheckoutSetupSession.use(ensureStripe, body.json(), async function (req, res) {