diff --git a/Adyen.IntegrationTest/Adyen.IntegrationTest.csproj b/Adyen.IntegrationTest/Adyen.IntegrationTest.csproj index ebc0d85b..ee14425b 100644 --- a/Adyen.IntegrationTest/Adyen.IntegrationTest.csproj +++ b/Adyen.IntegrationTest/Adyen.IntegrationTest.csproj @@ -1,7 +1,7 @@ - net6.0 + net8.0 false diff --git a/Adyen.Test/Adyen.Test.csproj b/Adyen.Test/Adyen.Test.csproj index 5a97f17b..590bc092 100644 --- a/Adyen.Test/Adyen.Test.csproj +++ b/Adyen.Test/Adyen.Test.csproj @@ -2,7 +2,7 @@ true - net6.0;netstandard2.0 + net8.0;netstandard2.0 false diff --git a/Adyen.Test/ClientTest.cs b/Adyen.Test/ClientTest.cs index e74781ee..4c332e4c 100644 --- a/Adyen.Test/ClientTest.cs +++ b/Adyen.Test/ClientTest.cs @@ -48,6 +48,71 @@ public void TestLogLine() Assert.AreEqual("testMessage", logLine); } + [DataTestMethod] + // Valid Live Region Test Cases + [DataRow(Model.Environment.Live, Region.EU, "https://terminal-api-live.adyen.com", true)] + [DataRow(Model.Environment.Live, Region.AU, "https://terminal-api-live-au.adyen.com", true)] + [DataRow(Model.Environment.Live, Region.US, "https://terminal-api-live-us.adyen.com", true)] + [DataRow(Model.Environment.Live, Region.APSE, "https://terminal-api-live-apse.adyen.com", true)] + [DataRow(Model.Environment.Live, null, "https://terminal-api-live.adyen.com", true)] // Default to EU + + public void TestGetCloudApiEndpoint(Model.Environment environment, Region? region, string expectedEndpoint, bool shouldSucceed) + { + var config = new Config + { + Environment = environment, + TerminalApiRegion = region + }; + var client = new Client(config); + + if (shouldSucceed) + { + var actualEndpoint = client.GetCloudApiEndpoint(); + + Assert.AreEqual(expectedEndpoint, actualEndpoint); + } + else + { + Assert.ThrowsException(() => client.GetCloudApiEndpoint()); + } + } + + [DataTestMethod] + [DataRow(Model.Environment.Test, Region.EU, "https://terminal-api-test.adyen.com")] + [DataRow(Model.Environment.Test, Region.AU, "https://terminal-api-test.adyen.com")] + [DataRow(Model.Environment.Test, Region.US, "https://terminal-api-test.adyen.com")] + [DataRow(Model.Environment.Test, Region.APSE, "https://terminal-api-test.adyen.com")] + [DataRow(Model.Environment.Test, null, "https://terminal-api-test.adyen.com")] // Defaults to Test endpoint + public void TestTestCloudApiEndpoints(Model.Environment environment, Region? region, string expectedEndpoint) + { + var config = new Config + { + Environment = environment, + TerminalApiRegion = region + }; + var client = new Client(config); + + var actualEndpoint = client.GetCloudApiEndpoint(); + + Assert.AreEqual(expectedEndpoint, actualEndpoint); + } + + [TestMethod] + public void TestUnsupportedRegionDefaultsToEU() + { + var config = new Config + { + Environment = Model.Environment.Live, + TerminalApiRegion = Region.IN + }; + var client = new Client(config); + + var actualEndpoint = client.GetCloudApiEndpoint(); + + Assert.AreEqual("https://terminal-api-live.adyen.com", actualEndpoint, + "Unsupported regions should default to the EU endpoint."); + } + [TestMethod] public void TestSetTerminalApiRegion() { diff --git a/Adyen/Adyen.csproj b/Adyen/Adyen.csproj index 5bd778d1..31d05796 100644 --- a/Adyen/Adyen.csproj +++ b/Adyen/Adyen.csproj @@ -1,7 +1,7 @@  - net6.0;netstandard2.0 + net8.0;netstandard2.0 false Adyen diff --git a/Adyen/Client.cs b/Adyen/Client.cs index 69591489..90079b72 100644 --- a/Adyen/Client.cs +++ b/Adyen/Client.cs @@ -71,33 +71,69 @@ public void SetEnvironment(Environment environment, string liveEndpointUrlPrefix { Config.Environment = environment; Config.LiveEndpointUrlPrefix = liveEndpointUrlPrefix; + + // Always use GetCloudApiEndpoint to determine the correct endpoint + Config.CloudApiEndPoint = GetCloudApiEndpoint(); } - public string GetCloudApiEndpoint() { - // Check if the cloud api endpoint has not already been set + // public string GetCloudApiEndpoint() { + // // Check if the cloud api endpoint has not already been set + // if (Config.CloudApiEndPoint != null) + // { + // return Config.CloudApiEndPoint; + // } + // // If not switch through environment and return default EU + // if (Config.Environment == Environment.Live) + // { + // switch (Config.TerminalApiRegion) + // { + // case Region.AU: + // return ClientConfig.CloudApiEndPointAULive; + // case Region.US: + // return ClientConfig.CloudApiEndPointUSLive; + // case Region.APSE: + // return ClientConfig.CloudApiEndPointAPSELive; + // case Region.EU: + // default: + // return ClientConfig.CloudApiEndPointEULive; + // } + // } + // return ClientConfig.CloudApiEndPointTest; + // } + + public string GetCloudApiEndpoint() + { + // Check if the cloud API endpoint has already been set if (Config.CloudApiEndPoint != null) { return Config.CloudApiEndPoint; } - // If not switch through environment and return default EU + + // If environment is TEST, return the test endpoint + if (Config.Environment == Environment.Test) + { + return ClientConfig.CloudApiEndPointTest; + } + + // For LIVE environment, handle region mapping if (Config.Environment == Environment.Live) { - switch (Config.TerminalApiRegion) + var region = Config.TerminalApiRegion ?? Region.EU; // Default to EU if region is null + + if (!RegionMapping.TERMINAL_API_ENDPOINTS_MAPPING.ContainsKey(region)) { - case Region.AU: - return ClientConfig.CloudApiEndPointAULive; - case Region.US: - return ClientConfig.CloudApiEndPointUSLive; - case Region.APSE: - return ClientConfig.CloudApiEndPointAPSELive; - case Region.EU: - default: - return ClientConfig.CloudApiEndPointEULive; + // Log a warning and default to EU endpoint for unsupported regions + Console.WriteLine($"Warning: Region '{region}' is not supported. Defaulting to EU endpoint."); + return ClientConfig.CloudApiEndPointEULive; } + + return RegionMapping.TERMINAL_API_ENDPOINTS_MAPPING[region]; } + + // Default to test endpoint if the environment is not set return ClientConfig.CloudApiEndPointTest; } - + // Get a new HttpClient and set a timeout private System.Net.Http.HttpClient GetHttpClient() { diff --git a/Adyen/Config.cs b/Adyen/Config.cs index 23106520..de5e67d5 100644 --- a/Adyen/Config.cs +++ b/Adyen/Config.cs @@ -24,6 +24,7 @@ public class Config public bool HasApiKey => !string.IsNullOrEmpty(XApiKey); public BaseUrlConfig BaseUrlConfig { get; set; } - public Region TerminalApiRegion { get; set; } + // public Region TerminalApiRegion { get; set; } + public Region? TerminalApiRegion { get; set; } } } \ No newline at end of file