diff --git a/changelog/fix-5854-use-correct-payment-method-id b/changelog/fix-5854-use-correct-payment-method-id new file mode 100644 index 00000000000..b8834ad2f85 --- /dev/null +++ b/changelog/fix-5854-use-correct-payment-method-id @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Supply correct payment method instance to process_redirect_payment. diff --git a/includes/payment-methods/class-upe-split-payment-gateway.php b/includes/payment-methods/class-upe-split-payment-gateway.php index d48c378986f..705ee64932a 100644 --- a/includes/payment-methods/class-upe-split-payment-gateway.php +++ b/includes/payment-methods/class-upe-split-payment-gateway.php @@ -160,13 +160,31 @@ public function should_use_stripe_platform_on_checkout_page() { * @return UPE_Payment_Method|false UPE payment method instance. */ public function get_selected_payment_method( $payment_method_type ) { - if ( $payment_method_type !== $this->stripe_id ) { - return false; + return WC_Payments::get_payment_method_by_id( $payment_method_type ); + } + + /** + * Set formatted readable payment method title for order, + * using payment method details from accompanying charge. + * + * @param WC_Order $order WC Order being processed. + * @param string $payment_method_type Stripe payment method key. + * @param array|bool $payment_method_details Array of payment method details from charge or false. + */ + public function set_payment_method_title_for_order( $order, $payment_method_type, $payment_method_details ) { + $payment_method = $this->get_selected_payment_method( $payment_method_type ); + if ( ! $payment_method ) { + return; } - return $this->payment_method; - } + $payment_method_title = $payment_method->get_title( $payment_method_details ); + $payment_gateway = 'card' === $payment_method->get_id() ? self::GATEWAY_ID : self::GATEWAY_ID . '_' . $payment_method_type; + + $order->set_payment_method( $payment_gateway ); + $order->set_payment_method_title( $payment_method_title ); + $order->save(); + } /** * Handle AJAX request for updating a payment intent for Stripe UPE. diff --git a/tests/unit/payment-methods/test-class-upe-split-payment-gateway.php b/tests/unit/payment-methods/test-class-upe-split-payment-gateway.php index 0616178084d..237b892ae0f 100644 --- a/tests/unit/payment-methods/test-class-upe-split-payment-gateway.php +++ b/tests/unit/payment-methods/test-class-upe-split-payment-gateway.php @@ -272,6 +272,8 @@ public function set_up() { 'is_payment_recurring', 'get_payment_method_ids_enabled_at_checkout', 'wc_payments_get_payment_gateway_by_id', + 'get_selected_payment_method', + 'remove_upe_payment_intent_from_session', ] ) ->getMock(); @@ -1199,6 +1201,8 @@ public function test_process_redirect_payment_intent_processing() { $intent_id = 'pi_mock'; $payment_method_id = 'pm_mock'; + $card_method = $this->mock_payment_methods['card']; + $payment_intent = WC_Helper_Intention::create_intention( [ 'status' => $intent_status ] ); $mock_upe_gateway->expects( $this->once() ) @@ -1207,6 +1211,10 @@ public function test_process_redirect_payment_intent_processing() { $this->returnValue( [ $user, $customer_id ] ) ); + $mock_upe_gateway->expects( $this->any() ) + ->method( 'get_selected_payment_method' ) + ->willReturn( $card_method ); + $this->mock_wcpay_request( Get_Intention::class, 1, $intent_id ) ->expects( $this->once() ) ->method( 'format_response' ) @@ -1216,6 +1224,9 @@ public function test_process_redirect_payment_intent_processing() { $mock_upe_gateway->process_redirect_payment( $order_id, $intent_id, $save_payment_method ); + $mock_upe_gateway->expects( $this->any() ) + ->method( 'remove_upe_payment_intent_from_session' ); + $result_order = wc_get_order( $order_id ); $note = wc_get_order_notes( [ @@ -1247,6 +1258,8 @@ public function test_process_redirect_payment_intent_succeded() { $intent_id = 'pi_mock'; $payment_method_id = 'pm_mock'; + $card_method = $this->mock_payment_methods['card']; + $payment_intent = WC_Helper_Intention::create_intention( [ 'status' => $intent_status ] ); $mock_upe_gateway->expects( $this->once() ) @@ -1260,6 +1273,10 @@ public function test_process_redirect_payment_intent_succeded() { ->method( 'format_response' ) ->willReturn( $payment_intent ); + $mock_upe_gateway->expects( $this->any() ) + ->method( 'get_selected_payment_method' ) + ->willReturn( $card_method ); + $this->set_cart_contains_subscription_items( false ); $mock_upe_gateway->process_redirect_payment( $order_id, $intent_id, $save_payment_method ); @@ -1289,6 +1306,8 @@ public function test_process_redirect_setup_intent_succeded() { $payment_method_id = 'pm_mock'; $token = WC_Helper_Token::create_token( $payment_method_id ); + $card_method = $this->mock_payment_methods['card']; + $order->set_shipping_total( 0 ); $order->set_shipping_tax( 0 ); $order->set_cart_tax( 0 ); @@ -1327,6 +1346,10 @@ public function test_process_redirect_setup_intent_succeded() { $this->returnValue( $token ) ); + $mock_upe_gateway->expects( $this->any() ) + ->method( 'get_selected_payment_method' ) + ->willReturn( $card_method ); + $this->set_cart_contains_subscription_items( true ); $mock_upe_gateway->process_redirect_payment( $order_id, $intent_id, $save_payment_method ); @@ -1356,6 +1379,8 @@ public function test_process_redirect_payment_save_payment_token() { $payment_method_id = 'pm_mock'; $token = WC_Helper_Token::create_token( $payment_method_id ); + $card_method = $this->mock_payment_methods['card']; + $payment_intent = WC_Helper_Intention::create_intention( [ 'status' => $intent_status ] ); $mock_upe_gateway->expects( $this->once() ) @@ -1375,6 +1400,10 @@ public function test_process_redirect_payment_save_payment_token() { $this->returnValue( $token ) ); + $mock_upe_gateway->expects( $this->any() ) + ->method( 'get_selected_payment_method' ) + ->willReturn( $card_method ); + $this->set_cart_contains_subscription_items( false ); $mock_upe_gateway->process_redirect_payment( $order_id, $intent_id, $save_payment_method ); @@ -1477,6 +1506,10 @@ public function test_correct_payment_method_title_for_order() { foreach ( $charge_payment_method_details as $i => $payment_method_details ) { $payment_method_id = $payment_method_details['type']; $mock_upe_gateway = $this->mock_payment_gateways[ $payment_method_id ]; + $payment_method = $this->mock_payment_methods[ $payment_method_id ]; + $mock_upe_gateway->expects( $this->any() ) + ->method( 'get_selected_payment_method' ) + ->willReturn( $payment_method ); $mock_upe_gateway->set_payment_method_title_for_order( $order, $payment_method_id, $payment_method_details ); $this->assertEquals( $expected_payment_method_titles[ $i ], $order->get_payment_method_title() ); }