2024-10-08 14:25:43 +02:00
|
|
|
import { useEffect, useRef, useState } from '@wordpress/element';
|
2024-07-12 12:58:34 +02:00
|
|
|
import { registerExpressPaymentMethod } from '@woocommerce/blocks-registry';
|
2024-10-08 14:25:43 +02:00
|
|
|
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';
|
2024-07-23 17:36:10 +02:00
|
|
|
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;
|
2024-10-23 15:46:38 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-11-14 09:25:09 +01:00
|
|
|
const ApplePayComponent = ( { isEditing, buttonAttributes } ) => {
|
2024-07-12 12:58:34 +02:00
|
|
|
const [ paypalLoaded, setPaypalLoaded ] = useState( false );
|
|
|
|
const [ applePayLoaded, setApplePayLoaded ] = useState( false );
|
2024-10-08 14:25:43 +02:00
|
|
|
const wrapperRef = useRef( null );
|
2023-08-31 12:48:01 +02:00
|
|
|
|
2024-07-12 12:58:34 +02:00
|
|
|
useEffect( () => {
|
2024-10-11 11:13:15 +02:00
|
|
|
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
|
2024-10-08 14:25:43 +02:00
|
|
|
loadPayPalScript( namespace, ppcpConfig )
|
|
|
|
.then( () => {
|
|
|
|
setPaypalLoaded( true );
|
|
|
|
} )
|
|
|
|
.catch( ( error ) => {
|
|
|
|
console.error( 'Failed to load PayPal script: ', error );
|
|
|
|
} );
|
2024-10-11 11:13:15 +02:00
|
|
|
}, [ isEditing ] );
|
2023-08-31 12:48:01 +02:00
|
|
|
|
2024-07-12 12:58:34 +02:00
|
|
|
useEffect( () => {
|
2024-10-11 11:13:15 +02:00
|
|
|
if ( isEditing || ! paypalLoaded || ! applePayLoaded ) {
|
2024-10-08 14:25:43 +02:00
|
|
|
return;
|
2024-07-12 12:58:34 +02:00
|
|
|
}
|
2024-10-08 14:25:43 +02:00
|
|
|
|
2024-10-11 11:13:15 +02:00
|
|
|
const ManagerClass = isEditing
|
2024-10-08 14:25:43 +02:00
|
|
|
? ApplePayManagerBlockEditor
|
|
|
|
: ApplePayManager;
|
|
|
|
|
|
|
|
buttonConfig.reactWrapper = wrapperRef.current;
|
|
|
|
|
2024-11-14 09:25:09 +01:00
|
|
|
new ManagerClass(
|
|
|
|
namespace,
|
|
|
|
buttonConfig,
|
|
|
|
ppcpConfig,
|
|
|
|
buttonAttributes
|
|
|
|
);
|
|
|
|
}, [ paypalLoaded, applePayLoaded, isEditing, buttonAttributes ] );
|
2024-10-11 11:13:15 +02:00
|
|
|
|
|
|
|
if ( isEditing ) {
|
|
|
|
return (
|
|
|
|
<ApplePayManagerBlockEditor
|
|
|
|
namespace={ namespace }
|
|
|
|
buttonConfig={ buttonConfig }
|
|
|
|
ppcpConfig={ ppcpConfig }
|
2024-11-14 09:25:09 +01:00
|
|
|
buttonAttributes={ buttonAttributes }
|
2024-10-11 11:13:15 +02:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2023-08-31 12:48:01 +02:00
|
|
|
|
2024-07-12 12:58:34 +02:00
|
|
|
return (
|
|
|
|
<div
|
2024-10-08 14:25:43 +02:00
|
|
|
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' );
|
2024-01-30 17:46:28 +00:00
|
|
|
}
|
|
|
|
|
2025-08-05 19:17:04 -04:00
|
|
|
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' ],
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
}
|