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
|
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 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-24 14:46:12 +02:00
|
|
|
import usePaymentSetupEffect from './hooks/usePaymentSetupEffect';
|
2024-10-05 02:26:09 +02:00
|
|
|
import usePayPalCommerceGateway from './hooks/usePayPalCommerceGateway';
|
2024-09-05 21:17:36 +02:00
|
|
|
|
|
|
|
// Components
|
2024-09-13 17:27:39 +02:00
|
|
|
import { Payment } from './components/Payment/Payment';
|
2024-10-16 12:51:26 +02:00
|
|
|
import { TitleLabel } from './components/TitleLabel';
|
2024-09-24 02:09:38 +02:00
|
|
|
|
2024-09-09 18:06:29 +02:00
|
|
|
const gatewayHandle = 'ppcp-axo-gateway';
|
2024-10-05 02:26:09 +02:00
|
|
|
const namespace = 'ppcpBlocksPaypalAxo';
|
|
|
|
const initialConfig = wc.wcSettings.getSetting( `${ gatewayHandle }_data` );
|
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-10-05 02:26:09 +02:00
|
|
|
const { isConfigLoaded, ppcpConfig } =
|
|
|
|
usePayPalCommerceGateway( initialConfig );
|
|
|
|
|
|
|
|
const axoConfig = window.wc_ppcp_axo;
|
|
|
|
|
|
|
|
const fastlaneSdk = useFastlaneSdk( namespace, axoConfig, ppcpConfig );
|
2024-09-14 00:21:27 +02:00
|
|
|
const tokenizedCustomerData = useTokenizeCustomerData();
|
|
|
|
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-10-05 02:26:09 +02:00
|
|
|
const isScriptLoaded = useAxoSetup(
|
|
|
|
namespace,
|
|
|
|
ppcpConfig,
|
|
|
|
isConfigLoaded,
|
|
|
|
fastlaneSdk,
|
|
|
|
paymentComponent
|
|
|
|
);
|
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-10-05 02:26:09 +02:00
|
|
|
if ( ! isConfigLoaded ) {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{ __(
|
|
|
|
'Loading configuration…',
|
|
|
|
'woocommerce-paypal-payments'
|
|
|
|
) }
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! isScriptLoaded ) {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{ __(
|
|
|
|
'Loading PayPal script…',
|
|
|
|
'woocommerce-paypal-payments'
|
|
|
|
) }
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! fastlaneSdk ) {
|
|
|
|
return (
|
|
|
|
<>{ __( 'Loading Fastlane…', 'woocommerce-paypal-payments' ) }</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2024-09-05 21:17:36 +02:00
|
|
|
<Payment
|
|
|
|
fastlaneSdk={ fastlaneSdk }
|
|
|
|
onPaymentLoad={ handlePaymentLoad }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
registerPaymentMethod( {
|
2024-10-05 02:26:09 +02:00
|
|
|
name: initialConfig.id,
|
2024-10-16 12:51:26 +02:00
|
|
|
label: <TitleLabel config={ initialConfig } />,
|
2024-09-05 21:17:36 +02:00
|
|
|
content: <Axo />,
|
2024-10-05 02:26:09 +02:00
|
|
|
edit: createElement( initialConfig.title ),
|
|
|
|
ariaLabel: initialConfig.title,
|
2024-09-13 19:24:23 +02:00
|
|
|
canMakePayment: () => true,
|
2024-09-05 21:17:36 +02:00
|
|
|
supports: {
|
|
|
|
showSavedCards: true,
|
2024-10-05 02:26:09 +02:00
|
|
|
features: initialConfig.supports,
|
2024-09-05 21:17:36 +02:00
|
|
|
},
|
|
|
|
} );
|
|
|
|
|
|
|
|
export default Axo;
|