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

Updated offer_id matching logic to use stripe_coupon_id instead of metadata (#390)

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

- Removed offer metadata again from offer (added in one of the previous commits)
- Added `getByStripeCouponId` method in offer repository (required to find an offer based on the stripe_coupon_id)
- Match discounts from Stripe based on the stripe_coupon_id instead of metadata
This commit is contained in:
Simon Backx 2022-04-19 11:21:48 +02:00 committed by GitHub
parent 526044e135
commit 41e0aa17ff
3 changed files with 28 additions and 14 deletions

View file

@ -702,14 +702,16 @@ module.exports = class MemberRepository {
logging.error(e);
}
let offerId = subscription.discount && (subscription.discount.coupon.metadata.offer || subscription.metadata.offer) ? (subscription.discount.coupon.metadata.offer ? subscription.discount.coupon.metadata.offer : subscription.metadata.offer) : null;
let stripeCouponId = subscription.discount && subscription.discount.coupon ? subscription.discount.coupon.id : null;
let offerId = null;
if (offerId) {
// Validate the offer id from the metadata
const offer = await this._offerRepository.getById(offerId, {transacting: options.transacting});
if (!offer) {
logging.error(`Received an invalid offer id (${offerId}) in the metadata of a subscription - ${subscription.id}.`);
offerId = null;
if (stripeCouponId) {
// Get the offer from our database
const offer = await this._offerRepository.getByStripeCouponId(stripeCouponId, {transacting: options.transacting});
if (offer) {
offerId = offer.id;
} else {
logging.error(`Received an unknown stripe coupon id (${stripeCouponId}) for subscription - ${subscription.id}.`);
}
}
@ -739,8 +741,6 @@ module.exports = class MemberRepository {
canceled: subscription.cancel_at_period_end,
discount: subscription.discount
}),
// We try to use the offer_id that was stored in Stripe coupon and fallback to the one stored in the subscription
// This allows us to catch the offer_id from discounts (created by Ghost) that were applied via the Stripe dashboard
offer_id: offerId
};

View file

@ -141,6 +141,24 @@ class OfferRepository {
return this.mapToOffer(model, options);
}
/**
* @param {string} id stripe_coupon_id
* @param {BaseOptions} [options]
* @returns {Promise<Offer>}
*/
async getByStripeCouponId(id, options) {
const model = await this.OfferModel.findOne({stripe_coupon_id: id}, {
...options,
withRelated: ['product']
});
if (!model) {
return null;
}
return this.mapToOffer(model, options);
}
/**
* @param {ListOptions} options
* @returns {Promise<Offer[]>}

View file

@ -48,11 +48,7 @@ class PaymentsService {
/** @type {import('stripe').Stripe.CouponCreateParams} */
const couponData = {
name: offer.name,
duration: offer.duration,
// Note that the metadata is not present for older coupons
metadata: {
offer: offer.id
}
duration: offer.duration
};
if (offer.duration === 'repeating') {