mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-01 07:02:48 +08:00
Do not load paypal script on checkout block methods registration
This commit is contained in:
parent
5ab6d3b27d
commit
a99fbc6eb3
1 changed files with 19 additions and 3 deletions
|
@ -15,6 +15,8 @@ window.ppcpFundingSource = config.fundingSource;
|
|||
|
||||
let registeredContext = false;
|
||||
|
||||
let paypalScriptPromise = null;
|
||||
|
||||
const PayPalComponent = ({
|
||||
onClick,
|
||||
onClose,
|
||||
|
@ -32,6 +34,15 @@ const PayPalComponent = ({
|
|||
|
||||
const [paypalOrder, setPaypalOrder] = useState(null);
|
||||
|
||||
const [paypalScriptLoaded, setPaypalScriptLoaded] = useState(false);
|
||||
|
||||
if (!paypalScriptLoaded) {
|
||||
if (!paypalScriptPromise) {
|
||||
paypalScriptPromise = loadPaypalScriptPromise(config.scriptData)
|
||||
}
|
||||
paypalScriptPromise.then(() => setPaypalScriptLoaded(true));
|
||||
}
|
||||
|
||||
const methodId = fundingSource ? `${config.id}-${fundingSource}` : config.id;
|
||||
|
||||
useEffect(() => {
|
||||
|
@ -307,7 +318,11 @@ const PayPalComponent = ({
|
|||
|
||||
const style = normalizeStyleForFundingSource(config.scriptData.button.style, fundingSource);
|
||||
|
||||
const PayPalButton = window.paypal.Buttons.driver("react", { React, ReactDOM });
|
||||
if (!paypalScriptLoaded) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const PayPalButton = paypal.Buttons.driver("react", { React, ReactDOM });
|
||||
|
||||
return (
|
||||
<PayPalButton
|
||||
|
@ -361,8 +376,6 @@ if (config.scriptData.continuation) {
|
|||
},
|
||||
});
|
||||
} else if (!config.usePlaceOrder) {
|
||||
const paypalScriptPromise = loadPaypalScriptPromise(config.scriptData);
|
||||
|
||||
for (const fundingSource of ['paypal', ...config.enabledFundingSources]) {
|
||||
registerExpressPaymentMethod({
|
||||
name: `${config.id}-${fundingSource}`,
|
||||
|
@ -372,6 +385,9 @@ if (config.scriptData.continuation) {
|
|||
edit: <PayPalComponent isEditing={true} fundingSource={fundingSource}/>,
|
||||
ariaLabel: config.title,
|
||||
canMakePayment: async () => {
|
||||
if (!paypalScriptPromise) {
|
||||
paypalScriptPromise = loadPaypalScriptPromise(config.scriptData)
|
||||
}
|
||||
await paypalScriptPromise;
|
||||
|
||||
return paypal.Buttons({fundingSource}).isEligible();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue