mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-10 23:36:14 -05:00
Updated webhook service to work with multiple products
no-issue Since we do not necessarily have a single stripe product anymore, we should be checking if an invoice webhook is for a stripe product which we know about. We use the Products repository to search our database for one.
This commit is contained in:
parent
a3f7f3d1a0
commit
45d338730c
2 changed files with 11 additions and 7 deletions
|
@ -6,22 +6,22 @@ module.exports = class StripeWebhookService {
|
||||||
* @param {object} deps
|
* @param {object} deps
|
||||||
* @param {any} deps.StripeWebhook
|
* @param {any} deps.StripeWebhook
|
||||||
* @param {import('../stripe-api')} deps.stripeAPIService
|
* @param {import('../stripe-api')} deps.stripeAPIService
|
||||||
* @param {import('../stripe-plans')} deps.stripePlansService
|
|
||||||
* @param {import('../../repositories/member')} deps.memberRepository
|
* @param {import('../../repositories/member')} deps.memberRepository
|
||||||
|
* @param {import('../../repositories/product')} deps.productRepository
|
||||||
* @param {import('../../repositories/event')} deps.eventRepository
|
* @param {import('../../repositories/event')} deps.eventRepository
|
||||||
* @param {any} deps.sendEmailWithMagicLink
|
* @param {any} deps.sendEmailWithMagicLink
|
||||||
*/
|
*/
|
||||||
constructor({
|
constructor({
|
||||||
StripeWebhook,
|
StripeWebhook,
|
||||||
stripeAPIService,
|
stripeAPIService,
|
||||||
stripePlansService,
|
productRepository,
|
||||||
memberRepository,
|
memberRepository,
|
||||||
eventRepository,
|
eventRepository,
|
||||||
sendEmailWithMagicLink
|
sendEmailWithMagicLink
|
||||||
}) {
|
}) {
|
||||||
this._StripeWebhook = StripeWebhook;
|
this._StripeWebhook = StripeWebhook;
|
||||||
this._stripeAPIService = stripeAPIService;
|
this._stripeAPIService = stripeAPIService;
|
||||||
this._stripePlansService = stripePlansService;
|
this._productRepository = productRepository;
|
||||||
this._memberRepository = memberRepository;
|
this._memberRepository = memberRepository;
|
||||||
this._eventRepository = eventRepository;
|
this._eventRepository = eventRepository;
|
||||||
this._sendEmailWithMagicLink = sendEmailWithMagicLink;
|
this._sendEmailWithMagicLink = sendEmailWithMagicLink;
|
||||||
|
@ -169,9 +169,13 @@ module.exports = class StripeWebhookService {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Subscription is for a different product - ignore.
|
// Subscription is for a different product - ignore.
|
||||||
if (this._stripePlansService.getProduct().id !== subscription.plan.product) {
|
const product = await this._productRepository.get({
|
||||||
|
stripe_product_id: subscription.plan.product
|
||||||
|
});
|
||||||
|
if (!product) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Could not find the member, which we need in order to insert an payment event.
|
// Could not find the member, which we need in order to insert an payment event.
|
||||||
throw new errors.NotFoundError({
|
throw new errors.NotFoundError({
|
||||||
message: `No member found for customer ${subscription.customer}`
|
message: `No member found for customer ${subscription.customer}`
|
||||||
|
|
|
@ -2,8 +2,8 @@ const {describe, it} = require('mocha');
|
||||||
const should = require('should');
|
const should = require('should');
|
||||||
const sinon = require('sinon');
|
const sinon = require('sinon');
|
||||||
const StripeAPIService = require('../../../../lib/services/stripe-api');
|
const StripeAPIService = require('../../../../lib/services/stripe-api');
|
||||||
const StripePlansService = require('../../../../lib/services/stripe-plans');
|
|
||||||
const StripeWebhookService = require('../../../../lib/services/stripe-webhook');
|
const StripeWebhookService = require('../../../../lib/services/stripe-webhook');
|
||||||
|
const ProductRepository = require('../../../../lib/repositories/product');
|
||||||
const MemberRepository = require('../../../../lib/repositories/member');
|
const MemberRepository = require('../../../../lib/repositories/member');
|
||||||
|
|
||||||
function mock(Class) {
|
function mock(Class) {
|
||||||
|
@ -15,7 +15,7 @@ describe('StripeWebhookService', function () {
|
||||||
it('Should throw a 404 error when a member is not found for a valid Ghost Members invoice', async function () {
|
it('Should throw a 404 error when a member is not found for a valid Ghost Members invoice', async function () {
|
||||||
const stripeWebhookService = new StripeWebhookService({
|
const stripeWebhookService = new StripeWebhookService({
|
||||||
stripeAPIService: mock(StripeAPIService),
|
stripeAPIService: mock(StripeAPIService),
|
||||||
stripePlansService: mock(StripePlansService),
|
productRepository: mock(ProductRepository),
|
||||||
memberRepository: mock(MemberRepository)
|
memberRepository: mock(MemberRepository)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -28,7 +28,7 @@ describe('StripeWebhookService', function () {
|
||||||
|
|
||||||
stripeWebhookService._memberRepository.get.resolves(null);
|
stripeWebhookService._memberRepository.get.resolves(null);
|
||||||
|
|
||||||
stripeWebhookService._stripePlansService.getProduct.returns({
|
stripeWebhookService._productRepository.get.resolves({
|
||||||
id: 'product_id'
|
id: 'product_id'
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue