2019-10-09 13:02:56 +03:00
|
|
|
import Component from '@ember/component';
|
|
|
|
import {computed} from '@ember/object';
|
2021-02-25 12:47:08 +03:00
|
|
|
import {currencies} from 'ghost-admin/utils/currency';
|
2020-01-16 20:01:12 +03:00
|
|
|
import {reads} from '@ember/object/computed';
|
2019-10-09 13:02:56 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2020-06-08 23:22:58 +03:00
|
|
|
import {task} from 'ember-concurrency';
|
2019-11-15 16:10:45 +03:00
|
|
|
|
2019-10-09 13:02:56 +03:00
|
|
|
export default Component.extend({
|
|
|
|
feature: service(),
|
|
|
|
config: service(),
|
|
|
|
mediaQueries: service(),
|
2020-06-02 14:58:43 +03:00
|
|
|
ghostPaths: service(),
|
2020-06-08 23:22:58 +03:00
|
|
|
ajax: service(),
|
2020-06-11 16:15:44 +03:00
|
|
|
settings: service(),
|
2019-10-09 13:02:56 +03:00
|
|
|
|
2021-02-25 14:09:06 +03:00
|
|
|
topCurrencies: null,
|
2020-03-04 08:31:39 +03:00
|
|
|
currencies: null,
|
2021-02-25 14:09:06 +03:00
|
|
|
allCurrencies: null,
|
2020-07-03 11:36:59 +03:00
|
|
|
stripePlanInvalidAmount: false,
|
|
|
|
_scratchStripeYearlyAmount: null,
|
|
|
|
_scratchStripeMonthlyAmount: null,
|
2020-07-30 13:56:07 +03:00
|
|
|
showLeaveSettingsModal: false,
|
2020-03-04 08:31:39 +03:00
|
|
|
|
|
|
|
// passed in actions
|
2020-06-02 14:58:43 +03:00
|
|
|
setStripeConnectIntegrationTokenSetting() {},
|
2020-03-04 08:31:39 +03:00
|
|
|
|
2020-01-16 20:01:12 +03:00
|
|
|
defaultContentVisibility: reads('settings.defaultContentVisibility'),
|
|
|
|
|
2020-05-20 17:24:43 +03:00
|
|
|
stripeDirect: reads('config.stripeDirect'),
|
|
|
|
|
2020-06-29 20:37:11 +03:00
|
|
|
allowSelfSignup: reads('settings.membersAllowFreeSignup'),
|
2021-02-25 12:47:08 +03:00
|
|
|
|
2020-06-29 20:37:11 +03:00
|
|
|
/** OLD **/
|
|
|
|
stripeDirectPublicKey: reads('settings.stripePublishableKey'),
|
|
|
|
stripeDirectSecretKey: reads('settings.stripeSecretKey'),
|
|
|
|
|
|
|
|
stripeConnectAccountId: reads('settings.stripeConnectAccountId'),
|
2020-06-30 12:50:43 +03:00
|
|
|
stripeConnectAccountName: reads('settings.stripeConnectDisplayName'),
|
2020-06-29 20:37:11 +03:00
|
|
|
stripeConnectLivemode: reads('settings.stripeConnectLivemode'),
|
|
|
|
|
2020-10-22 13:39:00 +03:00
|
|
|
portalSettingsBorderColor: reads('settings.accentColor'),
|
|
|
|
|
2020-06-29 20:37:11 +03:00
|
|
|
selectedCurrency: computed('stripePlans.monthly.currency', function () {
|
2021-02-25 12:47:08 +03:00
|
|
|
return this.get('currencies').findBy('value', this.get('stripePlans.monthly.currency'));
|
2020-03-04 08:31:39 +03:00
|
|
|
}),
|
|
|
|
|
2020-06-09 08:44:53 +03:00
|
|
|
blogDomain: computed('config.blogDomain', function () {
|
|
|
|
let blogDomain = this.config.blogDomain || '';
|
|
|
|
const domainExp = blogDomain.replace('https://', '').replace('http://', '').match(new RegExp('^([^/:?#]+)(?:[/:?#]|$)', 'i'));
|
|
|
|
return (domainExp && domainExp[1]) || '';
|
2020-06-08 23:22:58 +03:00
|
|
|
}),
|
|
|
|
|
2020-06-29 20:37:11 +03:00
|
|
|
stripePlans: computed('settings.stripePlans', function () {
|
|
|
|
const plans = this.settings.get('stripePlans');
|
|
|
|
const monthly = plans.find(plan => plan.interval === 'month');
|
|
|
|
const yearly = plans.find(plan => plan.interval === 'year' && plan.name !== 'Complimentary');
|
|
|
|
|
|
|
|
return {
|
|
|
|
monthly: {
|
2020-07-03 11:36:59 +03:00
|
|
|
amount: parseInt(monthly.amount) / 100 || 5,
|
2020-06-29 20:37:11 +03:00
|
|
|
currency: monthly.currency
|
|
|
|
},
|
|
|
|
yearly: {
|
2020-07-03 11:36:59 +03:00
|
|
|
amount: parseInt(yearly.amount) / 100 || 50,
|
2020-07-02 13:36:32 +03:00
|
|
|
currency: yearly.currency
|
2020-06-29 20:37:11 +03:00
|
|
|
}
|
2019-10-09 13:02:56 +03:00
|
|
|
};
|
|
|
|
}),
|
|
|
|
|
2019-11-15 16:10:45 +03:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
2021-02-25 14:09:06 +03:00
|
|
|
|
|
|
|
const noOfTopCurrencies = 5;
|
|
|
|
this.set('topCurrencies', currencies.slice(0, noOfTopCurrencies).map((currency) => {
|
|
|
|
return {
|
|
|
|
value: currency.isoCode.toLowerCase(),
|
|
|
|
label: `${currency.isoCode} - ${currency.name}`,
|
|
|
|
isoCode: currency.isoCode
|
|
|
|
};
|
|
|
|
}));
|
|
|
|
|
|
|
|
this.set('currencies', currencies.slice(noOfTopCurrencies, currencies.length).map((currency) => {
|
2021-02-25 12:47:08 +03:00
|
|
|
return {
|
|
|
|
value: currency.isoCode.toLowerCase(),
|
|
|
|
label: `${currency.isoCode} - ${currency.name}`,
|
|
|
|
isoCode: currency.isoCode
|
|
|
|
};
|
|
|
|
}));
|
2021-02-25 14:09:06 +03:00
|
|
|
|
|
|
|
this.set('allCurrencies', [
|
|
|
|
{
|
|
|
|
groupName: '—',
|
|
|
|
options: this.get('topCurrencies')
|
|
|
|
},
|
|
|
|
{
|
|
|
|
groupName: '—',
|
|
|
|
options: this.get('currencies')
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
|
2021-02-10 18:50:34 +03:00
|
|
|
if (this.get('stripeConnectAccountId')) {
|
|
|
|
this.set('membersStripeOpen', false);
|
|
|
|
} else {
|
|
|
|
this.set('membersStripeOpen', true);
|
|
|
|
}
|
2019-11-15 16:10:45 +03:00
|
|
|
},
|
|
|
|
|
2019-10-09 13:02:56 +03:00
|
|
|
actions: {
|
2020-09-24 09:46:18 +03:00
|
|
|
closePortalSettings() {
|
2020-07-30 13:56:07 +03:00
|
|
|
const changedAttributes = this.settings.changedAttributes();
|
|
|
|
if (changedAttributes && Object.keys(changedAttributes).length > 0) {
|
|
|
|
this.set('showLeaveSettingsModal', true);
|
|
|
|
} else {
|
2020-09-24 09:46:18 +03:00
|
|
|
this.set('showPortalSettings', false);
|
2020-07-30 13:56:07 +03:00
|
|
|
}
|
2020-06-19 19:06:49 +03:00
|
|
|
},
|
|
|
|
|
2019-10-09 13:02:56 +03:00
|
|
|
setDefaultContentVisibility(value) {
|
|
|
|
this.setDefaultContentVisibility(value);
|
|
|
|
},
|
2020-03-04 08:31:39 +03:00
|
|
|
|
2020-06-29 20:37:11 +03:00
|
|
|
toggleSelfSignup() {
|
|
|
|
this.set('settings.membersAllowFreeSignup', !this.get('allowSelfSignup'));
|
|
|
|
},
|
2020-03-10 10:15:48 +03:00
|
|
|
|
2020-06-29 20:37:11 +03:00
|
|
|
setStripeDirectPublicKey(event) {
|
|
|
|
this.set('settings.stripeProductName', this.get('settings.title'));
|
|
|
|
this.set('settings.stripePublishableKey', event.target.value);
|
|
|
|
},
|
2020-03-04 08:31:39 +03:00
|
|
|
|
2020-06-29 20:37:11 +03:00
|
|
|
setStripeDirectSecretKey(event) {
|
|
|
|
this.set('settings.stripeProductName', this.get('settings.title'));
|
|
|
|
this.set('settings.stripeSecretKey', event.target.value);
|
|
|
|
},
|
2020-03-04 08:31:39 +03:00
|
|
|
|
2020-07-03 11:36:59 +03:00
|
|
|
validateStripePlans() {
|
2021-02-25 12:47:08 +03:00
|
|
|
this.validateStripePlans();
|
2020-06-29 20:37:11 +03:00
|
|
|
},
|
2020-03-10 08:39:38 +03:00
|
|
|
|
2020-06-29 20:37:11 +03:00
|
|
|
setStripePlansCurrency(event) {
|
|
|
|
const newCurrency = event.value;
|
|
|
|
const updatedPlans = this.get('settings.stripePlans').map((plan) => {
|
|
|
|
if (plan.name !== 'Complimentary') {
|
|
|
|
return Object.assign({}, plan, {
|
|
|
|
currency: newCurrency
|
|
|
|
});
|
2020-03-04 08:31:39 +03:00
|
|
|
}
|
2020-06-29 20:37:11 +03:00
|
|
|
return plan;
|
|
|
|
});
|
2020-03-04 08:31:39 +03:00
|
|
|
|
2020-06-29 20:37:11 +03:00
|
|
|
const currentComplimentaryPlan = updatedPlans.find((plan) => {
|
|
|
|
return plan.name === 'Complimentary' && plan.currency === event.value;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!currentComplimentaryPlan) {
|
|
|
|
updatedPlans.push({
|
|
|
|
name: 'Complimentary',
|
|
|
|
currency: event.value,
|
|
|
|
interval: 'year',
|
|
|
|
amount: 0
|
|
|
|
});
|
2020-03-04 08:31:39 +03:00
|
|
|
}
|
|
|
|
|
2020-06-29 20:37:11 +03:00
|
|
|
this.set('settings.stripePlans', updatedPlans);
|
2021-02-25 12:47:08 +03:00
|
|
|
this.set('_scratchStripeYearlyAmount', null);
|
|
|
|
this.set('_scratchStripeMonthlyAmount', null);
|
|
|
|
this.validateStripePlans();
|
2020-06-02 14:58:43 +03:00
|
|
|
},
|
|
|
|
|
2020-06-29 20:37:11 +03:00
|
|
|
setStripeConnectIntegrationToken(event) {
|
|
|
|
this.set('settings.stripeProductName', this.get('settings.title'));
|
2020-06-02 14:58:43 +03:00
|
|
|
this.setStripeConnectIntegrationTokenSetting(event.target.value);
|
2020-06-23 14:29:12 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
openDisconnectStripeModal() {
|
|
|
|
this.openDisconnectStripeConnectModal.perform();
|
|
|
|
},
|
|
|
|
|
|
|
|
closeDisconnectStripeModal() {
|
|
|
|
this.set('showDisconnectStripeConnectModal', false);
|
|
|
|
},
|
|
|
|
|
|
|
|
disconnectStripeConnectIntegration() {
|
|
|
|
this.disconnectStripeConnectIntegration.perform();
|
2020-07-30 13:56:07 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
closeLeaveSettingsModal() {
|
|
|
|
this.set('showLeaveSettingsModal', false);
|
|
|
|
},
|
|
|
|
|
|
|
|
leavePortalSettings() {
|
|
|
|
this.settings.rollbackAttributes();
|
2020-09-24 09:46:18 +03:00
|
|
|
this.set('showPortalSettings', false);
|
2020-07-30 13:56:07 +03:00
|
|
|
this.set('showLeaveSettingsModal', false);
|
2020-10-22 13:39:00 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
openStripeSettings() {
|
|
|
|
this.set('membersStripeOpen', true);
|
2019-10-09 13:02:56 +03:00
|
|
|
}
|
2020-06-02 14:58:43 +03:00
|
|
|
},
|
|
|
|
|
2021-02-25 12:47:08 +03:00
|
|
|
validateStripePlans() {
|
|
|
|
this.get('settings.errors').remove('stripePlans');
|
|
|
|
this.get('settings.hasValidated').removeObject('stripePlans');
|
|
|
|
|
|
|
|
if (this._scratchStripeYearlyAmount === null) {
|
|
|
|
this._scratchStripeYearlyAmount = this.get('stripePlans').yearly.amount;
|
|
|
|
}
|
|
|
|
if (this._scratchStripeMonthlyAmount === null) {
|
|
|
|
this._scratchStripeMonthlyAmount = this.get('stripePlans').monthly.amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const selectedCurrency = this.selectedCurrency;
|
|
|
|
const yearlyAmount = parseInt(this._scratchStripeYearlyAmount);
|
|
|
|
const monthlyAmount = parseInt(this._scratchStripeMonthlyAmount);
|
|
|
|
if (!yearlyAmount || yearlyAmount < 1 || !monthlyAmount || monthlyAmount < 1) {
|
|
|
|
const minimum = Intl.NumberFormat(this.settings.get('lang'), {
|
|
|
|
currency: selectedCurrency.isoCode,
|
|
|
|
style: 'currency'
|
|
|
|
}).format(1);
|
|
|
|
|
|
|
|
throw new TypeError(`Subscription amount must be at least ${minimum}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const updatedPlans = this.get('settings.stripePlans').map((plan) => {
|
|
|
|
if (plan.name !== 'Complimentary') {
|
|
|
|
let newAmount;
|
|
|
|
if (plan.interval === 'year') {
|
|
|
|
newAmount = yearlyAmount * 100;
|
|
|
|
} else if (plan.interval === 'month') {
|
|
|
|
newAmount = monthlyAmount * 100;
|
|
|
|
}
|
|
|
|
return Object.assign({}, plan, {
|
|
|
|
amount: newAmount
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return plan;
|
|
|
|
});
|
|
|
|
|
|
|
|
this.set('settings.stripePlans', updatedPlans);
|
|
|
|
} catch (err) {
|
|
|
|
this.get('settings.errors').add('stripePlans', err.message);
|
|
|
|
} finally {
|
|
|
|
this.get('settings.hasValidated').pushObject('stripePlans');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-06-23 14:29:12 +03:00
|
|
|
openDisconnectStripeConnectModal: task(function* () {
|
|
|
|
this.set('hasActiveStripeSubscriptions', false);
|
2020-06-30 19:27:13 +03:00
|
|
|
if (!this.get('stripeConnectAccountId')) {
|
2020-06-23 14:29:12 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const url = this.get('ghostPaths.url').api('/members/hasActiveStripeSubscriptions');
|
|
|
|
const response = yield this.ajax.request(url);
|
|
|
|
|
|
|
|
if (response.hasActiveStripeSubscriptions) {
|
|
|
|
this.set('hasActiveStripeSubscriptions', true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.set('showDisconnectStripeConnectModal', true);
|
|
|
|
}).drop(),
|
|
|
|
|
|
|
|
disconnectStripeConnectIntegration: task(function* () {
|
|
|
|
this.set('disconnectStripeError', false);
|
|
|
|
const url = this.get('ghostPaths.url').api('/settings/stripe/connect');
|
|
|
|
|
|
|
|
yield this.ajax.delete(url);
|
|
|
|
yield this.settings.reload();
|
|
|
|
}),
|
|
|
|
|
2020-06-11 16:15:44 +03:00
|
|
|
saveStripeSettings: task(function* () {
|
|
|
|
this.set('stripeConnectError', null);
|
2020-06-11 17:55:26 +03:00
|
|
|
this.set('stripeConnectSuccess', null);
|
2020-06-11 16:15:44 +03:00
|
|
|
if (this.get('settings.stripeConnectIntegrationToken')) {
|
|
|
|
try {
|
2020-06-11 17:55:26 +03:00
|
|
|
const response = yield this.settings.save();
|
|
|
|
this.set('membersStripeOpen', false);
|
|
|
|
this.set('stripeConnectSuccess', true);
|
|
|
|
return response;
|
2020-06-11 16:15:44 +03:00
|
|
|
} catch (error) {
|
|
|
|
if (error.payload && error.payload.errors) {
|
|
|
|
this.set('stripeConnectError', 'Invalid secure key');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.set('stripeConnectError', 'Please enter a secure key');
|
|
|
|
}
|
|
|
|
}).drop(),
|
|
|
|
|
|
|
|
get liveStripeConnectAuthUrl() {
|
|
|
|
return this.ghostPaths.url.api('members/stripe_connect') + '?mode=live';
|
|
|
|
},
|
|
|
|
|
|
|
|
get testStripeConnectAuthUrl() {
|
|
|
|
return this.ghostPaths.url.api('members/stripe_connect') + '?mode=test';
|
2019-10-09 13:02:56 +03:00
|
|
|
}
|
|
|
|
});
|