diff --git a/Adyen.EcommLibrary/HttpClient/HttpClientException.cs b/Adyen.EcommLibrary/HttpClient/HttpClientException.cs index a0a94490b..8a820db00 100644 --- a/Adyen.EcommLibrary/HttpClient/HttpClientException.cs +++ b/Adyen.EcommLibrary/HttpClient/HttpClientException.cs @@ -10,20 +10,6 @@ public class HttpClientException : Exception public WebHeaderCollection WebHeaderCollection { get; private set; } public string ResponseBody { get; private set; } - public HttpClientException() - { - } - - public HttpClientException(string message) : base(message) - { - } - - public HttpClientException(int code, string message) : base(message) - { - this.Code = code; - } - - public HttpClientException( int code, string message, WebHeaderCollection webHeaderCollection, string responseBody) : base(message) { this.Code = code; diff --git a/Adyen.EcommLibrary/HttpClient/HttpURLConnectionClient.cs b/Adyen.EcommLibrary/HttpClient/HttpURLConnectionClient.cs index 93b790131..24a1687f0 100644 --- a/Adyen.EcommLibrary/HttpClient/HttpURLConnectionClient.cs +++ b/Adyen.EcommLibrary/HttpClient/HttpURLConnectionClient.cs @@ -17,7 +17,7 @@ public class HttpUrlConnectionClient : IClient public string Request(string endpoint, string json, Config config, bool isApiKeyRequired) { - string responseText; + string responseText = null; //Set security protocol. Only TLS1.2 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; @@ -29,15 +29,22 @@ public string Request(string endpoint, string json, Config config, bool isApiKey streamWriter.Flush(); streamWriter.Close(); } - - using (var response = (HttpWebResponse)httpWebRequest.GetResponse()) + try { - using (var reader = new StreamReader(response.GetResponseStream(), _encoding)) + using (var response = (HttpWebResponse)httpWebRequest.GetResponse()) { - responseText = reader.ReadToEnd(); + using (var reader = new StreamReader(response.GetResponseStream(), _encoding)) + { + responseText = reader.ReadToEnd(); + } + } } - + catch (WebException e) + { + var response = (HttpWebResponse)e.Response; + throw new HttpClientException((int)response.StatusCode, "HTTP Exception", response.Headers, response.StatusDescription); + } return responseText; } diff --git a/Adyen.EcommLibrary/Service/Payment.cs b/Adyen.EcommLibrary/Service/Payment.cs index fb562ebe2..4060756fa 100644 --- a/Adyen.EcommLibrary/Service/Payment.cs +++ b/Adyen.EcommLibrary/Service/Payment.cs @@ -1,7 +1,6 @@ using Adyen.EcommLibrary.Model; using Adyen.EcommLibrary.Service.Resource.Payment; using Newtonsoft.Json; -using System; using System.Threading.Tasks; namespace Adyen.EcommLibrary.Service @@ -20,51 +19,23 @@ public Payment(Client client) public PaymentResult Authorise(PaymentRequest paymentRequest) { - PaymentResult paymentResult = null; - try - { - var jsonRequest = Util.JsonOperation.SerializeRequest(paymentRequest); - - var jsonResponse = _authorise.Request(jsonRequest); - paymentResult = JsonConvert.DeserializeObject(jsonResponse); - } - catch (Exception ex) - { - throw ex; - } - return paymentResult; + var jsonRequest = Util.JsonOperation.SerializeRequest(paymentRequest); + var jsonResponse = _authorise.Request(jsonRequest); + return JsonConvert.DeserializeObject(jsonResponse); } public async Task AuthoriseAsync(PaymentRequest paymentRequest) { - PaymentResult paymentResult = null; - try - { - var jsonRequest = Util.JsonOperation.SerializeRequest(paymentRequest); - var jsonResponse = await _authorise.RequestAsync(jsonRequest); - paymentResult = JsonConvert.DeserializeObject(jsonResponse); - } - catch (Exception ex) - { - throw ex; - } - return paymentResult; + var jsonRequest = Util.JsonOperation.SerializeRequest(paymentRequest); + var jsonResponse = await _authorise.RequestAsync(jsonRequest); + return JsonConvert.DeserializeObject(jsonResponse); } public PaymentResult Authorise3D(PaymentRequest3D paymentRequest3D) { - PaymentResult paymentResult = null; - try - { - var jsonRequest = JsonConvert.SerializeObject(paymentRequest3D); - var jsonResponse = _authorise3D.Request(jsonRequest); - paymentResult = JsonConvert.DeserializeObject(jsonResponse); - } - catch (Exception ex) - { - throw ex; - } - return paymentResult; + var jsonRequest = JsonConvert.SerializeObject(paymentRequest3D); + var jsonResponse = _authorise3D.Request(jsonRequest); + return JsonConvert.DeserializeObject(jsonResponse); } } } diff --git a/AdyenEcommLibrarySolution.sln b/AdyenEcommLibrarySolution.sln index 950e94374..390d6ce41 100644 --- a/AdyenEcommLibrarySolution.sln +++ b/AdyenEcommLibrarySolution.sln @@ -3,24 +3,24 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 VisualStudioVersion = 15.0.27130.2026 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Adyen.EcommLibrary.Test", "Adyen.EcommLibrary.Test\Adyen.EcommLibrary.Test.csproj", "{8AC1D1BC-1B6A-4DE4-93C2-91C845EC31D9}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Adyen.EcommLibrary", "Adyen.EcommLibrary\Adyen.EcommLibrary.csproj", "{2C020234-B871-4F19-BB6A-2CECECAC3D20}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Adyen.EcommLibrary.Test", "Adyen.EcommLibrary.Test\Adyen.EcommLibrary.Test.csproj", "{B0005586-6CC5-4AD2-AE84-0D38EF6CA301}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {8AC1D1BC-1B6A-4DE4-93C2-91C845EC31D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {8AC1D1BC-1B6A-4DE4-93C2-91C845EC31D9}.Debug|Any CPU.Build.0 = Debug|Any CPU - {8AC1D1BC-1B6A-4DE4-93C2-91C845EC31D9}.Release|Any CPU.ActiveCfg = Release|Any CPU - {8AC1D1BC-1B6A-4DE4-93C2-91C845EC31D9}.Release|Any CPU.Build.0 = Release|Any CPU {2C020234-B871-4F19-BB6A-2CECECAC3D20}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {2C020234-B871-4F19-BB6A-2CECECAC3D20}.Debug|Any CPU.Build.0 = Debug|Any CPU {2C020234-B871-4F19-BB6A-2CECECAC3D20}.Release|Any CPU.ActiveCfg = Release|Any CPU {2C020234-B871-4F19-BB6A-2CECECAC3D20}.Release|Any CPU.Build.0 = Release|Any CPU + {B0005586-6CC5-4AD2-AE84-0D38EF6CA301}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B0005586-6CC5-4AD2-AE84-0D38EF6CA301}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B0005586-6CC5-4AD2-AE84-0D38EF6CA301}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B0005586-6CC5-4AD2-AE84-0D38EF6CA301}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE