2024-09-10 12:03:50 +02:00
|
|
|
import { useCallback } from '@wordpress/element';
|
2024-09-24 02:09:38 +02:00
|
|
|
import { useDispatch } from '@wordpress/data';
|
2024-09-26 12:47:04 +02:00
|
|
|
import { log } from '../../../../ppcp-axo/resources/js/Helper/Debug';
|
2024-09-13 22:30:04 +02:00
|
|
|
import { useAddressEditing } from './useAddressEditing';
|
2024-09-13 23:09:51 +02:00
|
|
|
import useCustomerData from './useCustomerData';
|
2024-09-24 02:09:38 +02:00
|
|
|
import { STORE_NAME } from '../stores/axoStore';
|
2024-09-10 12:03:50 +02:00
|
|
|
|
2024-10-03 14:09:12 +02:00
|
|
|
/**
|
|
|
|
* Custom hook to handle the 'Choose a different card' selection.
|
|
|
|
*
|
|
|
|
* @param {Object} fastlaneSdk - The Fastlane SDK instance.
|
|
|
|
* @return {Function} Callback function to trigger card selection and update related data.
|
|
|
|
*/
|
2024-09-24 02:09:38 +02:00
|
|
|
export const useCardChange = ( fastlaneSdk ) => {
|
2024-09-13 22:30:04 +02:00
|
|
|
const { setBillingAddressEditing } = useAddressEditing();
|
2024-09-13 23:09:51 +02:00
|
|
|
const { setBillingAddress: setWooBillingAddress } = useCustomerData();
|
2024-10-03 14:09:12 +02:00
|
|
|
const { setCardDetails } = useDispatch( STORE_NAME );
|
2024-09-13 22:30:04 +02:00
|
|
|
|
2024-09-10 12:03:50 +02:00
|
|
|
return useCallback( async () => {
|
|
|
|
if ( fastlaneSdk ) {
|
2024-10-03 14:09:12 +02:00
|
|
|
// Show card selector and get the user's selection
|
2024-09-10 12:03:50 +02:00
|
|
|
const { selectionChanged, selectedCard } =
|
|
|
|
await fastlaneSdk.profile.showCardSelector();
|
2024-09-24 02:09:38 +02:00
|
|
|
|
|
|
|
if ( selectionChanged && selectedCard?.paymentSource?.card ) {
|
2024-10-03 14:09:12 +02:00
|
|
|
// Extract cardholder and billing information from the selected card
|
2024-09-11 22:58:13 +02:00
|
|
|
const { name, billingAddress } =
|
|
|
|
selectedCard.paymentSource.card;
|
|
|
|
|
2024-10-03 14:09:12 +02:00
|
|
|
// Parse cardholder's name, using billing details as a fallback if missing
|
2024-09-24 02:09:38 +02:00
|
|
|
let firstName = '';
|
|
|
|
let lastName = '';
|
|
|
|
|
|
|
|
if ( name ) {
|
|
|
|
const nameParts = name.split( ' ' );
|
|
|
|
firstName = nameParts[ 0 ];
|
|
|
|
lastName = nameParts.slice( 1 ).join( ' ' );
|
|
|
|
}
|
2024-09-11 22:58:13 +02:00
|
|
|
|
2024-10-03 14:09:12 +02:00
|
|
|
// Transform the billing address into WooCommerce format
|
2024-09-13 22:30:04 +02:00
|
|
|
const newBillingAddress = {
|
2024-09-11 22:58:13 +02:00
|
|
|
first_name: firstName,
|
|
|
|
last_name: lastName,
|
2024-09-24 02:09:38 +02:00
|
|
|
address_1: billingAddress?.addressLine1 || '',
|
|
|
|
address_2: billingAddress?.addressLine2 || '',
|
|
|
|
city: billingAddress?.adminArea2 || '',
|
|
|
|
state: billingAddress?.adminArea1 || '',
|
|
|
|
postcode: billingAddress?.postalCode || '',
|
|
|
|
country: billingAddress?.countryCode || '',
|
2024-09-13 22:30:04 +02:00
|
|
|
};
|
|
|
|
|
2024-10-03 14:09:12 +02:00
|
|
|
// Batch update states
|
2024-09-24 02:09:38 +02:00
|
|
|
await Promise.all( [
|
2024-10-03 14:09:12 +02:00
|
|
|
// Update the selected card details in the custom store
|
2024-09-24 02:09:38 +02:00
|
|
|
new Promise( ( resolve ) => {
|
|
|
|
setCardDetails( selectedCard );
|
|
|
|
resolve();
|
|
|
|
} ),
|
2024-10-03 14:09:12 +02:00
|
|
|
// Update the WooCommerce billing address in the WooCommerce store
|
2024-09-24 02:09:38 +02:00
|
|
|
new Promise( ( resolve ) => {
|
|
|
|
setWooBillingAddress( newBillingAddress );
|
|
|
|
resolve();
|
|
|
|
} ),
|
2024-10-03 14:09:12 +02:00
|
|
|
// Trigger the Address Card view by setting the billing address editing state to false
|
2024-09-24 02:09:38 +02:00
|
|
|
new Promise( ( resolve ) => {
|
|
|
|
setBillingAddressEditing( false );
|
|
|
|
resolve();
|
|
|
|
} ),
|
|
|
|
] );
|
|
|
|
} else {
|
2024-09-26 12:47:04 +02:00
|
|
|
log( 'Selected card or billing address is missing.', 'error' );
|
2024-09-10 12:03:50 +02:00
|
|
|
}
|
|
|
|
}
|
2024-09-13 22:30:04 +02:00
|
|
|
}, [
|
|
|
|
fastlaneSdk,
|
2024-09-24 02:09:38 +02:00
|
|
|
setCardDetails,
|
2024-09-13 22:30:04 +02:00
|
|
|
setWooBillingAddress,
|
|
|
|
setBillingAddressEditing,
|
|
|
|
] );
|
2024-09-10 12:03:50 +02:00
|
|
|
};
|
2024-09-13 15:27:10 +02:00
|
|
|
|
|
|
|
export default useCardChange;
|