2022-01-21 22:25:47 +03:00
|
|
|
import Service, {inject as service} from '@ember/service';
|
2022-02-03 01:11:11 +03:00
|
|
|
import classic from 'ember-classic-decorator';
|
2020-04-21 09:54:29 +03:00
|
|
|
|
2022-02-03 01:11:11 +03:00
|
|
|
@classic
|
|
|
|
export default class BillingService extends Service {
|
2022-02-03 22:04:43 +03:00
|
|
|
@service router;
|
|
|
|
@service config;
|
2020-04-21 09:54:29 +03:00
|
|
|
|
2022-02-03 22:04:43 +03:00
|
|
|
@service ghostPaths;
|
|
|
|
@service store;
|
2022-02-03 01:11:11 +03:00
|
|
|
|
|
|
|
billingRouteRoot = '#/pro';
|
|
|
|
billingWindowOpen = false;
|
|
|
|
subscription = null;
|
|
|
|
previousRoute = null;
|
|
|
|
action = null;
|
|
|
|
ownerUser = null;
|
2020-04-22 06:35:56 +03:00
|
|
|
|
2020-05-22 05:44:37 +03:00
|
|
|
init() {
|
2022-02-03 01:11:11 +03:00
|
|
|
super.init(...arguments);
|
2020-05-22 05:44:37 +03:00
|
|
|
|
2022-10-07 17:24:03 +03:00
|
|
|
if (this.config.hostSettings?.billing?.url) {
|
2020-05-22 05:44:37 +03:00
|
|
|
window.addEventListener('message', (event) => {
|
|
|
|
if (event && event.data && event.data.route) {
|
|
|
|
this.handleRouteChangeInIframe(event.data.route);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|
2020-04-21 09:54:29 +03:00
|
|
|
|
2020-05-22 05:44:37 +03:00
|
|
|
handleRouteChangeInIframe(destinationRoute) {
|
2022-01-21 22:25:47 +03:00
|
|
|
if (this.billingWindowOpen) {
|
|
|
|
let billingRoute = this.billingRouteRoot;
|
2020-05-22 05:44:37 +03:00
|
|
|
|
|
|
|
if (destinationRoute !== '/') {
|
|
|
|
billingRoute += destinationRoute;
|
|
|
|
}
|
2020-04-21 09:54:29 +03:00
|
|
|
|
2020-05-22 05:44:37 +03:00
|
|
|
if (window.location.hash !== billingRoute) {
|
|
|
|
window.history.replaceState(window.history.state, '', billingRoute);
|
|
|
|
}
|
2020-04-21 09:54:29 +03:00
|
|
|
}
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|
2020-04-21 09:54:29 +03:00
|
|
|
|
2020-05-22 05:44:37 +03:00
|
|
|
getIframeURL() {
|
2021-10-28 13:09:36 +03:00
|
|
|
// initiate getting owner user in the background
|
|
|
|
this.getOwnerUser();
|
|
|
|
|
2022-10-07 17:24:03 +03:00
|
|
|
let url = this.config.hostSettings?.billing?.url;
|
2020-05-22 05:44:37 +03:00
|
|
|
|
2022-01-21 22:25:47 +03:00
|
|
|
if (window.location.hash && window.location.hash.includes(this.billingRouteRoot)) {
|
|
|
|
let destinationRoute = window.location.hash.replace(this.billingRouteRoot, '');
|
2020-05-22 05:44:37 +03:00
|
|
|
|
|
|
|
if (destinationRoute) {
|
|
|
|
url += destinationRoute;
|
|
|
|
}
|
2020-04-22 06:35:56 +03:00
|
|
|
}
|
|
|
|
|
2020-04-21 09:54:29 +03:00
|
|
|
return url;
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|
2020-05-22 05:44:37 +03:00
|
|
|
|
2021-10-22 13:29:55 +03:00
|
|
|
async getOwnerUser() {
|
2022-01-21 22:25:47 +03:00
|
|
|
if (!this.ownerUser) {
|
2021-10-22 13:29:55 +03:00
|
|
|
// Try to receive the owner user from the store
|
|
|
|
let user = this.store.peekAll('user').findBy('isOwnerOnly', true);
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
// load it when it's not there yet
|
2021-10-28 13:09:36 +03:00
|
|
|
await this.store.findAll('user', {reload: true});
|
2021-10-22 13:29:55 +03:00
|
|
|
user = this.store.peekAll('user').findBy('isOwnerOnly', true);
|
|
|
|
}
|
|
|
|
this.set('ownerUser', user);
|
|
|
|
}
|
2022-01-21 22:25:47 +03:00
|
|
|
return this.ownerUser;
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|
2021-10-22 13:29:55 +03:00
|
|
|
|
2021-09-30 15:54:54 +03:00
|
|
|
// Sends a route update to a child route in the BMA, because we can't control
|
|
|
|
// navigating to it otherwise
|
|
|
|
sendRouteUpdate() {
|
2022-01-21 22:25:47 +03:00
|
|
|
const action = this.action;
|
2021-09-30 15:54:54 +03:00
|
|
|
|
|
|
|
if (action) {
|
|
|
|
if (action === 'checkout') {
|
|
|
|
this.getBillingIframe().contentWindow.postMessage({
|
|
|
|
query: 'routeUpdate',
|
2022-01-21 22:25:47 +03:00
|
|
|
response: this.checkoutRoute
|
2021-09-30 15:54:54 +03:00
|
|
|
}, '*');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.set('action', null);
|
|
|
|
}
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|
2021-09-30 15:54:54 +03:00
|
|
|
|
2020-05-22 05:44:37 +03:00
|
|
|
// Controls billing window modal visibility and sync of the URL visible in browser
|
|
|
|
// and the URL opened on the iframe. It is responsible to non user triggered iframe opening,
|
2021-03-23 14:59:52 +03:00
|
|
|
// for example: by entering "/pro" route in the URL or using history navigation (back and forward)
|
2021-09-10 17:22:52 +03:00
|
|
|
toggleProWindow(value) {
|
2022-01-21 22:25:47 +03:00
|
|
|
if (this.billingWindowOpen && value && !this.action) {
|
2021-09-10 17:22:52 +03:00
|
|
|
// don't attempt to open again
|
|
|
|
return;
|
2020-05-22 05:44:37 +03:00
|
|
|
}
|
|
|
|
|
2021-09-30 15:54:54 +03:00
|
|
|
this.sendRouteUpdate();
|
|
|
|
|
2020-05-22 05:44:37 +03:00
|
|
|
this.set('billingWindowOpen', value);
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|
2020-05-22 05:44:37 +03:00
|
|
|
|
|
|
|
// Controls navigation to billing window modal which is triggered from the application UI.
|
|
|
|
// For example: pressing "View Billing" link in navigation menu. It's main side effect is
|
|
|
|
// remembering the route from which the action has been triggered - "previousRoute" so it
|
|
|
|
// could be reused when closing billing window
|
|
|
|
openBillingWindow(currentRoute, childRoute) {
|
2021-10-22 13:29:55 +03:00
|
|
|
// initiate getting owner user in the background
|
|
|
|
this.getOwnerUser();
|
|
|
|
|
2022-01-21 22:25:47 +03:00
|
|
|
if (this.billingWindowOpen) {
|
2021-09-10 17:22:52 +03:00
|
|
|
// don't attempt to open again
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-22 05:44:37 +03:00
|
|
|
this.set('previousRoute', currentRoute);
|
|
|
|
|
|
|
|
// Ensures correct "getIframeURL" calculation when syncing iframe location
|
2021-09-10 17:22:52 +03:00
|
|
|
// in toggleProWindow
|
2021-03-23 14:59:52 +03:00
|
|
|
window.location.hash = childRoute || '/pro';
|
2020-05-22 05:44:37 +03:00
|
|
|
|
2021-09-30 15:54:54 +03:00
|
|
|
this.sendRouteUpdate();
|
|
|
|
|
2021-03-23 14:59:52 +03:00
|
|
|
this.router.transitionTo(childRoute || '/pro');
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|
2020-05-22 05:44:37 +03:00
|
|
|
|
|
|
|
getBillingIframe() {
|
|
|
|
return document.getElementById('billing-frame');
|
|
|
|
}
|
2022-02-03 01:11:11 +03:00
|
|
|
}
|