diff --git a/src/Adyen/Client.php b/src/Adyen/Client.php index e81aef79a..b5e80dae2 100644 --- a/src/Adyen/Client.php +++ b/src/Adyen/Client.php @@ -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 { @@ -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; diff --git a/src/Adyen/Service/PosPayment.php b/src/Adyen/Service/PosPayment.php index dfd8dd8b8..251d8c7ab 100644 --- a/src/Adyen/Service/PosPayment.php +++ b/src/Adyen/Service/PosPayment.php @@ -2,6 +2,7 @@ namespace Adyen\Service; +use Adyen\AdyenException; use Adyen\Client; use Adyen\Environment; use Adyen\Region; @@ -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] diff --git a/tests/Unit/RegionTest.php b/tests/Unit/RegionTest.php index 4393ec3ad..b8eb5c651 100644 --- a/tests/Unit/RegionTest.php +++ b/tests/Unit/RegionTest.php @@ -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; @@ -17,7 +21,7 @@ private function getRegionValues(): array { $reflection = new \ReflectionClass(Region::class); $constants = $reflection->getConstants(); - + $enumConstants = array_filter($constants, function ($value) { return is_string($value); }); @@ -71,4 +75,35 @@ public function testTerminalApiEndpointsMapping(): void "TERMINAL_API_ENDPOINTS_MAPPING should match the expected mappings." ); } + + /** + * @throws AdyenException + */ + public function testSetsUnitedStatesTerminalCloudEndpoint(): void + { + $client = new Client(); + $client->setRegion(Region::US); + $client->setEnvironment(Environment::LIVE); + + new PosPayment($client); + + $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); + } }