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

View file

@ -17,6 +17,8 @@ const _ = require('lodash');
*/ */
module.exports = class RouterController { module.exports = class RouterController {
constructor({ constructor({
offerRepository,
productRepository,
memberRepository, memberRepository,
StripePrice, StripePrice,
allowSelfSignup, allowSelfSignup,
@ -28,6 +30,8 @@ module.exports = class RouterController {
config, config,
logging logging
}) { }) {
this._offerRepository = offerRepository;
this._productRepository = productRepository;
this._memberRepository = memberRepository; this._memberRepository = memberRepository;
this._StripePrice = StripePrice; this._StripePrice = StripePrice;
this._allowSelfSignup = allowSelfSignup; this._allowSelfSignup = allowSelfSignup;
@ -116,15 +120,41 @@ module.exports = class RouterController {
} }
async createCheckoutSession(req, res) { async createCheckoutSession(req, res) {
const ghostPriceId = req.body.priceId; let ghostPriceId = req.body.priceId;
const identity = req.body.identity; const identity = req.body.identity;
const offerId = req.body.offerId; const offerId = req.body.offerId;
if (!ghostPriceId) { if (!ghostPriceId && !offerId) {
res.writeHead(400); res.writeHead(400);
return res.end('Bad Request.'); 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({ const price = await this._StripePrice.findOne({
id: ghostPriceId id: ghostPriceId
}); });
@ -151,14 +181,6 @@ module.exports = class RouterController {
const member = email ? await this._memberRepository.get({email}, {withRelated: ['stripeCustomers', 'products']}) : null; 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) { if (!member) {
const customer = null; const customer = null;
const session = await this._stripeAPIService.createCheckoutSession(priceId, customer, { const session = await this._stripeAPIService.createCheckoutSession(priceId, customer, {