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

82 lines
2.1 KiB
JavaScript
Raw Normal View History

2024-07-12 12:58:34 +02:00
import SingleProductActionHandler from '../../../../ppcp-button/resources/js/modules/ActionHandler/SingleProductActionHandler';
import SimulateCart from '../../../../ppcp-button/resources/js/modules/Helper/SimulateCart';
import ErrorHandler from '../../../../ppcp-button/resources/js/modules/ErrorHandler';
import UpdateCart from '../../../../ppcp-button/resources/js/modules/Helper/UpdateCart';
import BaseHandler from './BaseHandler';
2024-08-13 18:41:42 +02:00
import TransactionInfo from '../Helper/TransactionInfo';
2023-08-28 17:19:07 +01:00
2023-08-29 14:57:10 +01:00
class SingleProductHandler extends BaseHandler {
2024-07-12 12:58:34 +02:00
validateContext() {
if ( this.ppcpConfig?.locations_with_subscription_product?.product ) {
return false;
}
return true;
}
2023-08-28 17:19:07 +01:00
2024-07-12 12:58:34 +02:00
transactionInfo() {
const errorHandler = new ErrorHandler(
this.ppcpConfig.labels.error.generic,
document.querySelector( '.woocommerce-notices-wrapper' )
);
2024-07-12 12:58:34 +02:00
function form() {
return document.querySelector( 'form.cart' );
}
2023-08-28 17:19:07 +01:00
2024-07-12 12:58:34 +02:00
const actionHandler = new SingleProductActionHandler(
null,
null,
form(),
errorHandler
);
2023-08-28 17:19:07 +01:00
2024-07-12 12:58:34 +02:00
const hasSubscriptions =
PayPalCommerceGateway.data_client_id.has_subscriptions &&
PayPalCommerceGateway.data_client_id.paypal_subscriptions_enabled;
2023-08-28 17:19:07 +01:00
2024-07-12 12:58:34 +02:00
const products = hasSubscriptions
? actionHandler.getSubscriptionProducts()
: actionHandler.getProducts();
2023-08-28 17:19:07 +01:00
2024-07-12 12:58:34 +02:00
return new Promise( ( resolve, reject ) => {
new SimulateCart(
this.ppcpConfig.ajax.simulate_cart.endpoint,
this.ppcpConfig.ajax.simulate_cart.nonce
).simulate( ( 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
}, products );
} );
}
2023-08-28 17:19:07 +01:00
2024-07-12 12:58:34 +02:00
createOrder() {
return this.actionHandler()
.configuration()
.createOrder( null, null, {
updateCartOptions: {
keepShipping: true,
},
} );
}
2023-08-28 17:19:07 +01:00
2024-07-12 12:58:34 +02:00
actionHandler() {
return new SingleProductActionHandler(
this.ppcpConfig,
new UpdateCart(
this.ppcpConfig.ajax.change_cart.endpoint,
this.ppcpConfig.ajax.change_cart.nonce
),
document.querySelector( 'form.cart' ),
this.errorHandler()
);
}
2023-08-28 17:19:07 +01:00
}
export default SingleProductHandler;