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})
|
|
|
|
*saveProduct() {
|
|
|
|
let pollTimeout = 0;
|
|
|
|
while (pollTimeout < RETRY_PRODUCT_SAVE_MAX_POLL) {
|
|
|
|
yield timeout(RETRY_PRODUCT_SAVE_POLL_LENGTH);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const updatedProduct = yield this.product.save();
|
2021-08-23 09:53:34 +03:00
|
|
|
const existingPortalProducts = this.settings.get('portalProducts');
|
|
|
|
if (!existingPortalProducts?.length) {
|
|
|
|
this.settings.set('portalProducts', [updatedProduct.id]);
|
|
|
|
}
|
|
|
|
yield this.settings.save();
|
|
|
|
|
2021-05-24 11:32:43 +03:00
|
|
|
return updatedProduct;
|
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this.product;
|
|
|
|
}
|
|
|
|
|
2021-02-04 19:49:06 +03:00
|
|
|
@task({drop: true})
|
|
|
|
*openDisconnectStripeConnectModalTask() {
|
|
|
|
this.hasActiveStripeSubscriptions = false;
|
|
|
|
|
|
|
|
const url = this.ghostPaths.url.api('/members/hasActiveStripeSubscriptions');
|
|
|
|
const response = yield this.ajax.request(url);
|
|
|
|
|
|
|
|
if (response.hasActiveStripeSubscriptions) {
|
|
|
|
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-01-17 21:53:43 +03:00
|
|
|
const products = yield this.store.query('product', {filter: 'type:paid', include: 'monthly_price,yearly_price'});
|
2021-05-20 18:53:38 +03:00
|
|
|
this.product = products.firstObject;
|
2022-01-17 21:53:43 +03:00
|
|
|
|
2021-05-20 18:53:38 +03:00
|
|
|
if (this.product) {
|
|
|
|
const yearlyDiscount = this.calculateDiscount(5, 50);
|
2021-06-04 10:52:20 +03:00
|
|
|
this.product.set('monthlyPrice', {
|
|
|
|
nickname: 'Monthly',
|
|
|
|
amount: 500,
|
|
|
|
active: 1,
|
|
|
|
description: 'Full access',
|
|
|
|
currency: 'usd',
|
|
|
|
interval: 'month',
|
|
|
|
type: 'recurring'
|
|
|
|
});
|
|
|
|
this.product.set('yearlyPrice', {
|
|
|
|
nickname: 'Yearly',
|
|
|
|
amount: 5000,
|
|
|
|
active: 1,
|
|
|
|
currency: 'usd',
|
|
|
|
description: yearlyDiscount > 0 ? `${yearlyDiscount}% discount` : 'Full access',
|
|
|
|
interval: 'year',
|
|
|
|
type: 'recurring'
|
|
|
|
});
|
|
|
|
yield this.saveProduct.perform();
|
|
|
|
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
|
|
|
}
|