woocommerce-paypal-payments/modules/ppcp-applepay/resources/js/boot-block.js

117 lines
3.2 KiB
JavaScript
Raw Normal View History

import { useEffect, useRef, useState } from '@wordpress/element';
2024-07-12 12:58:34 +02:00
import { registerExpressPaymentMethod } from '@woocommerce/blocks-registry';
import { __ } from '@wordpress/i18n';
import { loadPayPalScript } from '../../../ppcp-button/resources/js/modules/Helper/PayPalScriptLoading';
2024-07-12 12:58:34 +02:00
import { cartHasSubscriptionProducts } from '../../../ppcp-blocks/resources/js/Helper/Subscription';
import { loadCustomScript } from '@paypal/paypal-js';
import CheckoutHandler from './Context/CheckoutHandler';
import ApplePayManager from './ApplepayManager';
import ApplePayManagerBlockEditor from './ApplepayManagerBlockEditor';
2023-08-31 12:48:01 +02:00
2024-07-12 12:58:34 +02:00
const ppcpData = wc.wcSettings.getSetting( 'ppcp-gateway_data' );
2023-08-31 12:48:01 +02:00
const ppcpConfig = ppcpData.scriptData;
2024-07-12 12:58:34 +02:00
const buttonData = wc.wcSettings.getSetting( 'ppcp-applepay_data' );
2023-08-31 12:48:01 +02:00
const buttonConfig = buttonData.scriptData;
const namespace = 'ppcpBlocksPaypalApplepay';
2023-08-31 12:48:01 +02:00
2024-07-12 12:58:34 +02:00
if ( typeof window.PayPalCommerceGateway === 'undefined' ) {
window.PayPalCommerceGateway = ppcpConfig;
2023-08-31 12:48:01 +02:00
}
const ApplePayComponent = ( { isEditing, buttonAttributes } ) => {
2024-07-12 12:58:34 +02:00
const [ paypalLoaded, setPaypalLoaded ] = useState( false );
const [ applePayLoaded, setApplePayLoaded ] = useState( false );
const wrapperRef = useRef( null );
2023-08-31 12:48:01 +02:00
2024-07-12 12:58:34 +02:00
useEffect( () => {
if ( isEditing ) {
return;
}
2024-07-12 12:58:34 +02:00
// Load ApplePay SDK
loadCustomScript( { url: buttonConfig.sdk_url } ).then( () => {
setApplePayLoaded( true );
} );
2023-08-31 12:48:01 +02:00
2024-07-12 12:58:34 +02:00
ppcpConfig.url_params.components += ',applepay';
2024-04-23 15:00:05 +02:00
2024-07-12 12:58:34 +02:00
// Load PayPal
loadPayPalScript( namespace, ppcpConfig )
.then( () => {
setPaypalLoaded( true );
} )
.catch( ( error ) => {
console.error( 'Failed to load PayPal script: ', error );
} );
}, [ isEditing ] );
2023-08-31 12:48:01 +02:00
2024-07-12 12:58:34 +02:00
useEffect( () => {
if ( isEditing || ! paypalLoaded || ! applePayLoaded ) {
return;
2024-07-12 12:58:34 +02:00
}
const ManagerClass = isEditing
? ApplePayManagerBlockEditor
: ApplePayManager;
buttonConfig.reactWrapper = wrapperRef.current;
new ManagerClass(
namespace,
buttonConfig,
ppcpConfig,
buttonAttributes
);
}, [ paypalLoaded, applePayLoaded, isEditing, buttonAttributes ] );
if ( isEditing ) {
return (
<ApplePayManagerBlockEditor
namespace={ namespace }
buttonConfig={ buttonConfig }
ppcpConfig={ ppcpConfig }
buttonAttributes={ buttonAttributes }
/>
);
}
2023-08-31 12:48:01 +02:00
2024-07-12 12:58:34 +02:00
return (
<div
ref={ wrapperRef }
2024-07-12 12:58:34 +02:00
id={ buttonConfig.button.wrapper.replace( '#', '' ) }
className="ppcp-button-apm ppcp-button-applepay"
></div>
);
};
2023-08-31 12:48:01 +02:00
2024-07-12 12:58:34 +02:00
const features = [ 'products' ];
2023-08-31 12:48:01 +02:00
2024-01-31 10:32:21 +00:00
if (
2024-07-12 12:58:34 +02:00
cartHasSubscriptionProducts( ppcpConfig ) &&
new CheckoutHandler( buttonConfig, ppcpConfig ).isVaultV3Mode()
2024-01-30 18:22:53 +00:00
) {
2024-07-12 12:58:34 +02:00
features.push( 'subscriptions' );
}
if ( buttonConfig?.is_enabled ) {
registerExpressPaymentMethod( {
name: buttonData.id,
title: `PayPal - ${ buttonData.title }`,
description: __(
'Eligible users will see the PayPal button.',
'woocommerce-paypal-payments'
),
label: <div dangerouslySetInnerHTML={ { __html: buttonData.title } } />,
content: <ApplePayComponent isEditing={ false } />,
edit: <ApplePayComponent isEditing={ true } />,
ariaLabel: buttonData.title,
canMakePayment: () =>
buttonData.enabled && window.ApplePaySession?.canMakePayments(),
supports: {
features,
style: [ 'height', 'borderRadius' ],
},
} );
}