2024-09-14 00:21:27 +02:00
|
|
|
import { useState } from '@wordpress/element';
|
2024-09-24 02:09:38 +02:00
|
|
|
import { useSelect } from '@wordpress/data';
|
2024-09-05 21:17:36 +02:00
|
|
|
import { registerPaymentMethod } from '@woocommerce/blocks-registry';
|
|
|
|
|
|
|
|
// Hooks
|
2024-09-12 14:37:22 +02:00
|
|
|
import useFastlaneSdk from './hooks/useFastlaneSdk';
|
2024-09-14 00:21:27 +02:00
|
|
|
import useTokenizeCustomerData from './hooks/useTokenizeCustomerData';
|
2024-09-13 15:27:10 +02:00
|
|
|
import useCardChange from './hooks/useCardChange';
|
2024-09-13 19:24:23 +02:00
|
|
|
import useAxoSetup from './hooks/useAxoSetup';
|
|
|
|
import useAxoCleanup from './hooks/useAxoCleanup';
|
2024-09-14 00:21:27 +02:00
|
|
|
import useHandlePaymentSetup from './hooks/useHandlePaymentSetup';
|
2024-09-05 21:17:36 +02:00
|
|
|
|
|
|
|
// Components
|
2024-09-13 17:27:39 +02:00
|
|
|
import { Payment } from './components/Payment/Payment';
|
2024-09-14 00:21:27 +02:00
|
|
|
import usePaymentSetupEffect from './hooks/usePaymentSetupEffect';
|
2024-09-05 21:17:36 +02:00
|
|
|
|
2024-09-24 02:09:38 +02:00
|
|
|
// Store
|
|
|
|
import { STORE_NAME } from './stores/axoStore';
|
|
|
|
|
2024-09-09 18:06:29 +02:00
|
|
|
const gatewayHandle = 'ppcp-axo-gateway';
|
|
|
|
const ppcpConfig = wc.wcSettings.getSetting( `${ gatewayHandle }_data` );
|
2024-09-05 21:17:36 +02:00
|
|
|
|
|
|
|
if ( typeof window.PayPalCommerceGateway === 'undefined' ) {
|
|
|
|
window.PayPalCommerceGateway = ppcpConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
const axoConfig = window.wc_ppcp_axo;
|
|
|
|
|
2024-09-11 13:00:25 +02:00
|
|
|
const Axo = ( props ) => {
|
|
|
|
const { eventRegistration, emitResponse } = props;
|
|
|
|
const { onPaymentSetup } = eventRegistration;
|
2024-09-11 16:08:59 +02:00
|
|
|
const [ paymentComponent, setPaymentComponent ] = useState( null );
|
2024-09-11 12:50:05 +02:00
|
|
|
|
2024-09-24 02:09:38 +02:00
|
|
|
const { cardDetails } = useSelect(
|
|
|
|
( select ) => ( {
|
|
|
|
cardDetails: select( STORE_NAME ).getCardDetails(),
|
|
|
|
} ),
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
2024-09-12 14:37:22 +02:00
|
|
|
const fastlaneSdk = useFastlaneSdk( axoConfig, ppcpConfig );
|
2024-09-14 00:21:27 +02:00
|
|
|
const tokenizedCustomerData = useTokenizeCustomerData();
|
2024-09-24 02:09:38 +02:00
|
|
|
const onChangeCardButtonClick = useCardChange( fastlaneSdk );
|
2024-09-14 00:21:27 +02:00
|
|
|
const handlePaymentSetup = useHandlePaymentSetup(
|
|
|
|
emitResponse,
|
2024-09-11 16:18:08 +02:00
|
|
|
paymentComponent,
|
2024-09-14 00:21:27 +02:00
|
|
|
tokenizedCustomerData
|
2024-09-11 22:58:13 +02:00
|
|
|
);
|
2024-09-10 12:03:50 +02:00
|
|
|
|
2024-09-13 19:24:23 +02:00
|
|
|
useAxoSetup(
|
|
|
|
ppcpConfig,
|
2024-09-11 00:39:09 +02:00
|
|
|
fastlaneSdk,
|
2024-09-16 17:52:32 +02:00
|
|
|
paymentComponent,
|
2024-09-24 02:09:38 +02:00
|
|
|
onChangeCardButtonClick
|
2024-09-13 19:24:23 +02:00
|
|
|
);
|
2024-09-05 21:17:36 +02:00
|
|
|
|
2024-09-14 00:21:27 +02:00
|
|
|
const { handlePaymentLoad } = usePaymentSetupEffect(
|
|
|
|
onPaymentSetup,
|
2024-09-16 15:45:41 +02:00
|
|
|
handlePaymentSetup,
|
|
|
|
setPaymentComponent
|
2024-09-14 00:21:27 +02:00
|
|
|
);
|
2024-09-05 21:17:36 +02:00
|
|
|
|
2024-09-14 00:21:27 +02:00
|
|
|
useAxoCleanup();
|
2024-09-05 21:17:36 +02:00
|
|
|
|
2024-09-11 22:58:13 +02:00
|
|
|
console.log( 'Rendering Axo component', {
|
|
|
|
fastlaneSdk,
|
|
|
|
} );
|
|
|
|
|
2024-09-05 21:17:36 +02:00
|
|
|
return fastlaneSdk ? (
|
|
|
|
<Payment
|
|
|
|
fastlaneSdk={ fastlaneSdk }
|
2024-09-24 02:09:38 +02:00
|
|
|
card={ cardDetails }
|
|
|
|
onChange={ onChangeCardButtonClick }
|
2024-09-05 21:17:36 +02:00
|
|
|
onPaymentLoad={ handlePaymentLoad }
|
2024-09-10 12:03:50 +02:00
|
|
|
onChangeButtonClick={ onChangeCardButtonClick }
|
2024-09-05 21:17:36 +02:00
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<div>Loading Fastlane...</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
registerPaymentMethod( {
|
|
|
|
name: ppcpConfig.id,
|
|
|
|
label: (
|
|
|
|
<div
|
|
|
|
id="ppcp-axo-block-radio-label"
|
|
|
|
dangerouslySetInnerHTML={ { __html: ppcpConfig.title } }
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
content: <Axo />,
|
|
|
|
edit: <h1>This is Axo Blocks in the editor</h1>,
|
|
|
|
ariaLabel: ppcpConfig.title,
|
2024-09-13 19:24:23 +02:00
|
|
|
canMakePayment: () => true,
|
2024-09-05 21:17:36 +02:00
|
|
|
supports: {
|
|
|
|
showSavedCards: true,
|
|
|
|
features: ppcpConfig.supports,
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
|
|
|
|
export default Axo;
|