2021-01-21 12:20:06 +03:00
|
|
|
import Component from '@glimmer/component';
|
|
|
|
import {action} from '@ember/object';
|
|
|
|
import {inject as service} from '@ember/service';
|
2022-02-09 13:49:38 +03:00
|
|
|
import {task, timeout} from 'ember-concurrency';
|
2021-01-21 12:20:06 +03:00
|
|
|
import {tracked} from '@glimmer/tracking';
|
|
|
|
|
2021-05-24 11:32:43 +03:00
|
|
|
const RETRY_PRODUCT_SAVE_POLL_LENGTH = 1000;
|
|
|
|
const RETRY_PRODUCT_SAVE_MAX_POLL = 15 * RETRY_PRODUCT_SAVE_POLL_LENGTH;
|
|
|
|
|
2021-01-21 12:20:06 +03:00
|
|
|
export default class GhLaunchWizardConnectStripeComponent extends Component {
|
2021-02-04 19:49:06 +03:00
|
|
|
@service ajax;
|
2021-01-21 12:20:06 +03:00
|
|
|
@service config;
|
|
|
|
@service ghostPaths;
|
|
|
|
@service settings;
|
2021-05-20 18:53:38 +03:00
|
|
|
@service store;
|
2021-01-21 12:20:06 +03:00
|
|
|
|
2021-02-04 19:49:06 +03:00
|
|
|
@tracked hasActiveStripeSubscriptions = false;
|
|
|
|
@tracked showDisconnectStripeConnectModal = false;
|
2021-01-21 12:20:06 +03:00
|
|
|
@tracked stripeConnectTestMode = false;
|
|
|
|
@tracked stripeConnectError = null;
|
2021-02-03 19:45:09 +03:00
|
|
|
@tracked stripePublishableKeyError = null;
|
|
|
|
@tracked stripeSecretKeyError = null;
|
2021-01-21 12:20:06 +03:00
|
|
|
|
|
|
|
get stripeConnectAuthUrl() {
|
|
|
|
const mode = this.stripeConnectTestMode ? 'test' : 'live';
|
|
|
|
return `${this.ghostPaths.url.api('members/stripe_connect')}?mode=${mode}`;
|
|
|
|
}
|
|
|
|
|
2021-01-28 17:25:05 +03:00
|
|
|
constructor() {
|
|
|
|
super(...arguments);
|
|
|
|
this.args.updatePreview('');
|
|
|
|
}
|
|
|
|
|
2021-01-21 12:20:06 +03:00
|
|
|
willDestroy() {
|
2021-07-15 17:27:29 +03:00
|
|
|
super.willDestroy?.(...arguments);
|
2021-01-21 12:20:06 +03:00
|
|
|
// clear any unsaved settings changes when going back/forward/closing
|
|
|
|
this.settings.rollbackAttributes();
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setStripeDirectPublicKey(event) {
|
|
|
|
this.settings.set('stripeProductName', this.settings.get('title'));
|
|
|
|
this.settings.set('stripePublishableKey', event.target.value);
|
2021-02-03 19:45:09 +03:00
|
|
|
this.stripePublishableKeyError = null;
|
2021-01-21 12:20:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setStripeDirectSecretKey(event) {
|
|
|
|
this.settings.set('stripeProductName', this.settings.get('title'));
|
2021-02-03 19:45:09 +03:00
|
|
|
this.settings.set('stripeSecretKey', event.target.value);
|
|
|
|
this.stripeSecretKeyError = null;
|
2021-01-21 12:20:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
toggleStripeConnectTestMode() {
|
|
|
|
this.stripeConnectTestMode = !this.stripeConnectTestMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setStripeConnectIntegrationToken(event) {
|
|
|
|
this.settings.set('stripeProductName', this.settings.get('title'));
|
|
|
|
this.settings.set('stripeConnectIntegrationToken', event.target.value);
|
|
|
|
this.stripeConnectError = null;
|
|
|
|
}
|
|
|
|
|
2021-05-20 18:53:38 +03:00
|
|
|
calculateDiscount(monthly, yearly) {
|
|
|
|
if (isNaN(monthly) || isNaN(yearly)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return monthly ? 100 - Math.floor((yearly / 12 * 100) / monthly) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
getActivePrice(prices, interval, amount, currency) {
|
|
|
|
return prices.find((price) => {
|
|
|
|
return (
|
|
|
|
price.active && price.amount === amount && price.type === 'recurring' &&
|
|
|
|
price.interval === interval && price.currency.toLowerCase() === currency.toLowerCase()
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-05-24 11:32:43 +03:00
|
|
|
@task({drop: true})
|
2022-05-11 20:11:54 +03:00
|
|
|
*saveTier() {
|
2021-05-24 11:32:43 +03:00
|
|
|
let pollTimeout = 0;
|
|
|
|
while (pollTimeout < RETRY_PRODUCT_SAVE_MAX_POLL) {
|
|
|
|
yield timeout(RETRY_PRODUCT_SAVE_POLL_LENGTH);
|
|
|
|
|
|
|
|
try {
|
2022-05-11 20:11:54 +03:00
|
|
|
const updatedTier = yield this.tier.save();
|
2022-03-08 10:30:31 +03:00
|
|
|
|
2021-08-23 09:53:34 +03:00
|
|
|
yield this.settings.save();
|
|
|
|
|
2022-05-11 20:11:54 +03:00
|
|
|
return updatedTier;
|
2021-05-24 11:32:43 +03:00
|
|
|
} catch (error) {
|
|
|
|
if (error.payload?.errors && error.payload.errors[0].code === 'STRIPE_NOT_CONFIGURED') {
|
|
|
|
pollTimeout += RETRY_PRODUCT_SAVE_POLL_LENGTH;
|
|
|
|
// no-op: will try saving again as stripe is not ready
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-11 20:11:54 +03:00
|
|
|
return this.tier;
|
2021-05-24 11:32:43 +03:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:49:06 +03:00
|
|
|
@task({drop: true})
|
|
|
|
*openDisconnectStripeConnectModalTask() {
|
|
|
|
this.hasActiveStripeSubscriptions = false;
|
|
|
|
|
2022-05-04 19:24:56 +03:00
|
|
|
const url = this.ghostPaths.url.api('/members/') + '?filter=status:paid&limit=0';
|
2021-02-04 19:49:06 +03:00
|
|
|
const response = yield this.ajax.request(url);
|
|
|
|
|
2022-05-04 19:24:56 +03:00
|
|
|
if (response?.meta?.pagination?.total !== 0) {
|
2021-02-04 19:49:06 +03:00
|
|
|
this.hasActiveStripeSubscriptions = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.showDisconnectStripeConnectModal = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
closeDisconnectStripeModal() {
|
|
|
|
this.showDisconnectStripeConnectModal = false;
|
|
|
|
}
|
|
|
|
|
2021-01-21 12:20:06 +03:00
|
|
|
@task
|
2021-02-04 19:49:06 +03:00
|
|
|
*disconnectStripeConnectIntegrationTask() {
|
|
|
|
this.disconnectStripeError = false;
|
|
|
|
const url = this.ghostPaths.url.api('/settings/stripe/connect');
|
|
|
|
|
|
|
|
yield this.ajax.delete(url);
|
|
|
|
yield this.settings.reload();
|
|
|
|
}
|
|
|
|
|
|
|
|
@task
|
|
|
|
*saveAndContinueTask() {
|
2021-02-03 19:45:09 +03:00
|
|
|
if (this.config.get('stripeDirect')) {
|
|
|
|
if (!this.settings.get('stripePublishableKey')) {
|
|
|
|
this.stripePublishableKeyError = 'Enter your publishable key to continue';
|
|
|
|
}
|
2021-01-21 12:20:06 +03:00
|
|
|
|
2021-02-03 19:45:09 +03:00
|
|
|
if (!this.settings.get('stripeSecretKey')) {
|
|
|
|
this.stripeSecretKeyError = 'Enter your secret key to continue';
|
2021-01-21 12:20:06 +03:00
|
|
|
}
|
2021-02-03 19:45:09 +03:00
|
|
|
|
|
|
|
if (this.stripePublishableKeyError || this.stripeSecretKeyError) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-02-04 19:49:06 +03:00
|
|
|
} else if (!this.settings.get('stripeConnectAccountId') && !this.settings.get('stripeConnectIntegrationToken')) {
|
2021-01-28 17:25:05 +03:00
|
|
|
this.stripeConnectError = 'Paste your secure key to continue';
|
|
|
|
return false;
|
2021-01-21 12:20:06 +03:00
|
|
|
}
|
2021-02-03 19:45:09 +03:00
|
|
|
|
2021-02-04 19:49:06 +03:00
|
|
|
if (!this.config.get('stripeDirect') && this.settings.get('stripeConnectAccountId')) {
|
|
|
|
this.args.nextStep();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-02-03 19:45:09 +03:00
|
|
|
try {
|
|
|
|
yield this.settings.save();
|
2021-05-20 18:53:38 +03:00
|
|
|
|
2022-05-11 20:11:54 +03:00
|
|
|
const tiers = yield this.store.query('tier', {filter: 'type:paid', include: 'monthly_price,yearly_price'});
|
|
|
|
this.tier = tiers.firstObject;
|
2022-01-17 21:53:43 +03:00
|
|
|
|
2022-05-11 20:11:54 +03:00
|
|
|
if (this.tier) {
|
2021-05-20 18:53:38 +03:00
|
|
|
const yearlyDiscount = this.calculateDiscount(5, 50);
|
2022-05-11 20:11:54 +03:00
|
|
|
this.tier.set('monthlyPrice', {
|
2021-06-04 10:52:20 +03:00
|
|
|
nickname: 'Monthly',
|
|
|
|
amount: 500,
|
2022-05-12 17:51:10 +03:00
|
|
|
active: true,
|
2021-06-04 10:52:20 +03:00
|
|
|
description: 'Full access',
|
|
|
|
currency: 'usd',
|
|
|
|
interval: 'month',
|
|
|
|
type: 'recurring'
|
|
|
|
});
|
2022-05-11 20:11:54 +03:00
|
|
|
this.tier.set('yearlyPrice', {
|
2021-06-04 10:52:20 +03:00
|
|
|
nickname: 'Yearly',
|
|
|
|
amount: 5000,
|
2022-05-12 17:51:10 +03:00
|
|
|
active: true,
|
2021-06-04 10:52:20 +03:00
|
|
|
currency: 'usd',
|
|
|
|
description: yearlyDiscount > 0 ? `${yearlyDiscount}% discount` : 'Full access',
|
|
|
|
interval: 'year',
|
|
|
|
type: 'recurring'
|
|
|
|
});
|
2022-05-11 20:11:54 +03:00
|
|
|
yield this.saveTier.perform();
|
2021-06-04 10:52:20 +03:00
|
|
|
this.settings.set('portalPlans', ['free', 'monthly', 'yearly']);
|
2021-05-20 18:53:38 +03:00
|
|
|
yield this.settings.save();
|
|
|
|
}
|
|
|
|
|
2021-02-04 19:49:06 +03:00
|
|
|
this.pauseAndContinueTask.perform();
|
2021-02-03 19:45:09 +03:00
|
|
|
return true;
|
|
|
|
} catch (error) {
|
|
|
|
if (error.payload?.errors && error.payload.errors[0].type === 'ValidationError') {
|
|
|
|
const [validationError] = error.payload.errors;
|
|
|
|
|
|
|
|
if (this.config.get('stripeDirect')) {
|
|
|
|
if (validationError.context.match(/stripe_publishable_key/)) {
|
|
|
|
this.stripePublishableKeyError = 'Invalid publishable key';
|
|
|
|
} else {
|
|
|
|
this.stripeSecretKeyError = 'Invalid secret key';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.stripeConnectError = 'Invalid secure key';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
2021-01-21 12:20:06 +03:00
|
|
|
}
|
2021-01-28 18:54:44 +03:00
|
|
|
|
|
|
|
@task
|
2021-02-04 19:49:06 +03:00
|
|
|
*pauseAndContinueTask() {
|
2021-02-09 18:53:26 +03:00
|
|
|
this.args.refreshPreview();
|
2021-01-28 18:54:44 +03:00
|
|
|
yield timeout(500);
|
|
|
|
this.args.nextStep();
|
|
|
|
}
|
2021-01-21 12:20:06 +03:00
|
|
|
}
|