Merge branch 'PCP-3649-fastlane-backend-logic-for-blocks' of github.com:woocommerce/woocommerce-paypal-payments into PCP-3380-prepare-fastlane-integration-on-block-checkout

This commit is contained in:
Daniel Dudzic 2024-09-14 00:21:27 +02:00
commit 75c808216c
No known key found for this signature in database
GPG key ID: 31B40D33E3465483
8 changed files with 250 additions and 79 deletions

View file

@ -0,0 +1,49 @@
import { useMemo } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
export const useTokenizeCustomerData = () => {
const customerData = useSelect( ( select ) =>
select( 'wc/store/cart' ).getCustomerData()
);
const isValidAddress = ( address ) => {
// At least one name must be present.
if ( ! address.first_name && ! address.last_name ) {
return false;
}
// Street, city, postcode, country are mandatory; state is optional.
return (
address.address_1 &&
address.city &&
address.postcode &&
address.country
);
};
// Memoize the customer data to avoid unnecessary re-renders (and potential infinite loops).
return useMemo( () => {
const { billingAddress, shippingAddress } = customerData;
// Prefer billing address, but fallback to shipping address if billing address is not valid.
const mainAddress = isValidAddress( billingAddress )
? billingAddress
: shippingAddress;
return {
cardholderName: {
fullName: `${ mainAddress.first_name } ${ mainAddress.last_name }`,
},
billingAddress: {
addressLine1: mainAddress.address_1,
addressLine2: mainAddress.address_2,
adminArea1: mainAddress.state,
adminArea2: mainAddress.city,
postalCode: mainAddress.postcode,
countryCode: mainAddress.country,
},
};
}, [ customerData ] );
};
export default useTokenizeCustomerData;