From ece598921c949e86019e483cb74eab0bd5329a0e Mon Sep 17 00:00:00 2001 From: dinamiko Date: Thu, 14 Jul 2022 10:38:27 +0200 Subject: [PATCH] Run checkout js validation before calling PayPal --- modules/ppcp-wc-gateway/resources/js/oxxo.js | 32 ++++++++----------- .../ppcp-wc-gateway/src/Gateway/OXXO/OXXO.php | 25 +++++++++++---- 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/modules/ppcp-wc-gateway/resources/js/oxxo.js b/modules/ppcp-wc-gateway/resources/js/oxxo.js index 76ecc83c3..2c55d2454 100644 --- a/modules/ppcp-wc-gateway/resources/js/oxxo.js +++ b/modules/ppcp-wc-gateway/resources/js/oxxo.js @@ -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); - } - - */ }); diff --git a/modules/ppcp-wc-gateway/src/Gateway/OXXO/OXXO.php b/modules/ppcp-wc-gateway/src/Gateway/OXXO/OXXO.php index 9d158a998..0c6cb2cf4 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/OXXO/OXXO.php +++ b/modules/ppcp-wc-gateway/src/Gateway/OXXO/OXXO.php @@ -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 ''; - }); + echo ''; + } + ); } /** @@ -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' + ), + ), ) ); }