Skip to content

Commit b36c453

Browse files
authored
Restrict Link payment method to US merchants (#2452)
Restrict Link payment method to US merchants
1 parent 13f80da commit b36c453

8 files changed

+141
-3
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Add - Allow subscription orders to be paid with Link payment method.
77
* Add - Add inbox notification for Link payment method.
88
* Tweak - Adjust texts and links in WC admin advanced settings.
9+
* Add - Restrict the Link payment method only for US merchants.
910

1011
= 6.9.0 - 2022-10-19 =
1112
* Tweak - Remove remaining traces of old Stripe settings.

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ class WC_Stripe_UPE_Payment_Gateway extends WC_Gateway_Stripe {
1515

1616
const ID = 'stripe';
1717

18+
/**
19+
* Upe Available Methods
20+
*
21+
* @type WC_Stripe_UPE_Payment_Method[]
22+
*/
1823
const UPE_AVAILABLE_METHODS = [
1924
WC_Stripe_UPE_Payment_Method_CC::class,
2025
WC_Stripe_UPE_Payment_Method_Giropay::class,
@@ -408,8 +413,11 @@ public function get_upe_enabled_at_checkout_payment_method_ids( $order_id = null
408413
public function get_upe_available_payment_methods() {
409414
$available_payment_methods = [];
410415

411-
foreach ( self::UPE_AVAILABLE_METHODS as $payment_method_class ) {
412-
$available_payment_methods[] = $payment_method_class::STRIPE_ID;
416+
foreach ( $this->payment_methods as $payment_method ) {
417+
if ( ! $payment_method->is_available() ) {
418+
continue;
419+
}
420+
$available_payment_methods[] = $payment_method->get_id();
413421
}
414422
return $available_payment_methods;
415423
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function __construct() {
3333
* @return bool
3434
*/
3535
public static function is_link_enabled() {
36+
3637
// Assume Link is disabled if UPE is disabled.
3738
if ( ! WC_Stripe_Feature_Flags::is_upe_checkout_enabled() ) {
3839
return false;
@@ -71,4 +72,17 @@ public function create_payment_token_for_user( $user_id, $payment_method ) {
7172
$token->save();
7273
return $token;
7374
}
75+
76+
/**
77+
* Returns true if the UPE method is available.
78+
*
79+
* @return bool
80+
*/
81+
public function is_available() {
82+
//if merchant is outside US, Link payment method should not be available
83+
$cached_account_data = WC_Stripe::get_instance()->account->get_cached_account_data();
84+
$account_country = $cached_account_data['country'] ?? null;
85+
86+
return 'US' === $account_country && parent::is_available();
87+
}
7488
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,15 @@ public function is_enabled() {
114114
return $this->enabled;
115115
}
116116

117+
/**
118+
* Returns true if the UPE method is available.
119+
*
120+
* @return bool
121+
*/
122+
public function is_available() {
123+
return true;
124+
}
125+
117126
/**
118127
* Returns payment method title
119128
*

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,5 +134,6 @@ If you get stuck, you can ask for help in the Plugin Forum.
134134
* Add - Allow subscription orders to be paid with Link payment method.
135135
* Add - Add inbox notification for Link payment method.
136136
* Tweak - Adjust texts and links in WC admin advanced settings.
137+
* Add - Restrict the Link payment method only for US merchants.
137138

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

tests/phpunit/admin/test-class-wc-rest-stripe-settings-controller.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,21 @@ public function test_short_statement_descriptor_is_not_updated() {
228228
}
229229

230230
public function test_get_settings_returns_available_payment_method_ids() {
231+
//link is available only in US
232+
WC_Stripe::get_instance()->account = $this->getMockBuilder( 'WC_Stripe_Account' )
233+
->disableOriginalConstructor()
234+
->setMethods(
235+
[
236+
'get_cached_account_data',
237+
]
238+
)
239+
->getMock();
240+
241+
WC_Stripe::get_instance()->account->method( 'get_cached_account_data' )->willReturn(
242+
[
243+
'country' => 'US',
244+
]
245+
);
231246
$response = $this->rest_get_settings();
232247

233248
$expected_method_ids = WC_Stripe_UPE_Payment_Gateway::UPE_AVAILABLE_METHODS;

tests/phpunit/test-class-wc-stripe-notes.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ public function test_create_upe_stripelink_note() {
6262
'upe_checkout_experience_enabled' => 'yes',
6363
]
6464
);
65+
WC_Stripe::get_instance()->account = $this->getMockBuilder( 'WC_Stripe_Account' )
66+
->disableOriginalConstructor()
67+
->setMethods(
68+
[
69+
'get_cached_account_data',
70+
]
71+
)
72+
->getMock();
73+
WC_Stripe::get_instance()->account->method( 'get_cached_account_data' )->willReturn( [ 'country' => 'US' ] );
6574
WC_Stripe_Inbox_Notes::create_upe_notes();
6675
$admin_note_store = WC_Data_Store::load( 'admin-note' );
6776
$this->assertSame( 1, count( $admin_note_store->get_notes_with_name( WC_Stripe_UPE_StripeLink_Note::NOTE_NAME ) ) );
@@ -84,7 +93,15 @@ public function test_create_upe_notes_does_not_create_availability_note_when_upe
8493
'upe_checkout_experience_enabled' => 'yes',
8594
]
8695
);
87-
96+
WC_Stripe::get_instance()->account = $this->getMockBuilder( 'WC_Stripe_Account' )
97+
->disableOriginalConstructor()
98+
->setMethods(
99+
[
100+
'get_cached_account_data',
101+
]
102+
)
103+
->getMock();
104+
WC_Stripe::get_instance()->account->method( 'get_cached_account_data' )->willReturn( [ 'country' => 'US' ] );
88105
WC_Stripe_Inbox_Notes::create_upe_notes();
89106

90107
$admin_note_store = WC_Data_Store::load( 'admin-note' );

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

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,65 @@ private function get_order_details( $order ) {
156156
return [ $amount, $description, $metadata ];
157157
}
158158

159+
/**
160+
* @dataProvider get_upe_available_payment_methods_provider
161+
*/
162+
public function test_get_upe_available_payment_methods( $country, $available_payment_methods ) {
163+
$this->set_stripe_account_data( [ 'country' => $country ] );
164+
$this->assertSame( $available_payment_methods, $this->mock_gateway->get_upe_available_payment_methods() );
165+
}
166+
167+
public function test_get_upe_enabled_at_checkout_payment_method_ids() {
168+
$available_payment_methods = [
169+
WC_Stripe_UPE_Payment_Method_CC::STRIPE_ID,
170+
WC_Stripe_UPE_Payment_Method_Link::STRIPE_ID,
171+
];
172+
$this->mock_gateway->update_option(
173+
'upe_checkout_experience_accepted_payments',
174+
[
175+
'card',
176+
'link',
177+
]
178+
);
179+
$this->assertSame( $available_payment_methods, $this->mock_gateway->get_upe_enabled_at_checkout_payment_method_ids() );
180+
}
181+
182+
public function get_upe_available_payment_methods_provider() {
183+
return [
184+
[
185+
'US',
186+
[
187+
WC_Stripe_UPE_Payment_Method_CC::STRIPE_ID,
188+
WC_Stripe_UPE_Payment_Method_Giropay::STRIPE_ID,
189+
WC_Stripe_UPE_Payment_Method_Eps::STRIPE_ID,
190+
WC_Stripe_UPE_Payment_Method_Bancontact::STRIPE_ID,
191+
WC_Stripe_UPE_Payment_Method_Boleto::STRIPE_ID,
192+
WC_Stripe_UPE_Payment_Method_Ideal::STRIPE_ID,
193+
WC_Stripe_UPE_Payment_Method_Oxxo::STRIPE_ID,
194+
WC_Stripe_UPE_Payment_Method_Sepa::STRIPE_ID,
195+
WC_Stripe_UPE_Payment_Method_P24::STRIPE_ID,
196+
WC_Stripe_UPE_Payment_Method_Sofort::STRIPE_ID,
197+
WC_Stripe_UPE_Payment_Method_Link::STRIPE_ID,
198+
],
199+
],
200+
[
201+
'NON_US',
202+
[
203+
WC_Stripe_UPE_Payment_Method_CC::STRIPE_ID,
204+
WC_Stripe_UPE_Payment_Method_Giropay::STRIPE_ID,
205+
WC_Stripe_UPE_Payment_Method_Eps::STRIPE_ID,
206+
WC_Stripe_UPE_Payment_Method_Bancontact::STRIPE_ID,
207+
WC_Stripe_UPE_Payment_Method_Boleto::STRIPE_ID,
208+
WC_Stripe_UPE_Payment_Method_Ideal::STRIPE_ID,
209+
WC_Stripe_UPE_Payment_Method_Oxxo::STRIPE_ID,
210+
WC_Stripe_UPE_Payment_Method_Sepa::STRIPE_ID,
211+
WC_Stripe_UPE_Payment_Method_P24::STRIPE_ID,
212+
WC_Stripe_UPE_Payment_Method_Sofort::STRIPE_ID,
213+
],
214+
],
215+
];
216+
}
217+
159218
/**
160219
* CLASSIC CHECKOUT TESTS.
161220
*/
@@ -1343,4 +1402,18 @@ public function test_pre_order_without_payment_uses_setup_intents() {
13431402
$this->assertEquals( $customer_id, $final_order->get_meta( '_stripe_customer_id', true ) );
13441403
$this->assertTrue( (bool) $final_order->get_meta( '_stripe_upe_redirect_processed', true ) );
13451404
}
1405+
1406+
1407+
/**
1408+
* @param array $account_data
1409+
*
1410+
* @return void
1411+
*/
1412+
private function set_stripe_account_data( $account_data ) {
1413+
WC_Stripe::get_instance()->account = $this->getMockBuilder( 'WC_Stripe_Account' )
1414+
->disableOriginalConstructor()
1415+
->setMethods( [ 'get_cached_account_data' ] )
1416+
->getMock();
1417+
WC_Stripe::get_instance()->account->method( 'get_cached_account_data' )->willReturn( $account_data );
1418+
}
13461419
}

0 commit comments

Comments
 (0)