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

96 lines
2.9 KiB
JavaScript
Raw Normal View History

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;
}
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
}
validateContext() {
if ( this.ppcpConfig?.locations_with_subscription_product?.cart ) {
return this.isVaultV3Mode();
}
return true;
}
2023-10-31 10:35:29 +00:00
shippingAllowed() {
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) => {
const endpoint = this.ppcpConfig.ajax.cart_script_params.endpoint;
const separator = (endpoint.indexOf('?') !== -1) ? '&' : '?';
2023-09-07 09:56:46 +02:00
fetch(
endpoint + separator + 'shipping=1',
2023-09-07 09:56:46 +02:00
{
method: 'GET',
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',
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')
);
}
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;