2020-03-02 08:06:54 +03:00
|
|
|
import Component from '@ember/component';
|
|
|
|
import {inject as service} from '@ember/service';
|
|
|
|
|
|
|
|
export default Component.extend({
|
2020-04-21 09:54:29 +03:00
|
|
|
billing: service(),
|
2020-03-02 08:06:54 +03:00
|
|
|
config: service(),
|
|
|
|
ghostPaths: service(),
|
|
|
|
ajax: service(),
|
|
|
|
|
2020-05-22 05:44:37 +03:00
|
|
|
didInsertElement() {
|
|
|
|
let fetchingSubscription = false;
|
|
|
|
this.billing.getBillingIframe().src = this.billing.getIframeURL();
|
|
|
|
|
2020-03-02 08:06:54 +03:00
|
|
|
window.addEventListener('message', (event) => {
|
|
|
|
if (event && event.data && event.data.request === 'token') {
|
2020-03-12 15:15:54 +03:00
|
|
|
const ghostIdentityUrl = this.get('ghostPaths.url').api('identities');
|
2020-03-02 08:06:54 +03:00
|
|
|
|
2020-03-12 15:15:54 +03:00
|
|
|
this.ajax.request(ghostIdentityUrl).then((response) => {
|
|
|
|
const token = response && response.identities && response.identities[0] && response.identities[0].token;
|
2020-05-22 05:44:37 +03:00
|
|
|
this.billing.getBillingIframe().contentWindow.postMessage({
|
2020-03-02 08:06:54 +03:00
|
|
|
request: 'token',
|
|
|
|
response: token
|
|
|
|
}, '*');
|
|
|
|
});
|
2020-05-22 05:44:37 +03:00
|
|
|
|
|
|
|
// 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')) {
|
|
|
|
fetchingSubscription = true;
|
|
|
|
this.billing.getBillingIframe().contentWindow.postMessage({
|
|
|
|
query: 'getSubscription',
|
|
|
|
response: 'subscription'
|
|
|
|
}, '*');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event && event.data && event.data.subscription) {
|
|
|
|
this.billing.set('subscription', event.data.subscription);
|
2020-03-02 08:06:54 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|