0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-11 02:12:21 -05:00

Added stripeCouponId to Offer

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

When creating a Stripe Checkout Session for an Offer - we need access to
the underlying Stripe Coupon. Exposing it here allows consumers of the
OfferRepository access.
This commit is contained in:
Fabien O'Carroll 2021-10-06 15:13:08 +02:00
parent f0141f08ff
commit 6ae8b7eb0c
3 changed files with 23 additions and 2 deletions

View file

@ -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});
}
}

View file

@ -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

View file

@ -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
};