2024-10-05 02:26:09 +02:00
|
|
|
import { useEffect } from '@wordpress/element';
|
|
|
|
import { useDispatch, useSelect } from '@wordpress/data';
|
2024-09-26 12:47:04 +02:00
|
|
|
import { log } from '../../../../ppcp-axo/resources/js/Helper/Debug';
|
2024-10-08 00:41:30 +02:00
|
|
|
import { loadPayPalScript } from '../../../../ppcp-button/resources/js/modules/Helper/PayPalScriptLoading';
|
2024-10-05 02:26:09 +02:00
|
|
|
import { STORE_NAME } from '../stores/axoStore';
|
2024-09-13 19:24:23 +02:00
|
|
|
|
2024-10-03 14:09:12 +02:00
|
|
|
/**
|
2024-10-07 15:42:01 +02:00
|
|
|
* Custom hook to load the PayPal script.
|
2024-10-03 14:09:12 +02:00
|
|
|
*
|
2024-10-05 02:26:09 +02:00
|
|
|
* @param {string} namespace - Namespace for the PayPal script.
|
|
|
|
* @param {Object} ppcpConfig - Configuration object for PayPal script.
|
|
|
|
* @param {boolean} isConfigLoaded - Whether the PayPal Commerce Gateway config is loaded.
|
2024-10-03 14:09:12 +02:00
|
|
|
* @return {boolean} True if the PayPal script has loaded, false otherwise.
|
|
|
|
*/
|
2024-10-05 02:26:09 +02:00
|
|
|
const usePayPalScript = ( namespace, ppcpConfig, isConfigLoaded ) => {
|
|
|
|
// Get dispatch functions from the AXO store
|
|
|
|
const { setIsPayPalLoaded } = useDispatch( STORE_NAME );
|
|
|
|
|
|
|
|
// Select relevant states from the AXO store
|
|
|
|
const { isPayPalLoaded } = useSelect(
|
|
|
|
( select ) => ( {
|
|
|
|
isPayPalLoaded: select( STORE_NAME ).getIsPayPalLoaded(),
|
|
|
|
} ),
|
|
|
|
[]
|
|
|
|
);
|
2024-09-13 19:24:23 +02:00
|
|
|
|
|
|
|
useEffect( () => {
|
2024-10-05 02:26:09 +02:00
|
|
|
const loadScript = async () => {
|
|
|
|
if ( ! isPayPalLoaded && isConfigLoaded ) {
|
2025-07-18 11:09:23 +02:00
|
|
|
const axoConfig = window.wc_ppcp_axo;
|
|
|
|
|
2024-10-05 02:26:09 +02:00
|
|
|
try {
|
2025-07-18 11:09:23 +02:00
|
|
|
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 ) {
|
|
|
|
log(
|
|
|
|
`Failed to load axo script attributes: ${ json.data.message }`,
|
|
|
|
'error'
|
|
|
|
);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-07-17 15:07:13 +02:00
|
|
|
await loadPayPalScript( namespace, {
|
|
|
|
...ppcpConfig,
|
|
|
|
script_attributes: {
|
|
|
|
...ppcpConfig.script_attributes,
|
2025-07-18 11:09:23 +02:00
|
|
|
'data-sdk-client-token': json.data.sdk_client_token,
|
2025-07-17 15:07:13 +02:00
|
|
|
},
|
|
|
|
} );
|
2024-10-05 02:26:09 +02:00
|
|
|
setIsPayPalLoaded( true );
|
|
|
|
} catch ( error ) {
|
|
|
|
log(
|
2024-10-07 10:42:56 +02:00
|
|
|
`Error loading PayPal script for namespace: ${ namespace }. Error: ${ error }`,
|
|
|
|
'error'
|
2024-10-05 02:26:09 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2024-10-03 14:09:12 +02:00
|
|
|
|
2024-10-05 02:26:09 +02:00
|
|
|
loadScript();
|
|
|
|
}, [ ppcpConfig, isConfigLoaded, isPayPalLoaded ] );
|
2024-09-13 19:24:23 +02:00
|
|
|
|
2024-10-05 02:26:09 +02:00
|
|
|
return isPayPalLoaded;
|
2024-09-13 19:24:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default usePayPalScript;
|