woocommerce-paypal-payments/modules/ppcp-button/resources/js/modules/Helper/FormValidator.js
Alex P 3c557907f2
Refresh checkout totals after validation if needed
Refreshing the totals the same way as WC does this.
2023-04-04 11:35:16 +03:00

38 lines
966 B
JavaScript

export default class FormValidator {
constructor(url, nonce) {
this.url = url;
this.nonce = nonce;
}
async validate(form) {
const formData = new FormData(form);
const formJsonObj = Object.fromEntries(formData.entries());
const res = await fetch(this.url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
credentials: 'same-origin',
body: JSON.stringify({
nonce: this.nonce,
form: formJsonObj,
}),
});
const data = await res.json();
if (!data.success) {
if (data.data.refresh) {
jQuery( document.body ).trigger( 'update_checkout' );
}
if (data.data.errors) {
return data.data.errors;
}
throw Error(data.data.message);
}
return [];
}
}