mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-09-20 09:08:54 +03:00
Added support for creating fixed offers with currency
refs https://github.com/TryGhost/Team/issues/1083 We now allow creating offers for a fixed amount, rather than a percentage. These require a currency to be passed as a fixed amount is meaningless without one.
This commit is contained in:
parent
2c04afe810
commit
2b58ecd82e
@ -122,7 +122,8 @@ class OfferRepository {
|
||||
discount_amount: offer.amount.value,
|
||||
interval: offer.cadence.value,
|
||||
product_id: offer.tier.id,
|
||||
duration: offer.duration.value
|
||||
duration: offer.duration.value,
|
||||
currency: offer.currency.value
|
||||
});
|
||||
|
||||
if (offer.codeChanged || offer.isNew) {
|
||||
@ -143,6 +144,9 @@ class OfferRepository {
|
||||
|
||||
if (offer.type.value === 'percent') {
|
||||
coupon.percent_off = offer.amount.value;
|
||||
} else {
|
||||
coupon.amount_off = offer.amount.value;
|
||||
coupon.currency = offer.currency.value;
|
||||
}
|
||||
|
||||
const couponData = await this.stripeAPIService.createCoupon(coupon);
|
||||
|
@ -9,6 +9,7 @@ const OfferDescription = require('./OfferDescription');
|
||||
const OfferCadence = require('./OfferCadence');
|
||||
const OfferType = require('./OfferType');
|
||||
const OfferDuration = require('./OfferDuration');
|
||||
const OfferCurrency = require('./OfferCurrency');
|
||||
|
||||
/**
|
||||
* @typedef {object} OfferProps
|
||||
@ -21,7 +22,7 @@ const OfferDuration = require('./OfferDuration');
|
||||
* @prop {OfferType} type
|
||||
* @prop {OfferAmount} amount
|
||||
* @prop {OfferDuration} duration
|
||||
* @prop {string} currency
|
||||
* @prop {OfferCurrency} currency
|
||||
* @prop {string} [stripe_coupon_id]
|
||||
* @prop {OfferTier} tier
|
||||
*/
|
||||
@ -226,11 +227,14 @@ class Offer {
|
||||
const type = OfferType.create(data.type);
|
||||
const cadence = OfferCadence.create(data.cadence);
|
||||
const duration = OfferDuration.create(data.duration);
|
||||
|
||||
let currency = null;
|
||||
let amount;
|
||||
if (type.equals(OfferType.Percent)) {
|
||||
if (type.equals(OfferType.Percentage)) {
|
||||
amount = OfferAmount.OfferPercentageAmount.create(data.amount);
|
||||
} else {
|
||||
amount = OfferAmount.OfferAbsoluteAmount.create(data.amount);
|
||||
} else if (type.equals(OfferType.Fixed)) {
|
||||
amount = OfferAmount.OfferFixedAmount.create(data.amount);
|
||||
currency = OfferCurrency.create(data.currency);
|
||||
}
|
||||
|
||||
if (isNew) {
|
||||
@ -255,8 +259,6 @@ class Offer {
|
||||
});
|
||||
}
|
||||
|
||||
const currency = data.currency;
|
||||
|
||||
if (isNew && data.stripe_coupon_id) {
|
||||
throw new errors.InvalidOfferCoupon({
|
||||
message: 'Cannot supply a stripe_coupon_id for new Offers.'
|
||||
|
18
ghost/offers/lib/domain/models/OfferCurrency.js
Normal file
18
ghost/offers/lib/domain/models/OfferCurrency.js
Normal file
@ -0,0 +1,18 @@
|
||||
const ValueObject = require('../../shared/ValueObject');
|
||||
const InvalidOfferCurrency = require('../../errors').InvalidOfferCurrency;
|
||||
|
||||
/** @extends ValueObject<string> */
|
||||
class OfferCurrency extends ValueObject {
|
||||
/** @param {unknown} currency */
|
||||
static create(currency) {
|
||||
if (typeof currency !== 'string') {
|
||||
throw new InvalidOfferCurrency({
|
||||
message: 'Offer `currency` must be a string.'
|
||||
});
|
||||
}
|
||||
// TODO: Validate it is a country code we support?
|
||||
return new OfferCurrency(currency);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = OfferCurrency;
|
Loading…
Reference in New Issue
Block a user