Skip to content

Commit c16b463

Browse files
authored
Checking for the subscription detached payment method (#4565)
* Checking for the subscription detached payment method * Changelog and readme entries * Unit tests * Fix tests * Fix tests * Fix tests
1 parent 4d5c30e commit c16b463

7 files changed

+25
-1
lines changed

changelog.txt

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

33
= 9.8.0 - xxxx-xx-xx =
4+
* Fix - Checks for the subscription payment method (if it is Stripe) when verifying for the payment method detachment
45
* Update - Removes the ability to change the title for the Optimized Checkout payment element, as it is now set to "Stripe" by default
56
* Update - Copy for the Optimized Checkout settings and notices
67
* Dev - Implements WooCommerce constants for the tax statuses

includes/compat/class-wc-stripe-subscriptions-helper.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ public static function is_subscription_payment_method_detached( $subscription )
151151
return false;
152152
}
153153

154+
if ( 'stripe' !== substr( (string) $subscription->get_payment_method(), 0, 6 ) ) {
155+
// If the payment method is not a Stripe method, we don't need to check further.
156+
return false;
157+
}
158+
154159
$source_id = $subscription->get_meta( '_stripe_source_id' );
155160
if ( ! $source_id ) {
156161
return false;

readme.txt

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

113113
= 9.8.0 - xxxx-xx-xx =
114+
* Fix - Checks for the subscription payment method (if it is Stripe) when verifying for the payment method detachment
114115
* Update - Removes the ability to change the title for the Optimized Checkout payment element, as it is now set to "Stripe" by default
115116
* Update - Copy for the Optimized Checkout settings and notices
116117
* Dev - Implements WooCommerce constants for the tax statuses

tests/phpunit/Admin/WC_Stripe_Admin_Notices_Test.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,7 @@ public function provide_test_subscription_check_detachment() {
646646
$subscription = new WC_Subscription();
647647
$subscription->set_id( 123 );
648648
$subscription->set_status( 'active' );
649+
$subscription->set_payment_method( 'stripe_klarna' );
649650
$subscription->save();
650651

651652
$subscription->update_meta_data( '_stripe_source_id', $source_id );

tests/phpunit/Admin/WC_Stripe_Subscription_Detached_Bulk_Action_Test.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public function provide_test_handle_subscription_detachment_check() {
8080
$source_id = 'src_123';
8181
$subscription_detached = new WC_Subscription();
8282
$subscription_detached->set_id( 2 );
83+
$subscription_detached->set_payment_method( 'stripe_klarna' );
8384
$subscription_detached->update_meta_data( '_stripe_source_id', $source_id );
8485

8586
$payment_method = (object) [

tests/phpunit/Compat/WC_Stripe_Subscriptions_Helper_Test.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public function test_get_detached_subscriptions() {
6060
$subscription = new WC_Subscription();
6161
$subscription->set_id( $subscription_id );
6262
$subscription->set_status( 'active' );
63+
$subscription->set_payment_method( 'stripe_klarna' );
6364
$subscription->set_time( 'next_payment_date', strtotime( '+1 week' ) );
6465
$subscription->save();
6566

@@ -178,19 +179,21 @@ public function provide_test_build_subscriptions_detached_messages() {
178179
/**
179180
* Tests for {@see WC_Stripe_Subscriptions_Helper::is_subscription_payment_method_detached()}.
180181
*
182+
* @param string $payment_method The payment method type for the subscription.
181183
* @param array|null $source_meta The source meta data for the subscription.
182184
* @param array|\WP_Error $mocked_response The mocked response from the Stripe API.
183185
* @param bool $expected The expected result of the check.
184186
* @return void
185187
*
186188
* @dataProvider provide_test_is_subscription_payment_method_detached
187189
*/
188-
public function test_is_subscription_payment_method_detached( $source_meta, $mocked_response, $expected ) {
190+
public function test_is_subscription_payment_method_detached( $payment_method, $source_meta, $mocked_response, $expected ) {
189191
WC_Stripe_Database_Cache::delete( 'payment_method_for_source_' . $source_meta );
190192

191193
$subscription = new WC_Subscription();
192194
$subscription->set_id( 1 );
193195
$subscription->set_status( 'active' );
196+
$subscription->set_payment_method( $payment_method );
194197
$subscription->save();
195198

196199
if ( ! is_null( $source_meta ) ) {
@@ -223,17 +226,26 @@ public function test_is_subscription_payment_method_detached( $source_meta, $moc
223226
*/
224227
public function provide_test_is_subscription_payment_method_detached() {
225228
return [
229+
'not a Stripe subscription' => [
230+
'payment method' => 'not_stripe',
231+
'source meta' => null,
232+
'mocked response' => null,
233+
'expected' => false,
234+
],
226235
'missing meta' => [
236+
'payment method' => 'stripe_klarna',
227237
'source meta' => null,
228238
'mocked response' => null,
229239
'expected' => false,
230240
],
231241
'wp error response, assumed detached' => [
242+
'payment method' => 'stripe_klarna',
232243
'source meta' => 'src_123',
233244
'mocked response' => new \WP_Error( 'error', 'An error occurred.' ),
234245
'expected' => true,
235246
],
236247
'Stripe error response, assumed detached' => [
248+
'payment method' => 'stripe_klarna',
237249
'source meta' => 'src_123',
238250
'mocked response' => [
239251
'response' => 400,
@@ -250,6 +262,7 @@ public function provide_test_is_subscription_payment_method_detached() {
250262
'expected' => true,
251263
],
252264
'existing customer data' => [
265+
'payment method' => 'stripe_klarna',
253266
'source meta' => 'src_123',
254267
'mocked response' => [
255268
'response' => 200,
@@ -263,6 +276,7 @@ public function provide_test_is_subscription_payment_method_detached() {
263276
'expected' => false,
264277
],
265278
'detached payment method' => [
279+
'payment method' => 'stripe_klarna',
266280
'source meta' => 'src_123',
267281
'mocked response' => [
268282
'response' => 200,

tests/phpunit/WC_Stripe_Status_Test.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ public function test_list_detached_subscriptions( $subscriptions, $expected_outp
115115
$subscription = new WC_Subscription();
116116
$subscription->set_id( $subscription_data['id'] );
117117
$subscription->set_status( 'active' );
118+
$subscription->set_payment_method( 'stripe_klarna' );
118119
$subscription->save();
119120

120121
$subscription->update_meta_data( '_stripe_customer_id', $subscription_data['customer_id'] );

0 commit comments

Comments
 (0)