Restructured gh-billing-iframe

This commit is contained in:
Aileen Nowak 2021-10-26 14:06:15 +02:00
parent caea9ec656
commit 8f8809996c

View File

@ -10,17 +10,33 @@ export default Component.extend({
notifications: service(),
isOwner: null,
fetchingSubscription: false,
didInsertElement() {
this._super(...arguments);
let fetchingSubscription = false;
this.billing.getBillingIframe().src = this.billing.getIframeURL();
window.addEventListener('message', (event) => {
let token;
if (event?.data) {
if (event.data?.request === 'token') {
this._handleTokenRequest();
}
if (event && event.data && event.data.request === 'token') {
if (event.data?.request === 'forceUpgradeInfo') {
this._handleForceUpgradeRequest();
}
if (event.data?.subscription) {
this._handleSubscriptionUpdate(event.data);
}
}
});
},
_handleTokenRequest() {
this.set('fetchingSubscription', false);
let token;
const ghostIdentityUrl = this.get('ghostPaths.url').api('identities');
this.ajax.request(ghostIdentityUrl).then((response) => {
@ -48,16 +64,16 @@ export default Component.extend({
// NOTE: the handler is placed here to avoid additional logic to check if iframe has loaded
// receiving a 'token' request is an indication that page is ready
if (!fetchingSubscription && !this.billing.get('subscription') && token) {
fetchingSubscription = true;
if (!this.fetchingSubscription && !this.billing.get('subscription') && token) {
this.set('fetchingSubscription', true);
this.billing.getBillingIframe().contentWindow.postMessage({
query: 'getSubscription',
response: 'subscription'
}, '*');
}
}
},
if (event && event.data && event.data.request === 'forceUpgradeInfo') {
_handleForceUpgradeRequest() {
// Send BMA requested information about forceUpgrade and owner name/email
let ownerUser = null;
const owner = this.billing.ownerUser;
@ -76,14 +92,14 @@ export default Component.extend({
ownerUser
}
}, '*');
}
},
if (event && event.data && event.data.subscription) {
this.billing.set('subscription', event.data.subscription);
this.billing.set('checkoutRoute', event.data?.checkoutRoute || '/plans');
_handleSubscriptionUpdate(data) {
this.billing.set('subscription', data.subscription);
this.billing.set('checkoutRoute', data?.checkoutRoute || '/plans');
// Detect if the current subscription is in a grace state and render a notification
if (event.data.subscription.status === 'past_due' || event.data.subscription.status === 'unpaid') {
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 {
@ -91,10 +107,10 @@ export default Component.extend({
}
// Detect if the current member limits are exceeded and render a notification
if (
event.data?.exceededLimits
&& event.data?.exceededLimits.length
&& event.data?.exceededLimits.indexOf('members') >= 0
&& event.data?.checkoutRoute
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.
@ -105,6 +121,4 @@ export default Component.extend({
this.notifications.closeAlerts('billing.exceeded');
}
}
});
}
});