Skip to content

Commit 49ab42e

Browse files
authored
Fix unnecessary payment method creation when checkout has validation errors (#4502)
* check if block checkout has validation error * check if shortcode checkout has validation error * use comment and variable * add changelog * use 'hasValidationErrors' to check for error on block checkout
1 parent d9160e3 commit 49ab42e

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* Update - Add the number of pending webhooks to the Account status section
2828
* Fix - Prevent "Undefined array key charges_enabled" PHP warning when determining live‑mode status
2929
* Update - Deprecate `wc_gateway_stripe_process_payment`, `wc_gateway_stripe_process_redirect_payment` and `wc_gateway_stripe_process_webhook_payment` actions in favour of `wc_gateway_stripe_process_payment_charge`
30+
* Tweak - Check for checkout validation error before creating a payment method in Stripe
3031
* Add - Add state mapping for Lithuania in express checkout
3132
* Tweak - Use wp_ajax prefix for its built-in security for Add Payment Method action
3233

client/blocks/upe/upe-deferred-intent-creation/payment-processor.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44
import { getPaymentMethods } from '@woocommerce/blocks-registry';
55
import { __ } from '@wordpress/i18n';
6+
import { select } from '@wordpress/data';
67
import {
78
PaymentElement,
89
useElements,
@@ -194,6 +195,16 @@ const PaymentProcessor = ( {
194195
};
195196
}
196197

198+
const { validationStore } = window.wc?.wcBlocksData ?? {};
199+
if ( validationStore ) {
200+
const store = select( validationStore );
201+
const hasValidationErrors = store.hasValidationErrors();
202+
// Return if there is a validation error on the checkout fields.
203+
if ( hasValidationErrors ) {
204+
return;
205+
}
206+
}
207+
197208
// BLIK is a special case which is not handled through the Stripe element.
198209
if ( ! ( isPaymentElementComplete || isBlikSelected ) ) {
199210
return {

client/classic/upe/deferred-intent.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,22 @@ jQuery( function ( $ ) {
4141
} );
4242

4343
$( 'form.checkout' ).on( generateCheckoutEventNames(), function () {
44+
const $form = $( 'form.checkout' );
45+
// Lose focus for all fields to trigger validation
46+
$form
47+
.find( '.input-text, select, input:checkbox' )
48+
.trigger( 'validate' )
49+
.trigger( 'blur' );
50+
51+
const hasValidationErrors =
52+
$form.find( '.woocommerce-invalid' ).length > 0 &&
53+
$form.find( '.woocommerce-invalid' ).is( ':visible' );
54+
55+
// Return if there is a validation error on the checkout fields
56+
if ( hasValidationErrors ) {
57+
return;
58+
}
59+
4460
return processPaymentIfNotUsingSavedMethod( $( this ) );
4561
} );
4662

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
137137
* Update - Add the number of pending webhooks to the Account status section
138138
* Fix - Prevent "Undefined array key charges_enabled" PHP warning when determining live‑mode status
139139
* Update - Deprecate `wc_gateway_stripe_process_payment`, `wc_gateway_stripe_process_redirect_payment` and `wc_gateway_stripe_process_webhook_payment` actions in favour of `wc_gateway_stripe_process_payment_charge`
140+
* Tweak - Check for checkout validation error before creating a payment method in Stripe
140141
* Add - Add state mapping for Lithuania in express checkout
141142
* Tweak - Use wp_ajax prefix for its built-in security for Add Payment Method action
142143

0 commit comments

Comments
 (0)