mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 18:16:38 +08:00
Run checkout js validation before calling PayPal
This commit is contained in:
parent
09ef106a80
commit
ece598921c
2 changed files with 33 additions and 24 deletions
|
@ -1,9 +1,22 @@
|
||||||
|
import ErrorHandler from '../../../ppcp-button/resources/js/modules/ErrorHandler';
|
||||||
|
|
||||||
window.addEventListener('load', function() {
|
window.addEventListener('load', function() {
|
||||||
|
|
||||||
const oxxoButton = document.getElementById('ppcp-oxxo');
|
const oxxoButton = document.getElementById('ppcp-oxxo');
|
||||||
oxxoButton?.addEventListener('click', (event) => {
|
oxxoButton?.addEventListener('click', (event) => {
|
||||||
event.preventDefault();
|
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, {
|
fetch(OXXOConfig.oxxo_endpoint, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
|
@ -13,7 +26,7 @@ window.addEventListener('load', function() {
|
||||||
return res.json();
|
return res.json();
|
||||||
}).then((data)=>{
|
}).then((data)=>{
|
||||||
if (!data.success) {
|
if (!data.success) {
|
||||||
alert('Could not update signup buttons: ' + JSON.stringify(data));
|
alert('Could not get payer action from PayPal: ' + JSON.stringify(data));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,21 +39,4 @@ window.addEventListener('load', function() {
|
||||||
document.querySelector('#place_order').click()
|
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
*/
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -94,10 +94,13 @@ class OXXO {
|
||||||
array( $this, 'register_assets' )
|
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 {
|
public function register_assets(): void {
|
||||||
$gateway_settings = get_option( 'woocommerce_ppcp-oxxo-gateway_settings' );
|
$gateway_settings = get_option( 'woocommerce_ppcp-oxxo-gateway_settings' );
|
||||||
$gateway_enabled = $gateway_settings['enabled'] ?? '';
|
$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(
|
wp_enqueue_script(
|
||||||
'ppcp-oxxo',
|
'ppcp-oxxo',
|
||||||
trailingslashit( $this->module_url ) . 'assets/js/oxxo.js',
|
trailingslashit( $this->module_url ) . 'assets/js/oxxo.js',
|
||||||
|
@ -144,6 +147,16 @@ class OXXO {
|
||||||
array(
|
array(
|
||||||
'oxxo_endpoint' => \WC_AJAX::get_endpoint( 'ppc-oxxo' ),
|
'oxxo_endpoint' => \WC_AJAX::get_endpoint( 'ppc-oxxo' ),
|
||||||
'oxxo_nonce' => wp_create_nonce( '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'
|
||||||
|
),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue