Skip to content

Commit

Permalink
Used subscription currency for setup session (#19991)
Browse files Browse the repository at this point in the history
ref https://linear.app/tryghost/issue/ENG-812
ref 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.
  • Loading branch information
allouis committed May 9, 2024
1 parent 50a1ef1 commit 56d984f
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions ghost/members-api/lib/controllers/RouterController.js
Expand Up @@ -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;
});
Expand All @@ -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,
Expand Down

0 comments on commit 56d984f

Please sign in to comment.