diff --git a/ghost/offers/lib/Offer.js b/ghost/offers/lib/Offer.js index 702c25df97..6e7a69e3e6 100644 --- a/ghost/offers/lib/Offer.js +++ b/ghost/offers/lib/Offer.js @@ -12,6 +12,7 @@ const ObjectID = require('bson-objectid').default; * @prop {'percent'|'amount'} type * @prop {number} amount * @prop {string} currency + * @prop {string} [stripe_coupon_id] * @prop {OfferTier} tier */ @@ -167,6 +168,10 @@ class Offer { return !!this.options.isNew; } + get stripeCouponId() { + return this.props.stripe_coupon_id; + } + /** * @private * @param {OfferProps} props @@ -291,6 +296,18 @@ class Offer { const cadence = data.cadence; const currency = data.currency; + if (isNew && data.stripe_coupon_id) { + throw new errors.InvalidOfferCoupon({ + message: 'Cannot supply a stripe_coupon_id for new Offers.' + }); + } + if (!isNew && !data.stripe_coupon_id) { + throw new errors.InvalidOfferCoupon({ + message: 'Offers must have a stripe_coupon_id.' + }); + } + const couponId = data.stripe_coupon_id; + const tier = OfferTier.create(data.tier); return new Offer({ @@ -303,7 +320,8 @@ class Offer { amount, cadence, currency, - tier + tier, + stripe_coupon_id: couponId }, {isNew}); } } diff --git a/ghost/offers/lib/OfferRepository.js b/ghost/offers/lib/OfferRepository.js index a22a64bbee..2e61836dab 100644 --- a/ghost/offers/lib/OfferRepository.js +++ b/ghost/offers/lib/OfferRepository.js @@ -22,6 +22,7 @@ function toDomain(json) { amount: json.discount_amount, cadence: json.interval, currency: json.currency, + stripe_coupon_id: json.stripe_coupon_id, tier: { id: json.product.id, name: json.product.name diff --git a/ghost/offers/lib/errors.js b/ghost/offers/lib/errors.js index 2685168dd6..54613aa874 100644 --- a/ghost/offers/lib/errors.js +++ b/ghost/offers/lib/errors.js @@ -23,6 +23,7 @@ class InvalidOfferAmount extends InvalidPropError {} class InvalidOfferCurrency extends InvalidPropError {} class InvalidOfferTierName extends InvalidPropError {} class InvalidOfferCadence extends InvalidPropError {} +class InvalidOfferCoupon extends InvalidPropError {} module.exports = { InvalidOfferNameError, @@ -33,5 +34,6 @@ module.exports = { InvalidOfferAmount, InvalidOfferCurrency, InvalidOfferCadence, - InvalidOfferTierName + InvalidOfferTierName, + InvalidOfferCoupon };