Skip to content

Commit b5fadd4

Browse files
authored
Blocking disabling of cards when OC is enabled (#4540)
* Blocking disabling of cards when OC is enabled * Changelog and readme entries * Refactor logic to force enablement during init * Unit tests * Fix tests * Removing unnecessary import
1 parent 082c6d5 commit b5fadd4

File tree

4 files changed

+50
-12
lines changed

4 files changed

+50
-12
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 - Force the card payment method to be enabled when the Optimized Checkout is enabled in the merchant's Payment Method Configuration
45
* Update - Deactivates Affirm or Klarna when other official plugins are active in merchant's Payment Method Configuration
56
* Fix - Fixes issues related to booking multiple slots with express checkout payment methods enabled
67
* Fix - Update the Optimized Checkout promotional inbox note to link to the relevant section in the Stripe settings page

includes/class-wc-stripe.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,10 @@ public function init() {
282282

283283
add_action( 'init', [ $this, 'initialize_apple_pay_registration' ] );
284284

285-
// Check for payment methods that should be deactivated, e.g. unreleased,
286-
// BNPLs when official plugins are active, etc.
287-
add_action( 'init', [ $this, 'maybe_deactivate_payment_methods' ] );
285+
// Check for payment methods that should be toggled, e.g. unreleased,
286+
// BNPLs when official plugins are active,
287+
// cards when the Optimized Checkout is enabled, etc.
288+
add_action( 'init', [ $this, 'maybe_toggle_payment_methods' ] );
288289
}
289290

290291
/**
@@ -863,18 +864,20 @@ public function initialize_status_page() {
863864
}
864865

865866
/**
866-
* Deactivate payment methods that should be deactivated, e.g. unreleased,
867-
* BNPLs when other official plugins are active, etc.
867+
* Toggle payment methods that should be enabled/disabled, e.g. unreleased,
868+
* BNPLs when other official plugins are active,
869+
* cards when the Optimized Checkout is enabled, etc.
868870
*
869871
* @return void
870872
*/
871-
public function maybe_deactivate_payment_methods() {
873+
public function maybe_toggle_payment_methods() {
872874
$gateway = $this->get_main_stripe_gateway();
873875
if ( ! is_a( $gateway, 'WC_Stripe_UPE_Payment_Gateway' ) ) {
874876
return;
875877
}
876878

877879
$payment_method_ids_to_disable = [];
880+
$payment_method_ids_to_enable = [];
878881
$enabled_payment_methods = $gateway->get_upe_enabled_payment_method_ids();
879882

880883
// Check for BNPLs that should be deactivated.
@@ -889,10 +892,22 @@ public function maybe_deactivate_payment_methods() {
889892
$this->maybe_deactivate_amazon_pay( $enabled_payment_methods )
890893
);
891894

892-
if ( [] === $payment_method_ids_to_disable ) {
895+
// Check if cards should be activated.
896+
// TODO: Remove this once card is not a requirement for the Optimized Checkout.
897+
if ( $gateway->is_oc_enabled()
898+
&& ! in_array( WC_Stripe_Payment_Methods::CARD, $enabled_payment_methods, true ) ) {
899+
$payment_method_ids_to_enable[] = WC_Stripe_Payment_Methods::CARD;
900+
}
901+
902+
if ( [] === $payment_method_ids_to_disable && [] === $payment_method_ids_to_enable ) {
893903
return;
894904
}
895905

906+
$enabled_payment_methods = array_merge(
907+
$enabled_payment_methods,
908+
$payment_method_ids_to_enable
909+
);
910+
896911
$gateway->update_enabled_payment_methods(
897912
array_diff( $enabled_payment_methods, $payment_method_ids_to_disable )
898913
);

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 - Force the card payment method to be enabled when the Optimized Checkout is enabled in the merchant's Payment Method Configuration
114115
* Update - Deactivates Affirm or Klarna when other official plugins are active in merchant's Payment Method Configuration
115116
* Fix - Fixes issues related to booking multiple slots with express checkout payment methods enabled
116117
* Fix - Update the Optimized Checkout promotional inbox note to link to the relevant section in the Stripe settings page

tests/phpunit/WC_Stripe_Test.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,20 @@ public function test_constants_defined() {
2525
}
2626

2727
/**
28-
* Tests for `maybe_deactivate_payment_methods`.
28+
* Tests for `maybe_toggle_payment_methods`.
2929
*
30+
* @param array $active_gateways The active payment gateways.
31+
* @param array $enabled_payment_method_ids The enabled payment method IDs.
32+
* @param bool $oc_enabled Whether the one-click payment methods are enabled.
33+
* @param int $update_enable_payment_methods_calls The number of times `update_enabled_payment_methods` should be called.
3034
* @return void
3135
*
32-
* @dataProvider provide_test_maybe_deactivate_payment_methods
36+
* @dataProvider provide_test_maybe_toggle_payment_methods
3337
*/
34-
public function test_maybe_deactivate_payment_methods(
38+
public function test_maybe_toggle_payment_methods(
3539
$active_gateways,
3640
$enabled_payment_method_ids,
41+
$oc_enabled,
3742
$update_enable_payment_methods_calls
3843
) {
3944
$original_payment_gateways = WC()->payment_gateways->payment_gateways;
@@ -45,6 +50,10 @@ public function test_maybe_deactivate_payment_methods(
4550
->disableOriginalConstructor()
4651
->getMock();
4752

53+
$upe_payment_gateway->expects( $this->once() )
54+
->method( 'is_oc_enabled' )
55+
->willReturn( $oc_enabled );
56+
4857
$upe_payment_gateway->expects( $this->once() )
4958
->method( 'get_upe_enabled_payment_method_ids' )
5059
->willReturn( $enabled_payment_method_ids );
@@ -61,7 +70,7 @@ public function test_maybe_deactivate_payment_methods(
6170
$wc_stripe->method( 'get_main_stripe_gateway' )
6271
->willReturn( $upe_payment_gateway );
6372

64-
$wc_stripe->maybe_deactivate_payment_methods();
73+
$wc_stripe->maybe_toggle_payment_methods();
6574

6675
// Clean up.
6776
WC()->payment_gateways->payment_gateways = $original_payment_gateways;
@@ -72,13 +81,14 @@ public function test_maybe_deactivate_payment_methods(
7281
*
7382
* @return array
7483
*/
75-
public function provide_test_maybe_deactivate_payment_methods() {
84+
public function provide_test_maybe_toggle_payment_methods() {
7685
return [
7786
'none active' => [
7887
'active gateways' => [],
7988
'enabled payment method IDs' => [
8089
WC_Stripe_Payment_Methods::CARD,
8190
],
91+
'OC enabled' => false,
8292
'update enable payment methods calls' => 0,
8393
],
8494
'affirm' => [
@@ -92,6 +102,7 @@ public function provide_test_maybe_deactivate_payment_methods() {
92102
WC_Stripe_Payment_Methods::CARD,
93103
WC_Stripe_Payment_Methods::AFFIRM,
94104
],
105+
'OC enabled' => false,
95106
'update enable payment methods calls' => 1,
96107
],
97108
'klarna' => [
@@ -105,6 +116,7 @@ public function provide_test_maybe_deactivate_payment_methods() {
105116
WC_Stripe_Payment_Methods::CARD,
106117
WC_Stripe_Payment_Methods::KLARNA,
107118
],
119+
'OC enabled' => false,
108120
'update enable payment methods calls' => 1,
109121
],
110122
'klarna and affirm active, but not on Stripe' => [
@@ -121,6 +133,7 @@ public function provide_test_maybe_deactivate_payment_methods() {
121133
'enabled payment method IDs' => [
122134
WC_Stripe_Payment_Methods::CARD,
123135
],
136+
'OC enabled' => false,
124137
'update enable payment methods calls' => 0,
125138
],
126139
'klarna and affirm active in both' => [
@@ -139,6 +152,7 @@ public function provide_test_maybe_deactivate_payment_methods() {
139152
WC_Stripe_Payment_Methods::AFFIRM,
140153
WC_Stripe_Payment_Methods::KLARNA,
141154
],
155+
'OC enabled' => false,
142156
'update enable payment methods calls' => 1,
143157
],
144158
'amazon pay' => [
@@ -147,6 +161,13 @@ public function provide_test_maybe_deactivate_payment_methods() {
147161
WC_Stripe_Payment_Methods::CARD,
148162
WC_Stripe_Payment_Methods::AMAZON_PAY,
149163
],
164+
'OC enabled' => false,
165+
'update enable payment methods calls' => 1,
166+
],
167+
'card, OC enabled' => [
168+
'active gateways' => [],
169+
'enabled payment method IDs' => [],
170+
'OC enabled' => true,
150171
'update enable payment methods calls' => 1,
151172
],
152173
];

0 commit comments

Comments
 (0)