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

116 lines
3.6 KiB
JavaScript
Raw Normal View History

import { log } from '../../../../ppcp-axo/resources/js/Helper/Debug';
import { populateWooFields } from '../helpers/fieldHelpers';
import { injectShippingChangeButton } from '../components/Shipping';
import { setIsGuest, setIsEmailLookupCompleted } from '../stores/axoStore';
2024-09-05 21:17:36 +02:00
/**
* Creates an email lookup handler function for AXO checkout.
*
* @param {Object} fastlaneSdk - The Fastlane SDK instance.
* @param {Function} setShippingAddress - Function to set shipping address in the store.
* @param {Function} setCardDetails - Function to set card details in the store.
* @param {Function} snapshotFields - Function to save current field values.
* @param {Object} wooShippingAddress - Current WooCommerce shipping address.
* @param {Object} wooBillingAddress - Current WooCommerce billing address.
* @param {Function} setWooShippingAddress - Function to update WooCommerce shipping address.
* @param {Function} setWooBillingAddress - Function to update WooCommerce billing address.
* @param {Function} onChangeShippingAddressClick - Handler for shipping address change.
* @return {Function} The email lookup handler function.
*/
2024-09-11 00:39:09 +02:00
export const createEmailLookupHandler = (
2024-09-05 21:17:36 +02:00
fastlaneSdk,
setShippingAddress,
setCardDetails,
2024-09-05 21:17:36 +02:00
snapshotFields,
wooShippingAddress,
wooBillingAddress,
setWooShippingAddress,
setWooBillingAddress,
onChangeShippingAddressClick
2024-09-05 21:17:36 +02:00
) => {
2024-09-11 00:39:09 +02:00
return async ( email ) => {
try {
log( `Email value being looked up: ${ email }` );
2024-09-05 21:17:36 +02:00
// Validate Fastlane SDK initialization
2024-09-11 00:39:09 +02:00
if ( ! fastlaneSdk ) {
throw new Error( 'FastlaneSDK is not initialized' );
}
2024-09-05 21:17:36 +02:00
2024-09-11 00:39:09 +02:00
if ( ! fastlaneSdk.identity ) {
throw new Error(
'FastlaneSDK identity object is not available'
);
}
2024-09-05 21:17:36 +02:00
// Perform email lookup
2024-09-11 00:39:09 +02:00
const lookup =
await fastlaneSdk.identity.lookupCustomerByEmail( email );
2024-09-05 21:17:36 +02:00
log( `Lookup response: ${ JSON.stringify( lookup ) }` );
2024-09-05 21:17:36 +02:00
// Handle Gary flow (new user)
if ( lookup && lookup.customerContextId === '' ) {
setIsEmailLookupCompleted( true );
}
2024-09-11 00:39:09 +02:00
if ( ! lookup || ! lookup.customerContextId ) {
log( 'No customerContextId found in the response', 'warn' );
2024-09-11 00:39:09 +02:00
return;
}
2024-09-05 21:17:36 +02:00
// Trigger authentication flow
2024-09-11 00:39:09 +02:00
const authResponse =
await fastlaneSdk.identity.triggerAuthenticationFlow(
lookup.customerContextId
);
2024-09-11 00:39:09 +02:00
if ( ! authResponse || ! authResponse.authenticationState ) {
throw new Error( 'Invalid authentication response' );
}
2024-09-05 21:17:36 +02:00
2024-09-11 00:39:09 +02:00
const { authenticationState, profileData } = authResponse;
2024-09-05 21:17:36 +02:00
// Mark email lookup as completed for OTP flow
if ( authResponse ) {
setIsEmailLookupCompleted( true );
}
// Handle successful authentication
2024-09-11 00:39:09 +02:00
if ( authenticationState === 'succeeded' ) {
// Save current field values
2024-09-11 00:39:09 +02:00
snapshotFields( wooShippingAddress, wooBillingAddress );
setIsGuest( false );
2024-09-05 21:17:36 +02:00
// Update store with profile data
2024-09-11 00:39:09 +02:00
if ( profileData && profileData.shippingAddress ) {
setShippingAddress( profileData.shippingAddress );
}
if ( profileData && profileData.card ) {
setCardDetails( profileData.card );
2024-09-11 00:39:09 +02:00
}
log( `Profile Data: ${ JSON.stringify( profileData ) }` );
2024-09-11 00:39:09 +02:00
// Populate WooCommerce fields with profile data
2024-09-11 00:39:09 +02:00
populateWooFields(
profileData,
setWooShippingAddress,
setWooBillingAddress
);
// Inject the change button for shipping
2024-09-11 00:39:09 +02:00
injectShippingChangeButton( onChangeShippingAddressClick );
} else {
log( 'Authentication failed or did not succeed', 'warn' );
2024-09-11 00:39:09 +02:00
}
} catch ( error ) {
log(
`Error during email lookup or authentication:
${ error }`
2024-09-11 00:39:09 +02:00
);
throw error;
2024-09-05 21:17:36 +02:00
}
2024-09-11 00:39:09 +02:00
};
2024-09-05 21:17:36 +02:00
};