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

66 lines
1.8 KiB
JavaScript
Raw Normal View History

import { populateWooFields } from '../helpers/fieldHelpers';
import { injectShippingChangeButton } from '../helpers/shippingChangeButtonManager';
import { injectCardChangeButton } from '../helpers/cardChangeButtonManager';
import { setIsGuest } from '../stores/axoStore';
2024-09-05 21:17:36 +02:00
// Handle the logic for email submission and customer data retrieval
2024-09-05 21:17:36 +02:00
export const onEmailSubmit = async (
email,
fastlaneSdk,
setShippingAddress,
setCard,
snapshotFields,
wooShippingAddress,
wooBillingAddress,
setWooShippingAddress,
setWooBillingAddress,
onChangeShippingAddressClick,
onChangeButtonClick
2024-09-05 21:17:36 +02:00
) => {
try {
console.log( 'Email value being looked up:', email );
const lookup =
await fastlaneSdk.identity.lookupCustomerByEmail( email );
console.log( 'Lookup response:', lookup );
if ( ! lookup.customerContextId ) {
console.warn( 'No customerContextId found in the response' );
return;
}
const { authenticationState, profileData } =
await fastlaneSdk.identity.triggerAuthenticationFlow(
lookup.customerContextId
);
console.log( 'authenticationState', authenticationState );
if ( authenticationState === 'succeeded' ) {
// Capture the existing WooCommerce data before updating it
snapshotFields( wooShippingAddress, wooBillingAddress );
console.log( 'Setting isGuest to false' );
2024-09-05 21:17:36 +02:00
setIsGuest( false );
2024-09-05 21:17:36 +02:00
setShippingAddress( profileData.shippingAddress );
setCard( profileData.card );
console.log( 'Profile Data:', profileData );
populateWooFields(
profileData,
setWooShippingAddress,
setWooBillingAddress
2024-09-05 21:17:36 +02:00
);
injectShippingChangeButton( onChangeShippingAddressClick );
injectCardChangeButton( onChangeButtonClick );
2024-09-05 21:17:36 +02:00
} else {
console.warn( 'Authentication failed or did not succeed' );
}
} catch ( error ) {
console.error( 'Error during email lookup or authentication:', error );
}
};