Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supply correct payment method instance to process_redirect_payment #6170

Merged
merged 9 commits into from
May 10, 2023
4 changes: 4 additions & 0 deletions changelog/fix-5854-use-correct-payment-method-id
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fix

Supply correct payment method instance to process_redirect_payment.
26 changes: 22 additions & 4 deletions includes/payment-methods/class-upe-split-payment-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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_title;
FangedParakeet marked this conversation as resolved.
Show resolved Hide resolved

$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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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() )
Expand All @@ -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' )
Expand All @@ -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(
[
Expand Down Expand Up @@ -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() )
Expand All @@ -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 );
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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() )
Expand All @@ -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 );
Expand Down Expand Up @@ -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() );
}
Expand Down