Skip to content

Commit f95defe

Browse files
Mayishadaledupreez
authored andcommitted
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 Cherry-picked from 49ab42e.
1 parent 5c6f31c commit f95defe

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
@@ -2,6 +2,7 @@
22

33
= 9.7.1 - xxxx-xx-xx =
44
* Add - Add state mapping for Lithuania in express checkout
5+
* Tweak - Check for checkout validation error before creating a payment method in Stripe
56

67
= 9.7.0 - 2025-07-21 =
78
* Update - Removes BNPL payment methods (Klarna and Affirm) when other official plugins are active

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
@@ -112,6 +112,7 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
112112

113113
= 9.7.1 - xxxx-xx-xx
114114
* Add - Add state mapping for Lithuania in express checkout
115+
* Tweak - Check for checkout validation error before creating a payment method in Stripe
115116

116117
= 9.7.0 - 2025-07-21 =
117118

0 commit comments

Comments
 (0)