2022-10-14 07:52:51 +03:00
|
|
|
const {UpdateCollisionError, NotFoundError, MethodNotAllowedError, ValidationError, BadRequestError} = require('@tryghost/errors');
|
2022-02-08 19:01:29 +03:00
|
|
|
const tpl = require('@tryghost/tpl');
|
|
|
|
|
|
|
|
const messages = {
|
|
|
|
priceMustBeInteger: 'Tier prices must be an integer.',
|
|
|
|
priceIsNegative: 'Tier prices must not be negative',
|
|
|
|
maxPriceExceeded: 'Tier prices may not exceed 999999.99'
|
|
|
|
};
|
2021-05-21 16:26:51 +03:00
|
|
|
|
2021-04-12 17:30:17 +03:00
|
|
|
/**
|
|
|
|
* @typedef {object} ProductModel
|
|
|
|
*/
|
|
|
|
|
2021-04-19 17:09:28 +03:00
|
|
|
/**
|
|
|
|
* @typedef {object} StripePriceInput
|
|
|
|
* @param {string} nickname
|
|
|
|
* @param {string} currency
|
|
|
|
* @param {number} amount
|
|
|
|
* @param {'recurring'|'one-time'} type
|
|
|
|
* @param {string | null} interval
|
|
|
|
* @param {string?} stripe_product_id
|
|
|
|
* @param {string?} stripe_price_id
|
|
|
|
*/
|
|
|
|
|
2021-06-24 18:53:56 +03:00
|
|
|
/**
|
|
|
|
* @typedef {object} BenefitInput
|
|
|
|
* @param {string} name
|
|
|
|
*/
|
|
|
|
|
2022-02-08 19:01:29 +03:00
|
|
|
function validatePrice(price) {
|
|
|
|
if (!Number.isInteger(price.amount)) {
|
|
|
|
throw new ValidationError({
|
|
|
|
message: tpl(messages.priceMustBeInteger)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (price.amount < 0) {
|
|
|
|
throw new ValidationError({
|
|
|
|
message: tpl(messages.priceIsNegative)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (price.amount > 9999999999) {
|
|
|
|
throw new ValidationError({
|
|
|
|
message: tpl(messages.maxPriceExceeded)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-12 17:30:17 +03:00
|
|
|
class ProductRepository {
|
|
|
|
/**
|
|
|
|
* @param {object} deps
|
|
|
|
* @param {any} deps.Product
|
2022-01-24 14:10:14 +03:00
|
|
|
* @param {any} deps.Settings
|
2021-04-12 17:30:17 +03:00
|
|
|
* @param {any} deps.StripeProduct
|
|
|
|
* @param {any} deps.StripePrice
|
|
|
|
* @param {import('@tryghost/members-api/lib/services/stripe-api')} deps.stripeAPIService
|
|
|
|
*/
|
|
|
|
constructor({
|
|
|
|
Product,
|
2022-01-24 14:10:14 +03:00
|
|
|
Settings,
|
2021-04-12 17:30:17 +03:00
|
|
|
StripeProduct,
|
|
|
|
StripePrice,
|
|
|
|
stripeAPIService
|
|
|
|
}) {
|
|
|
|
this._Product = Product;
|
2022-01-24 14:10:14 +03:00
|
|
|
this._Settings = Settings;
|
2021-04-12 17:30:17 +03:00
|
|
|
this._StripeProduct = StripeProduct;
|
|
|
|
this._StripePrice = StripePrice;
|
|
|
|
this._stripeAPIService = stripeAPIService;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves a Product by either stripe_product_id, stripe_price_id, id or slug
|
|
|
|
*
|
|
|
|
* @param {{stripe_product_id: string} | {stripe_price_id: string} | {id: string} | {slug: string}} data
|
|
|
|
* @param {object} options
|
|
|
|
*
|
|
|
|
* @returns {Promise<ProductModel>}
|
|
|
|
*/
|
2021-09-06 13:47:19 +03:00
|
|
|
async get(data, options = {}) {
|
2021-08-26 19:05:50 +03:00
|
|
|
if (!options.transacting) {
|
|
|
|
return this._Product.transaction((transacting) => {
|
|
|
|
return this.get(data, {
|
|
|
|
...options,
|
|
|
|
transacting
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2021-04-12 17:30:17 +03:00
|
|
|
if ('stripe_product_id' in data) {
|
|
|
|
const stripeProduct = await this._StripeProduct.findOne({
|
|
|
|
stripe_product_id: data.stripe_product_id
|
|
|
|
}, options);
|
|
|
|
|
|
|
|
if (!stripeProduct) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return await stripeProduct.related('product').fetch(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('stripe_price_id' in data) {
|
|
|
|
const stripePrice = await this._StripePrice.findOne({
|
|
|
|
stripe_price_id: data.stripe_price_id
|
|
|
|
}, options);
|
|
|
|
|
|
|
|
if (!stripePrice) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const stripeProduct = await stripePrice.related('stripeProduct').fetch(options);
|
|
|
|
|
|
|
|
if (!stripeProduct) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return await stripeProduct.related('product').fetch(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('id' in data) {
|
|
|
|
return await this._Product.findOne({id: data.id}, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('slug' in data) {
|
|
|
|
return await this._Product.findOne({slug: data.slug}, options);
|
|
|
|
}
|
|
|
|
|
2022-02-15 15:27:22 +03:00
|
|
|
throw new NotFoundError({message: 'Missing id, slug, stripe_product_id or stripe_price_id from data'});
|
2021-04-12 17:30:17 +03:00
|
|
|
}
|
|
|
|
|
2022-10-20 09:56:57 +03:00
|
|
|
/**
|
|
|
|
* Fetches the default product
|
|
|
|
* @param {Object} options
|
|
|
|
* @returns {Promise<ProductModel>}
|
|
|
|
*/
|
|
|
|
async getDefaultProduct(options = {}) {
|
|
|
|
const defaultProductPage = await this.list({
|
|
|
|
filter: 'type:paid+active:true',
|
|
|
|
limit: 1,
|
|
|
|
...options
|
|
|
|
});
|
|
|
|
|
|
|
|
return defaultProductPage.data[0];
|
|
|
|
}
|
|
|
|
|
2021-04-12 17:30:17 +03:00
|
|
|
/**
|
|
|
|
* Creates a product from a name
|
|
|
|
*
|
|
|
|
* @param {object} data
|
|
|
|
* @param {string} data.name
|
2021-05-04 19:00:16 +03:00
|
|
|
* @param {string} data.description
|
2022-03-07 17:09:07 +03:00
|
|
|
* @param {'public'|'none'} data.visibility
|
2022-03-07 14:42:51 +03:00
|
|
|
* @param {string} data.welcome_page_url
|
2021-06-24 18:53:56 +03:00
|
|
|
* @param {BenefitInput[]} data.benefits
|
2022-01-31 13:33:19 +03:00
|
|
|
* @param {StripePriceInput[]} data.stripe_prices
|
2021-05-26 17:58:34 +03:00
|
|
|
* @param {StripePriceInput|null} data.monthly_price
|
|
|
|
* @param {StripePriceInput|null} data.yearly_price
|
2021-04-20 14:51:16 +03:00
|
|
|
* @param {string} data.product_id
|
|
|
|
* @param {string} data.stripe_product_id
|
2022-08-05 13:55:17 +03:00
|
|
|
* @param {number} data.trial_days
|
2021-04-12 17:30:17 +03:00
|
|
|
*
|
|
|
|
* @param {object} options
|
|
|
|
*
|
|
|
|
* @returns {Promise<ProductModel>}
|
|
|
|
**/
|
2021-09-06 13:47:19 +03:00
|
|
|
async create(data, options = {}) {
|
2022-01-31 13:33:19 +03:00
|
|
|
if (!this._stripeAPIService.configured && (data.stripe_prices || data.monthly_price || data.yearly_price)) {
|
2021-05-21 16:26:51 +03:00
|
|
|
throw new UpdateCollisionError({
|
2021-05-24 18:54:09 +03:00
|
|
|
message: 'The requested functionality requires Stripe to be configured. See https://ghost.org/integrations/stripe/',
|
2021-05-21 16:26:51 +03:00
|
|
|
code: 'STRIPE_NOT_CONFIGURED'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-08-26 19:05:50 +03:00
|
|
|
if (!options.transacting) {
|
|
|
|
return this._Product.transaction((transacting) => {
|
|
|
|
return this.create(data, {
|
|
|
|
...options,
|
|
|
|
transacting
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-08 19:01:29 +03:00
|
|
|
if (data.monthly_price) {
|
|
|
|
validatePrice(data.monthly_price);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.yearly_price) {
|
|
|
|
validatePrice(data.monthly_price);
|
|
|
|
}
|
|
|
|
|
2022-10-14 07:52:51 +03:00
|
|
|
if (data.yearly_price && data.monthly_price && data.yearly_price.currency !== data.monthly_price.currency) {
|
|
|
|
throw new BadRequestError({
|
|
|
|
message: 'The monthly and yearly price must use the same currency'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-08 19:01:29 +03:00
|
|
|
if (data.stripe_prices) {
|
|
|
|
data.stripe_prices.forEach(validatePrice);
|
|
|
|
}
|
|
|
|
|
2021-04-12 17:30:17 +03:00
|
|
|
const productData = {
|
2022-01-17 20:32:02 +03:00
|
|
|
type: 'paid',
|
2022-01-19 15:27:45 +03:00
|
|
|
active: true,
|
2022-03-07 17:09:07 +03:00
|
|
|
visibility: data.visibility,
|
2021-05-04 19:00:16 +03:00
|
|
|
name: data.name,
|
2021-06-24 18:53:56 +03:00
|
|
|
description: data.description,
|
2022-01-27 17:35:23 +03:00
|
|
|
benefits: data.benefits,
|
|
|
|
welcome_page_url: data.welcome_page_url
|
2021-04-12 17:30:17 +03:00
|
|
|
};
|
|
|
|
|
2022-10-14 07:52:51 +03:00
|
|
|
if (data.monthly_price) {
|
|
|
|
productData.monthly_price = data.monthly_price.amount;
|
|
|
|
productData.currency = data.monthly_price.currency;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.yearly_price) {
|
|
|
|
productData.yearly_price = data.yearly_price.amount;
|
|
|
|
productData.currency = data.yearly_price.currency;
|
|
|
|
}
|
|
|
|
|
2022-08-05 13:55:17 +03:00
|
|
|
if (Reflect.has(data, 'trial_days')) {
|
|
|
|
productData.trial_days = data.trial_days;
|
|
|
|
}
|
|
|
|
|
2021-04-12 17:30:17 +03:00
|
|
|
const product = await this._Product.add(productData, options);
|
|
|
|
|
|
|
|
if (this._stripeAPIService.configured) {
|
|
|
|
const stripeProduct = await this._stripeAPIService.createProduct({
|
|
|
|
name: productData.name
|
|
|
|
});
|
|
|
|
|
|
|
|
await this._StripeProduct.add({
|
|
|
|
product_id: product.id,
|
|
|
|
stripe_product_id: stripeProduct.id
|
|
|
|
}, options);
|
|
|
|
|
2021-05-26 17:58:34 +03:00
|
|
|
if (data.monthly_price || data.yearly_price) {
|
|
|
|
if (data.monthly_price) {
|
|
|
|
const price = await this._stripeAPIService.createPrice({
|
|
|
|
product: stripeProduct.id,
|
|
|
|
active: true,
|
|
|
|
nickname: `Monthly`,
|
|
|
|
currency: data.monthly_price.currency,
|
|
|
|
amount: data.monthly_price.amount,
|
|
|
|
type: 'recurring',
|
|
|
|
interval: 'month'
|
|
|
|
});
|
|
|
|
|
|
|
|
const stripePrice = await this._StripePrice.add({
|
|
|
|
stripe_price_id: price.id,
|
|
|
|
stripe_product_id: stripeProduct.id,
|
|
|
|
active: true,
|
|
|
|
nickname: price.nickname,
|
|
|
|
currency: price.currency,
|
|
|
|
amount: price.unit_amount,
|
|
|
|
type: 'recurring',
|
|
|
|
interval: 'month'
|
|
|
|
}, options);
|
|
|
|
|
2021-08-26 21:04:06 +03:00
|
|
|
await this._Product.edit({monthly_price_id: stripePrice.id}, {id: product.id, transacting: options.transacting});
|
2021-05-26 17:58:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (data.yearly_price) {
|
|
|
|
const price = await this._stripeAPIService.createPrice({
|
|
|
|
product: stripeProduct.id,
|
|
|
|
active: true,
|
|
|
|
nickname: `Yearly`,
|
|
|
|
currency: data.yearly_price.currency,
|
|
|
|
amount: data.yearly_price.amount,
|
|
|
|
type: 'recurring',
|
|
|
|
interval: 'year'
|
|
|
|
});
|
|
|
|
|
|
|
|
const stripePrice = await this._StripePrice.add({
|
|
|
|
stripe_price_id: price.id,
|
|
|
|
stripe_product_id: stripeProduct.id,
|
|
|
|
active: true,
|
|
|
|
nickname: price.nickname,
|
|
|
|
currency: price.currency,
|
|
|
|
amount: price.unit_amount,
|
|
|
|
type: 'recurring',
|
|
|
|
interval: 'year'
|
|
|
|
}, options);
|
|
|
|
|
2021-08-26 21:04:06 +03:00
|
|
|
await this._Product.edit({yearly_price_id: stripePrice.id}, {id: product.id, transacting: options.transacting});
|
2021-05-26 17:58:34 +03:00
|
|
|
}
|
2022-01-31 13:33:19 +03:00
|
|
|
} else if (data.stripe_prices) {
|
|
|
|
for (const newPrice of data.stripe_prices) {
|
|
|
|
const price = await this._stripeAPIService.createPrice({
|
|
|
|
product: stripeProduct.id,
|
|
|
|
active: true,
|
|
|
|
nickname: newPrice.nickname,
|
|
|
|
currency: newPrice.currency,
|
|
|
|
amount: newPrice.amount,
|
|
|
|
type: newPrice.type,
|
|
|
|
interval: newPrice.interval
|
|
|
|
});
|
|
|
|
|
|
|
|
await this._StripePrice.add({
|
|
|
|
stripe_price_id: price.id,
|
|
|
|
stripe_product_id: stripeProduct.id,
|
|
|
|
active: true,
|
|
|
|
nickname: newPrice.nickname,
|
|
|
|
currency: newPrice.currency,
|
|
|
|
amount: newPrice.amount,
|
|
|
|
type: newPrice.type,
|
|
|
|
interval: newPrice.interval
|
|
|
|
}, options);
|
|
|
|
}
|
2021-04-19 17:09:28 +03:00
|
|
|
}
|
|
|
|
|
2022-01-31 13:33:19 +03:00
|
|
|
await product.related('stripePrices').fetch(options);
|
2021-05-26 17:58:34 +03:00
|
|
|
await product.related('monthlyPrice').fetch(options);
|
|
|
|
await product.related('yearlyPrice').fetch(options);
|
2021-04-12 17:30:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return product;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates a product by id
|
|
|
|
*
|
|
|
|
* @param {object} data
|
|
|
|
* @param {string} data.id
|
|
|
|
* @param {string} data.name
|
2021-05-04 19:00:16 +03:00
|
|
|
* @param {string} data.description
|
2022-08-05 12:58:01 +03:00
|
|
|
* @param {number} data.trial_days
|
2022-03-07 17:09:07 +03:00
|
|
|
* @param {'public'|'none'} data.visibility
|
2022-03-07 14:42:51 +03:00
|
|
|
* @param {string} data.welcome_page_url
|
2021-06-24 18:53:56 +03:00
|
|
|
* @param {BenefitInput[]} data.benefits
|
2021-04-12 17:30:17 +03:00
|
|
|
*
|
2022-10-16 10:43:35 +03:00
|
|
|
* @param {StripePriceInput[]} [data.stripe_prices]
|
2021-05-26 17:58:34 +03:00
|
|
|
* @param {StripePriceInput|null} data.monthly_price
|
|
|
|
* @param {StripePriceInput|null} data.yearly_price
|
2021-04-12 17:30:17 +03:00
|
|
|
*
|
|
|
|
* @param {object} options
|
|
|
|
*
|
|
|
|
* @returns {Promise<ProductModel>}
|
|
|
|
**/
|
2021-09-06 13:47:19 +03:00
|
|
|
async update(data, options = {}) {
|
2022-01-31 13:33:19 +03:00
|
|
|
if (!this._stripeAPIService.configured && (data.stripe_prices || data.monthly_price || data.yearly_price)) {
|
2021-05-21 16:26:51 +03:00
|
|
|
throw new UpdateCollisionError({
|
2021-05-24 18:54:09 +03:00
|
|
|
message: 'The requested functionality requires Stripe to be configured. See https://ghost.org/integrations/stripe/',
|
2021-05-21 16:26:51 +03:00
|
|
|
code: 'STRIPE_NOT_CONFIGURED'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-08-26 19:05:50 +03:00
|
|
|
if (!options.transacting) {
|
|
|
|
return this._Product.transaction((transacting) => {
|
|
|
|
return this.update(data, {
|
|
|
|
...options,
|
|
|
|
transacting
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2022-02-08 19:01:29 +03:00
|
|
|
|
|
|
|
if (data.monthly_price) {
|
|
|
|
validatePrice(data.monthly_price);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.yearly_price) {
|
|
|
|
validatePrice(data.monthly_price);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.stripe_prices) {
|
|
|
|
data.stripe_prices.forEach(validatePrice);
|
|
|
|
}
|
|
|
|
|
2022-10-14 07:52:51 +03:00
|
|
|
if (data.yearly_price && data.monthly_price && data.yearly_price.currency !== data.monthly_price.currency) {
|
|
|
|
throw new BadRequestError({
|
|
|
|
message: 'The monthly and yearly price must use the same currency'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-17 20:32:02 +03:00
|
|
|
const productId = data.id || options.id;
|
2021-08-26 19:05:50 +03:00
|
|
|
|
2022-01-17 20:32:02 +03:00
|
|
|
const existingProduct = await this._Product.findOne({id: productId}, options);
|
|
|
|
|
|
|
|
let productData = {
|
2021-05-04 19:00:16 +03:00
|
|
|
name: data.name,
|
2022-03-07 14:42:51 +03:00
|
|
|
visibility: data.visibility,
|
2021-06-24 18:53:56 +03:00
|
|
|
description: data.description,
|
2022-01-27 17:33:17 +03:00
|
|
|
benefits: data.benefits,
|
|
|
|
welcome_page_url: data.welcome_page_url
|
2021-04-12 17:30:17 +03:00
|
|
|
};
|
|
|
|
|
2022-10-14 07:52:51 +03:00
|
|
|
if (data.monthly_price) {
|
|
|
|
productData.monthly_price = data.monthly_price.amount;
|
|
|
|
productData.currency = data.monthly_price.currency;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.yearly_price) {
|
|
|
|
productData.yearly_price = data.yearly_price.amount;
|
|
|
|
productData.currency = data.yearly_price.currency;
|
|
|
|
}
|
|
|
|
|
2022-01-19 15:27:45 +03:00
|
|
|
if (Reflect.has(data, 'active')) {
|
|
|
|
productData.active = data.active;
|
|
|
|
}
|
|
|
|
|
2022-08-05 12:58:01 +03:00
|
|
|
if (Reflect.has(data, 'trial_days')) {
|
|
|
|
productData.trial_days = data.trial_days;
|
|
|
|
}
|
|
|
|
|
2022-01-17 20:32:02 +03:00
|
|
|
if (existingProduct.get('type') === 'free') {
|
|
|
|
delete productData.name;
|
2022-01-19 15:27:45 +03:00
|
|
|
delete productData.active;
|
2022-08-05 12:58:01 +03:00
|
|
|
delete productData.trial_days;
|
2022-01-17 20:32:02 +03:00
|
|
|
}
|
|
|
|
|
2022-01-24 14:10:14 +03:00
|
|
|
if (existingProduct.get('active') === true && productData.active === false) {
|
|
|
|
const portalProductsSetting = await this._Settings.findOne({
|
|
|
|
key: 'portal_products'
|
|
|
|
}, options);
|
|
|
|
|
|
|
|
let portalProducts;
|
|
|
|
try {
|
|
|
|
portalProducts = JSON.parse(portalProductsSetting.get('value'));
|
|
|
|
} catch (err) {
|
|
|
|
portalProducts = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const updatedProducts = portalProducts.filter(product => product !== productId);
|
|
|
|
|
|
|
|
await this._Settings.edit({
|
|
|
|
key: 'portal_products',
|
|
|
|
value: JSON.stringify(updatedProducts)
|
|
|
|
}, {
|
|
|
|
...options,
|
2022-02-04 15:53:24 +03:00
|
|
|
id: portalProductsSetting.get('id')
|
2022-01-24 14:10:14 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-05-26 17:58:34 +03:00
|
|
|
let product = await this._Product.edit(productData, {
|
2021-04-12 17:30:17 +03:00
|
|
|
...options,
|
2022-01-17 20:32:02 +03:00
|
|
|
id: productId
|
2021-04-12 17:30:17 +03:00
|
|
|
});
|
|
|
|
|
2022-01-17 20:32:02 +03:00
|
|
|
if (this._stripeAPIService.configured && product.get('type') !== 'free') {
|
2021-04-12 17:30:17 +03:00
|
|
|
await product.related('stripeProducts').fetch(options);
|
|
|
|
|
|
|
|
if (!product.related('stripeProducts').first()) {
|
|
|
|
const stripeProduct = await this._stripeAPIService.createProduct({
|
2021-05-10 20:39:16 +03:00
|
|
|
name: product.get('name')
|
2021-04-12 17:30:17 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
await this._StripeProduct.add({
|
|
|
|
product_id: product.id,
|
|
|
|
stripe_product_id: stripeProduct.id
|
|
|
|
}, options);
|
|
|
|
|
|
|
|
await product.related('stripeProducts').fetch(options);
|
2021-05-10 20:39:16 +03:00
|
|
|
} else {
|
|
|
|
if (product.attributes.name !== product._previousAttributes.name) {
|
|
|
|
const stripeProduct = product.related('stripeProducts').first();
|
|
|
|
await this._stripeAPIService.updateProduct(stripeProduct.get('stripe_product_id'), {
|
|
|
|
name: product.get('name')
|
|
|
|
});
|
|
|
|
}
|
2021-04-12 17:30:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const defaultStripeProduct = product.related('stripeProducts').first();
|
|
|
|
|
2021-05-26 17:58:34 +03:00
|
|
|
if (data.monthly_price || data.yearly_price) {
|
|
|
|
if (data.monthly_price) {
|
|
|
|
const existingPrice = await this._StripePrice.findOne({
|
|
|
|
stripe_product_id: defaultStripeProduct.get('stripe_product_id'),
|
|
|
|
amount: data.monthly_price.amount,
|
|
|
|
currency: data.monthly_price.currency,
|
|
|
|
type: 'recurring',
|
2022-08-08 18:18:07 +03:00
|
|
|
interval: 'month',
|
|
|
|
active: true
|
2021-09-01 20:10:12 +03:00
|
|
|
}, options);
|
2021-05-26 17:58:34 +03:00
|
|
|
let priceModel;
|
|
|
|
if (existingPrice) {
|
|
|
|
priceModel = existingPrice;
|
|
|
|
|
|
|
|
await this._stripeAPIService.updatePrice(priceModel.get('stripe_price_id'), {
|
|
|
|
active: true
|
|
|
|
});
|
|
|
|
|
|
|
|
await this._StripePrice.edit({
|
|
|
|
active: true
|
|
|
|
}, {...options, id: priceModel.id});
|
|
|
|
} else {
|
|
|
|
const price = await this._stripeAPIService.createPrice({
|
|
|
|
product: defaultStripeProduct.get('stripe_product_id'),
|
|
|
|
active: true,
|
|
|
|
nickname: `Monthly`,
|
|
|
|
currency: data.monthly_price.currency,
|
|
|
|
amount: data.monthly_price.amount,
|
|
|
|
type: 'recurring',
|
|
|
|
interval: 'month'
|
|
|
|
});
|
|
|
|
|
|
|
|
const stripePrice = await this._StripePrice.add({
|
|
|
|
stripe_price_id: price.id,
|
|
|
|
stripe_product_id: defaultStripeProduct.get('stripe_product_id'),
|
|
|
|
active: true,
|
|
|
|
nickname: price.nickname,
|
|
|
|
currency: price.currency,
|
|
|
|
amount: price.unit_amount,
|
|
|
|
type: 'recurring',
|
|
|
|
interval: 'month'
|
|
|
|
}, options);
|
|
|
|
|
|
|
|
priceModel = stripePrice;
|
|
|
|
}
|
|
|
|
|
|
|
|
product = await this._Product.edit({monthly_price_id: priceModel.id}, {...options, id: product.id});
|
2021-04-20 14:51:16 +03:00
|
|
|
}
|
2021-05-10 17:40:47 +03:00
|
|
|
|
2021-05-26 17:58:34 +03:00
|
|
|
if (data.yearly_price) {
|
|
|
|
const existingPrice = await this._StripePrice.findOne({
|
|
|
|
stripe_product_id: defaultStripeProduct.get('stripe_product_id'),
|
|
|
|
amount: data.yearly_price.amount,
|
|
|
|
currency: data.yearly_price.currency,
|
|
|
|
type: 'recurring',
|
2022-08-08 18:18:07 +03:00
|
|
|
interval: 'year',
|
|
|
|
active: true
|
2021-09-01 20:10:12 +03:00
|
|
|
}, options);
|
2021-05-26 17:58:34 +03:00
|
|
|
let priceModel;
|
|
|
|
|
|
|
|
if (existingPrice) {
|
|
|
|
priceModel = existingPrice;
|
|
|
|
|
|
|
|
await this._stripeAPIService.updatePrice(priceModel.get('stripe_price_id'), {
|
|
|
|
active: true
|
|
|
|
});
|
|
|
|
|
|
|
|
await this._StripePrice.edit({
|
|
|
|
active: true
|
|
|
|
}, {...options, id: priceModel.id});
|
|
|
|
} else {
|
|
|
|
const price = await this._stripeAPIService.createPrice({
|
|
|
|
product: defaultStripeProduct.get('stripe_product_id'),
|
|
|
|
active: true,
|
|
|
|
nickname: `Yearly`,
|
|
|
|
currency: data.yearly_price.currency,
|
|
|
|
amount: data.yearly_price.amount,
|
|
|
|
type: 'recurring',
|
|
|
|
interval: 'year'
|
|
|
|
});
|
|
|
|
|
|
|
|
const stripePrice = await this._StripePrice.add({
|
|
|
|
stripe_price_id: price.id,
|
|
|
|
stripe_product_id: defaultStripeProduct.get('stripe_product_id'),
|
|
|
|
active: true,
|
|
|
|
nickname: price.nickname,
|
|
|
|
currency: price.currency,
|
|
|
|
amount: price.unit_amount,
|
|
|
|
type: 'recurring',
|
|
|
|
interval: 'year'
|
|
|
|
}, options);
|
|
|
|
|
|
|
|
priceModel = stripePrice;
|
|
|
|
}
|
|
|
|
|
|
|
|
product = await this._Product.edit({yearly_price_id: priceModel.id}, {...options, id: product.id});
|
|
|
|
}
|
2022-01-31 13:33:19 +03:00
|
|
|
} else if (data.stripe_prices) {
|
|
|
|
const newPrices = data.stripe_prices.filter(price => !price.stripe_price_id);
|
|
|
|
const existingPrices = data.stripe_prices.filter((price) => {
|
|
|
|
return !!price.stripe_price_id && !!price.stripe_product_id;
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const existingPrice of existingPrices) {
|
2022-09-29 17:26:00 +03:00
|
|
|
const existingProductId = existingPrice.stripe_product_id;
|
|
|
|
let stripeProduct = await this._StripeProduct.findOne({stripe_product_id: existingProductId}, options);
|
2022-01-31 13:33:19 +03:00
|
|
|
if (!stripeProduct) {
|
|
|
|
stripeProduct = await this._StripeProduct.add({
|
|
|
|
product_id: product.id,
|
2022-09-29 17:26:00 +03:00
|
|
|
stripe_product_id: existingProductId
|
2022-01-31 13:33:19 +03:00
|
|
|
}, options);
|
|
|
|
}
|
|
|
|
const stripePrice = await this._StripePrice.findOne({stripe_price_id: existingPrice.stripe_price_id}, options);
|
|
|
|
|
|
|
|
if (!stripePrice) {
|
|
|
|
await this._StripePrice.add({
|
|
|
|
stripe_price_id: existingPrice.stripe_price_id,
|
|
|
|
stripe_product_id: stripeProduct.get('stripe_product_id'),
|
|
|
|
active: existingPrice.active,
|
|
|
|
nickname: existingPrice.nickname,
|
|
|
|
description: existingPrice.description,
|
|
|
|
currency: existingPrice.currency,
|
|
|
|
amount: existingPrice.amount,
|
|
|
|
type: existingPrice.type,
|
|
|
|
interval: existingPrice.interval
|
|
|
|
}, options);
|
|
|
|
} else {
|
|
|
|
const updated = await this._StripePrice.edit({
|
|
|
|
nickname: existingPrice.nickname,
|
|
|
|
description: existingPrice.description,
|
|
|
|
active: existingPrice.active
|
|
|
|
}, {
|
|
|
|
...options,
|
|
|
|
id: stripePrice.id
|
|
|
|
});
|
|
|
|
|
|
|
|
await this._stripeAPIService.updatePrice(updated.get('stripe_price_id'), {
|
|
|
|
nickname: updated.get('nickname'),
|
|
|
|
active: updated.get('active')
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const newPrice of newPrices) {
|
2022-09-29 17:26:00 +03:00
|
|
|
const newProductId = newPrice.stripe_product_id;
|
|
|
|
const stripeProduct = newProductId ?
|
|
|
|
await this._StripeProduct.findOne({stripe_product_id: newProductId}, options) : defaultStripeProduct;
|
2022-01-31 13:33:19 +03:00
|
|
|
|
|
|
|
const price = await this._stripeAPIService.createPrice({
|
|
|
|
product: stripeProduct.get('stripe_product_id'),
|
|
|
|
active: true,
|
|
|
|
nickname: newPrice.nickname,
|
|
|
|
currency: newPrice.currency,
|
|
|
|
amount: newPrice.amount,
|
|
|
|
type: newPrice.type,
|
|
|
|
interval: newPrice.interval
|
|
|
|
});
|
|
|
|
|
|
|
|
await this._StripePrice.add({
|
|
|
|
stripe_price_id: price.id,
|
|
|
|
stripe_product_id: stripeProduct.get('stripe_product_id'),
|
|
|
|
active: price.active,
|
|
|
|
nickname: price.nickname,
|
|
|
|
description: newPrice.description,
|
|
|
|
currency: price.currency,
|
|
|
|
amount: price.unit_amount,
|
|
|
|
type: price.type,
|
|
|
|
interval: price.recurring && price.recurring.interval || null
|
|
|
|
}, options);
|
|
|
|
}
|
2021-04-19 17:09:28 +03:00
|
|
|
}
|
2021-04-12 17:30:17 +03:00
|
|
|
|
2022-01-31 13:33:19 +03:00
|
|
|
await product.related('stripePrices').fetch(options);
|
2021-05-26 17:58:34 +03:00
|
|
|
await product.related('monthlyPrice').fetch(options);
|
|
|
|
await product.related('yearlyPrice').fetch(options);
|
2021-06-29 17:06:35 +03:00
|
|
|
await product.related('benefits').fetch(options);
|
2021-04-12 17:30:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return product;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a paginated list of Products
|
|
|
|
*
|
|
|
|
* @params {object} options
|
|
|
|
*
|
|
|
|
* @returns {Promise<{data: ProductModel[], meta: object}>}
|
|
|
|
**/
|
2021-09-06 13:47:19 +03:00
|
|
|
async list(options = {}) {
|
2021-08-26 19:05:50 +03:00
|
|
|
if (!options.transacting) {
|
|
|
|
return this._Product.transaction((transacting) => {
|
|
|
|
return this.list({
|
|
|
|
...options,
|
|
|
|
transacting
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2021-04-12 17:30:17 +03:00
|
|
|
return this._Product.findPage(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
async destroy() {
|
2022-02-15 15:27:22 +03:00
|
|
|
throw new MethodNotAllowedError({message: 'Cannot destroy products, yet...'});
|
2021-04-12 17:30:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ProductRepository;
|