Skip to content

Commit

Permalink
Fallback to card payment type in get_payment_method_types (#9829)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdmoore authored Dec 5, 2024
1 parent c9ebf93 commit 907c238
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 76 deletions.
5 changes: 5 additions & 0 deletions changelog/8969-fallback-to-card-payment-type
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: update
Comment: Small change to payment method types fallback scenario.


6 changes: 2 additions & 4 deletions includes/class-wc-payment-gateway-wcpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -1571,7 +1571,6 @@ public function process_payment_for_order( $cart, $payment_information, $schedul
throw new Exception( WC_Payments_Utils::get_filtered_error_message( $e ) );
}

$payment_methods = $this->get_payment_method_types( $payment_information );
// The sanitize_user call here is deliberate: it seems the most appropriate sanitization function
// for a string that will only contain latin alphanumeric characters and underscores.
// phpcs:ignore WordPress.Security.NonceVerification.Missing
Expand Down Expand Up @@ -1602,6 +1601,8 @@ public function process_payment_for_order( $cart, $payment_information, $schedul
}

if ( empty( $intent ) ) {
$payment_methods = $this->get_payment_method_types( $payment_information );

$request = Create_And_Confirm_Intention::create();
$request->set_amount( $converted_amount );
$request->set_currency_code( $currency );
Expand Down Expand Up @@ -2126,9 +2127,6 @@ public function get_payment_method_types( $payment_information ): array {
$order = $payment_information->get_order();
$order_id = $order instanceof WC_Order ? $order->get_id() : null;
$payment_methods = $this->get_payment_methods_from_gateway_id( $token->get_gateway_id(), $order_id );
} else {
// Final fallback case, if all else fails.
$payment_methods = WC_Payments::get_gateway()->get_payment_method_ids_enabled_at_checkout( null, true );
}

return $payment_methods;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1140,59 +1140,6 @@ public function test_get_payment_methods_without_request_context() {
$this->assertSame( [ Payment_Method::CARD ], $payment_methods );
}

/**
* Test get_payment_method_types without post request context or saved token.
*
* @return void
*/
public function test_get_payment_methods_without_request_context_or_token() {
$mock_upe_gateway = $this->getMockBuilder( WC_Payment_Gateway_WCPay::class )
->setConstructorArgs(
[
$this->mock_api_client,
$this->mock_wcpay_account,
$this->mock_customer_service,
$this->mock_token_service,
$this->mock_action_scheduler_service,
$this->mock_payment_methods[ Payment_Method::CARD ],
$this->mock_payment_methods,
$this->order_service,
$this->mock_dpps,
$this->mock_localization_service,
$this->mock_fraud_service,
$this->mock_duplicates_detection_service,
$this->mock_rate_limiter,
]
)
->setMethods(
[
'get_payment_methods_from_gateway_id',
'get_payment_method_ids_enabled_at_checkout',
]
)
->getMock();

$payment_information = new Payment_Information( 'pm_mock' );

unset( $_POST['payment_method'] ); // phpcs:ignore WordPress.Security.NonceVerification

$gateway = WC_Payments::get_gateway();
WC_Payments::set_gateway( $mock_upe_gateway );

$mock_upe_gateway->expects( $this->never() )
->method( 'get_payment_methods_from_gateway_id' );

$mock_upe_gateway->expects( $this->once() )
->method( 'get_payment_method_ids_enabled_at_checkout' )
->willReturn( [ Payment_Method::CARD ] );

$payment_methods = $mock_upe_gateway->get_payment_method_types( $payment_information );

$this->assertSame( [ Payment_Method::CARD ], $payment_methods );

WC_Payments::set_gateway( $gateway );
}

/**
* Test get_payment_methods_from_gateway_id function with UPE enabled.
*
Expand Down
32 changes: 13 additions & 19 deletions tests/unit/test-class-wc-payment-gateway-wcpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -1239,21 +1239,6 @@ public function test_get_payment_methods_without_post_request_context() {
$this->assertSame( [ Payment_Method::CARD ], $payment_methods );
}

public function test_get_payment_methods_without_request_context_or_token() {
$payment_information = new Payment_Information( 'pm_mock' );

unset( $_POST['payment_method'] ); // phpcs:ignore WordPress.Security.NonceVerification

$gateway = WC_Payments::get_gateway();
WC_Payments::set_gateway( $this->card_gateway );

$payment_methods = $this->card_gateway->get_payment_method_types( $payment_information );

$this->assertSame( [ Payment_Method::CARD ], $payment_methods );

WC_Payments::set_gateway( $gateway );
}

public function test_get_payment_methods_from_gateway_id_upe() {
WC_Helper_Order::create_order();

Expand Down Expand Up @@ -2542,7 +2527,7 @@ public function test_process_payment_for_order_not_from_request() {
$order->add_payment_token( $token );
$order->save();

$pi = new Payment_Information( 'pm_test', $order, null, null, null, null, null, '', 'card' );
$pi = new Payment_Information( 'pm_test', $order, null, $token, null, null, null, '', 'card' );

$request = $this->mock_wcpay_request( Create_And_Confirm_Intention::class );
$request->expects( $this->once() )
Expand Down Expand Up @@ -3086,7 +3071,10 @@ public function test_process_payment_caches_mimimum_amount_and_displays_error_up
->method( 'get_customer_id_by_user_id' )
->will( $this->returnValue( $customer ) );

$_POST = [ 'wcpay-payment-method' => $pm = 'pm_mock' ];
$_POST = [
'wcpay-payment-method' => $pm = 'pm_mock',
'payment_method' => 'woocommerce_payments',
];

$this->get_fraud_prevention_service_mock()
->expects( $this->once() )
Expand Down Expand Up @@ -3922,7 +3910,10 @@ public function test_process_payment_rate_limiter_enabled_throw_exception() {

public function test_process_payment_returns_correct_redirect() {
$order = WC_Helper_Order::create_order();
$_POST = [ 'wcpay-payment-method' => 'pm_mock' ];
$_POST = [
'wcpay-payment-method' => 'pm_mock',
'payment_method' => 'woocommerce_payments',
];

$this->mock_wcpay_request( Create_And_Confirm_Intention::class, 1 )
->expects( $this->once() )
Expand All @@ -3945,7 +3936,10 @@ public function test_process_payment_returns_correct_redirect() {
public function test_process_payment_returns_correct_redirect_when_using_payment_request() {
$order = WC_Helper_Order::create_order();
$_POST['payment_request_type'] = 'google_pay';
$_POST = [ 'wcpay-payment-method' => 'pm_mock' ];
$_POST = [
'wcpay-payment-method' => 'pm_mock',
'payment_method' => 'woocommerce_payments',
];

$this->mock_wcpay_request( Create_And_Confirm_Intention::class, 1 )
->expects( $this->once() )
Expand Down

0 comments on commit 907c238

Please sign in to comment.