🐛 Fixed importing Stripe Plans with amount 0 (#12062)

closes #12049

Stripe plans used to default to 0, and our new validation of plan
amounts were causing issues when importing from an older version of
Ghost, this updates the validation to be skipped when importing.

- Added regression test for importing plans
This commit is contained in:
Fabien 'egg' O'Carroll 2020-07-20 14:59:23 +02:00 committed by GitHub
parent b3aec95230
commit 6232981be7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 8 deletions

View File

@ -125,10 +125,10 @@ Settings = ghostBookshelf.Model.extend({
async onValidate(model, attr, options) {
await ghostBookshelf.Model.prototype.onValidate.call(this, model, attr, options);
await Settings.validators.all(model);
await Settings.validators.all(model, options);
if (typeof Settings.validators[model.get('key')] === 'function') {
await Settings.validators[model.get('key')](model);
await Settings.validators[model.get('key')](model, options);
}
},
@ -353,14 +353,20 @@ Settings = ghostBookshelf.Model.extend({
throw new errors.ValidationError(validationErrors.join('\n'));
}
},
async stripe_plans(model) {
async stripe_plans(model, options) {
const plans = JSON.parse(model.get('value'));
for (const plan of plans) {
// We check 100, not 1, because amounts are in fractional units
if (plan.amount < 100 && plan.name !== 'Complimentary') {
throw new errors.ValidationError({
message: 'Plans cannot have an amount less than 1'
});
// Stripe plans used to be allowed (and defaulted to!) 0 amount plans
// this causes issues to people importing from older versions of Ghost
// even if they don't use Members/Stripe
// issue: https://github.com/TryGhost/Ghost/issues/12049
if (!options.importing) {
// We check 100, not 1, because amounts are in fractional units
if (plan.amount < 100 && plan.name !== 'Complimentary') {
throw new errors.ValidationError({
message: 'Plans cannot have an amount less than 1'
});
}
}
if (typeof plan.name !== 'string') {

View File

@ -1185,6 +1185,33 @@ describe('Integration: Importer', function () {
posts[1].html.should.eql('<figure class="kg-card kg-image-card kg-width-wide"><img src="source" class="kg-image" alt></figure><!--kg-card-begin: markdown--><h1 id="postcontent">Post Content</h1>\n<!--kg-card-end: markdown-->');
});
});
it('Can import stripe plans with an "amount" of 0', async function () {
const exportData = exportedLatestBody().db[0];
exportData.data.settings.push({
id: 'blahblah',
group: 'members',
type: 'array',
flags: null,
created_at: '2020-04-20 16:20:00',
updated_at: '2020-04-20 16:20:00',
key: 'stripe_plans',
value: JSON.stringify([{
name: 'Monthly',
interval: 'month',
currency: 'usd',
amount: 0
}, {
name: 'Yearly',
interval: 'year',
currency: 'usd',
amount: 0
}])
});
await dataImporter.doImport(exportData, importOptions);
});
});
describe('Existing database', function () {