0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00

Applied Offers when creating Stripe Checkout Session

refs https://github.com/TryGhost/Team/issues/1090

Instead of the hardcoded 1-day version for Offers, we can now talk
directly to the Offers repository and use the real values for Stripe
Checkout.
This commit is contained in:
Fabien O'Carroll 2021-10-06 15:01:04 +02:00
parent d57d082c39
commit f0141f08ff
2 changed files with 35 additions and 10 deletions

View file

@ -55,6 +55,7 @@ module.exports = function MembersAPI({
Settings
},
stripeAPIService,
offerRepository,
logger,
labsService
}) {
@ -152,6 +153,8 @@ module.exports = function MembersAPI({
});
const routerController = new RouterController({
offerRepository,
productRepository,
memberRepository,
StripePrice,
allowSelfSignup,

View file

@ -17,6 +17,8 @@ const _ = require('lodash');
*/
module.exports = class RouterController {
constructor({
offerRepository,
productRepository,
memberRepository,
StripePrice,
allowSelfSignup,
@ -28,6 +30,8 @@ module.exports = class RouterController {
config,
logging
}) {
this._offerRepository = offerRepository;
this._productRepository = productRepository;
this._memberRepository = memberRepository;
this._StripePrice = StripePrice;
this._allowSelfSignup = allowSelfSignup;
@ -116,15 +120,41 @@ module.exports = class RouterController {
}
async createCheckoutSession(req, res) {
const ghostPriceId = req.body.priceId;
let ghostPriceId = req.body.priceId;
const identity = req.body.identity;
const offerId = req.body.offerId;
if (!ghostPriceId) {
if (!ghostPriceId && !offerId) {
res.writeHead(400);
return res.end('Bad Request.');
}
if (offerId && ghostPriceId) {
res.writeHead(400);
return res.end('Bad Request.');
}
let coupon = null;
if (offerId && this.labsService.isSet('offers')) {
try {
const offer = await this._offerRepository.getById(offerId);
const tier = (await this._productRepository.get(offer.tier)).toJSON();
if (offer.cadence === 'month') {
ghostPriceId = tier.monthly_price_id;
} else {
ghostPriceId = tier.yearly_price_id;
}
coupon = {
id: offer.stripeCouponId
};
} catch (err) {
res.writeHead(500);
return res.end('Could not use Offer.');
}
}
const price = await this._StripePrice.findOne({
id: ghostPriceId
});
@ -151,14 +181,6 @@ module.exports = class RouterController {
const member = email ? await this._memberRepository.get({email}, {withRelated: ['stripeCustomers', 'products']}) : null;
let coupon = null;
if (offerId && this.labsService.isSet('offers')) {
coupon = await this._stripeAPIService.createCoupon({
duration: 'forever',
percent_off: 50
});
}
if (!member) {
const customer = null;
const session = await this._stripeAPIService.createCheckoutSession(priceId, customer, {