2022-02-08 19:01:29 +03:00
|
|
|
const {UpdateCollisionError, NotFoundError, MethodNotAllowedError, ValidationError} = require('@tryghost/errors');
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
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-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-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-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-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',
|
|
|
|
interval: 'month'
|
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',
|
|
|
|
interval: 'year'
|
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) {
|
|
|
|
const productId = existingPrice.stripe_product_id;
|
|
|
|
let stripeProduct = await this._StripeProduct.findOne({stripe_product_id: productId}, options);
|
|
|
|
if (!stripeProduct) {
|
|
|
|
stripeProduct = await this._StripeProduct.add({
|
|
|
|
product_id: product.id,
|
|
|
|
stripe_product_id: productId
|
|
|
|
}, 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) {
|
|
|
|
const productId = newPrice.stripe_product_id;
|
|
|
|
const stripeProduct = productId ?
|
|
|
|
await this._StripeProduct.findOne({stripe_product_id: productId}, options) : defaultStripeProduct;
|
|
|
|
|
|
|
|
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;
|