Ghost/ghost/admin/app/components/gh-billing-iframe.js

143 lines
5.2 KiB
JavaScript
Raw Normal View History

import Component from '@glimmer/component';
import {action} from '@ember/object';
import {htmlSafe} from '@ember/template';
Fixed hosting management screen not loading after sign-in process (#15763) refs https://github.com/TryGhost/Team/issues/2110 - dynamically defined properties on the config service did not have autotracking set up properly if they were accessed in any way before the property was defined, this caused problems in a number of areas because we have both "unauthed" and "authed" sets of config and when not logged in we had parts of the app checking for authed config properties that don't exist until after sign-in and subsequent config re-fetch - renamed `config` service to `configManager` and updated to only contain methods for fetching config data - added a `config` instance initializer that sets up a `TrackedObject` instance with some custom properties/methods and registers it on `config:main` - uses application instance initializer rather than a standard initializer because standard initializers are only called once when setting up the test suite so we'd end up with config leaking across tests - added an `@inject` decorator that when used takes the property name and injects whatever is registered at `${propertyName}:main`, this allows us to use dependency injection for any object rather than just services or controllers - using `application.inject()` in the initializer was initially used but that only works for objects that extend from `EmberObject`, the injections weren't available in native-class glimmer components so this decorator keeps the injection syntax consistent - swapped all `@service config` uses to `@inject config`
2022-11-03 14:14:36 +03:00
import {inject} from 'ghost-admin/decorators/inject';
import {inject as service} from '@ember/service';
import {tracked} from '@glimmer/tracking';
export default class GhBillingIframe extends Component {
@service ajax;
@service billing;
@service ghostPaths;
@service notifications;
@service session;
Fixed hosting management screen not loading after sign-in process (#15763) refs https://github.com/TryGhost/Team/issues/2110 - dynamically defined properties on the config service did not have autotracking set up properly if they were accessed in any way before the property was defined, this caused problems in a number of areas because we have both "unauthed" and "authed" sets of config and when not logged in we had parts of the app checking for authed config properties that don't exist until after sign-in and subsequent config re-fetch - renamed `config` service to `configManager` and updated to only contain methods for fetching config data - added a `config` instance initializer that sets up a `TrackedObject` instance with some custom properties/methods and registers it on `config:main` - uses application instance initializer rather than a standard initializer because standard initializers are only called once when setting up the test suite so we'd end up with config leaking across tests - added an `@inject` decorator that when used takes the property name and injects whatever is registered at `${propertyName}:main`, this allows us to use dependency injection for any object rather than just services or controllers - using `application.inject()` in the initializer was initially used but that only works for objects that extend from `EmberObject`, the injections weren't available in native-class glimmer components so this decorator keeps the injection syntax consistent - swapped all `@service config` uses to `@inject config`
2022-11-03 14:14:36 +03:00
@inject config;
@tracked isOwner = null;
willDestroy() {
super.willDestroy(...arguments);
window.removeEventListener('message', this.handleIframeMessage);
}
@action
setup() {
this.billing.getBillingIframe().src = this.billing.getIframeURL();
window.addEventListener('message', this.handleIframeMessage);
}
@action
async handleIframeMessage(event) {
if (this.isDestroyed || this.isDestroying) {
return;
}
// only process messages coming from the billing iframe
if (event?.data && this.billing.getIframeURL().includes(event?.origin)) {
if (event.data?.request === 'token') {
this._handleTokenRequest();
}
if (event.data?.request === 'forceUpgradeInfo') {
this._handleForceUpgradeRequest();
}
if (event.data?.subscription) {
this._handleSubscriptionUpdate(event.data);
2021-10-26 15:06:15 +03:00
}
}
}
2021-10-26 15:06:15 +03:00
_handleTokenRequest() {
const handleNoPermission = () => {
// no permission means the current user requesting the token is not the owner of the site.
this.isOwner = false;
// Avoid letting the BMA waiting for a message and send an empty token response instead
this.billing.getBillingIframe().contentWindow.postMessage({
request: 'token',
response: null
}, '*');
};
2021-10-26 15:06:15 +03:00
if (!this.session.user?.isOwnerOnly) {
handleNoPermission();
return;
}
const ghostIdentityUrl = this.ghostPaths.url.api('identities');
2021-10-26 15:06:15 +03:00
this.ajax.request(ghostIdentityUrl).then((response) => {
const token = response?.identities?.[0]?.token;
2021-10-26 15:06:15 +03:00
this.billing.getBillingIframe().contentWindow.postMessage({
request: 'token',
response: token
}, '*');
this.isOwner = true;
2021-10-26 15:06:15 +03:00
}).catch((error) => {
if (error.payload?.errors?.[0]?.type === 'NoPermissionError') {
handleNoPermission();
2021-10-26 15:06:15 +03:00
} else {
throw error;
}
2021-10-26 15:06:15 +03:00
});
}
2021-10-26 15:06:15 +03:00
_handleForceUpgradeRequest() {
// Send BMA requested information about forceUpgrade and owner name/email
let ownerUser = null;
const owner = this.billing.ownerUser;
if (owner) {
ownerUser = {
name: owner?.name,
email: owner?.email
};
}
this.billing.getBillingIframe().contentWindow.postMessage({
request: 'forceUpgradeInfo',
response: {
forceUpgrade: this.config.hostSettings?.forceUpgrade,
2021-10-26 15:06:15 +03:00
isOwner: this.isOwner,
ownerUser
}
2021-10-26 15:06:15 +03:00
}, '*');
}
2021-10-26 15:06:15 +03:00
_handleSubscriptionUpdate(data) {
this.billing.subscription = data.subscription;
this.billing.checkoutRoute = data?.checkoutRoute ?? '/plans';
2021-10-26 15:06:15 +03:00
if (data.subscription.status === 'active' && this.config.hostSettings?.forceUpgrade) {
// config might not be updated after a subscription has been set to active.
// Until then assume the forceUpgrade is over and the subscription
// was activated successfully.
this.config.hostSettings.forceUpgrade = false;
}
2021-10-26 15:06:15 +03:00
// Detect if the current subscription is in a grace state and render a notification
if (data.subscription.status === 'past_due' || data.subscription.status === 'unpaid') {
// This notification needs to be shown to every user regardless their permissions to see billing
this.notifications.showAlert('Billing error: This site is queued for suspension. The owner of this site must update payment information.', {type: 'error', key: 'billing.overdue'});
} else {
this.notifications.closeAlerts('billing.overdue');
}
// Detect if the current member limits are exceeded and render a notification
if (
data?.exceededLimits
&& data?.exceededLimits.length
&& data?.exceededLimits.indexOf('members') >= 0
&& data?.checkoutRoute
) {
// The action param will be picked up on a transition from the router and can
// then send the destination route as a message to the BMA, which then handles the redirect.
const checkoutAction = this.billing.billingRouteRoot + '?action=checkout';
2021-10-26 15:06:15 +03:00
this.notifications.showAlert(htmlSafe(`Your audience has grown! To continue publishing, the site owner must confirm pricing for this number of members <a href="${checkoutAction}">here</a>`), {type: 'warn', key: 'billing.exceeded'});
} else {
this.notifications.closeAlerts('billing.exceeded');
}
}
}