From cd6e87774ad594ef725811af646d0b92f737711f Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Tue, 28 Sep 2021 13:36:30 +0200 Subject: [PATCH] Added 1-day version of Offers refs https://github.com/TryGhost/Team/issues/1090 This 1-day version of Offers allows us to test the full flow of the Offers feature without having to implement all of it. The focus here is that we can pass an Offer ID when creating a Stripe Checkout session and have it apply. Here we use hardcoded Stripe Coupons as we haven't yet got persistence implemented for Offers & their related Stripe Coupons --- ghost/members-api/lib/MembersAPI.js | 1 + ghost/members-api/lib/controllers/router.js | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/ghost/members-api/lib/MembersAPI.js b/ghost/members-api/lib/MembersAPI.js index cd570ce841..fa351086fd 100644 --- a/ghost/members-api/lib/MembersAPI.js +++ b/ghost/members-api/lib/MembersAPI.js @@ -169,6 +169,7 @@ module.exports = function MembersAPI({ stripeAPIService, tokenService, sendEmailWithMagicLink, + labsService, config: { checkoutSuccessUrl: stripeConfig.checkoutSuccessUrl, checkoutCancelUrl: stripeConfig.checkoutCancelUrl, diff --git a/ghost/members-api/lib/controllers/router.js b/ghost/members-api/lib/controllers/router.js index 10c8dc4c0e..e9b4bff91d 100644 --- a/ghost/members-api/lib/controllers/router.js +++ b/ghost/members-api/lib/controllers/router.js @@ -11,6 +11,7 @@ const _ = require('lodash'); * @param {any} deps.magicLinkService * @param {import('@tryghost/members-stripe-service')} deps.stripeAPIService * @param {any} deps.tokenService + * @param {{isSet(name: string): boolean}} deps.labsService * @param {any} deps.config * @param {any} deps.logging */ @@ -23,6 +24,7 @@ module.exports = class RouterController { stripeAPIService, tokenService, sendEmailWithMagicLink, + labsService, config, logging }) { @@ -33,6 +35,7 @@ module.exports = class RouterController { this._stripeAPIService = stripeAPIService; this._tokenService = tokenService; this._sendEmailWithMagicLink = sendEmailWithMagicLink; + this.labsService = labsService; this._config = config; this._logging = logging; } @@ -115,6 +118,7 @@ module.exports = class RouterController { async createCheckoutSession(req, res) { const ghostPriceId = req.body.priceId; const identity = req.body.identity; + const offerId = req.body.offerId; if (!ghostPriceId) { res.writeHead(400); @@ -147,9 +151,18 @@ 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, { + coupon, successUrl: req.body.successUrl || this._config.checkoutSuccessUrl, cancelUrl: req.body.cancelUrl || this._config.checkoutCancelUrl, customerEmail: req.body.customerEmail, @@ -194,6 +207,7 @@ module.exports = class RouterController { try { const session = await this._stripeAPIService.createCheckoutSession(priceId, stripeCustomer, { + coupon, successUrl: req.body.successUrl || this._config.checkoutSuccessUrl, cancelUrl: req.body.cancelUrl || this._config.checkoutCancelUrl, metadata: req.body.metadata