woocommerce-paypal-payments/modules/ppcp-googlepay/resources/js/Context/BaseHandler.js

71 lines
1.7 KiB
JavaScript
Raw Normal View History

2024-07-12 12:58:34 +02:00
import ErrorHandler from '../../../../ppcp-button/resources/js/modules/ErrorHandler';
import CartActionHandler from '../../../../ppcp-button/resources/js/modules/ActionHandler/CartActionHandler';
2024-08-13 18:41:42 +02:00
import TransactionInfo from '../Helper/TransactionInfo';
2023-08-29 14:57:10 +01:00
class BaseHandler {
2024-07-12 12:58:34 +02:00
constructor( buttonConfig, ppcpConfig, externalHandler ) {
this.buttonConfig = buttonConfig;
this.ppcpConfig = ppcpConfig;
this.externalHandler = externalHandler;
}
2023-08-29 14:57:10 +01:00
2024-07-12 12:58:34 +02:00
validateContext() {
if ( this.ppcpConfig?.locations_with_subscription_product?.cart ) {
return false;
}
return true;
}
2023-08-29 14:57:10 +01:00
2024-07-12 12:58:34 +02:00
shippingAllowed() {
// Status of the shipping settings in WooCommerce.
return this.buttonConfig.shipping.configured;
}
2024-07-12 12:58:34 +02:00
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;
}
2023-10-13 14:36:11 +01:00
2024-07-12 12:58:34 +02:00
// handle script reload
const data = result.data;
2024-08-13 18:53:39 +02:00
const transaction = new TransactionInfo(
data.total,
2024-08-13 19:31:51 +02:00
data.shipping_fee,
2024-08-13 18:53:39 +02:00
data.currency_code,
data.country_code
2024-08-13 18:41:42 +02:00
);
2024-08-13 18:53:39 +02:00
resolve( transaction );
2024-07-12 12:58:34 +02:00
} );
} );
}
2023-08-29 14:57:10 +01:00
2024-07-12 12:58:34 +02:00
createOrder() {
return this.actionHandler().configuration().createOrder( null, null );
}
2023-08-29 14:57:10 +01:00
2024-07-12 12:58:34 +02:00
approveOrder( data, actions ) {
return this.actionHandler().configuration().onApprove( data, actions );
}
2023-08-29 14:57:10 +01:00
2024-07-12 12:58:34 +02:00
actionHandler() {
return new CartActionHandler( this.ppcpConfig, this.errorHandler() );
}
2023-08-29 14:57:10 +01:00
2024-07-12 12:58:34 +02:00
errorHandler() {
return new ErrorHandler(
this.ppcpConfig.labels.error.generic,
document.querySelector( '.woocommerce-notices-wrapper' )
);
}
2023-08-29 14:57:10 +01:00
}
export default BaseHandler;