Run checkout js validation before calling PayPal

This commit is contained in:
dinamiko 2022-07-14 10:38:27 +02:00
parent 09ef106a80
commit ece598921c
2 changed files with 33 additions and 24 deletions

View file

@ -1,9 +1,22 @@
import ErrorHandler from '../../../ppcp-button/resources/js/modules/ErrorHandler';
window.addEventListener('load', function() {
const oxxoButton = document.getElementById('ppcp-oxxo');
oxxoButton?.addEventListener('click', (event) => {
event.preventDefault();
const requiredFields = jQuery('form.woocommerce-checkout .validate-required:visible :input');
requiredFields.each((i, input) => {
jQuery(input).trigger('validate');
});
if (jQuery('form.woocommerce-checkout .validate-required.woocommerce-invalid:visible').length) {
const errorHandler = new ErrorHandler(OXXOConfig.error.generic);
errorHandler.clear();
errorHandler.message(OXXOConfig.error.js_validation);
return;
}
fetch(OXXOConfig.oxxo_endpoint, {
method: 'POST',
body: JSON.stringify({
@ -13,7 +26,7 @@ window.addEventListener('load', function() {
return res.json();
}).then((data)=>{
if (!data.success) {
alert('Could not update signup buttons: ' + JSON.stringify(data));
alert('Could not get payer action from PayPal: ' + JSON.stringify(data));
return;
}
@ -26,21 +39,4 @@ window.addEventListener('load', function() {
document.querySelector('#place_order').click()
});
});
/*
const oxxoButton = document.getElementById('ppcp-oxxo-payer-action');
if(oxxoButton) {
oxxoButton.addEventListener('click', (event) => {
event.preventDefault();
window.open(
oxxoButton.href,
'_blank',
'popup'
);
});
window.open(oxxoButton.href);
}
*/
});

View file

@ -94,10 +94,13 @@ class OXXO {
array( $this, 'register_assets' )
);
add_action('woocommerce_review_order_after_payment', function () {
add_action(
'woocommerce_review_order_after_payment',
function () {
echo '<button class="button" id="ppcp-oxxo">Pago en OXXO</button>';
});
echo '<button class="button" id="ppcp-oxxo">Pago en OXXO</button>';
}
);
}
/**
@ -128,7 +131,7 @@ class OXXO {
public function register_assets(): void {
$gateway_settings = get_option( 'woocommerce_ppcp-oxxo-gateway_settings' );
$gateway_enabled = $gateway_settings['enabled'] ?? '';
if ( $gateway_enabled === 'yes' && is_checkout() ) { // && ! empty( is_wc_endpoint_url( 'order-received' ) )
if ( $gateway_enabled === 'yes' && is_checkout() ) {
wp_enqueue_script(
'ppcp-oxxo',
trailingslashit( $this->module_url ) . 'assets/js/oxxo.js',
@ -142,8 +145,18 @@ class OXXO {
'ppcp-oxxo',
'OXXOConfig',
array(
'oxxo_endpoint' => \WC_AJAX::get_endpoint( 'ppc-oxxo' ),
'oxxo_nonce' => wp_create_nonce( 'ppc-oxxo' ),
'oxxo_endpoint' => \WC_AJAX::get_endpoint( 'ppc-oxxo' ),
'oxxo_nonce' => wp_create_nonce( 'ppc-oxxo' ),
'error' => array(
'generic' => __(
'Something went wrong. Please try again or choose another payment source.',
'woocommerce-paypal-payments'
),
'js_validation' => __(
'Required form fields are not filled or invalid.',
'woocommerce-paypal-payments'
),
),
)
);
}