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

88 lines
2.5 KiB
JavaScript
Raw Normal View History

2024-07-12 12:58:34 +02:00
import { useEffect, useState } from '@wordpress/element';
import {
registerExpressPaymentMethod,
registerPaymentMethod,
} from '@woocommerce/blocks-registry';
2024-09-04 12:53:22 +01:00
import { __ } from '@wordpress/i18n';
2024-07-12 12:58:34 +02:00
import { loadPaypalScript } from '../../../ppcp-button/resources/js/modules/Helper/ScriptLoading';
import GooglepayManager from './GooglepayManager';
import { loadCustomScript } from '@paypal/paypal-js';
import GooglepayManagerBlockEditor from './GooglepayManagerBlockEditor';
2023-08-25 16:25:20 +01:00
2024-07-12 12:58:34 +02:00
const ppcpData = wc.wcSettings.getSetting( 'ppcp-gateway_data' );
2023-08-25 16:25:20 +01:00
const ppcpConfig = ppcpData.scriptData;
2024-07-12 12:58:34 +02:00
const buttonData = wc.wcSettings.getSetting( 'ppcp-googlepay_data' );
2023-08-25 16:25:20 +01:00
const buttonConfig = buttonData.scriptData;
const dataNamespace = 'ppcpBlocksEditorPaypalGooglepay';
2023-08-25 16:25:20 +01:00
2024-07-12 12:58:34 +02:00
if ( typeof window.PayPalCommerceGateway === 'undefined' ) {
window.PayPalCommerceGateway = ppcpConfig;
2023-08-25 16:25:20 +01:00
}
const GooglePayComponent = ( props ) => {
2024-07-12 12:58:34 +02:00
const [ bootstrapped, setBootstrapped ] = useState( false );
const [ paypalLoaded, setPaypalLoaded ] = useState( false );
const [ googlePayLoaded, setGooglePayLoaded ] = useState( false );
const bootstrap = function () {
const ManagerClass = props.isEditing
? GooglepayManagerBlockEditor
: GooglepayManager;
const manager = new ManagerClass( buttonConfig, ppcpConfig );
2024-07-12 12:58:34 +02:00
manager.init();
};
useEffect( () => {
// Load GooglePay SDK
loadCustomScript( { url: buttonConfig.sdk_url } ).then( () => {
setGooglePayLoaded( true );
} );
ppcpConfig.url_params.components += ',googlepay';
if ( props.isEditing ) {
ppcpConfig.data_namespace = dataNamespace;
}
2024-07-12 12:58:34 +02:00
// Load PayPal
loadPaypalScript( ppcpConfig, () => {
setPaypalLoaded( true );
} );
}, [] );
useEffect( () => {
if ( ! bootstrapped && paypalLoaded && googlePayLoaded ) {
setBootstrapped( true );
bootstrap();
}
}, [ paypalLoaded, googlePayLoaded ] );
return (
<div
id={ buttonConfig.button.wrapper.replace( '#', '' ) }
className="ppcp-button-apm ppcp-button-googlepay"
></div>
);
};
const features = [ 'products' ];
2023-08-25 16:25:20 +01:00
2024-07-12 12:58:34 +02:00
registerExpressPaymentMethod( {
name: buttonData.id,
title: `PayPal - ${ buttonData.title }`,
2024-09-04 14:57:41 +01:00
description: __(
'Eligible users will see the PayPal button.',
'woocommerce-paypal-payments'
),
gatewayId: 'ppcp-gateway',
2024-07-12 12:58:34 +02:00
label: <div dangerouslySetInnerHTML={ { __html: buttonData.title } } />,
content: <GooglePayComponent isEditing={ false } />,
edit: <GooglePayComponent isEditing={ true } />,
ariaLabel: buttonData.title,
canMakePayment: () => buttonData.enabled,
supports: {
features,
},
} );