2019-10-09 13:02:56 +03:00
|
|
|
import Component from '@ember/component';
|
|
|
|
import {computed} from '@ember/object';
|
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
|
|
|
|
|
|
|
const US = {flag: '🇺🇸', name: 'US', baseUrl: 'https://api.mailgun.net/v3'};
|
|
|
|
const EU = {flag: '🇪🇺', name: 'EU', baseUrl: 'https://api.eu.mailgun.net/v3'};
|
2019-10-09 13:02:56 +03:00
|
|
|
|
2020-03-04 08:31:39 +03:00
|
|
|
const CURRENCIES = [
|
|
|
|
{
|
2020-07-03 11:36:59 +03:00
|
|
|
label: 'USD - US Dollar', value: 'usd', symbol: '$'
|
2020-03-04 08:31:39 +03:00
|
|
|
},
|
|
|
|
{
|
2020-07-03 11:36:59 +03:00
|
|
|
label: 'AUD - Australian Dollar', value: 'aud', symbol: '$'
|
2020-03-04 08:31:39 +03:00
|
|
|
},
|
|
|
|
{
|
2020-07-03 11:36:59 +03:00
|
|
|
label: 'CAD - Canadian Dollar', value: 'cad', symbol: '$'
|
2020-03-04 08:31:39 +03:00
|
|
|
},
|
|
|
|
{
|
2020-07-03 11:36:59 +03:00
|
|
|
label: 'EUR - Euro', value: 'eur', symbol: '€'
|
2020-03-04 08:31:39 +03:00
|
|
|
},
|
|
|
|
{
|
2020-07-03 11:36:59 +03:00
|
|
|
label: 'GBP - British Pound', value: 'gbp', symbol: '£'
|
2020-06-08 13:30:05 +03:00
|
|
|
},
|
|
|
|
{
|
2020-07-03 11:36:59 +03:00
|
|
|
label: 'INR - Indian Rupee', value: 'inr', symbol: '₹'
|
2020-03-04 08:31:39 +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
|
|
|
|
2020-03-04 08:31:39 +03:00
|
|
|
currencies: null,
|
2020-06-08 23:22:58 +03:00
|
|
|
showFromAddressConfirmation: false,
|
2020-06-19 19:06:49 +03:00
|
|
|
showMembersModalSettings: false,
|
2020-07-03 11:36:59 +03:00
|
|
|
stripePlanInvalidAmount: false,
|
|
|
|
_scratchStripeYearlyAmount: null,
|
|
|
|
_scratchStripeMonthlyAmount: null,
|
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-07-03 12:48:54 +03:00
|
|
|
mailgunIsConfigured: reads('config.mailgunIsConfigured'),
|
|
|
|
|
2020-06-29 20:37:11 +03:00
|
|
|
allowSelfSignup: reads('settings.membersAllowFreeSignup'),
|
|
|
|
|
|
|
|
/** 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'),
|
|
|
|
|
|
|
|
selectedCurrency: computed('stripePlans.monthly.currency', function () {
|
|
|
|
return CURRENCIES.findBy('value', this.get('stripePlans.monthly.currency'));
|
2020-03-04 08:31:39 +03:00
|
|
|
}),
|
|
|
|
|
2020-06-09 08:44:53 +03:00
|
|
|
disableUpdateFromAddressButton: computed('fromAddress', function () {
|
2020-06-29 20:37:11 +03:00
|
|
|
const savedFromAddress = this.get('settings.membersFromAddress') || '';
|
2020-06-30 09:46:48 +03:00
|
|
|
if (!savedFromAddress.includes('@') && this.blogDomain) {
|
2020-06-09 08:44:53 +03:00
|
|
|
return (this.fromAddress === `${savedFromAddress}@${this.blogDomain}`);
|
|
|
|
}
|
|
|
|
return (this.fromAddress === savedFromAddress);
|
|
|
|
}),
|
|
|
|
|
|
|
|
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-07-03 12:48:54 +03:00
|
|
|
mailgunRegion: computed('settings.mailgunBaseUrl', function () {
|
|
|
|
if (!this.settings.get('mailgunBaseUrl')) {
|
2019-11-15 16:10:45 +03:00
|
|
|
return US;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [US, EU].find((region) => {
|
2020-07-03 12:48:54 +03:00
|
|
|
return region.baseUrl === this.settings.get('mailgunBaseUrl');
|
2019-11-15 16:10:45 +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
|
|
|
};
|
|
|
|
}),
|
|
|
|
|
2020-07-03 12:48:54 +03:00
|
|
|
mailgunSettings: computed('settings.{mailgunBaseUrl,mailgunApiKey,mailgunDomain}', function () {
|
|
|
|
return {
|
|
|
|
apiKey: this.get('settings.mailgunApiKey') || '',
|
|
|
|
domain: this.get('settings.mailgunDomain') || '',
|
|
|
|
baseUrl: this.get('settings.mailgunBaseUrl') || ''
|
|
|
|
};
|
2019-11-05 14:23:08 +03:00
|
|
|
}),
|
|
|
|
|
2019-11-15 16:10:45 +03:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.set('mailgunRegions', [US, EU]);
|
2020-03-04 08:31:39 +03:00
|
|
|
this.set('currencies', CURRENCIES);
|
2019-11-15 16:10:45 +03:00
|
|
|
},
|
|
|
|
|
2019-10-09 13:02:56 +03:00
|
|
|
actions: {
|
2020-06-08 23:22:58 +03:00
|
|
|
toggleFromAddressConfirmation() {
|
|
|
|
this.toggleProperty('showFromAddressConfirmation');
|
|
|
|
},
|
|
|
|
|
2020-06-19 19:06:49 +03:00
|
|
|
closeMembersModalSettings() {
|
|
|
|
this.set('showMembersModalSettings', false);
|
|
|
|
},
|
|
|
|
|
2019-10-09 13:02:56 +03:00
|
|
|
setDefaultContentVisibility(value) {
|
|
|
|
this.setDefaultContentVisibility(value);
|
|
|
|
},
|
2020-03-04 08:31:39 +03:00
|
|
|
|
2020-07-03 12:48:54 +03:00
|
|
|
setMailgunDomain(event) {
|
|
|
|
this.set('settings.mailgunDomain', event.target.value);
|
2020-07-16 12:13:36 +03:00
|
|
|
if (!this.get('settings.mailgunBaseUrl')) {
|
|
|
|
this.set('settings.mailgunBaseUrl', this.mailgunRegion.baseUrl);
|
|
|
|
}
|
2020-07-03 12:48:54 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
setMailgunApiKey(event) {
|
|
|
|
this.set('settings.mailgunApiKey', event.target.value);
|
2020-07-16 12:13:36 +03:00
|
|
|
if (!this.get('settings.mailgunBaseUrl')) {
|
|
|
|
this.set('settings.mailgunBaseUrl', this.mailgunRegion.baseUrl);
|
|
|
|
}
|
2019-11-15 16:10:45 +03:00
|
|
|
},
|
2020-03-04 08:31:39 +03:00
|
|
|
|
2020-07-03 12:48:54 +03:00
|
|
|
setMailgunRegion(region) {
|
|
|
|
this.set('settings.mailgunBaseUrl', region.baseUrl);
|
2019-11-13 07:01:31 +03:00
|
|
|
},
|
2020-03-04 08:31:39 +03:00
|
|
|
|
2020-06-09 08:44:53 +03:00
|
|
|
setFromAddress(fromAddress) {
|
|
|
|
this.setFromAddress(fromAddress);
|
|
|
|
},
|
|
|
|
|
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() {
|
|
|
|
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) {
|
|
|
|
throw new TypeError(`Subscription amount must be at least ${selectedCurrency.symbol}1.00`);
|
2020-06-29 20:37:11 +03:00
|
|
|
}
|
2020-03-04 08:31:39 +03:00
|
|
|
|
2020-07-03 11:36:59 +03:00
|
|
|
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-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);
|
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();
|
2019-10-09 13:02:56 +03:00
|
|
|
}
|
2020-06-02 14:58:43 +03:00
|
|
|
},
|
|
|
|
|
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(),
|
|
|
|
|
2020-06-08 23:22:58 +03:00
|
|
|
updateFromAddress: task(function* () {
|
|
|
|
let url = this.get('ghostPaths.url').api('/settings/members/email');
|
|
|
|
try {
|
|
|
|
const response = yield this.ajax.post(url, {
|
|
|
|
data: {
|
2020-06-09 09:22:09 +03:00
|
|
|
from_address: this.fromAddress
|
2020-06-08 23:22:58 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
this.toggleProperty('showFromAddressConfirmation');
|
|
|
|
return response;
|
|
|
|
} catch (e) {
|
|
|
|
// Failed to send email, retry
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}).drop(),
|
|
|
|
|
2020-06-11 16:15:44 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
});
|