mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 18:16:38 +08:00
37 lines
732 B
JavaScript
37 lines
732 B
JavaScript
export default class FormValidator {
|
|
constructor( url, nonce ) {
|
|
this.url = url;
|
|
this.nonce = nonce;
|
|
}
|
|
|
|
async validate( form ) {
|
|
const formData = new FormData( form );
|
|
|
|
const res = await fetch( this.url, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
credentials: 'same-origin',
|
|
body: JSON.stringify( {
|
|
nonce: this.nonce,
|
|
form_encoded: new URLSearchParams( formData ).toString(),
|
|
} ),
|
|
} );
|
|
|
|
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 [];
|
|
}
|
|
}
|