Skip to content

Commit

Permalink
tweak: 'add payment method page' to honor WC rate limiter (#3810)
Browse files Browse the repository at this point in the history
  • Loading branch information
frosso authored Feb 3, 2025
1 parent 47fd9cd commit 1d1f364
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*** Changelog ***

= 9.2.0 - xxxx-xx-xx =
* Tweak - Use WC Core's rate limiter on "Add payment method" page.
* Dev - Introduces new payment intent status constants for the frontend.
* Fix - Fix Stripe customer creation when using the Blocks API for express checkout.
* Add - Add new payment processing flow using confirmation tokens.
Expand Down
23 changes: 21 additions & 2 deletions includes/class-wc-stripe-intent-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,21 @@ public function create_setup_intent() {
return;
}

// similar rate limiter is present in WC Core, but it's executed on page submission (and not on AJAX calls).
$wc_add_payment_method_rate_limit_id = 'add_payment_method_' . get_current_user_id();
if ( WC_Rate_Limiter::retried_too_soon( $wc_add_payment_method_rate_limit_id ) ) {
echo wp_json_encode(
[
'status' => 'error',
'error' => [
'type' => 'setup_intent_error',
'message' => __( 'Failed to save payment method.', 'woocommerce-gateway-stripe' ),
],
]
);
exit;
}

try {
$source_id = wc_clean( wp_unslash( $_POST['stripe_source_id'] ) );

Expand Down Expand Up @@ -1034,9 +1049,13 @@ public function create_and_confirm_setup_intent( $payment_information ) {
* @throws Exception If the AJAX request is missing the required data or if there's an error creating and confirming the setup intent.
*/
public function create_and_confirm_setup_intent_ajax() {
$setup_intent = null;

try {
// similar rate limiter is present in WC Core, but it's executed on page submission (and not on AJAX calls).
$wc_add_payment_method_rate_limit_id = 'add_payment_method_' . get_current_user_id();
if ( WC_Rate_Limiter::retried_too_soon( $wc_add_payment_method_rate_limit_id ) ) {
throw new WC_Stripe_Exception( 'Failed to save payment method.', __( 'You cannot add a new payment method so soon after the previous one.', 'woocommerce-gateway-stripe' ) );
}

$is_nonce_valid = check_ajax_referer( 'wc_stripe_create_and_confirm_setup_intent_nonce', false, false );

if ( ! $is_nonce_valid ) {
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
== Changelog ==

= 9.2.0 - xxxx-xx-xx =
* Tweak - Use WC Core's rate limiter on "Add payment method" page.
* Dev - Introduces new payment intent status constants for the frontend.
* Fix - Fix Stripe customer creation when using the Blocks API for express checkout.
* Add - Add new payment processing flow using confirmation tokens.
Expand Down

0 comments on commit 1d1f364

Please sign in to comment.