Skip to content

Commit a44699a

Browse files
committed
Remove 401 flag block for API calls (#4342)
* Remove 401 block for API calls * Add readme and changelog * Remove flag reset code, constants * Remove unused unit test function
1 parent a8279a0 commit a44699a

File tree

5 files changed

+4
-89
lines changed

5 files changed

+4
-89
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
= 9.5.2 - xxxx-xx-xx =
44
* Add - Implement custom database cache for persistent caching with in-memory optimization.
5+
* Update - Remove feature that flags 401s and proactively blocks subsequent API calls until the store has reauthenticated.
56
* Fix - Disable payment settings sync when we receive unsupported payment method configurations.
67

78
= 9.5.1 - 2025-05-17 =

includes/class-wc-stripe-api.php

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,6 @@ class WC_Stripe_API {
1616
const ENDPOINT = 'https://api.stripe.com/v1/';
1717
const STRIPE_API_VERSION = '2024-06-20';
1818

19-
/**
20-
* The test mode invalid API keys option key.
21-
*
22-
* @var string
23-
*/
24-
const TEST_MODE_INVALID_API_KEYS_OPTION_KEY = 'wc_stripe_test_invalid_api_keys_detected';
25-
26-
/**
27-
* The live mode invalid API keys option key.
28-
*
29-
* @var string
30-
*/
31-
const LIVE_MODE_INVALID_API_KEYS_OPTION_KEY = 'wc_stripe_live_invalid_api_keys_detected';
32-
3319
/**
3420
* Secret API Key.
3521
*
@@ -245,13 +231,6 @@ public static function request( $request, $api = 'charges', $method = 'POST', $w
245231
* @param string $api
246232
*/
247233
public static function retrieve( $api ) {
248-
// If we have an option flag indicating that the secret key is not valid, we don't attempt the API call and we return an error.
249-
$invalid_api_keys_option_key = WC_Stripe_Mode::is_test() ? self::TEST_MODE_INVALID_API_KEYS_OPTION_KEY : self::LIVE_MODE_INVALID_API_KEYS_OPTION_KEY;
250-
$invalid_api_keys_detected = get_option( $invalid_api_keys_option_key );
251-
if ( $invalid_api_keys_detected ) {
252-
return null; // The UI expects this empty response in case of invalid API keys.
253-
}
254-
255234
WC_Stripe_Logger::log( "{$api}" );
256235

257236
$response = wp_safe_remote_get(
@@ -265,13 +244,6 @@ public static function retrieve( $api ) {
265244

266245
// If we get a 401 error, we know the secret key is not valid.
267246
if ( is_array( $response ) && isset( $response['response'] ) && is_array( $response['response'] ) && isset( $response['response']['code'] ) && 401 === $response['response']['code'] ) {
268-
// We save a flag in the options to avoid making calls until the secret key gets updated.
269-
update_option( $invalid_api_keys_option_key, true );
270-
update_option( $invalid_api_keys_option_key . '_at', time() );
271-
272-
// We delete the transient for the account data to trigger the not-connected UI in the admin dashboard.
273-
delete_transient( WC_Stripe_Mode::is_test() ? WC_Stripe_Account::TEST_ACCOUNT_OPTION : WC_Stripe_Account::LIVE_ACCOUNT_OPTION );
274-
275247
// Stripe redacts API keys in the response.
276248
WC_Stripe_Logger::log( "Error: GET {$api} returned a 401 " . print_r( $response, true ) );
277249

includes/connect/class-wc-stripe-connect.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,6 @@ private function save_stripe_keys( $result, $type = 'connect', $mode = 'live' )
183183
update_option( 'wc_stripe_' . $prefix . 'oauth_failed_attempts', 0 );
184184
update_option( 'wc_stripe_' . $prefix . 'oauth_last_failed_at', '' );
185185

186-
// Clear the invalid API keys transient.
187-
$invalid_api_keys_option_key = $is_test ? WC_Stripe_API::TEST_MODE_INVALID_API_KEYS_OPTION_KEY : WC_Stripe_API::LIVE_MODE_INVALID_API_KEYS_OPTION_KEY;
188-
update_option( $invalid_api_keys_option_key, false );
189-
update_option( $invalid_api_keys_option_key . '_at', time() );
190-
191186
if ( 'app' === $type ) {
192187
// Stripe App OAuth access_tokens expire after 1 hour:
193188
// https://docs.stripe.com/stripe-apps/api-authentication/oauth#refresh-access-token

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
112112

113113
= 9.5.2 - xxxx-xx-xx =
114114
* Add - Implement custom database cache for persistent caching with in-memory optimization.
115+
* Update - Remove feature that flags 401s and proactively blocks subsequent API calls until the store has reauthenticated.
115116
* Fix - Disable payment settings sync when we receive unsupported payment method configurations.
116117

117118
[See changelog for full details across versions](https://raw.githubusercontent.com/woocommerce/woocommerce-gateway-stripe/trunk/changelog.txt).

tests/phpunit/test-class-wc-stripe-api.php

Lines changed: 2 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -95,51 +95,10 @@ public function test_set_secret_key_for_mode_with_parameter() {
9595
$this->assertEquals( self::LIVE_SECRET_KEY, WC_Stripe_API::get_secret_key() );
9696
}
9797

98-
/**
99-
* Test WC_Stripe_API::retrieve() when API returns 401 error.
100-
*/
101-
public function test_retrieve_handles_401_error() {
102-
// Mock a 401 API response
103-
add_filter( 'pre_http_request', [ $this, 'mock_401_response' ] );
104-
105-
// Call the retrieve method
106-
$result = WC_Stripe_API::retrieve( 'test_endpoint' );
107-
108-
// Verify the result is null
109-
$this->assertNull( $result );
110-
111-
// Verify the invalid API keys option was set
112-
$this->assertTrue( get_option( WC_Stripe_API::TEST_MODE_INVALID_API_KEYS_OPTION_KEY ) );
113-
114-
// Clean up
115-
remove_filter( 'pre_http_request', [ $this, 'mock_401_response' ] );
116-
delete_option( WC_Stripe_API::TEST_MODE_INVALID_API_KEYS_OPTION_KEY );
117-
}
118-
119-
/**
120-
* Test WC_Stripe_API::retrieve() when API keys are invalid.
121-
*/
122-
public function test_retrieve_returns_null_when_api_keys_are_invalid() {
123-
// Set up the invalid API keys option
124-
update_option( WC_Stripe_API::TEST_MODE_INVALID_API_KEYS_OPTION_KEY, true );
125-
126-
// Call the retrieve method
127-
$result = WC_Stripe_API::retrieve( 'test_endpoint' );
128-
129-
// Verify the result is null
130-
$this->assertNull( $result );
131-
132-
// Clean up
133-
delete_option( WC_Stripe_API::TEST_MODE_INVALID_API_KEYS_OPTION_KEY );
134-
}
135-
13698
/**
13799
* Test WC_Stripe_API::retrieve() when API keys are valid.
138100
*/
139101
public function test_retrieve_makes_api_call_when_api_keys_are_valid() {
140-
// Ensure no invalid API keys option exists
141-
delete_option( WC_Stripe_API::TEST_MODE_INVALID_API_KEYS_OPTION_KEY );
142-
143102
// Mock a successful API response
144103
add_filter( 'pre_http_request', [ $this, 'mock_successful_response' ] );
145104

@@ -159,23 +118,10 @@ public function test_retrieve_makes_api_call_when_api_keys_are_valid() {
159118
public function mock_successful_response() {
160119
return [
161120
'response' => [
162-
'code' => 200,
121+
'code' => 200,
163122
'message' => 'OK',
164123
],
165-
'body' => json_encode( 'success' ),
166-
];
167-
}
168-
169-
/**
170-
* Helper method to mock a 401 API response.
171-
*/
172-
public function mock_401_response() {
173-
return [
174-
'response' => [
175-
'code' => 401,
176-
'message' => 'Unauthorized',
177-
],
178-
'body' => '',
124+
'body' => json_encode( 'success' ),
179125
];
180126
}
181127
}

0 commit comments

Comments
 (0)