Skip to content

Commit b5e6aa3

Browse files
authored
Enable Link PM for subscriptions (#2455)
1 parent 823c118 commit b5e6aa3

File tree

6 files changed

+165
-16
lines changed

6 files changed

+165
-16
lines changed

changelog.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
*** Changelog ***
22

33
= 7.0.0 - 2022-xx-xx =
4+
* Add - Auto-complete first and last name on checkout form when using Link payment method.
5+
* Add - Allow subscription orders to be paid with Link payment method.
46

57
= 6.9.0 - 2022-10-19 =
68
* Tweak - Remove remaining traces of old Stripe settings.
79
* Add - Add Boleto expiration setting.
810
* Add - Declare incompatibility with HPOS.
9-
* Add - Auto-complete first and last name on checkout form when using Link payment method.
1011

1112
= 6.8.0 - 2022-09-28 =
1213
* Fix - Minor adjustments for Custom Order Tables compatibility.
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
if ( ! defined( 'ABSPATH' ) ) {
4+
exit; // Exit if accessed directly
5+
}
6+
7+
// phpcs:disable WordPress.Files.FileName
8+
9+
/**
10+
* WooCommerce Stripe Link Payment Token.
11+
*
12+
* Representation of a payment token for Link.
13+
*
14+
* @class WC_Payment_Token_Link
15+
*/
16+
class WC_Payment_Token_Link extends WC_Payment_Token {
17+
18+
/**
19+
* Stores payment type.
20+
*
21+
* @var string
22+
*/
23+
protected $type = 'link';
24+
25+
/**
26+
* Stores Link payment token data.
27+
*
28+
* @var array
29+
*/
30+
protected $extra_data = [
31+
'email' => '',
32+
];
33+
34+
/**
35+
* Get type to display to user.
36+
*
37+
* @param string $deprecated Deprecated since WooCommerce 3.0
38+
* @return string
39+
*/
40+
public function get_display_name( $deprecated = '' ) {
41+
$display = sprintf(
42+
/* translators: customer email */
43+
__( 'Stripe Link email %s', 'woocommerce-gateway-stripe' ),
44+
$this->get_email()
45+
);
46+
47+
return $display;
48+
}
49+
50+
/**
51+
* Hook prefix
52+
*/
53+
protected function get_hook_prefix() {
54+
return 'woocommerce_payment_token_link_get_';
55+
}
56+
57+
/**
58+
* Set Stripe payment method type.
59+
*
60+
* @param string $type Payment method type.
61+
*/
62+
public function set_payment_method_type( $type ) {
63+
$this->set_prop( 'payment_method_type', $type );
64+
}
65+
66+
/**
67+
* Returns Stripe payment method type.
68+
*
69+
* @param string $context What the value is for. Valid values are view and edit.
70+
* @return string $payment_method_type
71+
*/
72+
public function get_payment_method_type( $context = 'view' ) {
73+
return $this->get_prop( 'payment_method_type', $context );
74+
}
75+
76+
/**
77+
* Returns the customer email.
78+
*
79+
* @param string $context What the value is for. Valid values are view and edit.
80+
*
81+
* @return string Customer email.
82+
*/
83+
public function get_email( $context = 'view' ) {
84+
return $this->get_prop( 'email', $context );
85+
}
86+
87+
/**
88+
* Set the customer email.
89+
*
90+
* @param string $email Customer email.
91+
*/
92+
public function set_email( $email ) {
93+
$this->set_prop( 'email', $email );
94+
}
95+
}

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function __construct() {
1717
parent::__construct();
1818
$this->stripe_id = self::STRIPE_ID;
1919
$this->title = __( 'Pay with Link', 'woocommerce-gateway-stripe' );
20-
$this->is_reusable = false;
20+
$this->is_reusable = true;
2121
$this->supported_currencies = [ 'USD' ];
2222
$this->label = __( 'Stripe Link', 'woocommerce-gateway-stripe' );
2323
$this->description = __(
@@ -44,4 +44,31 @@ public static function is_link_enabled() {
4444
true
4545
);
4646
}
47+
48+
/**
49+
* Returns string representing payment method type
50+
* to query to retrieve saved payment methods from Stripe.
51+
*/
52+
public function get_retrievable_type() {
53+
return $this->get_id();
54+
}
55+
56+
/**
57+
* Create new WC payment token and add to user.
58+
*
59+
* @param int $user_id WP_User ID
60+
* @param object $payment_method Stripe payment method object
61+
*
62+
* @return WC_Payment_Token_Link
63+
*/
64+
public function create_payment_token_for_user( $user_id, $payment_method ) {
65+
$token = new WC_Payment_Token_Link();
66+
$token->set_email( $payment_method->link->email );
67+
$token->set_gateway_id( WC_Stripe_UPE_Payment_Gateway::ID );
68+
$token->set_token( $payment_method->id );
69+
$token->set_payment_method_type( $this->get_id() );
70+
$token->set_user_id( $user_id );
71+
$token->save();
72+
return $token;
73+
}
4774
}

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,5 +130,6 @@ If you get stuck, you can ask for help in the Plugin Forum.
130130

131131
= 7.0.0 - 2022-xx-xx =
132132
* Add - Auto-complete first and last name on checkout form when using Link payment method.
133+
* Add - Allow subscription orders to be paid with Link payment method.
133134

134135
[See changelog for all versions](https://raw.githubusercontent.com/woocommerce/woocommerce-gateway-stripe/trunk/changelog.txt).

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

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ class WC_Stripe_UPE_Payment_Method_Test extends WP_UnitTestCase {
2626
],
2727
];
2828

29+
/**
30+
* Base template for Stripe link payment method.
31+
*/
32+
const MOCK_LINK_PAYMENT_METHOD_TEMPLATE = [
33+
'id' => 'pm_mock_payment_method_id',
34+
'type' => 'link',
35+
'link' => [
36+
'email' => '[email protected]',
37+
],
38+
];
39+
2940
/**
3041
* Base template for Stripe SEPA payment method.
3142
*/
@@ -387,8 +398,12 @@ public function test_payment_methods_are_reusable_if_cart_contains_subscription(
387398
$this->set_mock_payment_method_return_value( 'get_capabilities_response', self::MOCK_ACTIVE_CAPABILITIES_RESPONSE );
388399

389400
foreach ( $this->mock_payment_methods as $payment_method_id => $payment_method ) {
390-
$currency = 'link' === $payment_method_id ? 'USD' : 'EUR';
391-
$this->set_mock_payment_method_return_value( 'get_woocommerce_currency', $currency );
401+
$payment_method
402+
->expects( $this->any() )
403+
->method( 'get_woocommerce_currency' )
404+
->will(
405+
$this->returnValue( WC_Stripe_UPE_Payment_Method_Link::STRIPE_ID === $payment_method_id ? 'USD' : 'EUR' )
406+
);
392407

393408
if ( $payment_method->is_reusable() ) {
394409
$this->assertTrue( $payment_method->is_enabled_at_checkout() );
@@ -409,18 +424,27 @@ public function test_create_payment_token_for_user() {
409424
continue;
410425
}
411426

412-
if ( 'card' === $payment_method_id ) {
413-
$card_payment_method_mock = $this->array_to_object( self::MOCK_CARD_PAYMENT_METHOD_TEMPLATE );
414-
$token = $payment_method->create_payment_token_for_user( $user_id, $card_payment_method_mock );
415-
$this->assertTrue( 'WC_Payment_Token_CC' === get_class( $token ) );
416-
$this->assertEquals( $token->get_last4(), $card_payment_method_mock->card->last4 );
417-
$this->assertEquals( $token->get_token(), $card_payment_method_mock->id );
418-
} else {
419-
$sepa_payment_method_mock = $this->array_to_object( self::MOCK_SEPA_PAYMENT_METHOD_TEMPLATE );
420-
$token = $payment_method->create_payment_token_for_user( $user_id, $sepa_payment_method_mock );
421-
$this->assertTrue( 'WC_Payment_Token_SEPA' === get_class( $token ) );
422-
$this->assertEquals( $token->get_last4(), $sepa_payment_method_mock->sepa_debit->last4 );
423-
$this->assertEquals( $token->get_token(), $sepa_payment_method_mock->id );
427+
switch ( $payment_method_id ) {
428+
case WC_Stripe_UPE_Payment_Method_CC::STRIPE_ID:
429+
$card_payment_method_mock = $this->array_to_object( self::MOCK_CARD_PAYMENT_METHOD_TEMPLATE );
430+
$token = $payment_method->create_payment_token_for_user( $user_id, $card_payment_method_mock );
431+
$this->assertTrue( 'WC_Payment_Token_CC' === get_class( $token ) );
432+
$this->assertSame( $token->get_last4(), $card_payment_method_mock->card->last4 );
433+
$this->assertSame( $token->get_token(), $card_payment_method_mock->id );
434+
break;
435+
case WC_Stripe_UPE_Payment_Method_Link::STRIPE_ID:
436+
$link_payment_method_mock = $this->array_to_object( self::MOCK_LINK_PAYMENT_METHOD_TEMPLATE );
437+
$token = $payment_method->create_payment_token_for_user( $user_id, $link_payment_method_mock );
438+
$this->assertTrue( 'WC_Payment_Token_Link' === get_class( $token ) );
439+
$this->assertSame( $token->get_email(), $link_payment_method_mock->link->email );
440+
break;
441+
default:
442+
$sepa_payment_method_mock = $this->array_to_object( self::MOCK_SEPA_PAYMENT_METHOD_TEMPLATE );
443+
$token = $payment_method->create_payment_token_for_user( $user_id, $sepa_payment_method_mock );
444+
$this->assertTrue( 'WC_Payment_Token_SEPA' === get_class( $token ) );
445+
$this->assertSame( $token->get_last4(), $sepa_payment_method_mock->sepa_debit->last4 );
446+
$this->assertSame( $token->get_token(), $sepa_payment_method_mock->id );
447+
424448
}
425449
}
426450
}

woocommerce-gateway-stripe.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ public function init() {
167167
require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-webhook-state.php';
168168
require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-webhook-handler.php';
169169
require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-sepa-payment-token.php';
170+
require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-link-payment-token.php';
170171
require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-apple-pay-registration.php';
171172
require_once dirname( __FILE__ ) . '/includes/class-wc-gateway-stripe.php';
172173
require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php';

0 commit comments

Comments
 (0)