woocommerce-paypal-payments/modules/ppcp-axo-block/resources/js/index.js

272 lines
7 KiB
JavaScript
Raw Normal View History

import { useCallback, useEffect, useState } from '@wordpress/element';
import { useDispatch } from '@wordpress/data';
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
import useFastlaneSdk from './hooks/useFastlaneSdk';
import {
useCustomerData,
useTokenizeCustomerData,
} from './hooks/useCustomerData';
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';
import { removeWatermark, setupWatermark } from './helpers/watermarkHelpers';
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
// 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';
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;
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 );
const [ paymentComponent, setPaymentComponent ] = useState( null );
const tokenizedCustomerData = useTokenizeCustomerData();
const fastlaneSdk = useFastlaneSdk( axoConfig, ppcpConfig );
2024-09-05 21:17:36 +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,
] );
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,
},
},
};
}, [
emitResponse.responseTypes.ERROR,
emitResponse.responseTypes.SUCCESS,
card,
] );
2024-09-11 00:39:09 +02:00
const { setIsAxoActive, setIsGuest, setIsAxoScriptLoaded } =
useDispatch( STORE_NAME );
2024-09-05 21:17:36 +02:00
const {
shippingAddress: wooShippingAddress,
billingAddress: wooBillingAddress,
setShippingAddress: updateWooShippingAddress,
setBillingAddress: updateWooBillingAddress,
} = useCustomerData();
/**
* `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( () => {
console.log( 'Initializing class toggles' );
2024-09-10 18:46:27 +02:00
initializeClassToggles();
}, [] );
useEffect( () => {
console.log( 'Setting up cleanup for WooCommerce fields' );
return () => {
console.log( 'Cleaning up: Restoring WooCommerce fields' );
2024-09-05 21:17:36 +02:00
restoreOriginalFields(
updateWooShippingAddress,
updateWooBillingAddress
);
2024-09-05 21:17:36 +02:00
};
}, [ updateWooShippingAddress, updateWooBillingAddress ] );
2024-09-05 21:17:36 +02:00
useEffect( () => {
if ( ! paypalLoaded ) {
console.log( 'Loading PayPal script' );
loadPaypalScript( ppcpConfig, () => {
console.log( 'PayPal script loaded' );
setPaypalLoaded( true );
} );
}
}, [ paypalLoaded, ppcpConfig ] );
2024-09-05 21:17:36 +02:00
const {
setShippingAddress: setWooShippingAddress,
setBillingAddress: setWooBillingAddress,
} = useCustomerData();
const onChangeShippingAddressClick = useShippingAddressChange(
fastlaneSdk,
setShippingAddress,
updateWooShippingAddress
);
const onChangeCardButtonClick = useCardChange(
fastlaneSdk,
setCard,
updateWooBillingAddress
);
2024-09-05 21:17:36 +02:00
useEffect( () => {
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 ) {
console.log(
'PayPal loaded and FastlaneSDK available, setting up email functionality'
);
2024-09-11 00:39:09 +02:00
setIsAxoScriptLoaded( true );
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
useEffect( () => {
console.log( 'Setting up cleanup for Axo component' );
2024-09-05 21:17:36 +02:00
return () => {
console.log( 'Cleaning up Axo component' );
setIsAxoActive( false );
setIsGuest( true );
removeShippingChangeButton();
removeCardChangeButton();
removeWatermark();
2024-09-11 00:39:09 +02:00
if ( isEmailFunctionalitySetup() ) {
console.log( 'Removing email functionality' );
2024-09-11 00:39:09 +02:00
removeEmailFunctionality();
}
};
}, [] );
2024-09-05 21:17:36 +02:00
const handlePaymentLoad = useCallback( ( component ) => {
setPaymentComponent( component );
}, [] );
2024-09-05 21:17:36 +02:00
const handleChange = ( selectedCard ) => {
console.log( 'Card selection changed', selectedCard );
2024-09-05 21:17:36 +02:00
setCard( selectedCard );
};
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 }
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,
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,
},
} );
console.log( 'Axo module loaded' );
2024-09-05 21:17:36 +02:00
export default Axo;