Mapped Stripe Product name to Product name

closes https://github.com/TryGhost/Team/issues/682

This ensures that the Stripe Product name is updated during the
migrations of an existing site and any future updates to the Product
name.
This commit is contained in:
Fabien O'Carroll 2021-05-10 18:39:16 +01:00
parent 15b535fd45
commit d32e44c73b
3 changed files with 27 additions and 4 deletions

View File

@ -127,11 +127,11 @@ module.exports = class StripeMigrations {
if (!defaultStripeProduct) {
this._logging.info('Could not find Stripe Product - creating one');
const stripeProduct = await this._stripeAPIService.createProduct({
name: 'Ghost Product'
});
const productsPage = await this._Product.findPage({limit: 1});
const defaultProduct = productsPage.data[0];
const stripeProduct = await this._stripeAPIService.createProduct({
name: defaultProduct.get('name')
});
if (!defaultProduct) {
this._logging.error('Could not find Product - skipping stripe_plans -> stripe_prices migration');
return;

View File

@ -176,7 +176,7 @@ class ProductRepository {
if (!product.related('stripeProducts').first()) {
const stripeProduct = await this._stripeAPIService.createProduct({
name: productData.name
name: product.get('name')
});
await this._StripeProduct.add({
@ -185,6 +185,13 @@ class ProductRepository {
}, options);
await product.related('stripeProducts').fetch(options);
} 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')
});
}
}
const defaultStripeProduct = product.related('stripeProducts').first();

View File

@ -136,6 +136,22 @@ module.exports = class StripeAPIService {
return price;
}
/**
* @param {string} id
* @param {object} options
* @param {string} options.name
*
* @returns {Promise<IProduct>}
*/
async updateProduct(id, options) {
await this._rateLimitBucket.throttle();
const product = await this._stripe.products.update(id, {
name: options.name
});
return product;
}
/**
* ensureProduct.
*