Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/Adyen/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,20 @@ public function setEnvironment(string $environment, ?string $liveEndpointUrlPref
$this->config->set('terminalApiCloudEndpoint', $endpoint);
}

public function setRegion(string $region): void
{
$this->config->set('region', $region);
}

/**
* Retrieve the cloud endpoint for a given region and environment.
*
* @param string|null $region The region for which the endpoint is requested. Defaults to the EU endpoint if null or unsupported.
* @param string|null $region The region for which the endpoint is requested.
* Defaults to the EU endpoint if null or unsupported.
* @param string $environment The environment, either 'test' or 'live'.
* @return string The endpoint URL.
* @throws AdyenException
* @deprecated
*/
public function retrieveCloudEndpoint(?string $region, string $environment): string
{
Expand All @@ -196,13 +203,11 @@ public function retrieveCloudEndpoint(?string $region, string $environment): str

// Check if the environment is LIVE
if ($environment === Environment::LIVE) {
if ($environment === Environment::LIVE) {
$region = $region ?? Region::EU;
if (!array_key_exists($region, Region::TERMINAL_API_ENDPOINTS_MAPPING)) {
throw new AdyenException("TerminalAPI endpoint for $region is not supported yet");
}
return Region::TERMINAL_API_ENDPOINTS_MAPPING[$region];
if (!array_key_exists($region, Region::TERMINAL_API_ENDPOINTS_MAPPING)) {
throw new AdyenException("TerminalAPI endpoint for $region is not supported yet");
}
return Region::TERMINAL_API_ENDPOINTS_MAPPING[$region];
}
// Default to TEST endpoint if no valid environment is specified
return self::ENDPOINT_TERMINAL_CLOUD_TEST;
Expand Down
4 changes: 4 additions & 0 deletions src/Adyen/Service/PosPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Adyen\Service;

use Adyen\AdyenException;
use Adyen\Client;
use Adyen\Environment;
use Adyen\Region;
Expand Down Expand Up @@ -37,6 +38,9 @@ public function __construct(Client $client)
$environment = $this->getClient()->getConfig()->get('environment');

if (isset($region) && $environment == Environment::LIVE) {
if (!array_key_exists($region, Region::TERMINAL_API_ENDPOINTS_MAPPING)) {
throw new AdyenException("TerminalAPI endpoint for $region is not supported yet");
}
$this->getClient()->getConfig()->set(
'endpointTerminalCloud',
Region::TERMINAL_API_ENDPOINTS_MAPPING[$region]
Expand Down
37 changes: 36 additions & 1 deletion tests/Unit/RegionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace Adyen\Tests\Unit;

use Adyen\AdyenException;
use Adyen\Client;
use Adyen\Environment;
use Adyen\Service\PosPayment;
use PHPUnit\Framework\TestCase;
use Adyen\Region;

Expand All @@ -17,7 +21,7 @@
{
$reflection = new \ReflectionClass(Region::class);
$constants = $reflection->getConstants();

$enumConstants = array_filter($constants, function ($value) {
return is_string($value);
});
Expand Down Expand Up @@ -71,4 +75,35 @@
"TERMINAL_API_ENDPOINTS_MAPPING should match the expected mappings."
);
}

Comment thread
ashwaniarya-adyen marked this conversation as resolved.
/**
* @throws AdyenException
*/
public function testSetsUnitedStatesTerminalCloudEndpoint(): void
{
$client = new Client();
$client->setRegion(Region::US);
$client->setEnvironment(Environment::LIVE);

new PosPayment($client);

Check warning on line 88 in tests/Unit/RegionTest.php

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Either remove this useless object instantiation of class "PosPayment" or use it

See more on https://sonarcloud.io/project/issues?id=Adyen_adyen-php-api-library&issues=AaBnBE0YZ1yRfay0X8eG&open=AaBnBE0YZ1yRfay0X8eG&pullRequest=936
Comment thread
ashwaniarya-adyen marked this conversation as resolved.

$this->assertSame(
Client::ENDPOINT_TERMINAL_CLOUD_US_LIVE,
$client->getConfig()->get('endpointTerminalCloud')
);
}

public function testRejectsUnsupportedRegion(): void
{
$client = new Client();

$this->expectException(AdyenException::class);
$this->expectExceptionMessage(
'TerminalAPI endpoint for in is not supported yet'
);

$client->setRegion(Region::IN);
$client->setEnvironment(Environment::LIVE);
new PosPayment($client);

Check warning on line 107 in tests/Unit/RegionTest.php

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Either remove this useless object instantiation of class "PosPayment" or use it

See more on https://sonarcloud.io/project/issues?id=Adyen_adyen-php-api-library&issues=AaBry9LDFQatcxLG2qtZ&open=AaBry9LDFQatcxLG2qtZ&pullRequest=936
}
}
Loading