-
Notifications
You must be signed in to change notification settings - Fork 69
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
Fix: Hide payment methods with domestic transactions restrictions (Klarna, Affirm, Afterpay) when conditions are not met #8980
Merged
danielmx-dev
merged 7 commits into
develop
from
fix/8718-restrict-klarna-non-domestic-transactions
Jun 20, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
085a0eb
Restrict Klarna visibility in checkout based on the account country
danielmx-dev 93ea2d9
Include additional check to validate the store currency and the avail…
danielmx-dev f53cc30
Add new UPE_Payment_Method_Test file to test get_countries
danielmx-dev 0d489ee
Add new Klarna_Payment_Method_Test file to test get_countries
danielmx-dev a156cc2
Add changelog and update some of the comments
danielmx-dev f54e9ef
Merge branch 'develop' into fix/8718-restrict-klarna-non-domestic-tra…
danielmx-dev 9343e12
Merge branch 'develop' into fix/8718-restrict-klarna-non-domestic-tra…
brettshumaker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: minor | ||
Type: fix | ||
|
||
Hide payment methods with domestic transactions restrictions (Klarna, Affirm, Afterpay) when conditions are not met. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
tests/unit/payment-methods/test-class-klarna-payment-method.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
<?php | ||
/** | ||
* Class Klarna_Payment_Method_Test | ||
* | ||
* @package WooCommerce\Payments\Tests | ||
*/ | ||
|
||
namespace WCPay\Payment_Methods; | ||
|
||
use PHPUnit\Framework\MockObject\MockObject; | ||
use WCPay\Constants\Country_Code; | ||
use WCPay\Constants\Currency_Code; | ||
use WCPAY_UnitTestCase; | ||
use WC_Payments_Account; | ||
use WC_Payments_Token_Service; | ||
use WC_Payments; | ||
|
||
/** | ||
* Klarna_Payment_Method unit tests | ||
*/ | ||
class Klarna_Payment_Method_Test extends WCPAY_UnitTestCase { | ||
|
||
/** | ||
* Mock site currency string | ||
* | ||
* @var string | ||
*/ | ||
public static $mock_site_currency = ''; | ||
|
||
/** | ||
* Mock WC_Payments_Token_Service. | ||
* | ||
* @var WC_Payments_Token_Service|MockObject | ||
*/ | ||
private $mock_token_service; | ||
|
||
/** | ||
* Klarna Payment Method Mock | ||
* | ||
* @var UPE_Payment_Method|MockObject | ||
*/ | ||
private $mock_payment_method; | ||
|
||
/** | ||
* WC_Payments_Account mocked instance. | ||
* | ||
* @var WC_Payments_Account|MockObject | ||
*/ | ||
private $mock_wcpay_account; | ||
|
||
/** | ||
* WC_Payments_Account original instance. | ||
* | ||
* @var WC_Payments_Account | ||
*/ | ||
private $original_account_service; | ||
|
||
/** | ||
* Pre-test setup | ||
*/ | ||
public function set_up() { | ||
parent::set_up(); | ||
|
||
$this->mock_wcpay_account = $this->createMock( WC_Payments_Account::class ); | ||
$this->original_account_service = WC_Payments::get_account_service(); | ||
WC_Payments::set_account_service( $this->mock_wcpay_account ); | ||
|
||
// Arrange: Mock WC_Payments_Token_Service so its methods aren't called directly. | ||
$this->mock_token_service = $this->getMockBuilder( 'WC_Payments_Token_Service' ) | ||
->disableOriginalConstructor() | ||
->onlyMethods( [ 'add_payment_method_to_user' ] ) | ||
->getMock(); | ||
|
||
$this->mock_payment_method = $this->getMockBuilder( Klarna_Payment_Method::class ) | ||
->setConstructorArgs( [ $this->mock_token_service ] ) | ||
->onlyMethods( [] ) | ||
->getMock(); | ||
} | ||
|
||
/** | ||
* Cleanup after tests. | ||
* | ||
* @return void | ||
*/ | ||
public function tear_down() { | ||
parent::tear_down(); | ||
wcpay_get_test_container()->reset_all_replacements(); | ||
WC_Payments::set_account_service( $this->original_account_service ); | ||
WC_Helper_Site_Currency::$mock_site_currency = ''; | ||
} | ||
|
||
/** | ||
* @dataProvider provider_test_get_countries | ||
*/ | ||
public function test_get_countries( | ||
string $account_country, | ||
?string $site_currency, | ||
array $expected_result | ||
) { | ||
$this->mock_wcpay_account->method( 'get_cached_account_data' )->willReturn( | ||
[ | ||
'country' => $account_country, | ||
] | ||
); | ||
|
||
if ( $site_currency ) { | ||
WC_Helper_Site_Currency::$mock_site_currency = $site_currency; | ||
} | ||
|
||
$this->assertEqualsCanonicalizing( $expected_result, $this->mock_payment_method->get_countries() ); | ||
} | ||
|
||
public function provider_test_get_countries() { | ||
return [ | ||
'US account' => [ | ||
'account_country' => Country_Code::UNITED_STATES, | ||
'site_currency' => null, | ||
'expected_result' => [ Country_Code::UNITED_STATES ], | ||
], | ||
'UK account with GBP store currency' => [ | ||
'account_country' => Country_Code::UNITED_KINGDOM, | ||
'site_currency' => Currency_Code::POUND_STERLING, | ||
'expected_result' => [ Country_Code::UNITED_KINGDOM ], | ||
], | ||
'UK account with EUR store currency' => [ | ||
'account_country' => Country_Code::UNITED_KINGDOM, | ||
'site_currency' => Currency_Code::EURO, | ||
'expected_result' => [ | ||
Country_Code::AUSTRIA, | ||
Country_Code::BELGIUM, | ||
Country_Code::FINLAND, | ||
Country_Code::GERMANY, | ||
Country_Code::IRELAND, | ||
Country_Code::ITALY, | ||
Country_Code::NETHERLANDS, | ||
Country_Code::SPAIN, | ||
], | ||
], | ||
'BE account with EUR store currency' => [ | ||
'account_country' => Country_Code::BELGIUM, | ||
'site_currency' => Currency_Code::EURO, | ||
'expected_result' => [ | ||
Country_Code::AUSTRIA, | ||
Country_Code::BELGIUM, | ||
Country_Code::FINLAND, | ||
Country_Code::GERMANY, | ||
Country_Code::IRELAND, | ||
Country_Code::ITALY, | ||
Country_Code::NETHERLANDS, | ||
Country_Code::SPAIN, | ||
], | ||
], | ||
]; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although this is a valid scenario, currently there is not way to manually test it due to the limitations described here #8718 (comment) Which should be addressed as part of a new issue.