Skip to content

Commit 624c8fe

Browse files
Prevent PMC migration to run when the plugin is not connected to Stripe (#4593)
* Add is_connected check to PMC is_enabled method * Update includes/class-wc-stripe-helper.php * Udpate is_enabled() docblock --------- Co-authored-by: daledupreez <[email protected]>
1 parent 9bc2005 commit 624c8fe

11 files changed

+45
-16
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* Fix - Relax customer validation that was preventing payments from the pay for order page
1212
* Fix - Remove connection type requirement from PMC sync migration attempt
1313
* Tweak - Small improvements to e2e tests
14+
* Fix - Prevent the PMC migration to run when the plugin is not connected to Stripe
1415

1516
= 9.8.0 - 2025-08-11 =
1617
* Add - Adds the current setting value for the Optimized Checkout to the Stripe System Status Report data

includes/class-wc-stripe-helper.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1983,4 +1983,24 @@ public static function validate_intent_for_order( $order, $intent, ?string $sele
19831983

19841984
throw new Exception( __( "We're not able to process this request. Please try again later.", 'woocommerce-gateway-stripe' ) );
19851985
}
1986+
1987+
/**
1988+
* Determines if the store is connected to Stripe.
1989+
*
1990+
* @param string $mode Optional. The mode to check. 'live' or 'test' - if not provided, the currently enabled mode will be checked.
1991+
* @return bool True if connected, false otherwise.
1992+
*/
1993+
public static function is_connected( $mode = null ) {
1994+
// If the mode is not provided, we'll check the current mode.
1995+
if ( null === $mode ) {
1996+
$mode = WC_Stripe_Mode::is_test() ? 'test' : 'live';
1997+
}
1998+
1999+
$options = self::get_stripe_settings();
2000+
if ( 'test' === $mode ) {
2001+
return isset( $options['test_publishable_key'], $options['test_secret_key'] ) && trim( $options['test_publishable_key'] ) && trim( $options['test_secret_key'] );
2002+
} else {
2003+
return isset( $options['publishable_key'], $options['secret_key'] ) && trim( $options['publishable_key'] ) && trim( $options['secret_key'] );
2004+
}
2005+
}
19862006
}

