woocommerce-paypal-payments/modules/ppcp-wc-gateway/resources/js/fraudnet.js
Narek Zakarian 64b424a2aa
fix(wc-gateway): guard against undefined PAYPAL global in fraudnet.js
The hosted_fields_loaded listener referenced the bare PAYPAL global
without checking whether it existed. PAYPAL is set asynchronously by
the externally-loaded c.paypal.com/da/r/fb.js beacon script, which has
no sequencing guarantee against hosted_fields_loaded firing (a load-
order race, or fb.js being blocked by a content/privacy blocker),
causing an uncaught ReferenceError on classic checkout with Advanced
Card Processing enabled. Check window.PAYPAL (a safe property access
returning undefined) instead of the bare identifier.
2026-07-14 17:45:59 +04:00

65 lines
1.7 KiB
JavaScript

window.addEventListener( 'load', function () {
function _loadBeaconJS( options ) {
const script = document.createElement( 'script' );
script.src = options.fnUrl;
document.body.appendChild( script );
}
function _injectConfig() {
let script = document.querySelector(
"[fncls='fnparams-dede7cc5-15fd-4c75-a9f4-36c430ee3a99']"
);
if ( script ) {
if ( script.parentNode ) {
script.parentNode.removeChild( script );
}
}
script = document.createElement( 'script' );
script.id = 'fconfig';
script.type = 'application/json';
script.setAttribute(
'fncls',
'fnparams-dede7cc5-15fd-4c75-a9f4-36c430ee3a99'
);
const configuration = {
f: FraudNetConfig.f,
s: FraudNetConfig.s,
};
if ( FraudNetConfig.sandbox === '1' ) {
configuration.sandbox = true;
}
script.text = JSON.stringify( configuration );
document.body.appendChild( script );
const payForOrderForm = document.forms.order_review;
if ( payForOrderForm ) {
const puiPayForOrderSessionId = document.createElement( 'input' );
puiPayForOrderSessionId.setAttribute( 'type', 'hidden' );
puiPayForOrderSessionId.setAttribute(
'name',
'pui_pay_for_order_session_id'
);
puiPayForOrderSessionId.setAttribute( 'value', FraudNetConfig.f );
payForOrderForm.appendChild( puiPayForOrderSessionId );
}
_loadBeaconJS( { fnUrl: 'https://c.paypal.com/da/r/fb.js' } );
}
document.addEventListener( 'hosted_fields_loaded', ( event ) => {
if (
window.PAYPAL &&
window.PAYPAL.asyncData &&
typeof window.PAYPAL.asyncData.initAndCollect === 'function'
) {
window.PAYPAL.asyncData.initAndCollect();
}
_injectConfig();
} );
_injectConfig();
} );