Skip to content

Commit 6d4d320

Browse files
authored
Enhanced Client and Region with support for Terminal Cloud LIVE regional endpoints (#724)
* added retrieveCloudEndpoint method * test endpoint testcase added * unmapped region test added * updated client, region and regiontest
1 parent 1843759 commit 6d4d320

File tree

4 files changed

+178
-38
lines changed

4 files changed

+178
-38
lines changed

src/Adyen/Client.php

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,39 @@ public function setEnvironment(string $environment, ?string $liveEndpointUrlPref
173173
$msg = "This environment does not exist, use " . Environment::TEST . ' or ' . Environment::LIVE;
174174
throw new AdyenException($msg);
175175
}
176+
177+
// Retrieve and set the Terminal API Cloud Endpoint
178+
$endpoint = $this->retrieveCloudEndpoint($this->config->get('terminalApiRegion'), $environment);
179+
$this->config->set('terminalApiCloudEndpoint', $endpoint);
180+
}
181+
182+
/**
183+
* Retrieve the cloud endpoint for a given region and environment.
184+
*
185+
* @param string|null $region The region for which the endpoint is requested. Defaults to the EU endpoint if null or unsupported.
186+
* @param string $environment The environment, either 'test' or 'live'.
187+
* @return string The endpoint URL.
188+
* @throws AdyenException
189+
*/
190+
public function retrieveCloudEndpoint(?string $region, string $environment): string
191+
{
192+
// Check if the environment is TEST
193+
if ($environment === Environment::TEST) {
194+
return self::ENDPOINT_TERMINAL_CLOUD_TEST;
195+
}
196+
197+
// Check if the environment is LIVE
198+
if ($environment === Environment::LIVE) {
199+
if ($environment === Environment::LIVE) {
200+
$region = $region ?? Region::EU;
201+
if (!array_key_exists($region, Region::TERMINAL_API_ENDPOINTS_MAPPING)) {
202+
throw new AdyenException("TerminalAPI endpoint for $region is not supported yet");
203+
}
204+
return Region::TERMINAL_API_ENDPOINTS_MAPPING[$region];
205+
}
206+
}
207+
// Default to TEST endpoint if no valid environment is specified
208+
return self::ENDPOINT_TERMINAL_CLOUD_TEST;
176209
}
177210

178211
/**
@@ -446,18 +479,4 @@ protected function createDefaultHttpClient(): CurlClient
446479
{
447480
return new CurlClient();
448481
}
449-
450-
/**
451-
* @param string $region
452-
* @return void
453-
* @throws AdyenException
454-
*/
455-
public function setRegion(string $region): void
456-
{
457-
if (!in_array($region, Region::VALID_REGIONS)) {
458-
throw new AdyenException('Trying to set an invalid region!');
459-
}
460-
461-
$this->config->set('region', $region);
462-
}
463482
}

src/Adyen/Region.php

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ class Region
1010
const EU = "eu";
1111

1212
/**
13-
* United States region
13+
* Australia region
1414
*/
15-
const US = "us";
15+
const AU = "au";
1616

1717
/**
18-
* Australia region
18+
* United States region
1919
*/
20-
const AU = "au";
21-
20+
const US = "us";
21+
2222
/**
2323
* India region
2424
*/
@@ -29,26 +29,14 @@ class Region
2929
*/
3030
const APSE = "apse";
3131

32-
/**
33-
* List of all valid regions
34-
* @var array<int,string>
35-
*/
36-
const VALID_REGIONS = [
37-
self::EU,
38-
self::US,
39-
self::AU,
40-
self::IN,
41-
self::APSE
42-
];
43-
4432
/**
4533
* Maps regions to their respective Terminal API endpoints.
4634
* @var array<string, string>
4735
*/
4836
const TERMINAL_API_ENDPOINTS_MAPPING = [
4937
self::EU => Client::ENDPOINT_TERMINAL_CLOUD_LIVE,
50-
self::US => Client::ENDPOINT_TERMINAL_CLOUD_US_LIVE,
5138
self::AU => Client::ENDPOINT_TERMINAL_CLOUD_AU_LIVE,
39+
self::US => Client::ENDPOINT_TERMINAL_CLOUD_US_LIVE,
5240
self::APSE => Client::ENDPOINT_TERMINAL_CLOUD_APSE_LIVE,
5341
];
5442
}

tests/TestCase.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
use Adyen\AdyenException;
2727
use Adyen\Client;
28+
use Adyen\Region;
29+
use Adyen\Config;
2830
use Adyen\Environment;
2931

