From a31c51d9ad1d3eefd40e8cf5b63ba42a657d7248 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 7 Aug 2025 17:51:40 -0300 Subject: [PATCH 01/11] Checking for the subscription detached payment method --- includes/compat/class-wc-stripe-subscriptions-helper.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/includes/compat/class-wc-stripe-subscriptions-helper.php b/includes/compat/class-wc-stripe-subscriptions-helper.php index 92df9929b5..a992c89811 100644 --- a/includes/compat/class-wc-stripe-subscriptions-helper.php +++ b/includes/compat/class-wc-stripe-subscriptions-helper.php @@ -151,6 +151,11 @@ public static function is_subscription_payment_method_detached( $subscription ) return false; } + if ( 'stripe' !== substr( (string) $subscription->get_payment_method(), 0, 6 ) ) { + // If the payment method is not a Stripe method, we don't need to check further. + return false; + } + $source_id = $subscription->get_meta( '_stripe_source_id' ); if ( ! $source_id ) { return false; From 312bd95351a3c6ba2584b13fc5c0f36378b64bbe Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 7 Aug 2025 18:20:36 -0300 Subject: [PATCH 02/11] Changelog and readme entries --- changelog.txt | 1 + readme.txt | 1 + 2 files changed, 2 insertions(+) diff --git a/changelog.txt b/changelog.txt index 2b079c4a36..bcd31e2242 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,6 +1,7 @@ *** Changelog *** = 9.8.0 - xxxx-xx-xx = +* Fix - Checks for the subscription payment method (if it is Stripe) when verifying for the payment method detachment * Update - Removes the ability to change the title for the Optimized Checkout payment element, as it is now set to "Stripe" by default * Update - Copy for the Optimized Checkout settings and notices * Dev - Implements WooCommerce constants for the tax statuses diff --git a/readme.txt b/readme.txt index 5684ae3d25..391d6bf62f 100644 --- a/readme.txt +++ b/readme.txt @@ -111,6 +111,7 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o == Changelog == = 9.8.0 - xxxx-xx-xx = +* Fix - Checks for the subscription payment method (if it is Stripe) when verifying for the payment method detachment * Update - Removes the ability to change the title for the Optimized Checkout payment element, as it is now set to "Stripe" by default * Update - Copy for the Optimized Checkout settings and notices * Dev - Implements WooCommerce constants for the tax statuses From 3eaeb4e83b2757af7b0ec6c599108fc32c8c07ed Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 7 Aug 2025 18:34:23 -0300 Subject: [PATCH 03/11] Unit tests --- .../WC_Stripe_Subscriptions_Helper_Test.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/Compat/WC_Stripe_Subscriptions_Helper_Test.php b/tests/phpunit/Compat/WC_Stripe_Subscriptions_Helper_Test.php index 5a0fdfdc63..9d142aeca6 100644 --- a/tests/phpunit/Compat/WC_Stripe_Subscriptions_Helper_Test.php +++ b/tests/phpunit/Compat/WC_Stripe_Subscriptions_Helper_Test.php @@ -178,6 +178,7 @@ public function provide_test_build_subscriptions_detached_messages() { /** * Tests for {@see WC_Stripe_Subscriptions_Helper::is_subscription_payment_method_detached()}. * + * @param string $payment_method The payment method type for the subscription. * @param array|null $source_meta The source meta data for the subscription. * @param array|\WP_Error $mocked_response The mocked response from the Stripe API. * @param bool $expected The expected result of the check. @@ -185,12 +186,13 @@ public function provide_test_build_subscriptions_detached_messages() { * * @dataProvider provide_test_is_subscription_payment_method_detached */ - public function test_is_subscription_payment_method_detached( $source_meta, $mocked_response, $expected ) { + public function test_is_subscription_payment_method_detached( $payment_method, $source_meta, $mocked_response, $expected ) { WC_Stripe_Database_Cache::delete( 'payment_method_for_source_' . $source_meta ); $subscription = new WC_Subscription(); $subscription->set_id( 1 ); $subscription->set_status( 'active' ); + $subscription->set_payment_method( $payment_method ); $subscription->save(); if ( ! is_null( $source_meta ) ) { @@ -223,17 +225,26 @@ public function test_is_subscription_payment_method_detached( $source_meta, $moc */ public function provide_test_is_subscription_payment_method_detached() { return [ + 'not a Stripe subscription' => [ + 'payment method' => 'not_stripe', + 'source meta' => null, + 'mocked response' => null, + 'expected' => false, + ], 'missing meta' => [ + 'payment method' => 'stripe_klarna', 'source meta' => null, 'mocked response' => null, 'expected' => false, ], 'wp error response, assumed detached' => [ + 'payment method' => 'stripe_klarna', 'source meta' => 'src_123', 'mocked response' => new \WP_Error( 'error', 'An error occurred.' ), 'expected' => true, ], 'Stripe error response, assumed detached' => [ + 'payment method' => 'stripe_klarna', 'source meta' => 'src_123', 'mocked response' => [ 'response' => 400, @@ -250,6 +261,7 @@ public function provide_test_is_subscription_payment_method_detached() { 'expected' => true, ], 'existing customer data' => [ + 'payment method' => 'stripe_klarna', 'source meta' => 'src_123', 'mocked response' => [ 'response' => 200, @@ -263,6 +275,7 @@ public function provide_test_is_subscription_payment_method_detached() { 'expected' => false, ], 'detached payment method' => [ + 'payment method' => 'stripe_klarna', 'source meta' => 'src_123', 'mocked response' => [ 'response' => 200, From 37f02b5176cf414ddad5410e0b186dfd2e6bf4dc Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 7 Aug 2025 18:46:49 -0300 Subject: [PATCH 04/11] Fix tests --- tests/phpunit/Compat/WC_Stripe_Subscriptions_Helper_Test.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/phpunit/Compat/WC_Stripe_Subscriptions_Helper_Test.php b/tests/phpunit/Compat/WC_Stripe_Subscriptions_Helper_Test.php index 9d142aeca6..506c832434 100644 --- a/tests/phpunit/Compat/WC_Stripe_Subscriptions_Helper_Test.php +++ b/tests/phpunit/Compat/WC_Stripe_Subscriptions_Helper_Test.php @@ -60,6 +60,7 @@ public function test_get_detached_subscriptions() { $subscription = new WC_Subscription(); $subscription->set_id( $subscription_id ); $subscription->set_status( 'active' ); + $subscription->set_payment_method( 'stripe_klarna' ); $subscription->set_time( 'next_payment_date', strtotime( '+1 week' ) ); $subscription->save(); From f5047bc75c32f96d78b7fd38b5d75556e694dbce Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 7 Aug 2025 18:55:17 -0300 Subject: [PATCH 05/11] Fix tests --- tests/phpunit/Admin/WC_Stripe_Admin_Notices_Test.php | 1 + .../Admin/WC_Stripe_Subscription_Detached_Bulk_Action_Test.php | 1 + 2 files changed, 2 insertions(+) diff --git a/tests/phpunit/Admin/WC_Stripe_Admin_Notices_Test.php b/tests/phpunit/Admin/WC_Stripe_Admin_Notices_Test.php index d14b1bbe39..ab18647b02 100644 --- a/tests/phpunit/Admin/WC_Stripe_Admin_Notices_Test.php +++ b/tests/phpunit/Admin/WC_Stripe_Admin_Notices_Test.php @@ -646,6 +646,7 @@ public function provide_test_subscription_check_detachment() { $subscription = new WC_Subscription(); $subscription->set_id( 123 ); $subscription->set_status( 'active' ); + $subscription->set_payment_method( 'stripe_klarna' ); $subscription->save(); $subscription->update_meta_data( '_stripe_source_id', $source_id ); diff --git a/tests/phpunit/Admin/WC_Stripe_Subscription_Detached_Bulk_Action_Test.php b/tests/phpunit/Admin/WC_Stripe_Subscription_Detached_Bulk_Action_Test.php index 0342467ee1..6ec3ff1309 100644 --- a/tests/phpunit/Admin/WC_Stripe_Subscription_Detached_Bulk_Action_Test.php +++ b/tests/phpunit/Admin/WC_Stripe_Subscription_Detached_Bulk_Action_Test.php @@ -80,6 +80,7 @@ public function provide_test_handle_subscription_detachment_check() { $source_id = 'src_123'; $subscription_detached = new WC_Subscription(); $subscription_detached->set_id( 2 ); + $subscription_detached->set_payment_method( 'stripe_klarna' ); $subscription_detached->update_meta_data( '_stripe_source_id', $source_id ); $payment_method = (object) [ From f19c46af5bd57067bf2dd15ca65582148cdc37fc Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 7 Aug 2025 19:06:21 -0300 Subject: [PATCH 06/11] Fix tests --- tests/phpunit/WC_Stripe_Status_Test.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/phpunit/WC_Stripe_Status_Test.php b/tests/phpunit/WC_Stripe_Status_Test.php index c0bb63f1a9..ef1d7831c3 100644 --- a/tests/phpunit/WC_Stripe_Status_Test.php +++ b/tests/phpunit/WC_Stripe_Status_Test.php @@ -115,6 +115,7 @@ public function test_list_detached_subscriptions( $subscriptions, $expected_outp $subscription = new WC_Subscription(); $subscription->set_id( $subscription_data['id'] ); $subscription->set_status( 'active' ); + $subscription->set_payment_method( 'stripe_klarna' ); $subscription->save(); $subscription->update_meta_data( '_stripe_customer_id', $subscription_data['customer_id'] ); From 098ed166ea65b23becd249ddb2e1ea3341ea902e Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 7 Aug 2025 19:14:32 -0300 Subject: [PATCH 07/11] Helper method to check for an order gateway --- includes/class-wc-stripe-helper.php | 10 ++++++++++ includes/class-wc-stripe-order-handler.php | 2 +- includes/class-wc-stripe-webhook-handler.php | 4 ++-- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/includes/class-wc-stripe-helper.php b/includes/class-wc-stripe-helper.php index d1620b843d..43a4a5d2d0 100644 --- a/includes/class-wc-stripe-helper.php +++ b/includes/class-wc-stripe-helper.php @@ -1896,4 +1896,14 @@ public static function has_gateway_plugin_active( $plugin_id ) { } return false; } + + /** + * Checks if the order is using a Stripe payment method. + * + * @param $order WC_Order The order to check. + * @return bool + */ + public static function is_stripe_gateway_order( $order ) { + return WC_Gateway_Stripe::ID !== substr( (string) $order->get_payment_method(), 0, 6 ); + } } diff --git a/includes/class-wc-stripe-order-handler.php b/includes/class-wc-stripe-order-handler.php index b06a0632ca..4760060003 100644 --- a/includes/class-wc-stripe-order-handler.php +++ b/includes/class-wc-stripe-order-handler.php @@ -471,7 +471,7 @@ public function prevent_cancelling_orders_awaiting_action( $cancel_order, $order } // Bail if payment method is not stripe or `stripe_{apm_method}` or doesn't have an intent yet. - if ( substr( (string) $order->get_payment_method(), 0, 6 ) !== 'stripe' || ! $this->get_intent_from_order( $order ) ) { + if ( ! WC_Stripe_Helper::is_stripe_gateway_order( $order ) || ! $this->get_intent_from_order( $order ) ) { return $cancel_order; } diff --git a/includes/class-wc-stripe-webhook-handler.php b/includes/class-wc-stripe-webhook-handler.php index f2f8befd6f..8498386124 100644 --- a/includes/class-wc-stripe-webhook-handler.php +++ b/includes/class-wc-stripe-webhook-handler.php @@ -704,7 +704,7 @@ public function process_webhook_refund( $notification ) { $order_id = $order->get_id(); - if ( 'stripe' === substr( (string) $order->get_payment_method(), 0, 6 ) ) { + if ( WC_Stripe_Helper::is_stripe_gateway_order( $order ) ) { $charge = $order->get_transaction_id(); $captured = $order->get_meta( '_stripe_charge_captured' ); $refund_id = $order->get_meta( '_stripe_refund_id' ); @@ -787,7 +787,7 @@ public function process_webhook_refund_updated( $notification ) { $order_id = $order->get_id(); - if ( 'stripe' === substr( (string) $order->get_payment_method(), 0, 6 ) ) { + if ( WC_Stripe_Helper::is_stripe_gateway_order( $order ) ) { $charge = $order->get_transaction_id(); $refund_id = $order->get_meta( '_stripe_refund_id' ); $currency = $order->get_currency(); From 1fec43c06a65b8fb4a6fc3991b0fedb86232e113 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Wed, 20 Aug 2025 05:40:16 -0300 Subject: [PATCH 08/11] Replacing additional occurrence --- includes/compat/class-wc-stripe-subscriptions-helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/compat/class-wc-stripe-subscriptions-helper.php b/includes/compat/class-wc-stripe-subscriptions-helper.php index 4a82e5a4ed..e30a879b86 100644 --- a/includes/compat/class-wc-stripe-subscriptions-helper.php +++ b/includes/compat/class-wc-stripe-subscriptions-helper.php @@ -151,7 +151,7 @@ public static function is_subscription_payment_method_detached( $subscription ) return false; } - if ( 'stripe' !== substr( (string) $subscription->get_payment_method(), 0, 6 ) ) { + if ( ! WC_Stripe_Helper::is_stripe_gateway_order( $subscription ) ) { // If the payment method is not a Stripe method, we don't need to check further. return false; } From ff84cecf2c0eea2505025612295eda89ba5c1c93 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Wed, 20 Aug 2025 05:41:27 -0300 Subject: [PATCH 09/11] Changelog and readme entries --- changelog.txt | 1 + readme.txt | 1 + 2 files changed, 2 insertions(+) diff --git a/changelog.txt b/changelog.txt index d5e7435715..1a16a9eb11 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,6 +1,7 @@ *** Changelog *** = 9.9.0 - xxxx-xx-xx = +* Dev - Introduces a new helper method to identify Stripe orders * Fix - Removes the credit card payment method requirement for the Optimized Checkout feature * Fix - Payment method test instructions not showing up for the Optimized Checkout payment element * Add - Includes a new notice to highlight the Optimized Checkout feature above the payment methods list in the Stripe settings page diff --git a/readme.txt b/readme.txt index 1551591cba..2f41b548d6 100644 --- a/readme.txt +++ b/readme.txt @@ -111,6 +111,7 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o == Changelog == = 9.9.0 - xxxx-xx-xx = +* Dev - Introduces a new helper method to identify Stripe orders * Fix - Removes the credit card payment method requirement for the Optimized Checkout feature * Fix - Payment method test instructions not showing up for the Optimized Checkout payment element * Add - Includes a new notice to highlight the Optimized Checkout feature above the payment methods list in the Stripe settings page From 96bc32ea2435393d5057cc141d998b6ee3bf6e67 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Wed, 20 Aug 2025 05:44:26 -0300 Subject: [PATCH 10/11] Unit test --- tests/phpunit/WC_Stripe_Helper_Test.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/phpunit/WC_Stripe_Helper_Test.php b/tests/phpunit/WC_Stripe_Helper_Test.php index 7206db3be4..4d09f87802 100644 --- a/tests/phpunit/WC_Stripe_Helper_Test.php +++ b/tests/phpunit/WC_Stripe_Helper_Test.php @@ -895,4 +895,25 @@ public function is_webhook_url_provider() { 'webhook URL with extra parameters should match' => [ 'https://example.com/test/?wc-api=wc_stripe&foo=bar', 'https://example.com/test/?wc-api=wc_stripe', true ], ]; } + + /** + * Tests for `is_stripe_gateway_order`. + * + * @return void + */ + public function test_is_stripe_gateway_order() { + // Test with a Stripe order (Klarna). + $order = WC_Helper_Order::create_order(); + $order->set_payment_method( 'stripe_klarna' ); + $this->assertTrue( WC_Stripe_Helper::is_stripe_gateway_order( $order ) ); + + // Test with a non-Stripe order. + $order = WC_Helper_Order::create_order(); + $order->set_payment_method( 'cod' ); + $this->assertFalse( WC_Stripe_Helper::is_stripe_gateway_order( $order ) ); + + // Test with an empty order. + $order = new WC_Order(); + $this->assertFalse( WC_Stripe_Helper::is_stripe_gateway_order( $order ) ); + } } From aae3bdfb37180bbb6cdd968b13ba5cd8953f82f9 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Wed, 20 Aug 2025 12:47:44 -0300 Subject: [PATCH 11/11] Update includes/class-wc-stripe-helper.php Co-authored-by: Mayisha <33387139+Mayisha@users.noreply.github.com> --- includes/class-wc-stripe-helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-wc-stripe-helper.php b/includes/class-wc-stripe-helper.php index 608d7d48ad..5ed51f9e87 100644 --- a/includes/class-wc-stripe-helper.php +++ b/includes/class-wc-stripe-helper.php @@ -2011,6 +2011,6 @@ public static function is_connected( $mode = null ) { * @return bool */ public static function is_stripe_gateway_order( $order ) { - return WC_Gateway_Stripe::ID !== substr( (string) $order->get_payment_method(), 0, 6 ); + return WC_Gateway_Stripe::ID === substr( (string) $order->get_payment_method(), 0, 6 ); } }