Skip to content

Commit

Permalink
feat(my-account): reCAPTCHA check on add payment method (#3673)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkoo authored Jan 13, 2025
1 parent f8290a7 commit 4a46c8e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
25 changes: 25 additions & 0 deletions includes/class-recaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ public static function init() {
// Add reCAPTCHA to the Woo checkout form.
\add_action( 'woocommerce_review_order_before_submit', [ __CLASS__, 'add_recaptcha_v2_to_checkout' ] );
\add_action( 'woocommerce_checkout_after_customer_details', [ __CLASS__, 'add_recaptcha_v3_to_checkout' ] );
\add_action( 'woocommerce_add_payment_method_form_bottom', [ __CLASS__, 'add_recaptcha_v3_to_checkout' ] );

// Verify reCAPTCHA on checkout submission.
\add_action( 'woocommerce_checkout_process', [ __CLASS__, 'verify_recaptcha_on_checkout' ] );

// Verify reCAPTCHA when adding new payment method.
\add_filter( 'woocommerce_add_payment_method_form_is_valid', [ __CLASS__, 'verify_recaptcha_on_add_payment_method' ] );
}

/**
Expand Down Expand Up @@ -520,6 +524,27 @@ public static function verify_recaptcha_on_checkout() {
WooCommerce_Connection::add_wc_notice( $check->get_error_message(), 'error' );
}
}

/**
* Verify reCAPTCHA when adding new payment method in My Account.
*
* @param bool $is_valid Whether the form is valid.
*
* @rturn bool
*/
public static function verify_recaptcha_on_add_payment_method( $is_valid ) {
$url = \home_url( \add_query_arg( null, null ) );
$should_verify_captcha = apply_filters( 'newspack_recaptcha_verify_captcha', self::can_use_captcha(), $url );
if ( ! $should_verify_captcha ) {
return $is_valid;
}
$check = self::verify_captcha();
if ( \is_wp_error( $check ) ) {
WooCommerce_Connection::add_wc_notice( $check->get_error_message(), 'error' );
return false;
}
return $is_valid;
}
}

Recaptcha::init();
4 changes: 3 additions & 1 deletion src/other-scripts/recaptcha/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ function render( forms = [], onSuccess = null, onError = null ) {

const formsToHandle = forms.length
? forms
: [ ...document.querySelectorAll( 'form[data-newspack-recaptcha]' ) ];
: [ ...document.querySelectorAll(
'form[data-newspack-recaptcha],form#add_payment_method',
) ];

formsToHandle.forEach( form => {
const renderForm = () => {
Expand Down

0 comments on commit 4a46c8e

Please sign in to comment.