Skip to content

Commit 514ea97

Browse files
daledupreezdiegocurbelo
authored andcommitted
Ensure we clear the in-memory Stripe API keys for key situations (#4348)
* Ensure we clear the in-memory Stripe API keys for key situations * Call WC_Stripe_API::set_secret_key() statically
1 parent 8ccf2b5 commit 514ea97

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Add - Implement custom database cache for persistent caching with in-memory optimization.
55
* Update - Remove feature that flags 401s and proactively blocks subsequent API calls until the store has reauthenticated.
66
* Fix - Disable payment settings sync when we receive unsupported payment method configurations.
7+
* Fix - Ensure that we use current Stripe API keys after settings updates
78

89
= 9.5.1 - 2025-05-17 =
910
* Fix - Add a fetch cooldown to the payment method configuration retrieval endpoint to prevent excessive requests.

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,5 +114,6 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
114114
* Add - Implement custom database cache for persistent caching with in-memory optimization.
115115
* Update - Remove feature that flags 401s and proactively blocks subsequent API calls until the store has reauthenticated.
116116
* Fix - Disable payment settings sync when we receive unsupported payment method configurations.
117+
* Fix - Ensure that we use current Stripe API keys after settings updates
117118

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

woocommerce-gateway-stripe.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,13 +598,48 @@ public function gateway_settings_update( $settings, $old_settings ) {
598598
$settings = array_merge( $old_settings, $settings );
599599
}
600600

601+
// Note that we need to run these checks before we call toggle_upe() below.
602+
$this->maybe_reset_stripe_in_memory_key( $settings, $old_settings );
603+
601604
if ( ! WC_Stripe_Feature_Flags::is_upe_preview_enabled() ) {
602605
return $settings;
603606
}
604607

605608
return $this->toggle_upe( $settings, $old_settings );
606609
}
607610

611+
/**
612+
* Helper function that ensures we clear the in-memory Stripe API key in {@see WC_Stripe_API}
613+
* when we're making a change to our settings that impacts which secret key we should be using.
614+
*
615+
* @param array $settings New settings that have just been saved.
616+
* @param array $old_settings Old settings that were previously saved.
617+
* @return void
618+
*/
619+
protected function maybe_reset_stripe_in_memory_key( $settings, $old_settings ) {
620+
// If we're making a change that impacts which secret key we should be using,
621+
// we need to clear the static key being used by WC_Stripe_API.
622+
// Note that this also needs to run before we call toggle_upe() below.
623+
$should_clear_stripe_api_key = false;
624+
625+
$settings_to_check = [
626+
'testmode',
627+
'secret_key',
628+
'test_secret_key',
629+
];
630+
631+
foreach ( $settings_to_check as $setting_to_check ) {
632+
if ( isset( $settings[ $setting_to_check ] ) && isset( $old_settings[ $setting_to_check ] ) && $settings[ $setting_to_check ] !== $old_settings[ $setting_to_check ] ) {
633+
$should_clear_stripe_api_key = true;
634+
break;
635+
}
636+
}
637+
638+
if ( $should_clear_stripe_api_key ) {
639+
WC_Stripe_API::set_secret_key( '' );
640+
}
641+
}
642+
608643
/**
609644
* Enable or disable UPE.
610645
*

0 commit comments

Comments
 (0)