2023-09-07 09:56:46 +02:00
|
|
|
import ErrorHandler from "../../../../ppcp-button/resources/js/modules/ErrorHandler";
|
|
|
|
import CartActionHandler
|
|
|
|
from "../../../../ppcp-button/resources/js/modules/ActionHandler/CartActionHandler";
|
2024-01-31 10:32:21 +00:00
|
|
|
import {isPayPalSubscription} from "../../../../ppcp-blocks/resources/js/Helper/Subscription";
|
2023-09-07 09:56:46 +02:00
|
|
|
|
|
|
|
class BaseHandler {
|
|
|
|
|
|
|
|
constructor(buttonConfig, ppcpConfig) {
|
|
|
|
this.buttonConfig = buttonConfig;
|
|
|
|
this.ppcpConfig = ppcpConfig;
|
|
|
|
}
|
|
|
|
|
2024-01-30 17:46:28 +00:00
|
|
|
isVaultV3Mode() {
|
|
|
|
return this.ppcpConfig?.save_payment_methods?.id_token // vault v3
|
2024-01-31 10:32:21 +00:00
|
|
|
&& ! this.ppcpConfig?.data_client_id?.paypal_subscriptions_enabled // not PayPal Subscriptions mode
|
2024-01-31 14:59:47 +00:00
|
|
|
&& this.ppcpConfig?.can_save_vault_token; // vault is enabled
|
2024-01-30 17:46:28 +00:00
|
|
|
}
|
|
|
|
|
2023-11-13 17:36:18 +00:00
|
|
|
validateContext() {
|
|
|
|
if ( this.ppcpConfig?.locations_with_subscription_product?.cart ) {
|
2024-01-30 17:46:28 +00:00
|
|
|
return this.isVaultV3Mode();
|
2023-11-13 17:36:18 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-10-31 10:35:29 +00:00
|
|
|
shippingAllowed() {
|
2024-07-03 16:39:46 +02:00
|
|
|
return this.buttonConfig.product.needsShipping;
|
2023-10-31 10:35:29 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 09:56:46 +02:00
|
|
|
transactionInfo() {
|
|
|
|
return new Promise((resolve, reject) => {
|
2023-11-09 17:50:57 +00:00
|
|
|
const endpoint = this.ppcpConfig.ajax.cart_script_params.endpoint;
|
|
|
|
const separator = (endpoint.indexOf('?') !== -1) ? '&' : '?';
|
2023-09-07 09:56:46 +02:00
|
|
|
|
|
|
|
fetch(
|
2023-11-09 17:50:57 +00:00
|
|
|
endpoint + separator + 'shipping=1',
|
2023-09-07 09:56:46 +02:00
|
|
|
{
|
|
|
|
method: 'GET',
|
2023-11-09 17:50:57 +00:00
|
|
|
credentials: 'same-origin'
|
2023-09-07 09:56:46 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
.then(result => result.json())
|
|
|
|
.then(result => {
|
|
|
|
if (! result.success) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle script reload
|
|
|
|
const data = result.data;
|
|
|
|
|
|
|
|
resolve({
|
|
|
|
countryCode: data.country_code,
|
|
|
|
currencyCode: data.currency_code,
|
|
|
|
totalPriceStatus: 'FINAL',
|
2023-11-06 10:15:56 +00:00
|
|
|
totalPrice: data.total_str,
|
|
|
|
chosenShippingMethods: data.chosen_shipping_methods || null,
|
|
|
|
shippingPackages: data.shipping_packages || null,
|
2023-09-07 09:56:46 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
createOrder() {
|
2023-10-31 10:35:29 +00:00
|
|
|
return this.actionHandler().configuration().createOrder(null, null);
|
|
|
|
}
|
2023-09-07 09:56:46 +02:00
|
|
|
|
2023-10-31 10:35:29 +00:00
|
|
|
approveOrder(data, actions) {
|
|
|
|
return this.actionHandler().configuration().onApprove(data, actions);
|
|
|
|
}
|
|
|
|
|
|
|
|
actionHandler() {
|
|
|
|
return new CartActionHandler(
|
2023-09-07 09:56:46 +02:00
|
|
|
this.ppcpConfig,
|
2023-10-31 10:35:29 +00:00
|
|
|
this.errorHandler(),
|
2023-09-07 09:56:46 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-10-31 10:35:29 +00:00
|
|
|
errorHandler() {
|
|
|
|
return new ErrorHandler(
|
2023-09-07 09:56:46 +02:00
|
|
|
this.ppcpConfig.labels.error.generic,
|
|
|
|
document.querySelector('.woocommerce-notices-wrapper')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-10-30 14:27:42 +00:00
|
|
|
errorHandler() {
|
|
|
|
return new ErrorHandler(
|
|
|
|
this.ppcpConfig.labels.error.generic,
|
|
|
|
document.querySelector('.woocommerce-notices-wrapper')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-09-07 09:56:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default BaseHandler;
|