Skip to content

Commit

Permalink
fix: refresh v2 widgets on Woo checkout update or error
Browse files Browse the repository at this point in the history
  • Loading branch information
dkoo committed Dec 17, 2024
1 parent 9e9f4b9 commit fe03f44
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/other-scripts/recaptcha/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,10 @@ function addHiddenV3Field( form ) {
setInterval( () => refreshV3Token( field, action ), 30000 ); // Refresh token every 30 seconds.

// Refresh reCAPTCHAs on Woo checkout update and error.
( function ( $ ) {
if ( ! $ ) {
return;
}
$( document ).on( 'updated_checkout', () => refreshV3Token( field, action ) );
$( document.body ).on( 'checkout_error', () => refreshV3Token( field, action ) );
} )( jQuery );
if ( jQuery ) {
jQuery( document ).on( 'updated_checkout', () => refreshV3Token( field, action ) );
jQuery( document.body ).on( 'checkout_error', () => refreshV3Token( field, action ) );
}
}
}

Expand Down Expand Up @@ -179,6 +176,13 @@ function renderV2Widget( form, onSuccess = null, onError = null ) {
'expired-callback': errorCallback,
} );
button.setAttribute( 'data-recaptcha-widget-id', widgetId );

// Refresh reCAPTCHA widgets on Woo checkout update and error.
if ( jQuery ) {
jQuery( document ).on( 'updated_checkout', () => renderV2Widget( form, onSuccess, onError ) );
jQuery( document.body ).on( 'checkout_error', () => renderV2Widget( form, onSuccess, onError ) );
}

button.addEventListener( 'click', e => {
e.preventDefault();
e.stopImmediatePropagation();
Expand Down

0 comments on commit fe03f44

Please sign in to comment.