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';
|
2019-11-15 16:10:45 +03:00
|
|
|
import {set} from '@ember/object';
|
|
|
|
|
|
|
|
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 = [
|
|
|
|
{
|
|
|
|
label: 'USD - US Dollar', value: 'usd'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'AUD - Australian Dollar', value: 'aud'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'CAD - Canadian Dollar', value: 'cad'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'EUR - Euro', value: 'eur'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'GBP - British Pound', value: 'gbp'
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2019-10-09 13:02:56 +03:00
|
|
|
export default Component.extend({
|
|
|
|
feature: service(),
|
|
|
|
config: service(),
|
|
|
|
mediaQueries: service(),
|
|
|
|
|
2020-03-04 08:31:39 +03:00
|
|
|
currencies: null,
|
|
|
|
|
|
|
|
// passed in actions
|
|
|
|
setMembersSubscriptionSettings() {},
|
|
|
|
|
2020-01-16 20:01:12 +03:00
|
|
|
defaultContentVisibility: reads('settings.defaultContentVisibility'),
|
|
|
|
|
2020-03-04 08:31:39 +03:00
|
|
|
selectedCurrency: computed('subscriptionSettings.stripeConfig.plans.monthly.currency', function () {
|
|
|
|
return CURRENCIES.findBy('value', this.get('subscriptionSettings.stripeConfig.plans.monthly.currency'));
|
|
|
|
}),
|
|
|
|
|
2019-11-15 16:10:45 +03:00
|
|
|
mailgunRegion: computed('settings.bulkEmailSettings.baseUrl', function () {
|
|
|
|
if (!this.settings.get('bulkEmailSettings.baseUrl')) {
|
|
|
|
return US;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [US, EU].find((region) => {
|
|
|
|
return region.baseUrl === this.settings.get('bulkEmailSettings.baseUrl');
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
|
2019-11-07 13:46:02 +03:00
|
|
|
blogDomain: computed('config.blogDomain', function () {
|
|
|
|
let domain = this.config.blogDomain || '';
|
|
|
|
const host = domain.replace('https://', '').replace('http://', '').split('/');
|
|
|
|
return (host && host[0]) || '';
|
|
|
|
}),
|
|
|
|
|
2019-10-09 13:02:56 +03:00
|
|
|
subscriptionSettings: computed('settings.membersSubscriptionSettings', function () {
|
2020-02-24 12:08:47 +03:00
|
|
|
let subscriptionSettings = this.settings.parseSubscriptionSettings(this.get('settings.membersSubscriptionSettings'));
|
2019-10-09 13:02:56 +03:00
|
|
|
let stripeProcessor = subscriptionSettings.paymentProcessors.find((proc) => {
|
|
|
|
return (proc.adapter === 'stripe');
|
|
|
|
});
|
|
|
|
let monthlyPlan = stripeProcessor.config.plans.find(plan => plan.interval === 'month');
|
|
|
|
let yearlyPlan = stripeProcessor.config.plans.find(plan => plan.interval === 'year');
|
2020-03-04 08:31:39 +03:00
|
|
|
|
|
|
|
// NOTE: need to be careful about division by zero if we introduce zero decimal currencies
|
|
|
|
// ref.: https://stripe.com/docs/currencies#zero-decimal
|
|
|
|
monthlyPlan.amount = parseInt(monthlyPlan.amount) ? (monthlyPlan.amount / 100) : 0;
|
|
|
|
yearlyPlan.amount = parseInt(yearlyPlan.amount) ? (yearlyPlan.amount / 100) : 0;
|
|
|
|
|
2019-10-09 13:02:56 +03:00
|
|
|
stripeProcessor.config.plans = {
|
|
|
|
monthly: monthlyPlan,
|
|
|
|
yearly: yearlyPlan
|
|
|
|
};
|
|
|
|
subscriptionSettings.stripeConfig = stripeProcessor.config;
|
2019-10-11 11:34:39 +03:00
|
|
|
subscriptionSettings.allowSelfSignup = !!subscriptionSettings.allowSelfSignup;
|
2019-10-21 10:10:42 +03:00
|
|
|
subscriptionSettings.fromAddress = subscriptionSettings.fromAddress || '';
|
2019-10-11 11:20:30 +03:00
|
|
|
|
2019-10-09 13:02:56 +03:00
|
|
|
return subscriptionSettings;
|
|
|
|
}),
|
|
|
|
|
2019-11-13 07:01:31 +03:00
|
|
|
bulkEmailSettings: computed('settings.bulkEmailSettings', function () {
|
|
|
|
let bulkEmailSettings = this.get('settings.bulkEmailSettings') || {};
|
2019-11-15 16:10:45 +03:00
|
|
|
const {apiKey = '', baseUrl = US.baseUrl, domain = ''} = bulkEmailSettings;
|
2019-11-13 07:01:31 +03:00
|
|
|
return {apiKey, baseUrl, domain};
|
|
|
|
}),
|
|
|
|
|
|
|
|
hasBulkEmailConfig: computed('settings.bulkEmailSettings', function () {
|
|
|
|
let bulkEmailSettings = this.get('settings.bulkEmailSettings');
|
|
|
|
return !!bulkEmailSettings.isConfig;
|
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: {
|
|
|
|
setDefaultContentVisibility(value) {
|
|
|
|
this.setDefaultContentVisibility(value);
|
|
|
|
},
|
2020-03-04 08:31:39 +03:00
|
|
|
|
2019-11-13 07:01:31 +03:00
|
|
|
setBulkEmailSettings(key, event) {
|
|
|
|
let bulkEmailSettings = this.get('settings.bulkEmailSettings') || {};
|
|
|
|
bulkEmailSettings[key] = event.target.value;
|
2019-11-15 16:10:45 +03:00
|
|
|
if (!bulkEmailSettings.baseUrl) {
|
|
|
|
set(bulkEmailSettings, 'baseUrl', US.baseUrl);
|
|
|
|
}
|
|
|
|
this.setBulkEmailSettings(bulkEmailSettings);
|
|
|
|
},
|
2020-03-04 08:31:39 +03:00
|
|
|
|
2019-11-15 16:10:45 +03:00
|
|
|
setBulkEmailRegion(region) {
|
|
|
|
let bulkEmailSettings = this.get('settings.bulkEmailSettings') || {};
|
|
|
|
set(bulkEmailSettings, 'baseUrl', region.baseUrl);
|
2019-11-13 07:01:31 +03:00
|
|
|
this.setBulkEmailSettings(bulkEmailSettings);
|
|
|
|
},
|
2020-03-04 08:31:39 +03:00
|
|
|
|
2019-10-09 13:02:56 +03:00
|
|
|
setSubscriptionSettings(key, event) {
|
2020-02-24 12:08:47 +03:00
|
|
|
let subscriptionSettings = this.settings.parseSubscriptionSettings(this.get('settings.membersSubscriptionSettings'));
|
2019-10-09 13:02:56 +03:00
|
|
|
let stripeProcessor = subscriptionSettings.paymentProcessors.find((proc) => {
|
|
|
|
return (proc.adapter === 'stripe');
|
|
|
|
});
|
|
|
|
let stripeConfig = stripeProcessor.config;
|
|
|
|
stripeConfig.product = {
|
|
|
|
name: this.settings.get('title')
|
|
|
|
};
|
|
|
|
// TODO: this flag has to be removed as it doesn't serve any purpose
|
|
|
|
if (key === 'isPaid') {
|
|
|
|
subscriptionSettings.isPaid = event;
|
|
|
|
}
|
|
|
|
if (key === 'secret_token' || key === 'public_token') {
|
|
|
|
stripeConfig[key] = event.target.value;
|
|
|
|
}
|
|
|
|
if (key === 'month' || key === 'year') {
|
|
|
|
stripeConfig.plans = stripeConfig.plans.map((plan) => {
|
|
|
|
if (key === plan.interval) {
|
|
|
|
plan.amount = parseInt(event.target.value) ? (event.target.value * 100) : 0;
|
|
|
|
}
|
|
|
|
return plan;
|
|
|
|
});
|
|
|
|
}
|
2019-10-11 11:34:39 +03:00
|
|
|
if (key === 'allowSelfSignup') {
|
|
|
|
subscriptionSettings.allowSelfSignup = !subscriptionSettings.allowSelfSignup;
|
2019-10-09 13:02:56 +03:00
|
|
|
}
|
|
|
|
if (key === 'fromAddress') {
|
|
|
|
subscriptionSettings.fromAddress = event.target.value;
|
|
|
|
}
|
2020-03-04 08:31:39 +03:00
|
|
|
|
|
|
|
if (key === 'currency') {
|
|
|
|
stripeProcessor.config.plans.forEach((plan) => {
|
|
|
|
if (plan.name !== 'Complimentary') {
|
|
|
|
plan.currency = event.value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// NOTE: need to keep Complimentary plans with all available currencies so they don't conflict
|
|
|
|
// when applied to members with existing subscriptions in different currencies (ref. https://stripe.com/docs/billing/customer#currency)
|
|
|
|
let currentCurrencyComplimentary = stripeProcessor.config.plans.filter(plan => (plan.currency === event.value && plan.name === 'Complimentary'));
|
|
|
|
|
|
|
|
if (!currentCurrencyComplimentary.length) {
|
2020-03-10 08:39:38 +03:00
|
|
|
let complimentary = {
|
|
|
|
name: 'Complimentary',
|
|
|
|
currency: event.value,
|
|
|
|
interval: 'year',
|
|
|
|
amount: '0'
|
|
|
|
};
|
|
|
|
|
|
|
|
stripeProcessor.config.plans.push(complimentary);
|
2020-03-04 08:31:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
stripeProcessor.config.currency = event.value;
|
|
|
|
}
|
|
|
|
|
2019-10-09 13:02:56 +03:00
|
|
|
this.setMembersSubscriptionSettings(subscriptionSettings);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|