From fac6c3d97e82208940b23035bf07170e9a0899d4 Mon Sep 17 00:00:00 2001 From: Rish Date: Thu, 30 Apr 2020 15:56:04 +0530 Subject: [PATCH] Added ability to prefill customer email for anonymous checkouts refs https://github.com/TryGhost/members.js/issues/10 - Allows passing an additional `customerEmail` value to our checkout creation API - This value is used to pass `customer_email` option to stripe's checkout session - https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-customer_email. The `customer_email` allows pre-filling the customer's email field in case of an anonymous checkout as customer doesn't exist already, and also ensures the stripe subscription is created with same email address as given by user during signup flow. --- ghost/members-api/index.js | 3 ++- ghost/members-api/lib/stripe/index.js | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/ghost/members-api/index.js b/ghost/members-api/index.js index 822b896209..583d284c64 100644 --- a/ghost/members-api/index.js +++ b/ghost/members-api/index.js @@ -217,7 +217,8 @@ module.exports = function MembersApi({ const sessionInfo = await stripe.createCheckoutSession(member, plan, { successUrl: req.body.successUrl, - cancelUrl: req.body.cancelUrl + cancelUrl: req.body.cancelUrl, + customerEmail: req.body.customerEmail }); res.writeHead(200, { diff --git a/ghost/members-api/lib/stripe/index.js b/ghost/members-api/lib/stripe/index.js index e198e46989..c89d243ac1 100644 --- a/ghost/members-api/lib/stripe/index.js +++ b/ghost/members-api/lib/stripe/index.js @@ -113,11 +113,13 @@ module.exports = class StripePaymentProcessor { customer = null; } const plan = this._plans.find(plan => plan.nickname === planName); + const customerEmail = (!customer && options.customerEmail) ? options.customerEmail : undefined; const session = await this._stripe.checkout.sessions.create({ payment_method_types: ['card'], success_url: options.successUrl || this._checkoutSuccessUrl, cancel_url: options.cancelUrl || this._checkoutCancelUrl, customer: customer ? customer.id : undefined, + customer_email: customerEmail, subscription_data: { items: [{ plan: plan.id