2024-09-19 21:28:00 +02:00
|
|
|
import { useEffect, useRef, useState, useMemo } from '@wordpress/element';
|
2024-10-05 02:26:09 +02:00
|
|
|
import { useSelect } from '@wordpress/data';
|
2024-09-05 21:17:36 +02:00
|
|
|
import Fastlane from '../../../../ppcp-axo/resources/js/Connection/Fastlane';
|
|
|
|
import { log } from '../../../../ppcp-axo/resources/js/Helper/Debug';
|
2024-09-19 21:28:00 +02:00
|
|
|
import { useDeleteEmptyKeys } from './useDeleteEmptyKeys';
|
2024-10-17 23:56:03 +02:00
|
|
|
import useAllowedLocations from './useAllowedLocations';
|
2024-10-05 02:26:09 +02:00
|
|
|
import { STORE_NAME } from '../stores/axoStore';
|
2024-09-05 21:17:36 +02:00
|
|
|
|
2024-10-03 14:09:12 +02:00
|
|
|
/**
|
|
|
|
* Custom hook to initialize and manage the Fastlane SDK.
|
|
|
|
*
|
2024-10-05 02:26:09 +02:00
|
|
|
* @param {string} namespace - Namespace for the PayPal script.
|
2024-10-03 14:09:12 +02:00
|
|
|
* @param {Object} axoConfig - Configuration for AXO.
|
|
|
|
* @param {Object} ppcpConfig - Configuration for PPCP.
|
|
|
|
* @return {Object|null} The initialized Fastlane SDK instance or null.
|
|
|
|
*/
|
2024-10-05 02:26:09 +02:00
|
|
|
const useFastlaneSdk = ( namespace, axoConfig, ppcpConfig ) => {
|
2024-09-05 21:17:36 +02:00
|
|
|
const [ fastlaneSdk, setFastlaneSdk ] = useState( null );
|
2024-09-11 22:58:13 +02:00
|
|
|
const initializingRef = useRef( false );
|
|
|
|
const configRef = useRef( { axoConfig, ppcpConfig } );
|
2024-09-19 21:28:00 +02:00
|
|
|
const deleteEmptyKeys = useDeleteEmptyKeys();
|
|
|
|
|
2024-10-05 02:26:09 +02:00
|
|
|
const { isPayPalLoaded } = useSelect(
|
|
|
|
( select ) => ( {
|
|
|
|
isPayPalLoaded: select( STORE_NAME ).getIsPayPalLoaded(),
|
|
|
|
} ),
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
2024-09-19 21:28:00 +02:00
|
|
|
const styleOptions = useMemo( () => {
|
|
|
|
return deleteEmptyKeys( configRef.current.axoConfig.style_options );
|
|
|
|
}, [ deleteEmptyKeys ] );
|
2024-09-05 21:17:36 +02:00
|
|
|
|
2024-10-17 23:56:03 +02:00
|
|
|
const allowedLocations = useAllowedLocations( axoConfig );
|
|
|
|
|
2024-10-03 14:09:12 +02:00
|
|
|
// Effect to initialize Fastlane SDK
|
2024-09-05 21:17:36 +02:00
|
|
|
useEffect( () => {
|
|
|
|
const initFastlane = async () => {
|
2024-10-05 02:26:09 +02:00
|
|
|
if ( initializingRef.current || fastlaneSdk || ! isPayPalLoaded ) {
|
2024-09-05 21:17:36 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-09-11 22:58:13 +02:00
|
|
|
initializingRef.current = true;
|
|
|
|
log( 'Init Fastlane' );
|
2024-09-05 21:17:36 +02:00
|
|
|
|
2024-09-11 22:58:13 +02:00
|
|
|
try {
|
2024-10-05 02:26:09 +02:00
|
|
|
const fastlane = new Fastlane( namespace );
|
2024-09-05 21:17:36 +02:00
|
|
|
|
2024-10-03 14:09:12 +02:00
|
|
|
// Set sandbox environment if configured
|
2024-09-11 22:58:13 +02:00
|
|
|
if ( configRef.current.axoConfig.environment.is_sandbox ) {
|
|
|
|
window.localStorage.setItem( 'axoEnv', 'sandbox' );
|
|
|
|
}
|
2024-09-05 21:17:36 +02:00
|
|
|
|
2024-10-03 14:09:12 +02:00
|
|
|
// Connect to Fastlane with locale and style options
|
2024-09-11 22:58:13 +02:00
|
|
|
await fastlane.connect( {
|
|
|
|
locale: configRef.current.ppcpConfig.locale,
|
2024-09-19 21:28:00 +02:00
|
|
|
styles: styleOptions,
|
2024-10-17 23:56:03 +02:00
|
|
|
shippingAddressOptions: {
|
|
|
|
allowedLocations,
|
|
|
|
},
|
2024-09-11 22:58:13 +02:00
|
|
|
} );
|
2024-09-05 21:17:36 +02:00
|
|
|
|
2024-10-03 14:09:12 +02:00
|
|
|
// Set locale (hardcoded to 'en_us' for now)
|
2024-09-11 22:58:13 +02:00
|
|
|
fastlane.setLocale( 'en_us' );
|
2024-09-05 21:17:36 +02:00
|
|
|
|
2024-09-11 22:58:13 +02:00
|
|
|
setFastlaneSdk( fastlane );
|
|
|
|
} catch ( error ) {
|
2024-09-26 12:47:04 +02:00
|
|
|
log( `Failed to initialize Fastlane: ${ error }`, 'error' );
|
2024-09-11 22:58:13 +02:00
|
|
|
} finally {
|
|
|
|
initializingRef.current = false;
|
|
|
|
}
|
2024-09-05 21:17:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
initFastlane();
|
2024-10-17 23:56:03 +02:00
|
|
|
}, [
|
|
|
|
fastlaneSdk,
|
|
|
|
styleOptions,
|
|
|
|
isPayPalLoaded,
|
|
|
|
namespace,
|
|
|
|
allowedLocations,
|
|
|
|
] );
|
2024-09-11 22:58:13 +02:00
|
|
|
|
2024-10-03 14:09:12 +02:00
|
|
|
// Effect to update the config ref when configs change
|
2024-09-11 22:58:13 +02:00
|
|
|
useEffect( () => {
|
|
|
|
configRef.current = { axoConfig, ppcpConfig };
|
|
|
|
}, [ axoConfig, ppcpConfig ] );
|
2024-09-05 21:17:36 +02:00
|
|
|
|
|
|
|
return fastlaneSdk;
|
|
|
|
};
|
|
|
|
|
2024-09-12 14:37:22 +02:00
|
|
|
export default useFastlaneSdk;
|