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

88 lines
2.3 KiB
JavaScript
Raw Normal View History

import { useCallback, useState } from '@wordpress/element';
2024-09-05 21:17:36 +02:00
import { registerPaymentMethod } from '@woocommerce/blocks-registry';
// Hooks
import useFastlaneSdk from './hooks/useFastlaneSdk';
import useCardChange from './hooks/useCardChange';
import useAxoSetup from './hooks/useAxoSetup';
import usePaymentSetup from './hooks/usePaymentSetup';
import useAxoCleanup from './hooks/useAxoCleanup';
2024-09-05 21:17:36 +02:00
// Components
import { Payment } from './components/Payment/Payment';
2024-09-05 21:17:36 +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;
const Axo = ( props ) => {
const { eventRegistration, emitResponse } = props;
const { onPaymentSetup } = eventRegistration;
2024-09-05 21:17:36 +02:00
const [ shippingAddress, setShippingAddress ] = useState( null );
const [ card, setCard ] = useState( null );
const fastlaneSdk = useFastlaneSdk( axoConfig, ppcpConfig );
2024-09-13 23:09:51 +02:00
const onChangeCardButtonClick = useCardChange( fastlaneSdk, setCard );
useAxoSetup(
ppcpConfig,
2024-09-11 00:39:09 +02:00
fastlaneSdk,
onChangeCardButtonClick,
setShippingAddress,
setCard
);
usePaymentSetup( onPaymentSetup, emitResponse, card );
2024-09-13 23:09:51 +02:00
useAxoCleanup();
2024-09-05 21:17:36 +02:00
const handlePaymentLoad = useCallback( ( paymentComponent ) => {
console.log( 'Payment component loaded', paymentComponent );
}, [] );
2024-09-05 21:17:36 +02:00
const handleCardChange = ( selectedCard ) => {
console.log( 'Card selection changed', selectedCard );
2024-09-05 21:17:36 +02:00
setCard( selectedCard );
};
console.log( 'Rendering Axo component', {
fastlaneSdk,
card,
shippingAddress,
} );
2024-09-05 21:17:36 +02:00
return fastlaneSdk ? (
<Payment
fastlaneSdk={ fastlaneSdk }
card={ card }
onChange={ handleCardChange }
2024-09-05 21:17:36 +02:00
onPaymentLoad={ handlePaymentLoad }
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,
canMakePayment: () => true,
2024-09-05 21:17:36 +02:00
supports: {
showSavedCards: true,
features: ppcpConfig.supports,
},
} );
export default Axo;