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

77 lines
2 KiB
JavaScript
Raw Normal View History

2024-09-25 00:09:27 +02:00
import { useState, createElement } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
2024-09-05 21:17:36 +02:00
import { registerPaymentMethod } from '@woocommerce/blocks-registry';
// Hooks
import useFastlaneSdk from './hooks/useFastlaneSdk';
import useTokenizeCustomerData from './hooks/useTokenizeCustomerData';
import useAxoSetup from './hooks/useAxoSetup';
import useAxoCleanup from './hooks/useAxoCleanup';
import useHandlePaymentSetup from './hooks/useHandlePaymentSetup';
import usePaymentSetupEffect from './hooks/usePaymentSetupEffect';
2024-09-05 21:17:36 +02:00
// Components
import { Payment } from './components/Payment/Payment';
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;
const [ paymentComponent, setPaymentComponent ] = useState( null );
2024-09-11 12:50:05 +02:00
const fastlaneSdk = useFastlaneSdk( axoConfig, ppcpConfig );
const tokenizedCustomerData = useTokenizeCustomerData();
const handlePaymentSetup = useHandlePaymentSetup(
emitResponse,
paymentComponent,
tokenizedCustomerData
);
useAxoSetup( ppcpConfig, fastlaneSdk, paymentComponent );
2024-09-05 21:17:36 +02:00
const { handlePaymentLoad } = usePaymentSetupEffect(
onPaymentSetup,
handlePaymentSetup,
setPaymentComponent
);
2024-09-05 21:17:36 +02:00
useAxoCleanup();
2024-09-05 21:17:36 +02:00
return fastlaneSdk ? (
<Payment
fastlaneSdk={ fastlaneSdk }
onPaymentLoad={ handlePaymentLoad }
/>
) : (
2024-09-25 00:09:27 +02:00
<>{ __( 'Loading Fastlane…', 'woocommerce-paypal-payments' ) }</>
2024-09-05 21:17:36 +02:00
);
};
registerPaymentMethod( {
name: ppcpConfig.id,
label: (
<div
id="ppcp-axo-block-radio-label"
dangerouslySetInnerHTML={ { __html: ppcpConfig.title } }
/>
),
content: <Axo />,
2024-09-25 00:09:27 +02:00
edit: createElement( ppcpConfig.title ),
2024-09-05 21:17:36 +02:00
ariaLabel: ppcpConfig.title,
canMakePayment: () => true,
2024-09-05 21:17:36 +02:00
supports: {
showSavedCards: true,
features: ppcpConfig.supports,
},
} );
export default Axo;