Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions modules/ppcp-axo-block/resources/js/hooks/useAxoSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { snapshotFields } from '../helpers/fieldHelpers';
import useCustomerData from './useCustomerData';
import useShippingAddressChange from './useShippingAddressChange';
import useCardChange from './useCardChange';
import useSessionRestoration from './useSessionRestoration';

/**
* Custom hook to set up AXO functionality.
Expand Down Expand Up @@ -63,6 +64,9 @@ const useAxoSetup = (
// Set up phone sync handler
usePhoneSyncHandler( paymentComponent );

// Set up session restoration
useSessionRestoration( fastlaneSdk );

// Initialize class toggles on mount
useEffect( () => {
initializeClassToggles();
Expand Down Expand Up @@ -104,6 +108,7 @@ const useAxoSetup = (
setShippingAddress,
setCardDetails,
paymentComponent,
setCardChangeHandler,
] );

return paypalLoaded;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { useEffect, useRef } from '@wordpress/element';
import { useDispatch } from '@wordpress/data';
import { setIsEmailLookupCompleted, STORE_NAME } from '../stores/axoStore';
import { log } from '../../../../ppcp-axo/resources/js/Helper/Debug';

/**
* Hook to restore Fastlane session after payment failures using triggerAuthenticationFlow
* Only runs when ppcp_fastlane_error=1 URL parameter is present
* @param {Object} fastlaneSdk - The Fastlane SDK instance
*/
const useSessionRestoration = ( fastlaneSdk ) => {
const { setShippingAddress, setCardDetails, setIsGuest } =
useDispatch( STORE_NAME );
const hasProcessed = useRef( false );

useEffect( () => {
if ( ! fastlaneSdk || hasProcessed.current ) {
return;
}

const urlParams = new URLSearchParams( window.location.search );
const hasErrorParam = urlParams.get( 'ppcp_fastlane_error' ) === '1';

if ( ! hasErrorParam ) {
return;
}

// Remove the error parameter from URL
urlParams.delete( 'ppcp_fastlane_error' );
const newUrl = new URL( window.location );
newUrl.search = urlParams.toString();
window.history.replaceState( {}, '', newUrl );

hasProcessed.current = true;

const restoreSession = async () => {
try {
const emailInput = document.getElementById( 'email' );

if ( emailInput?.value ) {
const lookupResult =
await fastlaneSdk.identity.lookupCustomerByEmail(
emailInput.value
);

wp.data.dispatch( STORE_NAME ).setIsEmailSubmitted( true );

if ( lookupResult?.customerContextId ) {
const customerContextId =
lookupResult.customerContextId;

const authenticatedCustomerResult =
await fastlaneSdk.identity.triggerAuthenticationFlow(
customerContextId
);

if (
authenticatedCustomerResult?.authenticationState ===
'succeeded'
) {
const { profileData } = authenticatedCustomerResult;
setIsGuest( false );

if ( profileData?.shippingAddress ) {
setShippingAddress(
profileData.shippingAddress
);
}

if ( profileData?.card ) {
setCardDetails( profileData.card );
}

setIsEmailLookupCompleted( true );
}
}
}
} catch ( error ) {
log( 'Failed to restore Fastlane session', 'warn' );
}
};

restoreSession();
}, [ fastlaneSdk, setShippingAddress, setCardDetails, setIsGuest ] );
};

export default useSessionRestoration;
Loading
Loading