@@ -598,13 +598,48 @@ public function gateway_settings_update( $settings, $old_settings ) {
598
598
$ settings = array_merge ( $ old_settings , $ settings );
599
599
}
600
600
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
+
601
604
if ( ! WC_Stripe_Feature_Flags::is_upe_preview_enabled () ) {
602
605
return $ settings ;
603
606
}
604
607
605
608
return $ this ->toggle_upe ( $ settings , $ old_settings );
606
609
}
607
610
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
+
608
643
/**
609
644
* Enable or disable UPE.
610
645
*
0 commit comments