woocommerce-paypal-payments/modules/ppcp-axo/resources/js/boot.js

51 lines
1.3 KiB
JavaScript
Raw Normal View History

2024-07-12 12:58:34 +02:00
import AxoManager from './AxoManager';
2024-10-08 00:41:30 +02:00
import { loadPayPalScript } from '../../../ppcp-button/resources/js/modules/Helper/PayPalScriptLoading';
import { log } from './Helper/Debug';
2024-02-08 14:37:56 +00:00
( function ( { axoConfig, ppcpConfig } ) {
const namespace = 'ppcpPaypalClassicAxo';
2024-07-12 12:58:34 +02:00
const bootstrap = () => {
new AxoManager( namespace, axoConfig, ppcpConfig );
2024-07-12 12:58:34 +02:00
};
2024-02-08 14:37:56 +00:00
document.addEventListener( 'DOMContentLoaded', async () => {
if ( typeof PayPalCommerceGateway === 'undefined' ) {
2024-07-12 12:58:34 +02:00
console.error( 'AXO could not be configured.' );
return;
}
const res = await fetch(
axoConfig.ajax.axo_script_attributes.endpoint,
{
method: 'POST',
credentials: 'same-origin',
body: JSON.stringify( {
nonce: axoConfig.ajax.axo_script_attributes.nonce,
} ),
}
);
const json = await res.json();
if ( ! json.success ) {
throw new Error( json.data.message );
}
loadPayPalScript( namespace, {
...ppcpConfig,
script_attributes: {
...ppcpConfig.script_attributes,
'data-sdk-client-token': json.data.sdk_client_token,
},
} )
.then( () => {
bootstrap();
} )
.catch( ( error ) => {
log( `Failed to load PayPal script: ${ error }`, 'error' );
} );
2024-07-12 12:58:34 +02:00
} );
} )( {
axoConfig: window.wc_ppcp_axo,
ppcpConfig: window.PayPalCommerceGateway,
} );