From 56d984f05ffbfe740902919a89ac0a590c16f35d Mon Sep 17 00:00:00 2001 From: Fabien 'egg' O'Carroll Date: Thu, 9 May 2024 20:03:11 +0700 Subject: [PATCH] Used subscription currency for setup session (#19991) ref https://linear.app/tryghost/issue/ENG-812 ref https://github.com/TryGhost/Ghost/commit/5b694761bc We wanna use the currency of the subscription to avoid the edge-case where the subscription currency doesn't match the sites current tiers currency. --- .../members-api/lib/controllers/RouterController.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/ghost/members-api/lib/controllers/RouterController.js b/ghost/members-api/lib/controllers/RouterController.js index 8755a675fa..2e8b4961af 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') || undefined; + 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,12 +133,10 @@ module.exports = class RouterController { }); return res.end(`Could not find subscription ${req.body.subscription_id}`); } + currency = subscription.get('plan_currency') || undefined; 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,