Skip to content

Commit 1d33b0a

Browse files
annemirasoldiegocurbelo
authored andcommitted
Set correct payment method when using Link (#3636)
* Set correct payment method for Link cards with 3DS auth * Display 'Link' as the payment method title in Edit Order * Set correct gateway ID for subscriptions purchased via Link * Add changelog and readme entries * Add unit test
1 parent 9a75c56 commit 1d33b0a

File tree

5 files changed

+51
-5
lines changed

5 files changed

+51
-5
lines changed

changelog.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
*** Changelog ***
22

33
= 9.0.0 - xxxx-xx-xx =
4+
* Fix - Set correct payment method when using Link and a card that is 3D Secure authenticated.
5+
* Fix - Fix total calculation for custom product types when using the Payment Request Button.
46
* Fix - Fix order attribution metadata not included in PRBs or Express Checkout Element.
57
* Add - Pre-fill user email and phone number for Link in the Payment Element.
68
* Remove - Remove Link autofill modal feature.

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,10 +1614,12 @@ public function set_payment_method_title_for_order( $order, $payment_method_type
16141614

16151615
$payment_method = $this->payment_methods[ $payment_method_type ];
16161616
$payment_method_id = $payment_method instanceof WC_Stripe_UPE_Payment_Method_CC ? $this->id : $payment_method->id;
1617-
$is_stripe_link = isset( $stripe_payment_method->type ) && WC_Stripe_Payment_Methods::LINK === $stripe_payment_method->type;
1617+
$is_stripe_link = WC_Stripe_Payment_Methods::LINK === $payment_method_type ||
1618+
( isset( $stripe_payment_method->type ) && WC_Stripe_Payment_Methods::LINK === $stripe_payment_method->type );
16181619

16191620
// Stripe Link uses the main gateway to process payments, however Link payments should use the title of the Link payment method.
16201621
if ( $is_stripe_link && isset( $this->payment_methods[ WC_Stripe_Payment_Methods::LINK ] ) ) {
1622+
$payment_method_id = $this->id;
16211623
$payment_method_title = $this->payment_methods[ WC_Stripe_Payment_Methods::LINK ]->get_title( $stripe_payment_method );
16221624
} else {
16231625
$payment_method_title = $payment_method->get_title( $stripe_payment_method );
@@ -2488,11 +2490,12 @@ private function should_upe_payment_method_show_save_option( $payment_method ) {
24882490
protected function get_upe_gateway_id_for_order( $payment_method ) {
24892491
$token_gateway_type = $payment_method->get_retrievable_type();
24902492

2491-
if ( WC_Stripe_Payment_Methods::CARD !== $token_gateway_type ) {
2492-
return $this->payment_methods[ $token_gateway_type ]->id;
2493+
if ( WC_Stripe_Payment_Methods::CARD === $token_gateway_type ||
2494+
WC_Stripe_Payment_Methods::LINK === $token_gateway_type ) {
2495+
return $this->id;
24932496
}
24942497

2495-
return $this->id;
2498+
return $this->payment_methods[ $token_gateway_type ]->id;
24962499
}
24972500

24982501
/**

includes/payment-methods/class-wc-stripe-upe-payment-method-link.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public function __construct() {
2424
for further payments.',
2525
'woocommerce-gateway-stripe'
2626
);
27+
28+
add_filter( 'woocommerce_gateway_title', [ $this, 'filter_gateway_title' ], 10, 2 );
2729
}
2830

2931
/**
@@ -109,4 +111,30 @@ public function is_available() {
109111
public function requires_automatic_capture() {
110112
return false;
111113
}
114+
115+
/**
116+
* Filters the gateway title to reflect Link as the payment method.
117+
*/
118+
public function filter_gateway_title( $title, $id ) {
119+
global $theorder;
120+
121+
// If $theorder is empty (i.e. non-HPOS), fallback to using the global post object.
122+
if ( empty( $theorder ) && ! empty( $GLOBALS['post']->ID ) ) {
123+
$theorder = wc_get_order( $GLOBALS['post']->ID );
124+
}
125+
126+
if ( ! is_object( $theorder ) ) {
127+
return $title;
128+
}
129+
130+
$method_title = $theorder->get_payment_method_title();
131+
132+
if ( 'stripe' === $id && ! empty( $method_title ) ) {
133+
if ( 'Link' === $method_title ) {
134+
return $method_title;
135+
}
136+
}
137+
138+
return $title;
139+
}
112140
}

readme.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
111111
== Changelog ==
112112

113113
= 9.0.0 - xxxx-xx-xx =
114+
* Fix - Set correct payment method when using Link and a card that is 3D Secure authenticated.
115+
* Fix - Fix total calculation for custom product types when using the Payment Request Button.
114116
* Fix - Fix order attribution metadata not included in PRBs or Express Checkout Element.
115117
* Add - Pre-fill user email and phone number for Link in the Payment Element.
116118
* Remove - Remove Link autofill modal feature.

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2347,7 +2347,7 @@ public function test_set_payment_method_title_for_order() {
23472347

23482348
WC_Subscriptions_Helpers::$wcs_get_subscriptions_for_order = [ $mock_subscription_0, $mock_subscription_1 ];
23492349

2350-
$this->mock_gateway->expects( $this->exactly( 3 ) ) // 3 times because we test 3 payment methods.
2350+
$this->mock_gateway->expects( $this->exactly( 4 ) ) // 4 times because we test 4 payment methods.
23512351
->method( 'is_subscriptions_enabled' )
23522352
->willReturn( true );
23532353

@@ -2385,6 +2385,17 @@ public function test_set_payment_method_title_for_order() {
23852385

23862386
$this->assertEquals( 'stripe', $mock_subscription_0->get_payment_method() );
23872387
$this->assertEquals( 'stripe', $mock_subscription_0->get_payment_method() );
2388+
2389+
/**
2390+
* Link
2391+
*/
2392+
$this->mock_gateway->set_payment_method_title_for_order( $order, WC_Stripe_UPE_Payment_Method_Link::STRIPE_ID );
2393+
// Cards should be set to `stripe`.
2394+
$this->assertEquals( 'stripe', $order->get_payment_method() );
2395+
$this->assertEquals( 'Link', $order->get_payment_method_title() );
2396+
2397+
$this->assertEquals( 'stripe', $mock_subscription_0->get_payment_method() );
2398+
$this->assertEquals( 'stripe', $mock_subscription_0->get_payment_method() );
23882399
}
23892400

23902401
/**

0 commit comments

Comments
 (0)