-
Notifications
You must be signed in to change notification settings - Fork 216
Add rate-limit/cooldown to Stripe API requests after 401 errors #4497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add rate-limit/cooldown to Stripe API requests after 401 errors #4497
Conversation
…the next retry time in the context
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have some feedback based on inspection.
Co-authored-by: daledupreez <dale.du.preez@automattic.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works as described 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes look good to me and test well.
I have one non-blocking comment that we should think about, but I am not sure if we need to take action on it.
if ( null !== $invalid_api_key_error_count ) { | ||
WC_Stripe_Database_Cache::delete( self::INVALID_API_KEY_ERROR_COUNT_CACHE_KEY ); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not blocking, but do we want to confirm we got a 2xx response code before we clear this cache? I don't think it's a big thing either way, but clearing the cache for any non-401 response feels like it may be the wrong approach in some situations, e.g. 500 or 403 responses.
The That fix is outside the scope of this PR, and I'll fix it in a new PR. For now, I updated the
|
📈 PHP Unit Code Coverage Report
|
Fixes STRIPE-586
Changes proposed in this Pull Request:
When the UPE Gateway is instantiated, every available payment method is instantiated, and each one reads the account country to see if the PM can be enabled or not.
This is usually not a problem because we have a static cache and a database cache in place, so we only make one API call. However, when the Stripe keys are invalid and the account cache expires, each attempt to read the account country makes a Stripe API call, which means ~25 requests every time the UPE Gateway is instantiated.
This PR prevents Stripe API calls after several consecutive 401 (Unauthorized) responses (5) by detecting invalid API keys, storing a flag, and blocking further requests for 2 hours or until the keys are updated.
This improves performance and user feedback by avoiding repeated failed attempts and indicating a disconnected state in the UI.
NOTE: We should not be displaying the
Invalid API key...
error message to customers; I have created a follow-up issue STRIPE-632 for this.Testing instructions
WP-Admin > WooCommerce > Settings > Payments
, and select theStripe
option from the listSettings
tab and make sure the account is correctly connected:wp option patch update woocommerce_stripe_settings test_secret_key 'sk_test_INVALID'
WP-Admin
and into the pluginSettings
tab and make sure the account connection error message is displayed:Go to
WooCommerce > Status > Los
and then clickwoocommerce-gateway-stripe
:Configure connection
and make sure the Account status isDisconnected
Create or connect a test account
button, a re-connect your accountChangelog entry
Changelog Entry Comment
Comment
Post merge