2024-09-11 22:58:13 +02:00
|
|
|
import { useCallback, useEffect, useState } from '@wordpress/element';
|
|
|
|
import { useDispatch } from '@wordpress/data';
|
2024-09-10 12:03:50 +02:00
|
|
|
|
2024-09-05 21:17:36 +02:00
|
|
|
import { registerPaymentMethod } from '@woocommerce/blocks-registry';
|
|
|
|
|
|
|
|
import { loadPaypalScript } from '../../../ppcp-button/resources/js/modules/Helper/ScriptLoading';
|
|
|
|
|
|
|
|
// Hooks
|
2024-09-12 14:37:22 +02:00
|
|
|
import useFastlaneSdk from './hooks/useFastlaneSdk';
|
2024-09-11 16:08:59 +02:00
|
|
|
import {
|
|
|
|
useCustomerData,
|
|
|
|
useTokenizeCustomerData,
|
|
|
|
} from './hooks/useCustomerData';
|
2024-09-12 13:53:35 +02:00
|
|
|
import { useShippingAddressChange } from './hooks/useShippingAddressChange';
|
|
|
|
import { useCardChange } from './hooks/useCardChange';
|
2024-09-05 21:17:36 +02:00
|
|
|
|
|
|
|
// Components
|
|
|
|
import { Payment } from './components/Payment';
|
|
|
|
|
|
|
|
// Helpers
|
|
|
|
import { snapshotFields, restoreOriginalFields } from './helpers/fieldHelpers';
|
2024-09-10 12:03:50 +02:00
|
|
|
import { removeWatermark, setupWatermark } from './helpers/watermarkHelpers';
|
2024-09-06 11:33:01 +02:00
|
|
|
import { removeCardChangeButton } from './helpers/cardChangeButtonManager';
|
|
|
|
import { removeShippingChangeButton } from './helpers/shippingChangeButtonManager';
|
2024-09-10 18:46:27 +02:00
|
|
|
import { initializeClassToggles } from './helpers/classnamesManager';
|
2024-09-05 21:17:36 +02:00
|
|
|
|
2024-09-10 12:03:50 +02:00
|
|
|
// Stores
|
|
|
|
import { STORE_NAME } from './stores/axoStore';
|
|
|
|
|
2024-09-05 21:17:36 +02:00
|
|
|
// Event handlers
|
2024-09-11 00:39:09 +02:00
|
|
|
import { createEmailLookupHandler } from './events/emailLookupManager';
|
2024-09-10 12:03:50 +02:00
|
|
|
import {
|
2024-09-11 00:39:09 +02:00
|
|
|
setupEmailFunctionality,
|
|
|
|
removeEmailFunctionality,
|
|
|
|
isEmailFunctionalitySetup,
|
|
|
|
} from './helpers/emailSubmissionManager';
|
2024-09-05 21:17:36 +02:00
|
|
|
|
|
|
|
const ppcpConfig = wc.wcSettings.getSetting( 'ppcp-credit-card-gateway_data' );
|
|
|
|
|
|
|
|
if ( typeof window.PayPalCommerceGateway === 'undefined' ) {
|
|
|
|
window.PayPalCommerceGateway = ppcpConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
const axoConfig = window.wc_ppcp_axo;
|
|
|
|
|
2024-09-11 13:00:25 +02:00
|
|
|
const Axo = ( props ) => {
|
|
|
|
const { eventRegistration, emitResponse } = props;
|
|
|
|
const { onPaymentSetup } = eventRegistration;
|
2024-09-05 21:17:36 +02:00
|
|
|
const [ paypalLoaded, setPaypalLoaded ] = useState( false );
|
|
|
|
const [ shippingAddress, setShippingAddress ] = useState( null );
|
|
|
|
const [ card, setCard ] = useState( null );
|
2024-09-11 16:08:59 +02:00
|
|
|
const [ paymentComponent, setPaymentComponent ] = useState( null );
|
|
|
|
const tokenizedCustomerData = useTokenizeCustomerData();
|
2024-09-12 14:37:22 +02:00
|
|
|
const fastlaneSdk = useFastlaneSdk( axoConfig, ppcpConfig );
|
2024-09-05 21:17:36 +02:00
|
|
|
|
2024-09-11 23:01:45 +02:00
|
|
|
console.log( 'Axo component rendering' );
|
|
|
|
|
2024-09-11 12:50:05 +02:00
|
|
|
useEffect( () => {
|
|
|
|
const unsubscribe = onPaymentSetup( async () => {
|
|
|
|
// Validate payment options and emit response.
|
|
|
|
|
|
|
|
// Note: This response supports the Ryan flow (payment via saved card-token)
|
|
|
|
return {
|
|
|
|
type: emitResponse.responseTypes.SUCCESS,
|
|
|
|
meta: {
|
|
|
|
paymentMethodData: {
|
|
|
|
axo_nonce: card?.id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Unsubscribes when this component is unmounted.
|
|
|
|
return () => {
|
|
|
|
unsubscribe();
|
|
|
|
};
|
|
|
|
}, [
|
|
|
|
emitResponse.responseTypes.ERROR,
|
|
|
|
emitResponse.responseTypes.SUCCESS,
|
|
|
|
onPaymentSetup,
|
|
|
|
card,
|
|
|
|
] );
|
|
|
|
|
2024-09-11 16:17:12 +02:00
|
|
|
const handlePaymentSetup = useCallback( async () => {
|
|
|
|
const isRyanFlow = !! card?.id;
|
|
|
|
const cardToken = card?.id;
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: emitResponse.responseTypes.SUCCESS,
|
|
|
|
meta: {
|
|
|
|
paymentMethodData: {
|
|
|
|
fastlane_member: isRyanFlow,
|
|
|
|
axo_nonce: cardToken,
|
2024-09-11 13:00:25 +02:00
|
|
|
},
|
2024-09-11 16:17:12 +02:00
|
|
|
},
|
2024-09-11 13:00:25 +02:00
|
|
|
};
|
|
|
|
}, [
|
|
|
|
emitResponse.responseTypes.ERROR,
|
|
|
|
emitResponse.responseTypes.SUCCESS,
|
|
|
|
card,
|
|
|
|
] );
|
|
|
|
|
2024-09-11 00:39:09 +02:00
|
|
|
const { setIsAxoActive, setIsGuest, setIsAxoScriptLoaded } =
|
|
|
|
useDispatch( STORE_NAME );
|
2024-09-10 12:03:50 +02:00
|
|
|
|
2024-09-05 21:17:36 +02:00
|
|
|
const {
|
|
|
|
shippingAddress: wooShippingAddress,
|
|
|
|
billingAddress: wooBillingAddress,
|
|
|
|
setShippingAddress: updateWooShippingAddress,
|
|
|
|
setBillingAddress: updateWooBillingAddress,
|
|
|
|
} = useCustomerData();
|
|
|
|
|
2024-09-11 16:17:12 +02:00
|
|
|
/**
|
|
|
|
* `onPaymentSetup()` fires when we enter the "PROCESSING" state in the checkout flow.
|
|
|
|
* It pre-processes the payment details and returns data for server-side processing.
|
|
|
|
*/
|
|
|
|
useEffect( () => {
|
|
|
|
const unsubscribe = onPaymentSetup( handlePaymentSetup );
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
unsubscribe();
|
|
|
|
};
|
|
|
|
}, [ onPaymentSetup, handlePaymentSetup ] );
|
|
|
|
|
2024-09-10 18:46:27 +02:00
|
|
|
useEffect( () => {
|
2024-09-11 22:58:13 +02:00
|
|
|
console.log( 'Initializing class toggles' );
|
2024-09-10 18:46:27 +02:00
|
|
|
initializeClassToggles();
|
|
|
|
}, [] );
|
|
|
|
|
2024-09-10 12:03:50 +02:00
|
|
|
useEffect( () => {
|
2024-09-11 22:58:13 +02:00
|
|
|
console.log( 'Setting up cleanup for WooCommerce fields' );
|
2024-09-10 12:03:50 +02:00
|
|
|
return () => {
|
2024-09-11 22:58:13 +02:00
|
|
|
console.log( 'Cleaning up: Restoring WooCommerce fields' );
|
2024-09-05 21:17:36 +02:00
|
|
|
restoreOriginalFields(
|
|
|
|
updateWooShippingAddress,
|
|
|
|
updateWooBillingAddress
|
2024-09-06 03:21:27 +02:00
|
|
|
);
|
2024-09-05 21:17:36 +02:00
|
|
|
};
|
2024-09-06 03:21:27 +02:00
|
|
|
}, [ updateWooShippingAddress, updateWooBillingAddress ] );
|
2024-09-05 21:17:36 +02:00
|
|
|
|
|
|
|
useEffect( () => {
|
2024-09-06 03:21:27 +02:00
|
|
|
if ( ! paypalLoaded ) {
|
2024-09-11 22:58:13 +02:00
|
|
|
console.log( 'Loading PayPal script' );
|
2024-09-06 03:21:27 +02:00
|
|
|
loadPaypalScript( ppcpConfig, () => {
|
2024-09-11 22:58:13 +02:00
|
|
|
console.log( 'PayPal script loaded' );
|
2024-09-06 03:21:27 +02:00
|
|
|
setPaypalLoaded( true );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
}, [ paypalLoaded, ppcpConfig ] );
|
2024-09-05 21:17:36 +02:00
|
|
|
|
2024-09-11 22:58:13 +02:00
|
|
|
const {
|
|
|
|
setShippingAddress: setWooShippingAddress,
|
|
|
|
setBillingAddress: setWooBillingAddress,
|
|
|
|
} = useCustomerData();
|
|
|
|
|
2024-09-10 12:03:50 +02:00
|
|
|
const onChangeShippingAddressClick = useShippingAddressChange(
|
|
|
|
fastlaneSdk,
|
|
|
|
setShippingAddress,
|
|
|
|
updateWooShippingAddress
|
|
|
|
);
|
2024-09-11 22:58:13 +02:00
|
|
|
const onChangeCardButtonClick = useCardChange(
|
|
|
|
fastlaneSdk,
|
|
|
|
setCard,
|
|
|
|
updateWooBillingAddress
|
|
|
|
);
|
2024-09-10 12:03:50 +02:00
|
|
|
|
2024-09-05 21:17:36 +02:00
|
|
|
useEffect( () => {
|
2024-09-11 22:58:13 +02:00
|
|
|
console.log( 'Setting up Axo functionality' );
|
2024-09-11 00:39:09 +02:00
|
|
|
setupWatermark( fastlaneSdk );
|
2024-09-05 21:17:36 +02:00
|
|
|
if ( paypalLoaded && fastlaneSdk ) {
|
2024-09-11 22:58:13 +02:00
|
|
|
console.log(
|
|
|
|
'PayPal loaded and FastlaneSDK available, setting up email functionality'
|
|
|
|
);
|
2024-09-11 00:39:09 +02:00
|
|
|
setIsAxoScriptLoaded( true );
|
2024-09-10 12:03:50 +02:00
|
|
|
setIsAxoActive( true );
|
2024-09-11 00:39:09 +02:00
|
|
|
const emailLookupHandler = createEmailLookupHandler(
|
|
|
|
fastlaneSdk,
|
|
|
|
setShippingAddress,
|
|
|
|
setCard,
|
|
|
|
snapshotFields,
|
|
|
|
wooShippingAddress,
|
|
|
|
wooBillingAddress,
|
|
|
|
setWooShippingAddress,
|
|
|
|
setWooBillingAddress,
|
|
|
|
onChangeShippingAddressClick,
|
|
|
|
onChangeCardButtonClick
|
|
|
|
);
|
|
|
|
setupEmailFunctionality( emailLookupHandler );
|
2024-09-05 21:17:36 +02:00
|
|
|
}
|
2024-09-11 00:39:09 +02:00
|
|
|
}, [
|
|
|
|
paypalLoaded,
|
|
|
|
fastlaneSdk,
|
|
|
|
setIsAxoActive,
|
|
|
|
setIsAxoScriptLoaded,
|
|
|
|
wooShippingAddress,
|
|
|
|
wooBillingAddress,
|
|
|
|
setWooShippingAddress,
|
|
|
|
setWooBillingAddress,
|
|
|
|
onChangeShippingAddressClick,
|
|
|
|
onChangeCardButtonClick,
|
|
|
|
] );
|
2024-09-05 21:17:36 +02:00
|
|
|
|
2024-09-10 12:03:50 +02:00
|
|
|
useEffect( () => {
|
2024-09-11 22:58:13 +02:00
|
|
|
console.log( 'Setting up cleanup for Axo component' );
|
2024-09-05 21:17:36 +02:00
|
|
|
return () => {
|
2024-09-11 22:58:13 +02:00
|
|
|
console.log( 'Cleaning up Axo component' );
|
2024-09-10 12:03:50 +02:00
|
|
|
setIsAxoActive( false );
|
|
|
|
setIsGuest( true );
|
|
|
|
removeShippingChangeButton();
|
|
|
|
removeCardChangeButton();
|
2024-09-06 11:33:01 +02:00
|
|
|
removeWatermark();
|
2024-09-11 00:39:09 +02:00
|
|
|
if ( isEmailFunctionalitySetup() ) {
|
2024-09-11 22:58:13 +02:00
|
|
|
console.log( 'Removing email functionality' );
|
2024-09-11 00:39:09 +02:00
|
|
|
removeEmailFunctionality();
|
2024-09-10 12:03:50 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}, [] );
|
2024-09-05 21:17:36 +02:00
|
|
|
|
2024-09-11 16:08:59 +02:00
|
|
|
const handlePaymentLoad = useCallback( ( component ) => {
|
|
|
|
setPaymentComponent( component );
|
2024-09-11 22:58:13 +02:00
|
|
|
}, [] );
|
2024-09-05 21:17:36 +02:00
|
|
|
|
|
|
|
const handleChange = ( selectedCard ) => {
|
2024-09-11 22:58:13 +02:00
|
|
|
console.log( 'Card selection changed', selectedCard );
|
2024-09-05 21:17:36 +02:00
|
|
|
setCard( selectedCard );
|
|
|
|
};
|
|
|
|
|
2024-09-11 22:58:13 +02:00
|
|
|
console.log( 'Rendering Axo component', {
|
|
|
|
fastlaneSdk,
|
|
|
|
card,
|
|
|
|
shippingAddress,
|
|
|
|
} );
|
|
|
|
|
2024-09-05 21:17:36 +02:00
|
|
|
return fastlaneSdk ? (
|
|
|
|
<Payment
|
|
|
|
fastlaneSdk={ fastlaneSdk }
|
|
|
|
card={ card }
|
|
|
|
onChange={ handleChange }
|
|
|
|
onPaymentLoad={ handlePaymentLoad }
|
2024-09-10 12:03:50 +02:00
|
|
|
onChangeButtonClick={ onChangeCardButtonClick }
|
2024-09-05 21:17:36 +02:00
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<div>Loading Fastlane...</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Register the payment method
|
|
|
|
registerPaymentMethod( {
|
|
|
|
name: ppcpConfig.id,
|
|
|
|
label: (
|
|
|
|
<div
|
|
|
|
id="ppcp-axo-block-radio-label"
|
|
|
|
dangerouslySetInnerHTML={ { __html: ppcpConfig.title } }
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
content: <Axo />,
|
|
|
|
edit: <h1>This is Axo Blocks in the editor</h1>,
|
|
|
|
ariaLabel: ppcpConfig.title,
|
2024-09-11 22:58:13 +02:00
|
|
|
canMakePayment: () => {
|
|
|
|
console.log( 'Checking if payment can be made' );
|
|
|
|
return true;
|
|
|
|
},
|
2024-09-05 21:17:36 +02:00
|
|
|
supports: {
|
|
|
|
showSavedCards: true,
|
|
|
|
features: ppcpConfig.supports,
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
|
2024-09-11 22:58:13 +02:00
|
|
|
console.log( 'Axo module loaded' );
|
|
|
|
|
2024-09-05 21:17:36 +02:00
|
|
|
export default Axo;
|