Skip to content

Commit

Permalink
Passed currency to stripe checkout setup session
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
allouis committed Apr 4, 2024
1 parent a262a64 commit 303bf7e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
13 changes: 11 additions & 2 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');

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,13 +133,15 @@ 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'));
}

const session = await this._stripeAPIService.createCheckoutSetupSession(customer, {
successUrl: req.body.successUrl,
cancelUrl: req.body.cancelUrl,
subscription_id: req.body.subscription_id
subscription_id: req.body.subscription_id,
currency
});
const publicKey = this._stripeAPIService.getPublicKey();
const sessionInfo = {
Expand Down
1 change: 1 addition & 0 deletions ghost/stripe/lib/StripeAPI.js
Expand Up @@ -561,6 +561,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,
Expand Down

0 comments on commit 303bf7e

Please sign in to comment.