mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-10 23:36:14 -05:00
Wired up payments service
refs https://github.com/TryGhost/Team/issues/1166 By using the PaymentsService to fetch coupon information - we ensure that the coupons are created if they're missing. Like in the case of a Stripe disconnect/connect cycle.
This commit is contained in:
parent
5db41169aa
commit
efe5164eff
3 changed files with 33 additions and 20 deletions
|
@ -5,6 +5,7 @@ const common = require('./common');
|
||||||
|
|
||||||
const MemberAnalyticsService = require('@tryghost/member-analytics-service');
|
const MemberAnalyticsService = require('@tryghost/member-analytics-service');
|
||||||
const MembersAnalyticsIngress = require('@tryghost/members-analytics-ingress');
|
const MembersAnalyticsIngress = require('@tryghost/members-analytics-ingress');
|
||||||
|
const PaymentsService = require('@tryghost/members-payments');
|
||||||
|
|
||||||
const StripeWebhookService = require('./services/stripe-webhook');
|
const StripeWebhookService = require('./services/stripe-webhook');
|
||||||
const TokenService = require('./services/token');
|
const TokenService = require('./services/token');
|
||||||
|
@ -155,8 +156,15 @@ module.exports = function MembersAPI({
|
||||||
allowSelfSignup
|
allowSelfSignup
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const paymentsService = new PaymentsService({
|
||||||
|
Offer,
|
||||||
|
offersAPI,
|
||||||
|
stripeAPIService
|
||||||
|
});
|
||||||
|
|
||||||
const routerController = new RouterController({
|
const routerController = new RouterController({
|
||||||
offersAPI,
|
offersAPI,
|
||||||
|
paymentsService,
|
||||||
productRepository,
|
productRepository,
|
||||||
memberRepository,
|
memberRepository,
|
||||||
StripePrice,
|
StripePrice,
|
||||||
|
|
|
@ -1,23 +1,27 @@
|
||||||
const common = require('../../lib/common');
|
const common = require('../../lib/common');
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
|
|
||||||
/**
|
|
||||||
* RouterController
|
|
||||||
*
|
|
||||||
* @param {object} deps
|
|
||||||
* @param {any} deps.memberRepository
|
|
||||||
* @param {any} deps.StripePrice
|
|
||||||
* @param {boolean} deps.allowSelfSignup
|
|
||||||
* @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
|
|
||||||
*/
|
|
||||||
module.exports = class RouterController {
|
module.exports = class RouterController {
|
||||||
|
/**
|
||||||
|
* RouterController
|
||||||
|
*
|
||||||
|
* @param {object} deps
|
||||||
|
* @param {any} deps.offersAPI
|
||||||
|
* @param {any} deps.paymentsService
|
||||||
|
* @param {any} deps.productRepository
|
||||||
|
* @param {any} deps.memberRepository
|
||||||
|
* @param {any} deps.StripePrice
|
||||||
|
* @param {boolean} deps.allowSelfSignup
|
||||||
|
* @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
|
||||||
|
*/
|
||||||
constructor({
|
constructor({
|
||||||
offersAPI,
|
offersAPI,
|
||||||
|
paymentsService,
|
||||||
productRepository,
|
productRepository,
|
||||||
memberRepository,
|
memberRepository,
|
||||||
StripePrice,
|
StripePrice,
|
||||||
|
@ -31,6 +35,7 @@ module.exports = class RouterController {
|
||||||
logging
|
logging
|
||||||
}) {
|
}) {
|
||||||
this._offersAPI = offersAPI;
|
this._offersAPI = offersAPI;
|
||||||
|
this._paymentsService = paymentsService;
|
||||||
this._productRepository = productRepository;
|
this._productRepository = productRepository;
|
||||||
this._memberRepository = memberRepository;
|
this._memberRepository = memberRepository;
|
||||||
this._StripePrice = StripePrice;
|
this._StripePrice = StripePrice;
|
||||||
|
@ -135,7 +140,7 @@ module.exports = class RouterController {
|
||||||
return res.end('Bad Request.');
|
return res.end('Bad Request.');
|
||||||
}
|
}
|
||||||
|
|
||||||
let coupon = null;
|
let couponId = null;
|
||||||
if (offerId && this.labsService.isSet('offers')) {
|
if (offerId && this.labsService.isSet('offers')) {
|
||||||
try {
|
try {
|
||||||
const offer = await this._offersAPI.getOffer({id: offerId});
|
const offer = await this._offersAPI.getOffer({id: offerId});
|
||||||
|
@ -152,9 +157,8 @@ module.exports = class RouterController {
|
||||||
ghostPriceId = tier.yearly_price_id;
|
ghostPriceId = tier.yearly_price_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
coupon = {
|
const coupon = await this._paymentsService.getCouponForOffer(offerId);
|
||||||
id: offer.stripe_coupon_id
|
couponId = coupon.id;
|
||||||
};
|
|
||||||
|
|
||||||
metadata.offer = offer.id;
|
metadata.offer = offer.id;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
@ -192,7 +196,7 @@ module.exports = class RouterController {
|
||||||
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, {
|
||||||
coupon,
|
coupon: {id: couponId},
|
||||||
successUrl: req.body.successUrl || this._config.checkoutSuccessUrl,
|
successUrl: req.body.successUrl || this._config.checkoutSuccessUrl,
|
||||||
cancelUrl: req.body.cancelUrl || this._config.checkoutCancelUrl,
|
cancelUrl: req.body.cancelUrl || this._config.checkoutCancelUrl,
|
||||||
customerEmail: req.body.customerEmail,
|
customerEmail: req.body.customerEmail,
|
||||||
|
@ -237,7 +241,7 @@ module.exports = class RouterController {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const session = await this._stripeAPIService.createCheckoutSession(priceId, stripeCustomer, {
|
const session = await this._stripeAPIService.createCheckoutSession(priceId, stripeCustomer, {
|
||||||
coupon,
|
coupon: {id: couponId},
|
||||||
successUrl: req.body.successUrl || this._config.checkoutSuccessUrl,
|
successUrl: req.body.successUrl || this._config.checkoutSuccessUrl,
|
||||||
cancelUrl: req.body.cancelUrl || this._config.checkoutCancelUrl,
|
cancelUrl: req.body.cancelUrl || this._config.checkoutCancelUrl,
|
||||||
metadata: metadata
|
metadata: metadata
|
||||||
|
|
|
@ -35,6 +35,7 @@
|
||||||
"@tryghost/member-events": "^0.3.0",
|
"@tryghost/member-events": "^0.3.0",
|
||||||
"@tryghost/members-analytics-ingress": "^0.1.4",
|
"@tryghost/members-analytics-ingress": "^0.1.4",
|
||||||
"@tryghost/members-stripe-service": "^0.3.0",
|
"@tryghost/members-stripe-service": "^0.3.0",
|
||||||
|
"@tryghost/members-payments": "^0.0.0",
|
||||||
"@tryghost/tpl": "^0.1.2",
|
"@tryghost/tpl": "^0.1.2",
|
||||||
"@types/jsonwebtoken": "^8.5.1",
|
"@types/jsonwebtoken": "^8.5.1",
|
||||||
"bluebird": "^3.5.4",
|
"bluebird": "^3.5.4",
|
||||||
|
|
Loading…
Add table
Reference in a new issue