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

96 lines
2.4 KiB
JavaScript
Raw Normal View History

import { populateWooFields } from '../helpers/fieldHelpers';
import { injectShippingChangeButton } from '../components/Shipping';
import { injectCardChangeButton } from '../components/Card';
import { setIsGuest, setIsEmailLookupCompleted } from '../stores/axoStore';
2024-09-05 21:17:36 +02:00
2024-09-11 00:39:09 +02:00
export const createEmailLookupHandler = (
2024-09-05 21:17:36 +02:00
fastlaneSdk,
setShippingAddress,
setCard,
snapshotFields,
wooShippingAddress,
wooBillingAddress,
setWooShippingAddress,
setWooBillingAddress,
onChangeShippingAddressClick,
2024-09-11 00:39:09 +02:00
onChangeCardButtonClick
2024-09-05 21:17:36 +02:00
) => {
2024-09-11 00:39:09 +02:00
return async ( email ) => {
try {
console.log( 'Email value being looked up:', email );
2024-09-05 21:17:36 +02:00
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
2024-09-11 00:39:09 +02:00
const lookup =
await fastlaneSdk.identity.lookupCustomerByEmail( email );
2024-09-05 21:17:36 +02:00
2024-09-11 00:39:09 +02:00
console.log( 'Lookup response:', lookup );
2024-09-05 21:17:36 +02:00
// Gary flow
if ( lookup && lookup.customerContextId === '' ) {
setIsEmailLookupCompleted( true );
}
2024-09-11 00:39:09 +02:00
if ( ! lookup || ! lookup.customerContextId ) {
console.warn( 'No customerContextId found in the response' );
return;
}
2024-09-05 21:17:36 +02:00
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
// OTP success/fail/cancel flow
if ( authResponse ) {
setIsEmailLookupCompleted( true );
}
2024-09-11 00:39:09 +02:00
if ( authenticationState === 'succeeded' ) {
snapshotFields( wooShippingAddress, wooBillingAddress );
setIsGuest( false );
2024-09-05 21:17:36 +02:00
2024-09-11 00:39:09 +02:00
if ( profileData && profileData.shippingAddress ) {
setShippingAddress( profileData.shippingAddress );
}
if ( profileData && profileData.card ) {
setCard( profileData.card );
}
console.log( 'Profile Data:', profileData );
populateWooFields(
profileData,
setWooShippingAddress,
setWooBillingAddress
);
injectShippingChangeButton( onChangeShippingAddressClick );
injectCardChangeButton( onChangeCardButtonClick );
} else {
console.warn( 'Authentication failed or did not succeed' );
}
} catch ( error ) {
console.error(
'Error during email lookup or authentication:',
error
);
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
};