Merge branch 'trunk' into PCP-4156-implement-3ds-for-google-pay

This commit is contained in:
carmenmaymo 2025-03-20 16:30:05 +01:00
commit 784af34b1e
No known key found for this signature in database
GPG key ID: 6023F686B0F3102E
38 changed files with 1030 additions and 784 deletions

View file

@ -54,7 +54,7 @@ class Renderer {
const enabledSeparateGateways = Object.fromEntries(
Object.entries( settings.separate_buttons ).filter(
( [ s, data ] ) => document.querySelector( data.wrapper )
( [ , data ] ) => document.querySelector( data.wrapper )
)
);
const hasEnabledSeparateGateways =
@ -65,15 +65,16 @@ class Renderer {
this.renderButtons(
settings.button.wrapper,
settings.button.style,
contextConfig,
hasEnabledSeparateGateways
contextConfig
);
}
} else {
const allFundingSources = paypal.getFundingSources();
const separateFunding = allFundingSources.filter(
( s ) => ! ( s in enabledSeparateGateways )
);
// render each button separately
for ( const fundingSource of paypal
.getFundingSources()
.filter( ( s ) => ! ( s in enabledSeparateGateways ) ) ) {
for ( const fundingSource of separateFunding ) {
const style = normalizeStyleForFundingSource(
settings.button.style,
fundingSource
@ -83,7 +84,6 @@ class Renderer {
settings.button.wrapper,
style,
contextConfig,
hasEnabledSeparateGateways,
fundingSource
);
}
@ -103,26 +103,15 @@ class Renderer {
data.wrapper,
data.style,
contextConfig,
hasEnabledSeparateGateways,
fundingSource
);
}
}
renderButtons(
wrapper,
style,
contextConfig,
hasEnabledSeparateGateways,
fundingSource = null
) {
renderButtons( wrapper, style, contextConfig, fundingSource = null ) {
if (
! document.querySelector( wrapper ) ||
this.isAlreadyRendered(
wrapper,
fundingSource,
hasEnabledSeparateGateways
)
this.isAlreadyRendered( wrapper, fundingSource )
) {
// Try to render registered buttons again in case they were removed from the DOM by an external source.
widgetBuilder.renderButtons( [ wrapper, fundingSource ] );
@ -144,10 +133,7 @@ class Renderer {
this.onSmartButtonClick( data, actions );
}
venmoButtonClicked = false;
if ( data.fundingSource === 'venmo' ) {
venmoButtonClicked = true;
}
venmoButtonClicked = data.fundingSource === 'venmo';
},
onInit: ( data, actions ) => {
if ( this.onSmartButtonsInit ) {
@ -161,29 +147,29 @@ class Renderer {
if ( this.shouldEnableShippingCallback() ) {
options.onShippingOptionsChange = ( data, actions ) => {
const shippingOptionsChange =
! this.isVenmoButtonClickedWhenVaultingIsEnabled(
venmoButtonClicked
)
? handleShippingOptionsChange(
data,
actions,
this.defaultSettings
)
: null;
! this.isVenmoButtonClickedWhenVaultingIsEnabled(
venmoButtonClicked
)
? handleShippingOptionsChange(
data,
actions,
this.defaultSettings
)
: null;
return shippingOptionsChange;
};
options.onShippingAddressChange = ( data, actions ) => {
const shippingAddressChange =
! this.isVenmoButtonClickedWhenVaultingIsEnabled(
venmoButtonClicked
)
? handleShippingAddressChange(
data,
actions,
this.defaultSettings
)
: null;
! this.isVenmoButtonClickedWhenVaultingIsEnabled(
venmoButtonClicked
)
? handleShippingAddressChange(
data,
actions,
this.defaultSettings
)
: null;
return shippingAddressChange;
};
@ -228,12 +214,11 @@ class Renderer {
}
);
this.renderedSources.add( wrapper + ( fundingSource ?? '' ) );
this.renderedSources.add(
wrapper + ( fundingSource ? fundingSource : '' )
);
if (
typeof paypal !== 'undefined' &&
typeof paypal.Buttons !== 'undefined'
) {
if ( window.paypal?.Buttons ) {
widgetBuilder.registerButtons(
[ wrapper, fundingSource ],
buttonsOptions()
@ -246,15 +231,16 @@ class Renderer {
return venmoButtonClicked && this.defaultSettings.vaultingEnabled;
};
shouldEnableShippingCallback = () => {
shouldEnableShippingCallback = () => {
const needShipping =
this.defaultSettings.needShipping ||
this.defaultSettings.context === 'product';
return (
this.defaultSettings.should_handle_shipping_in_paypal &&
needShipping
);
};
};
isAlreadyRendered( wrapper, fundingSource ) {
return this.renderedSources.has( wrapper + ( fundingSource ?? '' ) );
@ -272,6 +258,7 @@ class Renderer {
this.onButtonsInitListeners[ wrapper ] = reset
? []
: this.onButtonsInitListeners[ wrapper ] || [];
this.onButtonsInitListeners[ wrapper ].push( handler );
}
@ -283,12 +270,11 @@ class Renderer {
if ( this.onButtonsInitListeners[ wrapper ] ) {
for ( const handler of this.onButtonsInitListeners[ wrapper ] ) {
if ( typeof handler === 'function' ) {
handler( {
wrapper,
...this.buttonsOptions[ wrapper ],
} );
if ( typeof handler !== 'function' ) {
continue;
}
handler( { wrapper, ...this.buttonsOptions[ wrapper ] } );
}
}
}
@ -297,10 +283,11 @@ class Renderer {
if ( ! this.buttonsOptions[ wrapper ] ) {
return;
}
try {
this.buttonsOptions[ wrapper ].actions.disable();
} catch ( err ) {
console.log( 'Failed to disable buttons: ' + err );
console.warn( 'Failed to disable buttons: ' + err );
}
}
@ -308,10 +295,11 @@ class Renderer {
if ( ! this.buttonsOptions[ wrapper ] ) {
return;
}
try {
this.buttonsOptions[ wrapper ].actions.enable();
} catch ( err ) {
console.log( 'Failed to enable buttons: ' + err );
console.warn( 'Failed to enable buttons: ' + err );
}
}
}

View file

@ -40,8 +40,9 @@ class WidgetBuilder {
renderButtons( wrapper ) {
wrapper = this.sanitizeWrapper( wrapper );
const entryKey = this.toKey( wrapper );
if ( ! this.buttons.has( this.toKey( wrapper ) ) ) {
if ( ! this.buttons.has( entryKey ) ) {
return;
}
@ -49,11 +50,11 @@ class WidgetBuilder {
return;
}
const entry = this.buttons.get( this.toKey( wrapper ) );
const entry = this.buttons.get( entryKey );
const btn = this.paypal.Buttons( entry.options );
if ( ! btn.isEligible() ) {
this.buttons.delete( this.toKey( wrapper ) );
this.buttons.delete( entryKey );
return;
}
@ -67,7 +68,7 @@ class WidgetBuilder {
}
renderAllButtons() {
for ( const [ wrapper, entry ] of this.buttons ) {
for ( const [ wrapper ] of this.buttons ) {
this.renderButtons( wrapper );
}
}