Skip to content

Commit 082c6d5

Browse files
annemirasolCopilotwjrosa
authored
Turn off Amazon Pay in PMC if feature flag is off (#4535)
* Disable unreleased Amazon Pay on init * Add changelog and readme entries * Remove unintended whitespace changes * Fix unit test Co-authored-by: Copilot <[email protected]> * Rephrase readme.txt entry Co-authored-by: Wesley Rosa <[email protected]> * Rephrase changelog.txt entry --------- Co-authored-by: Copilot <[email protected]> Co-authored-by: Wesley Rosa <[email protected]>
1 parent 9d98c93 commit 082c6d5

File tree

4 files changed

+87
-36
lines changed

4 files changed

+87
-36
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* Fix - Require credit cards to be enabled before Apple Pay and Google Pay can be enabled in PMC
1919
* Fix - Free trial subscription orders with payment methods that require redirection (eg: iDeal, Bancontact)
2020
* Tweak - Update checkout error message for invalid API key to be more generic and user-friendly
21+
* Tweak - Disable Amazon Pay in the merchant's Payment Method Configuration object if it is still behind a feature flag
2122

2223
= 9.7.1 - 2025-07-28 =
2324
* Add - Add state mapping for Lithuania in express checkout

includes/class-wc-stripe.php

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

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

285-
// Check if other official plugins are active for Klarna or Affirm and deactivate those BNPLs if so.
286-
add_action( 'init', [ $this, 'maybe_deactivate_bnpls' ] );
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' ] );
287288
}
288289

