Skip to content

Conversation

diegocurbelo
Copy link
Member

@diegocurbelo diegocurbelo commented May 15, 2025

Fixes #4270
Relates to STRIPE-445

Changes proposed in this Pull Request:

This PR implements a mechanism to prevent unnecessary Stripe API calls when the API keys are invalid (401 response).

  • Added an option to track invalid API keys state
  • Added automatic clearing of invalid API keys state when new keys are saved
  • Updated the notice messages, and the state in the connection status check to show the Account as disconnected when API keys are invalid

This helps reduce unnecessary API calls and improves error notices/UI for merchants with invalid API credentials:

Screenshot 2025-05-15 at 16 16 00

Screenshot 2025-05-16 at 10 42 29

Screenshot 2025-05-16 at 12 50 36

Testing instructions

  1. Navigate to WP-Admin > WooCommerce > Settings > Payments, and select the Stripe option from the list
  2. Go to the Settings tab and make sure the account is correctly connected:
    Screenshot 2025-05-16 at 13 10 22
  3. Replace the API keys with an invalid value:
    wp option patch update woocommerce_stripe_settings test_secret_key 'sk_test_INVALID'
  4. Navigate to the WP-Admin dashboard and check that Stripe notice is shwon:
    Screenshot 2025-05-16 at 13 13 54
  5. Navigate to WP-Admin > WooCommerce > Settings > Payments, and select the Stripe option from the list
  6. Go to the Settings tab and make sure the account status has the connection error message:
    Screenshot 2025-05-16 at 13 16 09
  7. Click Configure connection and make sure the Account status is Disconnected
    Screenshot 2025-05-16 at 13 16 59
  8. Click the Create or connect a test account button, a re-connect your account
  9. Check that after returning to the WP-Admin dasboard, the top notice is no longere present, and the acount status section has been restored:
    Screenshot 2025-05-16 at 13 25 43

  • Covered with tests (or have a good reason not to test in description ☝️)
  • Tested on mobile (or does not apply)

Changelog entry

  • This Pull Request does not require a changelog entry. (Comment required below)
Changelog Entry Comment

Comment

Post merge

Copy link
Contributor

@daledupreez daledupreez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is the general approach should work well, with a concern about triggering the code after one 401 response.

More broadly, I think we also need to clear the transient when we save/update the Stripe keys, which feels like it ought to occur in WC_Stripe_Connect->save_stripe_keys() and whatever code we use to disconnect/clear keys.

@diegocurbelo diegocurbelo marked this pull request as ready for review May 16, 2025 16:26
@diegocurbelo diegocurbelo requested review from a team and annemirasol and removed request for a team May 16, 2025 16:26
@@ -133,11 +133,11 @@ const AccountDetails = () => {
{ createInterpolateElement(
isTestModeEnabled
? __(
"Seems like the test API keys we've saved for you are no longer valid. If you recently updated them, use the <strong>Configure Connection</strong> button below to reconnect.",
"We couldn't connect to your account, it seems like the test API keys we've saved for you are no longer valid. Please use the <strong>Configure connection</strong> button below to reconnect.",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not urgent in this case. But, will this break internationalization of this string in the patch release?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That said, I think the trade-off for correctly communicating the status is probably worth it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, new strings need to be translated in the community translations site: https://translate.wordpress.org/projects/wp-plugins/woocommerce-gateway-stripe/

Copy link
Contributor

@daledupreez daledupreez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, this is looking good and tests as described.

I do think we need to fix the typos in the changelogs, and I had one minor comment about possibly making the path to identify the response code more defensive.

@@ -133,11 +133,11 @@ const AccountDetails = () => {
{ createInterpolateElement(
isTestModeEnabled
? __(
"Seems like the test API keys we've saved for you are no longer valid. If you recently updated them, use the <strong>Configure Connection</strong> button below to reconnect.",
"We couldn't connect to your account, it seems like the test API keys we've saved for you are no longer valid. Please use the <strong>Configure connection</strong> button below to reconnect.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call.

@@ -242,6 +263,18 @@ public static function retrieve( $api ) {
]
);

// If we get a 401 error, we know the secret key is not valid.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could consider later changing this to check for anything different from 200, to cover more possible issues.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My proposed changes in #4327 take that approach when looking at responses. 😁

Copy link
Contributor

@daledupreez daledupreez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the cleanup, @diegocurbelo! 🚀

@wjrosa wjrosa merged commit 962163c into develop May 17, 2025
49 of 51 checks passed
@wjrosa wjrosa deleted the fix/handle-stripe-api-401-errors branch May 17, 2025 14:41
wjrosa pushed a commit that referenced this pull request May 17, 2025
…401 response from API) (#4323)

* Add transient to prevent api calls after getting a 401 response

* Update includes/class-wc-stripe-api.php

Co-authored-by: daledupreez <[email protected]>

* Update error response to empty json

* Remove invalid transient when saving new keys

* Refactored to use get_option/update_option for concistency with how we save similar data for onboarding and webhooks stats

* Update code comments

* Update account not connected notification message

* Show account as disconnected if the account data is not valid

* Refactor 401 response to be null (needed for the Configure connection modal)

* Clear account cache after detecting a 401 response

* Add tests

* Simplify option keys in test

* Fix success response mock

* Add changelo entry

* Apply suggestions from code review

Co-authored-by: daledupreez <[email protected]>

---------

Co-authored-by: daledupreez <[email protected]>
@daledupreez daledupreez added this to the 9.5.1 milestone Jun 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unauthorized API requests tried far to many times
5 participants