From a8ce553e21f945dff423402db02cd11fe99ffad6 Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Thu, 4 Apr 2024 14:36:01 -0400 Subject: [PATCH 1/4] Passed currency to stripe checkout setup session ref ENG-812 ref https://linear.app/tryghost/issue/ENG-812 When we enable other payments, some of them only work for a subset of currencies. We wanna pass the currency here so that Stripe knows which payment methods to allow. --- ghost/members-api/lib/controllers/RouterController.js | 10 +++++++++- ghost/stripe/lib/StripeAPI.js | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/ghost/members-api/lib/controllers/RouterController.js b/ghost/members-api/lib/controllers/RouterController.js index 8755a675fa0..2990d147a10 100644 --- a/ghost/members-api/lib/controllers/RouterController.js +++ b/ghost/members-api/lib/controllers/RouterController.js @@ -111,11 +111,18 @@ module.exports = class RouterController { return res.end('Bad Request.'); } + const subscriptions = await member.related('stripeSubscriptions').fetch(); + + const activeSubscription = subscriptions.models.find((sub) => { + return ['active', 'trialing', 'unpaid', 'past_due'].includes(sub.get('status')); + }); + + let currency = activeSubscription.get('plan_currency'); + let customer; if (!req.body.subscription_id) { customer = await this._stripeAPIService.getCustomerForMemberCheckoutSession(member); } else { - const subscriptions = await member.related('stripeSubscriptions').fetch(); const subscription = subscriptions.models.find((sub) => { return sub.get('subscription_id') === req.body.subscription_id; }); @@ -126,6 +133,7 @@ module.exports = class RouterController { }); return res.end(`Could not find subscription ${req.body.subscription_id}`); } + currency = subscription.get('plan_currency'); customer = await this._stripeAPIService.getCustomer(subscription.get('customer_id')); } diff --git a/ghost/stripe/lib/StripeAPI.js b/ghost/stripe/lib/StripeAPI.js index 576f885359d..b57d22f2c98 100644 --- a/ghost/stripe/lib/StripeAPI.js +++ b/ghost/stripe/lib/StripeAPI.js @@ -565,6 +565,7 @@ module.exports = class StripeAPI { await this._rateLimitBucket.throttle(); const session = await this._stripe.checkout.sessions.create({ mode: 'setup', + currency: options.currency, payment_method_types: this.PAYMENT_METHOD_TYPES, success_url: options.successUrl || this._config.checkoutSetupSessionSuccessUrl, cancel_url: options.cancelUrl || this._config.checkoutSetupSessionCancelUrl, From c45888615ba5a1043f6fb9e529af740521c99441 Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Wed, 24 Apr 2024 10:42:36 +0700 Subject: [PATCH 2/4] fixup! Passed currency to stripe checkout setup session --- ghost/members-api/lib/controllers/RouterController.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ghost/members-api/lib/controllers/RouterController.js b/ghost/members-api/lib/controllers/RouterController.js index 2990d147a10..bce94a98010 100644 --- a/ghost/members-api/lib/controllers/RouterController.js +++ b/ghost/members-api/lib/controllers/RouterController.js @@ -117,7 +117,7 @@ module.exports = class RouterController { return ['active', 'trialing', 'unpaid', 'past_due'].includes(sub.get('status')); }); - let currency = activeSubscription.get('plan_currency'); + let currency = activeSubscription?.get('plan_currency') || undefined; let customer; if (!req.body.subscription_id) { @@ -133,7 +133,7 @@ module.exports = class RouterController { }); return res.end(`Could not find subscription ${req.body.subscription_id}`); } - currency = subscription.get('plan_currency'); + currency = subscription.get('plan_currency') || undefined; customer = await this._stripeAPIService.getCustomer(subscription.get('customer_id')); } From ed50db31ce3725ab9d2d7e121845953a359c95a6 Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Thu, 9 May 2024 19:48:26 +0700 Subject: [PATCH 3/4] fixup! Passed currency to stripe checkout setup session --- ghost/stripe/lib/StripeAPI.js | 1 - 1 file changed, 1 deletion(-) diff --git a/ghost/stripe/lib/StripeAPI.js b/ghost/stripe/lib/StripeAPI.js index b57d22f2c98..576f885359d 100644 --- a/ghost/stripe/lib/StripeAPI.js +++ b/ghost/stripe/lib/StripeAPI.js @@ -565,7 +565,6 @@ module.exports = class StripeAPI { await this._rateLimitBucket.throttle(); const session = await this._stripe.checkout.sessions.create({ mode: 'setup', - currency: options.currency, payment_method_types: this.PAYMENT_METHOD_TYPES, success_url: options.successUrl || this._config.checkoutSetupSessionSuccessUrl, cancel_url: options.cancelUrl || this._config.checkoutSetupSessionCancelUrl, From c5f18b1f47f8d0c44b08e40aedfe131c3cffc4ae Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Thu, 9 May 2024 19:52:42 +0700 Subject: [PATCH 4/4] fixup! Passed currency to stripe checkout setup session --- ghost/members-api/lib/controllers/RouterController.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/ghost/members-api/lib/controllers/RouterController.js b/ghost/members-api/lib/controllers/RouterController.js index bce94a98010..2e8b4961afe 100644 --- a/ghost/members-api/lib/controllers/RouterController.js +++ b/ghost/members-api/lib/controllers/RouterController.js @@ -137,9 +137,6 @@ module.exports = class RouterController { customer = await this._stripeAPIService.getCustomer(subscription.get('customer_id')); } - const defaultTier = await this._tiersService.api.readDefaultTier(); - const currency = defaultTier?.currency?.toLowerCase() || 'usd'; - const session = await this._stripeAPIService.createCheckoutSetupSession(customer, { successUrl: req.body.successUrl, cancelUrl: req.body.cancelUrl,