Skip to content

Commit 0496af5

Browse files
Mayishaannemirasol
authored andcommitted
Show payment methods available in the PMC on settings page (#4252)
* get available payment methods list from pmc * move if condition to the gateway class function * rename method * add changelog * force fetch pmc only once * revert cleanup * add sepa_debit in list of upe available methods * fix settings controller tests * remove sepa duplicate entry * mock configuration and fix tests * remove unused param * add comments * bail if pmc is not enabled
1 parent 0a9c6b9 commit 0496af5

9 files changed

+59
-12
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* Dev - Improve SPE e2e tests to reduce flakiness
2424
* Fix - Prevents fatal errors for cases where we fail to load product details
2525
* Fix - Address an edge case with webhook URL comparisons
26+
* Add - Only show payment methods in Stripe settings that are available for the connected Stripe account.
2627
* Fix - Show correct gateway name in non payments settings pages.
2728
* Update - Add support for customer order notes and express checkout
2829
* Dev - Minor fix to e2e setup code

includes/admin/class-wc-rest-stripe-settings-controller.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,13 @@ public function register_routes() {
244244
*/
245245
public function get_settings() {
246246
$is_upe_enabled = WC_Stripe_Feature_Flags::is_upe_checkout_enabled();
247+
// When UPE and the payment method configurations API are enabled, fetch the enabled payment methods from the payment method configurations API.
248+
// We force a refresh of the enabled payment methods (by passing true) when on the settings page to ensure the latest data.
249+
// The available payment methods are also fetched from the payment method configurations API under similar conditions,
250+
// but we do not force a refresh for available methods, since calling get_upe_enabled_payment_method_ids first already ensures the list is up to date.
251+
$enabled_payment_method_ids = $is_upe_enabled ? $this->gateway->get_upe_enabled_payment_method_ids( true ) : WC_Stripe_Helper::get_legacy_enabled_payment_method_ids();
247252
$available_payment_method_ids = $is_upe_enabled ? $this->gateway->get_upe_available_payment_methods() : WC_Stripe_Helper::get_legacy_available_payment_method_ids();
248253
$ordered_payment_method_ids = $is_upe_enabled ? WC_Stripe_Helper::get_upe_ordered_payment_method_ids( $this->gateway ) : $available_payment_method_ids;
249-
$enabled_payment_method_ids = $is_upe_enabled ? $this->gateway->get_upe_enabled_payment_method_ids( true ) : WC_Stripe_Helper::get_legacy_enabled_payment_method_ids();
250254

251255
return new WP_REST_Response(
252256
[

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,31 @@ public static function get_parent_configuration_id() {
143143
return self::get_primary_configuration()->parent ?? null;
144144
}
145145

146+
/**
147+
* Get the UPE available payment method IDs.
148+
*
149+
* @return array
150+
*/
151+
public static function get_upe_available_payment_method_ids() {
152+
// Bail if the payment method configurations API is not enabled.
153+
if ( ! self::is_enabled() ) {
154+
return [];
155+
}
156+
157+
$available_payment_method_ids = [];
158+
$merchant_payment_method_configuration = self::get_primary_configuration();
159+
160+
if ( $merchant_payment_method_configuration ) {
161+
foreach ( $merchant_payment_method_configuration as $payment_method_id => $payment_method ) {
162+
if ( isset( $payment_method->display_preference->value ) && isset( WC_Stripe_UPE_Payment_Gateway::UPE_AVAILABLE_METHODS[ $payment_method_id ] ) ) {
163+
$available_payment_method_ids[] = $payment_method_id;
164+
}
165+
}
166+
}
167+
168+
return $available_payment_method_ids;
169+
}
170+
146171
/**
147172
* Get the UPE enabled payment method IDs.
148173
*

includes/payment-methods/class-wc-stripe-upe-payment-gateway.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class WC_Stripe_UPE_Payment_Gateway extends WC_Gateway_Stripe {
3737
WC_Stripe_Payment_Methods::BOLETO => WC_Stripe_UPE_Payment_Method_Boleto::class,
3838
WC_Stripe_Payment_Methods::IDEAL => WC_Stripe_UPE_Payment_Method_Ideal::class,
3939
WC_Stripe_Payment_Methods::OXXO => WC_Stripe_UPE_Payment_Method_Oxxo::class,
40-
WC_Stripe_Payment_Methods::SEPA => WC_Stripe_UPE_Payment_Method_Sepa::class,
40+
WC_Stripe_Payment_Methods::SEPA_DEBIT => WC_Stripe_UPE_Payment_Method_Sepa::class,
4141
WC_Stripe_Payment_Methods::P24 => WC_Stripe_UPE_Payment_Method_P24::class,
4242
WC_Stripe_Payment_Methods::SOFORT => WC_Stripe_UPE_Payment_Method_Sofort::class,
4343
WC_Stripe_Payment_Methods::MULTIBANCO => WC_Stripe_UPE_Payment_Method_Multibanco::class,
@@ -684,16 +684,21 @@ public function get_upe_enabled_at_checkout_payment_method_ids( $order_id = null
684684
* @return string[]
685685
*/
686686
public function get_upe_available_payment_methods() {
687-
$available_payment_methods = [];
687+
// If the payment method configurations API is not enabled, fall back to determining available payment methods
688+
// based on the plugin's internal logic.
689+
if ( ! WC_Stripe_Payment_Method_Configurations::is_enabled() ) {
690+
$available_payment_methods = [];
688691

689-
foreach ( $this->payment_methods as $payment_method ) {
690-
if ( is_callable( [ $payment_method, 'is_available_for_account_country' ] ) && ! $payment_method->is_available_for_account_country() ) {
691-
continue;
692+
foreach ( $this->payment_methods as $payment_method ) {
693+
if ( is_callable( [ $payment_method, 'is_available_for_account_country' ] ) && ! $payment_method->is_available_for_account_country() ) {
694+
continue;
695+
}
696+
$available_payment_methods[] = $payment_method->get_id();
692697
}
693-
$available_payment_methods[] = $payment_method->get_id();
698+
return $available_payment_methods;
694699
}
695700

696-
return $available_payment_methods;
701+
return WC_Stripe_Payment_Method_Configurations::get_upe_available_payment_method_ids();
697702
}
698703

699704
/**
@@ -742,6 +747,12 @@ private function get_stripe_supported_payment_methods() {
742747
$supported_stripe_ids = [];
743748
$available_payment_method_ids = $this->get_upe_available_payment_methods();
744749

750+
// Return the list if the payment method configurations API is enabled.
751+
// We don't need any additional filtering as the list is already fetched from the payment method configurations API..
752+
if ( WC_Stripe_Payment_Method_Configurations::is_enabled() ) {
753+
return $available_payment_method_ids;
754+
}
755+
745756
foreach ( self::UPE_AVAILABLE_METHODS as $gateway_class ) {
746757
$gateway = new $gateway_class();
747758

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
133133
* Dev - Improve SPE e2e tests to reduce flakiness
134134
* Fix - Prevents fatal errors for cases where we fail to load product details
135135
* Fix - Address an edge case with webhook URL comparisons
136+
* Add - Only show payment methods in Stripe settings that are available for the connected Stripe account.
136137
* Fix - Show correct gateway name in non payments settings pages.
137138
* Update - Add support for customer order notes and express checkout
138139
* Dev - Minor fix to e2e setup code

tests/phpunit/admin/test-class-wc-rest-stripe-settings-controller-gb.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ public function set_up() {
9090
$upe_helper = new UPE_Test_Helper();
9191
$upe_helper->enable_upe();
9292
$upe_helper->reload_payment_gateways();
93-
$this->mock_payment_method_configurations( [ 'card' ], [] );
9493

9594
$this->controller = new WC_REST_Stripe_Settings_Controller( new WC_Stripe_UPE_Payment_Gateway() );
9695

@@ -117,6 +116,7 @@ public function test_get_settings_returns_available_payment_method_ids_for_gb()
117116
WC_Stripe_Payment_Methods::ACSS_DEBIT,
118117
WC_Stripe_Payment_Methods::BACS_DEBIT,
119118
];
119+
$this->mock_payment_method_configurations( $expected_method_ids, [] );
120120

121121
$response = $this->rest_get_settings();
122122
$available_method_ids = $response->get_data()['available_payment_method_ids'];
@@ -148,6 +148,7 @@ public function test_get_settings_returns_ordered_payment_method_ids_for_gb() {
148148
WC_Stripe_Payment_Methods::ACSS_DEBIT,
149149
WC_Stripe_Payment_Methods::BACS_DEBIT,
150150
];
151+
$this->mock_payment_method_configurations( $expected_ordered_method_ids, [] );
151152

152153
$response = $this->rest_get_settings();
153154

tests/phpunit/admin/test-class-wc-rest-stripe-settings-controller.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function test_get_stripe_payment_method_configurations_settings( $enabled
112112
*/
113113
public function test_update_stripe_payment_method_configurations_settings() {
114114
// Set up initial state with only card enabled
115-
$this->mock_payment_method_configurations( [ 'card' ], [] );
115+
$this->mock_payment_method_configurations( [ 'card' ], [ 'amazon_pay', 'google_pay', 'apple_pay' ] );
116116

117117
// Set pmc_enabled to yes to prevent migration
118118
$stripe_settings = WC_Stripe_Helper::get_stripe_settings();
@@ -285,8 +285,6 @@ public function test_individual_payment_method_settings() {
285285
}
286286

287287
public function test_get_settings_returns_available_payment_method_ids() {
288-
$response = $this->rest_get_settings();
289-
290288
$expected_method_ids = [
291289
WC_Stripe_Payment_Methods::CARD,
292290
WC_Stripe_Payment_Methods::ACH,
@@ -308,6 +306,9 @@ public function test_get_settings_returns_available_payment_method_ids() {
308306
WC_Stripe_Payment_Methods::CASHAPP_PAY,
309307
WC_Stripe_Payment_Methods::ACSS_DEBIT,
310308
];
309+
$this->mock_payment_method_configurations( $expected_method_ids, [] );
310+
311+
$response = $this->rest_get_settings();
311312
$available_method_ids = $response->get_data()['available_payment_method_ids'];
312313

313314
$this->assertEquals(
@@ -357,6 +358,7 @@ public function test_get_settings_returns_ordered_payment_method_ids() {
357358
WC_Stripe_Payment_Methods::CASHAPP_PAY,
358359
WC_Stripe_Payment_Methods::ACSS_DEBIT,
359360
];
361+
$this->mock_payment_method_configurations( $expected_method_ids, [] );
360362

361363
$response = $this->rest_get_settings();
362364
$ordered_method_ids = $response->get_data()['ordered_payment_method_ids'];

tests/phpunit/payment-methods/test-class-wc-stripe-upe-payment-gateway-gb.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public function tear_down() {
6464
* @dataProvider get_upe_available_payment_methods_provider
6565
*/
6666
public function test_get_upe_available_payment_methods_for_gb( $country, $available_payment_methods ) {
67+
$this->mock_payment_method_configurations( $available_payment_methods );
6768
$expected = $this->mock_gateway->get_upe_available_payment_methods();
6869

6970
$this->assertSame( $available_payment_methods, $expected );

tests/phpunit/payment-methods/test-class-wc-stripe-upe-payment-gateway.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ private function get_order_details( $order ) {
253253
* @dataProvider get_upe_available_payment_methods_provider
254254
*/
255255
public function test_get_upe_available_payment_methods( $country, $available_payment_methods ) {
256+
$this->mock_payment_method_configurations( $available_payment_methods );
256257
$this->set_stripe_account_data( [ 'country' => $country ] ); // TODO: Verify if the country is actually changing in the gateway.
257258
$this->assertSame( $available_payment_methods, $this->mock_gateway->get_upe_available_payment_methods(), "Available payment methods are not the same for $country" );
258259
}

0 commit comments

Comments
 (0)