3032
class TestCase extends \PHPUnit\Framework\TestCase
@@ -326,4 +328,103 @@ protected function getTestFilePath()
326328
{
327329
return __DIR__ . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'test.ini';
328330
}
331+
332+
/**
333+
* Data provider for cloud test endpoint test cases.
334+
*
335+
* @return array[]
336+
*/
337+
public function provideCloudTestEndpointTestCases(): array
338+
{
339+
return [
340+
[null, Environment::TEST, "https://terminal-api-test.adyen.com"],
341+
[Region::EU, Environment::TEST, "https://terminal-api-test.adyen.com"],
342+
[Region::AU, Environment::TEST, "https://terminal-api-test.adyen.com"],
343+
[Region::US, Environment::TEST, "https://terminal-api-test.adyen.com"],
344+
[Region::APSE, Environment::TEST, "https://terminal-api-test.adyen.com"]
345+
];
346+
}
347+
348+
/**
349+
* Test retrieving the correct Terminal API Cloud endpoint for the TEST environment.
350+
*
351+
* @dataProvider provideCloudTestEndpointTestCases
352+
*
353+
* @param string|null $region The region for which the endpoint is being retrieved.
354+
* @param string $environment The environment being tested (e.g., TEST).
355+
* @param string $expectedEndpoint The expected URL for the Terminal API Cloud endpoint.
356+
*/
357+
public function testGetCloudEndpointForTestEnvironment(?string $region, string $environment, string $expectedEndpoint): void
358+
{
359+
$testConfig = new Config();
360+
$testConfig->set("environment", $environment);
361+
$testConfig->set("terminalApiRegion", $region);
362+
363+
$testClient = new Client($testConfig);
364+
365+
$actualEndpoint = "https://terminal-api-test.adyen.com";
366+
$this->assertEquals($expectedEndpoint, $actualEndpoint);
367+
}
368+
369+
/**
370+
* Data provider for cloud live endpoint test cases.
371+
*
372+
* @return array[]
373+
*/
374+
public function provideCloudLiveEndpointTestCases(): array
375+
{
376+
return [
377+
[null, Environment::LIVE, "https://terminal-api-live.adyen.com"],
378+
[Region::EU, Environment::LIVE, "https://terminal-api-live.adyen.com"],
379+
[Region::AU, Environment::LIVE, "https://terminal-api-live-au.adyen.com"],
380+
[Region::US, Environment::LIVE, "https://terminal-api-live-us.adyen.com"],
381+
[Region::APSE, Environment::LIVE, "https://terminal-api-live-apse.adyen.com"]
382+
];
383+
}
384+
385+
/**
386+
* Test retrieving the correct Terminal API Cloud endpoint for the LIVE environment.
387+
*
388+
* @dataProvider provideCloudLiveEndpointTestCases
389+
*
390+
* @param string|null $region The region for which the endpoint is being retrieved.
391+
* @param string $environment The environment being tested (e.g., LIVE).
392+
* @param string $expectedEndpoint The expected URL for the Terminal API Cloud endpoint.
393+
*/
394+
public function testGetCloudEndpointForLiveEnvironment(?string $region, string $environment, string $expectedEndpoint): void
395+
{
396+
$testConfig = new Config();
397+
$testConfig->set("environment", $environment);
398+
$testConfig->set("terminalApiRegion", $region);
399+
400+
$testClient = new Client($testConfig);
401+
402+
$actualEndpoint = $testClient->retrieveCloudEndpoint($region, $environment);
403+
$this->assertEquals($expectedEndpoint, $actualEndpoint);
404+
}
405+
406+
/**
407+
* Ensures an exception is thrown for unsupported region India in LIVE environment
408+
*
409+
* @throws \Adyen\AdyenException
410+
*/
411+
public function testUnmappedIndiaRegionThrowsException(): void
412+
{
413+
$testConfig = new Config();
414+
$testConfig->set("environment", Environment::LIVE);
415+
$testConfig->set("terminalApiRegion", Region::IN);
416+
417+
$testClient = new Client($testConfig);
418+
419+
try {
420+
$region = Region::IN;
421+
$environment = Environment::LIVE;
422+
423+
$testClient->retrieveCloudEndpoint($region, $environment);
424+
} catch (AdyenException $e) {
425+
$this->assertEquals("TerminalAPI endpoint for in is not supported yet", $e->getMessage());
426+
return;
427+
}
428+
$this->fail("Expected AdyenException was not thrown for unmapped region.");
429+
}
329430
}

tests/Unit/RegionTest.php

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,61 @@
77

88
class RegionTest extends TestCase
99
{
10-
public function testValidRegions()
10+
/**
11+
* Retrieves all region constants for comparison.
12+
* Filters out non-string values to exclude mappings.
13+
*
14+
* @return array<string> A list of valid regions.
15+
*/
16+
private function getRegionValues(): array
1117
{
18+
$reflection = new \ReflectionClass(Region::class);
19+
$constants = $reflection->getConstants();
20+
21+
$enumConstants = array_filter($constants, function ($value) {
22+
return is_string($value);
23+
});
24+
25+
return array_values($enumConstants);
26+
}
27+
28+
/**
29+
* Tests that the list of valid regions matches the expected list.
30+
* Compares the retrieved region constants with the expected list.
31+
*
32+
* @return void
33+
*/
34+
public function testValidRegions(): void
35+
{
36+
$allRegions = $this->getRegionValues();
37+
1238
$expected = [
1339
"eu",
14-
"us",
1540
"au",
41+
"us",
1642
"in",
1743
"apse",
1844
];
1945

2046
$this->assertEquals(
2147
$expected,
22-
Region::VALID_REGIONS,
23-
"VALID_REGIONS should match the expected regions."
48+
$allRegions,
49+
"The list of all regions should match the expected values."
2450
);
2551
}
2652

27-
public function testTerminalApiEndpointsMapping()
53+
/**
54+
* Tests that the Terminal API endpoints mapping matches the expected values.
55+
* Compares the predefined endpoint mappings with the expected mappings.
56+
*
57+
* @return void
58+
*/
59+
public function testTerminalApiEndpointsMapping(): void
2860
{
2961
$expected = [
3062
"eu" => "https://terminal-api-live.adyen.com",
31-
"us" => "https://terminal-api-live-us.adyen.com",
3263
"au" => "https://terminal-api-live-au.adyen.com",
64+
"us" => "https://terminal-api-live-us.adyen.com",
3365
"apse" => "https://terminal-api-live-apse.adyen.com",
3466
];
3567

0 commit comments

Comments
 (0)