2020-03-02 08:06:54 +03:00
import Component from '@ember/component' ;
2021-09-30 15:54:54 +03:00
import { htmlSafe } from '@ember/template' ;
2020-03-02 08:06:54 +03:00
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 ( ) ,
2021-09-30 15:54:54 +03:00
notifications : service ( ) ,
2020-03-02 08:06:54 +03:00
2020-05-22 05:44:37 +03:00
didInsertElement ( ) {
2021-07-15 17:27:29 +03:00
this . _super ( ... arguments ) ;
2020-05-22 05:44:37 +03:00
let fetchingSubscription = false ;
this . billing . getBillingIframe ( ) . src = this . billing . getIframeURL ( ) ;
2020-03-02 08:06:54 +03:00
window . addEventListener ( 'message' , ( event ) => {
2021-05-20 07:15:29 +03:00
let token ;
2020-03-02 08:06:54 +03:00
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 ) => {
2021-05-20 07:15:29 +03:00
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
} , '*' ) ;
2021-06-18 12:34:30 +03:00
} ) . catch ( ( error ) => {
if ( error . payload ? . errors && error . payload . errors [ 0 ] ? . type === 'NoPermissionError' ) {
// noop - user doesn't have permission to access billing
return ;
}
throw error ;
2020-03-02 08:06:54 +03:00
} ) ;
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
2021-05-20 07:15:29 +03:00
if ( ! fetchingSubscription && ! this . billing . get ( 'subscription' ) && token ) {
2020-05-22 05:44:37 +03:00
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 ) ;
2021-09-30 15:54:54 +03:00
this . billing . set ( 'checkoutRoute' , event . 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' ) {
// 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' } ) ;
}
// 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
) {
// 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 . get ( 'billingRouteRoot' ) + '?action=checkout' ;
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' } ) ;
}
2020-03-02 08:06:54 +03:00
}
} ) ;
}
} ) ;