From 1ff1b75a6989bab17fb30071724f29573478bfa3 Mon Sep 17 00:00:00 2001 From: Fabien 'egg' O'Carroll Date: Wed, 2 Nov 2022 02:18:30 +0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fixed=20errors=20with=20Stripe?= =?UTF-8?q?=20Checkout=20(#15749)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit refs https://github.com/TryGhost/Ghost/commit/1f300fb781f0 The full customer object was not being passed to the StripeAPI service when it already exists, this was resulting in inconsistent behaviour when sending the customerEmail param to the API, causing `invalid_email` errors to be thrown from Stripe and breaking the checkout. --- .../create-stripe-checkout-session.test.js | 2 +- ghost/payments/lib/payments.js | 15 +++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/ghost/core/test/e2e-api/members/create-stripe-checkout-session.test.js b/ghost/core/test/e2e-api/members/create-stripe-checkout-session.test.js index 0ffd5aaf4f..7fef078afb 100644 --- a/ghost/core/test/e2e-api/members/create-stripe-checkout-session.test.js +++ b/ghost/core/test/e2e-api/members/create-stripe-checkout-session.test.js @@ -165,7 +165,7 @@ describe('Create Stripe Checkout Session', function () { if (uri === '/v1/checkout/sessions') { const bodyJSON = querystring.parse(body); // TODO: Actually work out what Stripe checks and when/how it errors - if (bodyJSON.customerEmail) { + if (Reflect.has(bodyJSON, 'customerEmail')) { return [400, {error: 'Invalid Email'}]; } return [200, {id: 'cs_123', url: 'https://site.com'}]; diff --git a/ghost/payments/lib/payments.js b/ghost/payments/lib/payments.js index d3a7218894..e420ae4a7e 100644 --- a/ghost/payments/lib/payments.js +++ b/ghost/payments/lib/payments.js @@ -88,14 +88,19 @@ class PaymentsService { const email = options.email || null; - const session = await this.stripeAPIService.createCheckoutSession(price.id, customer, { + const data = { metadata, successUrl: options.successUrl, cancelUrl: options.cancelUrl, - customerEmail: customer ? email : null, trialDays: trialDays ?? tier.trialDays, coupon: coupon?.id - }); + }; + + if (!customer && email) { + data.customerEmail = email; + } + + const session = await this.stripeAPIService.createCheckoutSession(price.id, customer, data); return session.url; } @@ -108,9 +113,7 @@ class PaymentsService { for (const row of rows) { const customer = await this.stripeAPIService.getCustomer(row.customer_id); if (!customer.deleted) { - return { - id: customer.id - }; + return customer; } }