2021-06-04 10:37:47 +03:00
|
|
|
import ModalBase from 'ghost-admin/components/modal-base';
|
2022-05-11 20:11:54 +03:00
|
|
|
import TierBenefitItem from '../models/tier-benefit-item';
|
2021-06-04 10:37:47 +03:00
|
|
|
import classic from 'ember-classic-decorator';
|
2022-09-13 11:23:54 +03:00
|
|
|
import {action} from '@ember/object';
|
2021-06-04 10:37:47 +03:00
|
|
|
import {currencies, getCurrencyOptions, getSymbol} from 'ghost-admin/utils/currency';
|
2021-06-21 11:36:17 +03:00
|
|
|
import {A as emberA} from '@ember/array';
|
2022-04-01 14:22:41 +03:00
|
|
|
import {htmlSafe} from '@ember/template';
|
2022-11-03 14:14:36 +03:00
|
|
|
import {inject} from 'ghost-admin/decorators/inject';
|
2023-05-15 11:47:07 +03:00
|
|
|
import {run} from '@ember/runloop';
|
2021-06-04 10:37:47 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2022-02-09 13:49:38 +03:00
|
|
|
import {task} from 'ember-concurrency';
|
2021-06-04 10:37:47 +03:00
|
|
|
import {tracked} from '@glimmer/tracking';
|
|
|
|
|
|
|
|
const CURRENCIES = currencies.map((currency) => {
|
|
|
|
return {
|
|
|
|
value: currency.isoCode.toLowerCase(),
|
|
|
|
label: `${currency.isoCode} - ${currency.name}`,
|
|
|
|
isoCode: currency.isoCode
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2023-03-09 17:09:39 +03:00
|
|
|
// Stripe has an upper amount limit of 999,999.99
|
|
|
|
// See https://stripe.com/docs/api/payment_intents/object#payment_intent_object-amount
|
|
|
|
const MAX_AMOUNT = 999_999.99;
|
|
|
|
|
2021-06-04 10:37:47 +03:00
|
|
|
// TODO: update modals to work fully with Glimmer components
|
|
|
|
@classic
|
2022-05-11 20:11:54 +03:00
|
|
|
export default class ModalTierPrice extends ModalBase {
|
2022-08-08 15:27:02 +03:00
|
|
|
@service feature;
|
2021-06-04 10:37:47 +03:00
|
|
|
@service settings;
|
2022-10-27 12:44:19 +03:00
|
|
|
@service membersUtils;
|
2022-11-03 14:14:36 +03:00
|
|
|
|
|
|
|
@inject config;
|
|
|
|
|
2021-06-04 10:37:47 +03:00
|
|
|
@tracked model;
|
2022-05-11 20:11:54 +03:00
|
|
|
@tracked tier;
|
2021-06-04 10:37:47 +03:00
|
|
|
@tracked periodVal;
|
|
|
|
@tracked stripeMonthlyAmount = 5;
|
|
|
|
@tracked stripeYearlyAmount = 50;
|
|
|
|
@tracked currency = 'usd';
|
|
|
|
@tracked stripePlanError = '';
|
2021-08-20 11:49:50 +03:00
|
|
|
@tracked benefits = emberA([]);
|
2021-06-21 11:36:17 +03:00
|
|
|
@tracked newBenefit = null;
|
2022-02-10 13:41:36 +03:00
|
|
|
@tracked welcomePageURL;
|
2022-04-01 14:22:41 +03:00
|
|
|
@tracked previewCadence = 'yearly';
|
|
|
|
@tracked discountValue = 0;
|
2022-05-16 21:51:49 +03:00
|
|
|
@tracked hasSaved = false;
|
2022-08-05 12:59:11 +03:00
|
|
|
@tracked freeTrialEnabled = false;
|
2022-05-16 21:51:49 +03:00
|
|
|
@tracked savedBenefits;
|
2022-04-01 14:22:41 +03:00
|
|
|
|
|
|
|
accentColorStyle = '';
|
2021-06-04 10:37:47 +03:00
|
|
|
|
|
|
|
confirm() {}
|
|
|
|
|
2022-05-11 20:11:54 +03:00
|
|
|
get isFreeTier() {
|
|
|
|
return this.tier.type === 'free';
|
2022-01-17 21:53:43 +03:00
|
|
|
}
|
|
|
|
|
2022-08-05 12:59:11 +03:00
|
|
|
get hasTrialDaysError() {
|
|
|
|
const trialDays = this.tier.get('trialDays');
|
|
|
|
return this.freeTrialEnabled && (!trialDays || trialDays < 1);
|
|
|
|
}
|
|
|
|
|
2021-06-04 10:37:47 +03:00
|
|
|
get allCurrencies() {
|
|
|
|
return getCurrencyOptions();
|
|
|
|
}
|
|
|
|
|
|
|
|
get selectedCurrency() {
|
|
|
|
return CURRENCIES.findBy('value', this.currency);
|
|
|
|
}
|
|
|
|
|
2022-08-12 11:51:45 +03:00
|
|
|
get isFreeTrialEnabled() {
|
2022-11-18 12:09:25 +03:00
|
|
|
return this.freeTrialEnabled && this.tier.get('trialDays') > 0;
|
2022-08-08 15:27:02 +03:00
|
|
|
}
|
|
|
|
|
2021-06-04 10:37:47 +03:00
|
|
|
init() {
|
|
|
|
super.init(...arguments);
|
2022-05-11 20:11:54 +03:00
|
|
|
this.tier = this.model.tier;
|
2022-05-16 21:51:49 +03:00
|
|
|
this.savedBenefits = this.model.tier?.get('benefits');
|
2022-05-11 20:11:54 +03:00
|
|
|
const monthlyPrice = this.tier.get('monthlyPrice');
|
|
|
|
const yearlyPrice = this.tier.get('yearlyPrice');
|
2021-06-04 10:37:47 +03:00
|
|
|
if (monthlyPrice) {
|
2022-05-16 21:51:49 +03:00
|
|
|
this.stripeMonthlyAmount = (monthlyPrice / 100);
|
2021-06-04 10:37:47 +03:00
|
|
|
}
|
|
|
|
if (yearlyPrice) {
|
2022-05-16 21:51:49 +03:00
|
|
|
this.stripeYearlyAmount = (yearlyPrice / 100);
|
2021-06-04 10:37:47 +03:00
|
|
|
}
|
2022-05-16 21:51:49 +03:00
|
|
|
this.currency = this.tier.get('currency') || 'usd';
|
2022-05-11 20:11:54 +03:00
|
|
|
this.benefits = this.tier.get('benefits') || emberA([]);
|
|
|
|
this.newBenefit = TierBenefitItem.create({
|
2021-06-18 16:39:02 +03:00
|
|
|
isNew: true,
|
2021-06-28 12:49:54 +03:00
|
|
|
name: ''
|
2021-06-18 16:39:02 +03:00
|
|
|
});
|
2022-04-01 14:22:41 +03:00
|
|
|
this.calculateDiscount();
|
2022-08-05 12:59:11 +03:00
|
|
|
if (this.tier.get('trialDays')) {
|
|
|
|
this.freeTrialEnabled = true;
|
|
|
|
}
|
2022-10-07 16:23:21 +03:00
|
|
|
this.accentColorStyle = htmlSafe(`color: ${this.settings.accentColor}`);
|
2021-06-04 10:37:47 +03:00
|
|
|
}
|
|
|
|
|
2022-01-27 14:10:53 +03:00
|
|
|
@action
|
|
|
|
validateWelcomePageURL() {
|
|
|
|
const siteUrl = this.siteUrl;
|
|
|
|
|
|
|
|
if (this.welcomePageURL === undefined) {
|
|
|
|
// Not initialised
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.welcomePageURL.href.startsWith(siteUrl)) {
|
|
|
|
const path = this.welcomePageURL.href.replace(siteUrl, '');
|
2022-05-11 20:11:54 +03:00
|
|
|
this.model.tier.welcomePageURL = path;
|
2022-01-27 14:10:53 +03:00
|
|
|
} else {
|
2022-05-11 20:11:54 +03:00
|
|
|
this.model.tier.welcomePageURL = this.welcomePageURL.href;
|
2022-01-27 14:10:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get siteUrl() {
|
2022-10-07 17:24:03 +03:00
|
|
|
return this.config.blogUrl;
|
2022-01-27 14:10:53 +03:00
|
|
|
}
|
|
|
|
|
2022-02-10 13:41:36 +03:00
|
|
|
// eslint-disable-next-line no-dupe-class-members
|
2022-01-27 14:10:53 +03:00
|
|
|
get welcomePageURL() {
|
2022-05-11 20:11:54 +03:00
|
|
|
return this.model.tier.welcomePageURL;
|
2022-01-27 14:10:53 +03:00
|
|
|
}
|
|
|
|
|
2021-06-04 10:37:47 +03:00
|
|
|
get title() {
|
2022-05-11 20:11:54 +03:00
|
|
|
if (this.isExistingTier) {
|
|
|
|
if (this.isFreeTier) {
|
2022-01-19 04:19:51 +03:00
|
|
|
return `Edit free membership`;
|
|
|
|
}
|
2021-07-19 13:38:51 +03:00
|
|
|
return `Edit tier`;
|
2021-06-04 10:37:47 +03:00
|
|
|
}
|
2021-07-19 13:38:51 +03:00
|
|
|
return 'New tier';
|
2021-06-04 10:37:47 +03:00
|
|
|
}
|
|
|
|
|
2022-05-11 20:11:54 +03:00
|
|
|
get isExistingTier() {
|
|
|
|
return !this.model.tier.isNew;
|
2021-06-04 10:37:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
close(event) {
|
2022-05-16 21:51:49 +03:00
|
|
|
if (!this.hasSaved) {
|
|
|
|
this.reset();
|
|
|
|
}
|
2021-06-04 10:37:47 +03:00
|
|
|
event?.preventDefault?.();
|
|
|
|
this.closeModal();
|
|
|
|
}
|
|
|
|
@action
|
|
|
|
setCurrency(event) {
|
|
|
|
const newCurrency = event.value;
|
|
|
|
this.currency = newCurrency;
|
|
|
|
}
|
2022-01-27 14:10:53 +03:00
|
|
|
@action
|
|
|
|
setWelcomePageURL(url) {
|
|
|
|
this.welcomePageURL = url;
|
|
|
|
}
|
2021-06-04 10:37:47 +03:00
|
|
|
|
2021-06-30 13:21:08 +03:00
|
|
|
reset() {
|
2022-05-11 20:11:54 +03:00
|
|
|
this.newBenefit = TierBenefitItem.create({isNew: true, name: ''});
|
2022-05-16 21:51:49 +03:00
|
|
|
const finalBenefits = this.savedBenefits || emberA([]);
|
|
|
|
this.tier.set('benefits', finalBenefits);
|
2022-08-05 16:04:19 +03:00
|
|
|
this.tier.rollbackAttributes();
|
2021-06-30 13:21:08 +03:00
|
|
|
}
|
|
|
|
|
2021-06-04 10:37:47 +03:00
|
|
|
@task({drop: true})
|
2022-05-11 20:11:54 +03:00
|
|
|
*saveTier() {
|
2021-06-04 10:37:47 +03:00
|
|
|
this.validatePrices();
|
2022-09-13 11:23:54 +03:00
|
|
|
|
2022-08-05 12:59:11 +03:00
|
|
|
if (this.stripePlanError || this.hasTrialDaysError) {
|
2021-06-04 10:37:47 +03:00
|
|
|
return;
|
|
|
|
}
|
2021-06-30 13:21:08 +03:00
|
|
|
|
|
|
|
if (!this.newBenefit.get('isBlank')) {
|
|
|
|
yield this.send('addBenefit', this.newBenefit);
|
|
|
|
}
|
|
|
|
|
2022-05-11 20:11:54 +03:00
|
|
|
if (!this.isFreeTier) {
|
2022-03-04 12:18:45 +03:00
|
|
|
const monthlyAmount = Math.round(this.stripeMonthlyAmount * 100);
|
|
|
|
const yearlyAmount = Math.round(this.stripeYearlyAmount * 100);
|
2022-05-16 21:51:49 +03:00
|
|
|
this.tier.set('monthlyPrice', monthlyAmount);
|
|
|
|
this.tier.set('yearlyPrice', yearlyAmount);
|
|
|
|
this.tier.set('currency', this.currency);
|
2022-01-17 21:53:43 +03:00
|
|
|
}
|
2022-08-05 12:59:11 +03:00
|
|
|
|
|
|
|
if (!this.freeTrialEnabled) {
|
|
|
|
this.tier.set('trialDays', 0);
|
|
|
|
}
|
|
|
|
|
2022-05-11 20:11:54 +03:00
|
|
|
this.tier.set('benefits', this.benefits.filter(benefit => !benefit.get('isBlank')));
|
2022-10-07 16:23:21 +03:00
|
|
|
|
2022-09-13 11:23:54 +03:00
|
|
|
try {
|
|
|
|
yield this.tier.save();
|
|
|
|
this.hasSaved = true;
|
|
|
|
yield this.confirm();
|
|
|
|
this.send('closeModal');
|
2022-10-27 12:44:19 +03:00
|
|
|
|
|
|
|
// Reload in the background (no await here)
|
|
|
|
this.membersUtils.reload();
|
2022-09-13 11:23:54 +03:00
|
|
|
} catch (error) {
|
|
|
|
if (error === undefined) {
|
|
|
|
// Validation error
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
2021-06-04 10:37:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
validatePrices() {
|
|
|
|
this.stripePlanError = undefined;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const yearlyAmount = this.stripeYearlyAmount;
|
|
|
|
const monthlyAmount = this.stripeMonthlyAmount;
|
|
|
|
const symbol = getSymbol(this.currency);
|
|
|
|
if (!yearlyAmount || yearlyAmount < 1 || !monthlyAmount || monthlyAmount < 1) {
|
|
|
|
throw new TypeError(`Subscription amount must be at least ${symbol}1.00`);
|
|
|
|
}
|
2023-03-09 17:09:39 +03:00
|
|
|
|
|
|
|
if (yearlyAmount > MAX_AMOUNT || monthlyAmount > MAX_AMOUNT) {
|
|
|
|
throw new TypeError(`Subscription amount cannot be higher than ${symbol}${MAX_AMOUNT}`);
|
|
|
|
}
|
2021-06-04 10:37:47 +03:00
|
|
|
} catch (err) {
|
|
|
|
this.stripePlanError = err.message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-21 11:36:17 +03:00
|
|
|
addNewBenefitItem(item) {
|
|
|
|
item.set('isNew', false);
|
|
|
|
this.benefits.pushObject(item);
|
|
|
|
|
2022-05-11 20:11:54 +03:00
|
|
|
this.newBenefit = TierBenefitItem.create({isNew: true, name: ''});
|
2021-06-21 11:36:17 +03:00
|
|
|
}
|
|
|
|
|
2022-04-01 14:22:41 +03:00
|
|
|
calculateDiscount() {
|
|
|
|
const discount = this.stripeMonthlyAmount ? 100 - Math.floor((this.stripeYearlyAmount / 12 * 100) / this.stripeMonthlyAmount) : 0;
|
|
|
|
this.discountValue = discount > 0 ? discount : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
changeCadence(cadence) {
|
|
|
|
this.previewCadence = cadence;
|
|
|
|
}
|
|
|
|
|
2022-08-05 12:59:11 +03:00
|
|
|
@action
|
|
|
|
setTrialDays(event) {
|
|
|
|
const value = parseInt(event.target.value);
|
|
|
|
this.tier.set('trialDays', value);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setFreeTrialEnabled(event) {
|
|
|
|
this.freeTrialEnabled = event.target.checked;
|
|
|
|
if (event.target.checked && !this.tier.get('trialDays')) {
|
|
|
|
this.tier.set('trialDays', 7);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-01 14:22:41 +03:00
|
|
|
@action
|
|
|
|
validateStripePlans() {
|
|
|
|
this.calculateDiscount();
|
|
|
|
this.stripePlanError = undefined;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const yearlyAmount = this.stripeYearlyAmount;
|
|
|
|
const monthlyAmount = this.stripeMonthlyAmount;
|
|
|
|
const symbol = getSymbol(this.currency);
|
|
|
|
if (!yearlyAmount || yearlyAmount < 1 || !monthlyAmount || monthlyAmount < 1) {
|
|
|
|
throw new TypeError(`Subscription amount must be at least ${symbol}1.00`);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
this.stripePlanError = err.message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-04 10:37:47 +03:00
|
|
|
actions = {
|
2021-06-21 11:36:17 +03:00
|
|
|
addBenefit(item) {
|
|
|
|
return item.validate().then(() => {
|
|
|
|
this.addNewBenefitItem(item);
|
2021-06-18 16:39:02 +03:00
|
|
|
});
|
|
|
|
},
|
2021-08-20 11:49:50 +03:00
|
|
|
focusItem() {
|
|
|
|
// Focus on next benefit on enter
|
|
|
|
},
|
2021-06-21 11:36:17 +03:00
|
|
|
deleteBenefit(item) {
|
|
|
|
if (!item) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.benefits.removeObject(item);
|
|
|
|
},
|
2021-06-30 13:21:08 +03:00
|
|
|
reorderItems() {
|
2022-05-11 20:11:54 +03:00
|
|
|
this.tier.set('benefits', this.benefits);
|
2021-06-30 13:21:08 +03:00
|
|
|
},
|
2021-06-21 11:36:17 +03:00
|
|
|
updateLabel(label, benefitItem) {
|
|
|
|
if (!benefitItem) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-28 12:49:54 +03:00
|
|
|
if (benefitItem.get('name') !== label) {
|
|
|
|
benefitItem.set('name', label);
|
2021-06-21 11:36:17 +03:00
|
|
|
}
|
2021-06-18 16:39:02 +03:00
|
|
|
},
|
2021-08-20 11:49:50 +03:00
|
|
|
// noop - we don't want the enter key doing anything
|
|
|
|
confirm() {},
|
2021-06-04 10:37:47 +03:00
|
|
|
setAmount(amount) {
|
|
|
|
this.price.amount = !isNaN(amount) ? parseInt(amount) : 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
setCurrency(event) {
|
|
|
|
const newCurrency = event.value;
|
|
|
|
this.currency = newCurrency;
|
|
|
|
},
|
2022-08-05 12:59:11 +03:00
|
|
|
|
2021-06-04 10:37:47 +03:00
|
|
|
// needed because ModalBase uses .send() for keyboard events
|
|
|
|
closeModal() {
|
|
|
|
this.close();
|
|
|
|
}
|
2022-02-10 13:41:36 +03:00
|
|
|
};
|
2023-05-15 11:47:07 +03:00
|
|
|
|
|
|
|
keyPress(event) {
|
|
|
|
// enter key
|
|
|
|
if (event.keyCode === 13) {
|
|
|
|
event.preventDefault();
|
|
|
|
run.scheduleOnce('actions', this, this.send, 'addBenefit', this.newBenefit);
|
|
|
|
}
|
|
|
|
}
|
2021-06-04 10:37:47 +03:00
|
|
|
}
|