includes/class-wc-stripe-payment-method-configurations.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,13 +296,16 @@ function ( $id ) use ( $is_test_mode ) {
296296

297297
/**
298298
* Check if the payment method configurations API can be used to store enabled payment methods.
299-
* This requires the Stripe account to be connected to our platform ('connection_type' option to be 'connect').
300-
*
301-
* This is temporary until we finish the re-authentication campaign.
299+
* This requires the Stripe account to be connected to Stripe.
302300
*
303301
* @return bool
304302
*/
305303
public static function is_enabled() {
304+
// Bail if account is not connected.
305+
if ( ! WC_Stripe_Helper::is_connected() ) {
306+
return false;
307+
}
308+
306309
$stripe_settings = WC_Stripe_Helper::get_stripe_settings();
307310

308311
// If we have the pmc_enabled flag, and it is set to no, we should not use the payment method configurations API.

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -269,17 +269,7 @@ private function get_default_stripe_config() {
269269
* @return bool True if connected, false otherwise.
270270
*/
271271
public function is_connected( $mode = null ) {
272-
// If the mode is not provided, we'll check the current mode.
273-
if ( is_null( $mode ) ) {
274-
$mode = WC_Stripe_Mode::is_test() ? 'test' : 'live';
275-
}
276-
277-
$options = WC_Stripe_Helper::get_stripe_settings();
278-
if ( 'test' === $mode ) {
279-
return isset( $options['test_publishable_key'], $options['test_secret_key'] ) && trim( $options['test_publishable_key'] ) && trim( $options['test_secret_key'] );
280-
} else {
281-
return isset( $options['publishable_key'], $options['secret_key'] ) && trim( $options['publishable_key'] ) && trim( $options['secret_key'] );
282-
}
272+
return WC_Stripe_Helper::is_connected( $mode );
283273
}
284274

285275
/**

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,5 +121,6 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
121121
* Fix - Relax customer validation that was preventing payments from the pay for order page
122122
* Fix - Remove connection type requirement from PMC sync migration attempt
123123
* Tweak - Small improvements to e2e tests
124+
* Fix - Prevent the PMC migration to run when the plugin is not connected to Stripe
124125

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

tests/phpunit/Admin/Migrations/Migrate_Payment_Methods_From_DB_To_PMC_Test.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ public function set_up() {
2424

2525
$this->pmc = new WC_Stripe_Payment_Method_Configurations();
2626

27-
// Set up test connection type to enable PMC
27+
// Set up test connection info to enable PMC
2828
$stripe_settings = WC_Stripe_Helper::get_stripe_settings();
29+
$stripe_settings['test_publishable_key'] = 'pk_test_1234567890';
30+
$stripe_settings['test_secret_key'] = 'sk_test_1234567890';
2931
$stripe_settings['test_connection_type'] = 'connect';
3032
WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings );
3133
}

tests/phpunit/Admin/WC_Stripe_Settings_Controller_Test.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ public function test_admin_options_when_stripe_is_not_connected() {
7777
$stripe_settings['testmode'] = 'yes';
7878
$stripe_settings['test_publishable_key'] = '';
7979
$stripe_settings['test_secret_key'] = '';
80+
$stripe_settings['publishable_key'] = '';
81+
$stripe_settings['secret_key'] = '';
8082
WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings );
8183

8284
ob_start();

tests/phpunit/Helpers/UPE_Test_Helper.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ function () {
4848
WC()->payment_gateways()->payment_gateways = [];
4949
WC()->payment_gateways()->init();
5050
$settings = WC_Stripe_Helper::get_stripe_settings();
51+
$settings['publishable_key'] = 'pk_live_1234567890';
52+
$settings['secret_key'] = 'sk_live_1234567890';
5153
$settings['connection_type'] = 'connect';
54+
$settings['test_publishable_key'] = 'pk_test_1234567890';
55+
$settings['test_secret_key'] = 'sk_test_1234567890';
5256
$settings['test_connection_type'] = 'connect';
5357
$settings['pmc_enabled'] = 'yes';
5458
WC_Stripe_Helper::update_main_stripe_settings( $settings );

tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Method_Test.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,8 @@ public function test_update_payment_token() {
931931
public function test_upe_method_enabled() {
932932
$stripe_settings = WC_Stripe_Helper::get_stripe_settings();
933933
$stripe_settings['enabled'] = 'yes';
934+
$stripe_settings['test_publishable_key'] = 'pk_test_1234567890';
935+
$stripe_settings['test_secret_key'] = 'sk_test_1234567890';
934936
$stripe_settings['test_connection_type'] = 'connect';
935937
$stripe_settings['pmc_enabled'] = 'yes';
936938
WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings );

tests/phpunit/WC_REST_Stripe_Account_Keys_Controller_Test.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ public function set_up() {
4242
// Setup existing keys
4343
$settings = WC_Stripe_Helper::get_stripe_settings();
4444
$settings['publishable_key'] = 'original-live-key-9999';
45+
$settings['secret_key'] = '';
4546
$settings['test_publishable_key'] = 'original-test-key-9999';
47+
$settings['test_secret_key'] = '';
4648
WC_Stripe_Helper::update_main_stripe_settings( $settings );
4749

4850
$mock_account = $this->getMockBuilder( WC_Stripe_Account::class )
@@ -176,7 +178,7 @@ public function test_changing_keys_resets_payment_methods() {
176178

177179
// Build request params
178180
$request = new WP_REST_Request( 'POST', self::ROUTE );
179-
$request->set_param( 'publishable_key', '' );
181+
$request->set_param( 'publishable_key', 'pk_live-key-updated' );
180182

181183
// Set initial payment methods
182184
$this->set_stripe_account_data( [ 'country' => 'US' ] );

0 commit comments

Comments
 (0)