woocommerce-paypal-payments/modules/ppcp-googlepay/resources/js/Context/BaseHandler.js
Narek Zakarian cb5060b10b
fix(googlepay): gate renewal rendering on manual renewals setting
Google Pay doesn't support vaulting, so it can't use the same
exemption Apple Pay relies on for subscription contexts. The only
valid case for Google Pay to render on a subscription is paying to
manually renew an existing one, so validateContext() now checks the
store's "Accept Manual Renewals" setting for that case, and still
unconditionally blocks a brand-new subscription in the cart.
2026-07-03 16:23:40 +04:00

78 lines
2 KiB
JavaScript

import ErrorHandler from '@ppcp-button/ErrorHandler';
import CartActionHandler from '@ppcp-button/ActionHandler/CartActionHandler';
import TransactionInfo from '@ppcp-googlepay/Helper/TransactionInfo';
class BaseHandler {
constructor( buttonConfig, ppcpConfig, externalHandler ) {
this.buttonConfig = buttonConfig;
this.ppcpConfig = ppcpConfig;
this.externalHandler = externalHandler;
}
validateContext() {
// Google Pay doesn't support vaulting, so the only case where it's allowed on a
// subscription is paying to manually renew an existing one.
if ( this.ppcpConfig?.locations_with_subscription_product?.payorder ) {
return !! this.ppcpConfig?.subscriptions_accept_manual_renewals;
}
if ( this.ppcpConfig?.locations_with_subscription_product?.cart ) {
return false;
}
return true;
}
shippingAllowed() {
// Status of the shipping settings in WooCommerce.
return (
this.buttonConfig.shipping.enabled &&
this.buttonConfig.shipping.configured
);
}
transactionInfo() {
return new Promise( ( resolve, reject ) => {
fetch( this.ppcpConfig.ajax.cart_script_params.endpoint, {
method: 'GET',
credentials: 'same-origin',
} )
.then( ( result ) => result.json() )
.then( ( result ) => {
if ( ! result.success ) {
return;
}
// handle script reload
const data = result.data;
const transaction = new TransactionInfo(
data.total,
data.shipping_fee,
data.currency_code,
data.country_code
);
resolve( transaction );
} );
} );
}
createOrder() {
return this.actionHandler().configuration().createOrder( null, null );
}
approveOrder( data, actions ) {
return this.actionHandler().configuration().onApprove( data, actions );
}
actionHandler() {
return new CartActionHandler( this.ppcpConfig, this.errorHandler() );
}
errorHandler() {
return new ErrorHandler(
this.ppcpConfig.labels.error.generic,
document.querySelector( '.woocommerce-notices-wrapper' )
);
}
}
export default BaseHandler;