289290
/**
@@ -862,23 +863,54 @@ public function initialize_status_page() {
862863
}
863864

864865
/**
865-
* Maybe deactivate Affirm or Klarna payment methods if other official plugins are active.
866+
* Deactivate payment methods that should be deactivated, e.g. unreleased,
867+
* BNPLs when other official plugins are active, etc.
866868
*
867869
* @return void
868870
*/
869-
public function maybe_deactivate_bnpls() {
870-
$has_affirm_plugin_active = WC_Stripe_Helper::has_gateway_plugin_active( WC_Stripe_Helper::OFFICIAL_PLUGIN_ID_AFFIRM );
871-
$has_klarna_plugin_active = WC_Stripe_Helper::has_gateway_plugin_active( WC_Stripe_Helper::OFFICIAL_PLUGIN_ID_KLARNA );
872-
if ( ! $has_affirm_plugin_active && ! $has_klarna_plugin_active ) {
873-
return;
874-
}
875-
871+
public function maybe_deactivate_payment_methods() {
876872
$gateway = $this->get_main_stripe_gateway();
877873
if ( ! is_a( $gateway, 'WC_Stripe_UPE_Payment_Gateway' ) ) {
878874
return;
879875
}
880876

877+
$payment_method_ids_to_disable = [];
881878
$enabled_payment_methods = $gateway->get_upe_enabled_payment_method_ids();
879+
880+
// Check for BNPLs that should be deactivated.
881+
$payment_method_ids_to_disable = array_merge(
882+
$payment_method_ids_to_disable,
883+
$this->maybe_deactivate_bnpls( $enabled_payment_methods )
884+
);
885+
886+
// Check if Amazon Pay should be deactivated.
887+
$payment_method_ids_to_disable = array_merge(
888+
$payment_method_ids_to_disable,
889+
$this->maybe_deactivate_amazon_pay( $enabled_payment_methods )
890+
);
891+
892+
if ( [] === $payment_method_ids_to_disable ) {
893+
return;
894+
}
895+
896+
$gateway->update_enabled_payment_methods(
897+
array_diff( $enabled_payment_methods, $payment_method_ids_to_disable )
898+
);
899+
}
900+
901+
/**
902+
* Deactivate Affirm or Klarna payment methods if other official plugins are active.
903+
*
904+
* @param array $enabled_payment_methods The enabled payment methods.
905+
* @return array The payment method IDs to disable.
906+
*/
907+
private function maybe_deactivate_bnpls( $enabled_payment_methods ) {
908+
$has_affirm_plugin_active = WC_Stripe_Helper::has_gateway_plugin_active( WC_Stripe_Helper::OFFICIAL_PLUGIN_ID_AFFIRM );
909+
$has_klarna_plugin_active = WC_Stripe_Helper::has_gateway_plugin_active( WC_Stripe_Helper::OFFICIAL_PLUGIN_ID_KLARNA );
910+
if ( ! $has_affirm_plugin_active && ! $has_klarna_plugin_active ) {
911+
return [];
912+
}
913+
882914
$payment_method_ids_to_disable = [];
883915
if ( $has_affirm_plugin_active && in_array( WC_Stripe_Payment_Methods::AFFIRM, $enabled_payment_methods, true ) ) {
884916
$payment_method_ids_to_disable[] = WC_Stripe_Payment_Methods::AFFIRM;
@@ -887,15 +919,30 @@ public function maybe_deactivate_bnpls() {
887919
$payment_method_ids_to_disable[] = WC_Stripe_Payment_Methods::KLARNA;
888920
}
889921

890-
if ( [] === $payment_method_ids_to_disable ) {
891-
return;
922+
return $payment_method_ids_to_disable;
923+
}
924+
925+
/**
926+
* Deactivate Amazon Pay if it's not available, i.e. unreleased.
927+
*
928+
* TODO: Remove this method once Amazon Pay is released.
929+
*
930+
* @param array $enabled_payment_methods The enabled payment methods.
931+
* @return array Amazon Pay payment method ID, if it should be disabled.
932+
*/
933+
private function maybe_deactivate_amazon_pay( $enabled_payment_methods ) {
934+
// Safety guard only. Ideally, we will remove this method once Amazon Pay is released.
935+
if ( WC_Stripe_Feature_Flags::is_amazon_pay_available() ) {
936+
// Nothing to do if Amazon Pay is already released.
937+
return [];
892938
}
893939

894-
$gateway->update_enabled_payment_methods(
895-
array_diff(
896-
$enabled_payment_methods,
897-
$payment_method_ids_to_disable
898-
)
899-
);
940+
if ( ! in_array( WC_Stripe_Payment_Methods::AMAZON_PAY, $enabled_payment_methods, true ) ) {
941+
// Nothing to do if Amazon Pay is not enabled.
942+
return [];
943+
}
944+
945+
// Disable Amazon Pay.
946+
return [ WC_Stripe_Payment_Methods::AMAZON_PAY ];
900947
}
901948
}

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,5 +127,6 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
127127
* Fix - Require credit cards to be enabled before Apple Pay and Google Pay can be enabled in PMC
128128
* Fix - Free trial subscription orders with payment methods that require redirection (eg: iDeal, Bancontact)
129129
* Tweak - Update checkout error message for invalid API key to be more generic and user-friendly
130+
* Tweak - Disable Amazon Pay in the merchant's Payment Method Configuration object if it is still behind a feature flag
130131

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

tests/phpunit/WC_Stripe_Test.php

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

2727
/**
28-
* Tests for `maybe_deactivate_bnpls`.
28+
* Tests for `maybe_deactivate_payment_methods`.
2929
*
3030
* @return void
3131
*
32-
* @dataProvider provide_test_maybe_deactivate_bnpls
32+
* @dataProvider provide_test_maybe_deactivate_payment_methods
3333
*/
34-
public function test_maybe_deactivate_bnpls(
34+
public function test_maybe_deactivate_payment_methods(
3535
$active_gateways,
36-
$get_upe_payment_method_ids_calls,
3736
$enabled_payment_method_ids,
3837
$update_enable_payment_methods_calls
3938
) {
@@ -46,7 +45,7 @@ public function test_maybe_deactivate_bnpls(
4645
->disableOriginalConstructor()
4746
->getMock();
4847

49-
$upe_payment_gateway->expects( $this->exactly( $get_upe_payment_method_ids_calls ) )
48+
$upe_payment_gateway->expects( $this->once() )
5049
->method( 'get_upe_enabled_payment_method_ids' )
5150
->willReturn( $enabled_payment_method_ids );
5251

@@ -62,56 +61,53 @@ public function test_maybe_deactivate_bnpls(
6261
$wc_stripe->method( 'get_main_stripe_gateway' )
6362
->willReturn( $upe_payment_gateway );
6463

65-
$wc_stripe->maybe_deactivate_bnpls();
64+
$wc_stripe->maybe_deactivate_payment_methods();
6665

6766
// Clean up.
6867
WC()->payment_gateways->payment_gateways = $original_payment_gateways;
6968
}
7069

7170
/**
72-
* Provider for `test_maybe_deactivate_bnpls`.
71+
* Provider for `test_maybe_deactivate_payment_methods`.
7372
*
7473
* @return array
7574
*/
76-
public function provide_test_maybe_deactivate_bnpls() {
75+
public function provide_test_maybe_deactivate_payment_methods() {
7776
return [
78-
'none active' => [
77+
'none active' => [
7978
'active gateways' => [],
80-
'get UPE payment method IDs calls' => 0,
8179
'enabled payment method IDs' => [
8280
WC_Stripe_Payment_Methods::CARD,
8381
],
8482
'update enable payment methods calls' => 0,
8583
],
86-
'affirm' => [
84+
'affirm' => [
8785
'active gateways' => [
8886
WC_Stripe_Helper::OFFICIAL_PLUGIN_ID_AFFIRM => (object) [
8987
'id' => WC_Stripe_Helper::OFFICIAL_PLUGIN_ID_AFFIRM,
9088
'enabled' => 'yes',
9189
],
9290
],
93-
'get UPE payment method IDs calls' => 1,
9491
'enabled payment method IDs' => [
9592
WC_Stripe_Payment_Methods::CARD,
9693
WC_Stripe_Payment_Methods::AFFIRM,
9794
],
9895
'update enable payment methods calls' => 1,
9996
],
100-
'klarna' => [
97+
'klarna' => [
10198
'active gateways' => [
10299
WC_Stripe_Helper::OFFICIAL_PLUGIN_ID_KLARNA => (object) [
103100
'id' => WC_Stripe_Helper::OFFICIAL_PLUGIN_ID_KLARNA,
104101
'enabled' => 'yes',
105102
],
106103
],
107-
'get UPE payment method IDs calls' => 1,
108104
'enabled payment method IDs' => [
109105
WC_Stripe_Payment_Methods::CARD,
110106
WC_Stripe_Payment_Methods::KLARNA,
111107
],
112108
'update enable payment methods calls' => 1,
113109
],
114-
'both active, but not on Stripe' => [
110+
'klarna and affirm active, but not on Stripe' => [
115111
'active gateways' => [
116112
WC_Stripe_Helper::OFFICIAL_PLUGIN_ID_AFFIRM => (object) [
117113
'id' => WC_Stripe_Helper::OFFICIAL_PLUGIN_ID_AFFIRM,
@@ -122,13 +118,12 @@ public function provide_test_maybe_deactivate_bnpls() {
122118
'enabled' => 'yes',
123119
],
124120
],
125-
'get UPE payment method IDs calls' => 1,
126121
'enabled payment method IDs' => [
127122
WC_Stripe_Payment_Methods::CARD,
128123
],
129124
'update enable payment methods calls' => 0,
130125
],
131-
'both active in both' => [
126+
'klarna and affirm active in both' => [
132127
'active gateways' => [
133128
WC_Stripe_Helper::OFFICIAL_PLUGIN_ID_AFFIRM => (object) [
134129
'id' => WC_Stripe_Helper::OFFICIAL_PLUGIN_ID_AFFIRM,
@@ -139,14 +134,21 @@ public function provide_test_maybe_deactivate_bnpls() {
139134
'enabled' => 'yes',
140135
],
141136
],
142-
'get UPE payment method IDs calls' => 1,
143137
'enabled payment method IDs' => [
144138
WC_Stripe_Payment_Methods::CARD,
145139
WC_Stripe_Payment_Methods::AFFIRM,
146140
WC_Stripe_Payment_Methods::KLARNA,
147141
],
148142
'update enable payment methods calls' => 1,
149143
],
144+
'amazon pay' => [
145+
'active gateways' => [],
146+
'enabled payment method IDs' => [
147+
WC_Stripe_Payment_Methods::CARD,
148+
WC_Stripe_Payment_Methods::AMAZON_PAY,
149+
],
150+
'update enable payment methods calls' => 1,
151+
],
150152
];
151153
}
152154
}

0 commit comments

Comments
 (0)