Skip to content

Commit

Permalink
Ensure gateway functionality for disabled payment method (#8216)
Browse files Browse the repository at this point in the history
Co-authored-by: Timur Karimov <[email protected]>
Co-authored-by: Timur Karimov <[email protected]>
  • Loading branch information
3 people authored Feb 28, 2024
1 parent e497461 commit 835df7f
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 33 deletions.
4 changes: 4 additions & 0 deletions changelog/fix-refunds-with-pms-disabled-on-checkout
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: fix

Ensure gateways accessibility for use cases which don't require the gateway to be enabled
6 changes: 6 additions & 0 deletions includes/class-wc-payment-gateway-wcpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,12 @@ public function is_available() {
return false;
}

// Disable the gateway if it should not be displayed on the checkout page.
$is_gateway_enabled = in_array( $this->stripe_id, $this->get_payment_method_ids_enabled_at_checkout(), true ) ? true : false;
if ( ! $is_gateway_enabled ) {
return false;
}

return parent::is_available() && ! $this->needs_setup();
}

Expand Down
2 changes: 1 addition & 1 deletion includes/class-wc-payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ public static function get_plugin_headers() {
* @return array The list of payment gateways that will be available, including WooPayments' Gateway class.
*/
public static function register_gateway( $gateways ) {
$payment_methods = self::$card_gateway->get_payment_method_ids_enabled_at_checkout();
$payment_methods = array_keys( self::get_payment_method_map() );

$key = array_search( 'link', $payment_methods, true );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,6 @@ public function set_up() {
'get_payment_method_ids_enabled_at_checkout',
'wc_payments_get_payment_gateway_by_id',
'get_selected_payment_method',
'get_upe_enabled_payment_method_ids',
]
)
->getMock();
Expand Down Expand Up @@ -381,12 +380,7 @@ public function test_should_not_use_stripe_platform_on_checkout_page_for_upe() {
public function test_link_payment_method_requires_mandate_data() {
$mock_upe_gateway = $this->mock_payment_gateways[ Payment_Method::CARD ];

$mock_upe_gateway
->expects( $this->once() )
->method( 'get_upe_enabled_payment_method_ids' )
->will(
$this->returnValue( [ 'link' ] )
);
$mock_upe_gateway->update_option( 'upe_enabled_payment_method_ids', [ 'link' ] );

$this->assertTrue( $mock_upe_gateway->is_mandate_data_required() );
}
Expand Down Expand Up @@ -422,11 +416,7 @@ public function test_process_payment_returns_correct_redirect_when_using_saved_p
->will(
$this->returnValue( [ $user, $customer_id ] )
);
$mock_card_payment_gateway->expects( $this->any() )
->method( 'get_upe_enabled_payment_method_ids' )
->will(
$this->returnValue( [ Payment_Method::CARD ] )
);
$mock_card_payment_gateway->update_option( 'upe_enabled_payment_method_ids', [ Payment_Method::CARD ] );
$this->mock_wcpay_request( Create_And_Confirm_Intention::class, 1 )
->expects( $this->once() )
->method( 'format_response' )
Expand Down Expand Up @@ -1216,7 +1206,6 @@ public function test_get_payment_methods_from_gateway_id_upe() {
)
->onlyMethods(
[
'get_upe_enabled_payment_method_ids',
'get_payment_method_ids_enabled_at_checkout',
]
)
Expand All @@ -1225,11 +1214,7 @@ public function test_get_payment_methods_from_gateway_id_upe() {
$gateway = WC_Payments::get_gateway();
WC_Payments::set_gateway( $mock_upe_gateway );

$mock_upe_gateway->expects( $this->any() )
->method( 'get_upe_enabled_payment_method_ids' )
->will(
$this->returnValue( [ Payment_Method::CARD, Payment_Method::LINK ] )
);
$mock_upe_gateway->update_option( 'upe_enabled_payment_method_ids', [ Payment_Method::CARD, Payment_Method::LINK ] );

$payment_methods = $mock_upe_gateway->get_payment_methods_from_gateway_id( WC_Payment_Gateway_WCPay::GATEWAY_ID . '_' . Payment_Method::BANCONTACT );
$this->assertSame( [ Payment_Method::BANCONTACT ], $payment_methods );
Expand Down
62 changes: 60 additions & 2 deletions tests/unit/test-class-wc-payment-gateway-wcpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -821,8 +821,8 @@ public function test_exception_will_be_thrown_if_phone_number_is_invalid() {
}

public function test_remove_link_payment_method_if_card_disabled() {
$link_gateway = $this->get_gateway( Payment_Method::LINK );
$link_gateway->settings['upe_enabled_payment_method_ids'] = [ 'link' ];
$link_gateway = $this->get_gateway( Payment_Method::LINK );
$link_gateway->settings['upe_enabled_payment_method_ids'] = [ 'link' ];

$this->mock_wcpay_account
->expects( $this->any() )
Expand Down Expand Up @@ -2622,6 +2622,22 @@ function ( $order ) {
}
}

public function test_gateway_enabled_when_payment_method_is_enabled() {
$afterpay = $this->get_gateway( Payment_Method::AFTERPAY );
$afterpay->update_option( 'upe_enabled_payment_method_ids', [ Payment_Method::AFTERPAY, Payment_Method::CARD, Payment_Method::P24, Payment_Method::BANCONTACT ] );
$this->prepare_gateway_for_availability_testing( $afterpay );

$this->assertTrue( $afterpay->is_available() );
}

public function test_gateway_disabled_when_payment_method_is_disabled() {
$afterpay = $this->get_gateway( Payment_Method::AFTERPAY );
$afterpay->update_option( 'upe_enabled_payment_method_ids', [ Payment_Method::CARD, Payment_Method::P24, Payment_Method::BANCONTACT ] );
$this->prepare_gateway_for_availability_testing( $afterpay );

$this->assertFalse( $afterpay->is_available() );
}

public function test_process_payment_for_order_cc_payment_method() {
$payment_method = 'woocommerce_payments';
$expected_upe_payment_method_for_pi_creation = 'card';
Expand Down Expand Up @@ -3374,6 +3390,48 @@ private function create_charge_object() {
return new WC_Payments_API_Charge( $this->mock_charge_id, 1500, $created );
}

private function prepare_gateway_for_availability_testing( $gateway ) {
WC_Payments::mode()->test();
$current_currency = strtolower( get_woocommerce_currency() );
$this->mock_wcpay_account->expects( $this->once() )->method( 'get_account_customer_supported_currencies' )->will(
$this->returnValue(
[
$current_currency,
]
)
);

$this->mock_wcpay_account
->expects( $this->any() )
->method( 'get_cached_account_data' )
->willReturn(
[
'capabilities' => [
'afterpay_clearpay_payments' => 'active',
],
'capability_requirements' => [
'afterpay_clearpay_payments' => [],
],
]
);

$this->mock_wcpay_account
->expects( $this->any() )
->method( 'is_stripe_connected' )
->willReturn( true );

$this->mock_wcpay_account
->expects( $this->any() )
->method( 'get_account_status_data' )
->willReturn(
[
'paymentsEnabled' => true,
]
);
$gateway->update_option( WC_Payment_Gateway_WCPay::METHOD_ENABLED_KEY, 'yes' );
$gateway->init_settings();
}

private function init_payment_methods() {
$payment_methods = [];

Expand Down
15 changes: 3 additions & 12 deletions tests/unit/test-class-wc-payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,9 @@ public function test_it_does_not_register_woopay_hooks_if_feature_flag_is_disabl
}

public function test_it_skips_stripe_link_gateway_registration() {
$this->mock_cache->method( 'get' )->willReturn( [ 'is_deferred_intent_creation_upe_enabled' => true ] );
$all_gateways_before_registration = count( WC_Payments::get_payment_method_map() );
$card_gateway_mock = $this->createMock( WC_Payment_Gateway_WCPay::class );

$card_gateway_mock = $this->createMock( WC_Payment_Gateway_WCPay::class );
$card_gateway_mock
->expects( $this->once() )
->method( 'get_payment_method_ids_enabled_at_checkout' )
->willReturn(
[
'link',
'card',
]
);
$card_gateway_mock
->expects( $this->once() )
->method( 'get_stripe_id' )
Expand All @@ -98,7 +89,7 @@ public function test_it_skips_stripe_link_gateway_registration() {

$registered_gateways = WC_Payments::register_gateway( [] );

$this->assertCount( 1, $registered_gateways );
$this->assertCount( $all_gateways_before_registration - 1, $registered_gateways );
$this->assertInstanceOf( WC_Payment_Gateway_WCPay::class, $registered_gateways[0] );
$this->assertEquals( $registered_gateways[0]->get_stripe_id(), 'card' );
}
Expand Down

0 comments on commit 835df7f

Please sign in to comment.