From a54347587c147350629fd5103ce5cf7915931af2 Mon Sep 17 00:00:00 2001 From: Dale du Preez Date: Fri, 16 May 2025 21:44:49 +0200 Subject: [PATCH] Add notices for active and repeated rate limiting --- .../admin/class-wc-stripe-admin-notices.php | 79 +++++++++++++++++++ includes/class-wc-stripe-api.php | 4 +- 2 files changed, 81 insertions(+), 2 deletions(-) diff --git a/includes/admin/class-wc-stripe-admin-notices.php b/includes/admin/class-wc-stripe-admin-notices.php index e9775ece05..89c24b10ad 100644 --- a/includes/admin/class-wc-stripe-admin-notices.php +++ b/includes/admin/class-wc-stripe-admin-notices.php @@ -63,6 +63,9 @@ public function admin_notices() { // Main Stripe payment method. $this->stripe_check_environment(); + // Check if we are hitting Stripe API rate limits. + $this->stripe_rate_limit_check(); + // All other payment methods. $this->payment_methods_check_environment(); @@ -458,6 +461,82 @@ public function payment_methods_check_environment() { } } + /** + * Check if we should show any notices due to us hitting Stripe API rate limits. + * + * @since x.x.x + */ + public function stripe_rate_limit_check() { + if ( WC_Stripe_API::is_stripe_api_rate_limited() ) { + $this->add_admin_notice( + 'wc_stripe_api_rate_limit_active', + 'notice notice-error', + __( + 'The Stripe API is currently rate limited. If this persists for more than a minute or two, please contact our support team to investigate.', + 'woocommerce-gateway-stripe' + ), + false + ); + return; + } + + $is_test_mode = WC_Stripe_Mode::is_test(); + $rate_limit_option_key = $is_test_mode ? WC_Stripe_API::TEST_MODE_STRIPE_API_RATE_LIMIT_OPTION_KEY : WC_Stripe_API::LIVE_MODE_STRIPE_API_RATE_LIMIT_OPTION_KEY; + $rate_limit_history = get_option( $rate_limit_option_key . '_history', [] ); + if ( empty( $rate_limit_history ) ) { + return; + } + + // Look back to see if we have had 3 or more rate limit errors in the last hour. + $timestamps = wp_list_pluck( $rate_limit_history, 'timestamp' ); + if ( count( $timestamps ) < 3 ) { + return; + } + + $current_timestamp = time(); + + $timestamps_in_last_hour = array_filter( + $timestamps, + function ( $timestamp ) use ( $current_timestamp ) { + return $current_timestamp - $timestamp <= HOUR_IN_SECONDS; + } + ); + + if ( count( $timestamps_in_last_hour ) >= 3 ) { + $this->add_admin_notice( + 'wc_stripe_api_rate_limit_hour_3', + 'notice notice-error', + __( + 'The Stripe API has been rate limited 3 or more times in the last hour. Please contact our support team to investigate.', + 'woocommerce-gateway-stripe' + ), + false + ); + return; + } + + $timestamps_in_last_day = array_filter( + $timestamps, + function ( $timestamp ) use ( $current_timestamp ) { + return $current_timestamp - $timestamp <= DAY_IN_SECONDS; + } + ); + + if ( count( $timestamps_in_last_day ) < 5 ) { + return; + } + + $this->add_admin_notice( + 'wc_stripe_api_rate_limit_day_10', + 'notice notice-error', + __( + 'The Stripe API has been rate limited 5 or more times in the last day. Please contact our support team to investigate.', + 'woocommerce-gateway-stripe' + ), + false + ); + } + /** * Environment check for subscriptions. * diff --git a/includes/class-wc-stripe-api.php b/includes/class-wc-stripe-api.php index 0d994cf810..5c64ab9ce5 100644 --- a/includes/class-wc-stripe-api.php +++ b/includes/class-wc-stripe-api.php @@ -351,8 +351,8 @@ protected static function check_stripe_api_error_response( $response ) { 'datetime' => gmdate( 'Y-m-d H:i:s', $timestamp ) . ' UTC', 'duration' => self::STRIPE_API_RATE_LIMIT_DURATION_IN_SECONDS, ]; - // Note that we set autoload to false - we don't want this option to be autoloaded by default. - update_option( $history_option_key, $history, false ); + + update_option( $history_option_key, $history ); } }