diff --git a/Iyzipay.Samples/CardManagementPageSample.cs b/Iyzipay.Samples/CardManagementPageSample.cs index bf94ce2..5b7b07b 100644 --- a/Iyzipay.Samples/CardManagementPageSample.cs +++ b/Iyzipay.Samples/CardManagementPageSample.cs @@ -30,7 +30,7 @@ public async Task Should_Initialize_Card_Management_PageAsync() Locale = Locale.TR.ToString() }; - CardManagementPageInitialize cardManagementPageInitialize = await CardManagementPageInitialize.Create(request, options); + CardManagementPageInitialize cardManagementPageInitialize = CardManagementPageInitialize.Create(request, options); PrintResponse(cardManagementPageInitialize); Assert.AreEqual(Locale.TR.ToString(), cardManagementPageInitialize.Locale); diff --git a/Iyzipay/IyzipayResourceV2.cs b/Iyzipay/IyzipayResourceV2.cs index e2d0e73..c5ec35f 100644 --- a/Iyzipay/IyzipayResourceV2.cs +++ b/Iyzipay/IyzipayResourceV2.cs @@ -4,107 +4,109 @@ using System.Linq; using System.Net.Http; using System.Net.Http.Headers; +using Newtonsoft.Json; namespace Iyzipay { - public class IyzipayResourceV2 - { - private static readonly string AUTHORIZATION = "Authorization"; - private static readonly string AUTHORIZATION_FALLBACK_HEADER = "AUTHORIZATION_FALLBACK_HEADER"; - private static readonly string CONVERSATION_ID_HEADER_NAME = "x-conversation-id"; - private static readonly string CLIENT_VERSION_HEADER_NAME = "x-iyzi-client-version"; - private static readonly string IYZIWS_V2_HEADER_NAME = "IYZWSv2 "; - private static readonly string IYZIWS_HEADER_NAME = "IYZWS "; - private static readonly string COLON = ":"; + public class IyzipayResourceV2 + { + private static readonly string AUTHORIZATION = "Authorization"; + private static readonly string AUTHORIZATION_FALLBACK_HEADER = "AUTHORIZATION_FALLBACK_HEADER"; + private static readonly string CONVERSATION_ID_HEADER_NAME = "x-conversation-id"; + private static readonly string CLIENT_VERSION_HEADER_NAME = "x-iyzi-client-version"; + private static readonly string IYZIWS_V2_HEADER_NAME = "IYZWSv2 "; + private static readonly string IYZIWS_HEADER_NAME = "IYZWS "; + private static readonly string COLON = ":"; - public String Status { get; set; } - public int StatusCode { get; set; } - public String ErrorMessage { get; set; } - public String ConversationId { get; set; } - public long SystemTime { get; set; } - public String Locale { get; set; } + public String Status { get; set; } + public int StatusCode { get; set; } + public String ErrorMessage { get; set; } + public String ConversationId { get; set; } + public long SystemTime { get; set; } + public String Locale { get; set; } - public IyzipayResourceV2() - { - } + public IyzipayResourceV2() + { + } - public void AppendWithHttpResponseHeaders(HttpResponseMessage httpResponseMessage) - { - HttpHeaders responseHeaders = httpResponseMessage.Headers; - this.StatusCode = Convert.ToInt32(httpResponseMessage.StatusCode); + public void AppendWithHttpResponseHeaders(HttpResponseMessage httpResponseMessage) + { + HttpHeaders responseHeaders = httpResponseMessage.Headers; + this.StatusCode = Convert.ToInt32(httpResponseMessage.StatusCode); - IEnumerable values; - if (responseHeaders.TryGetValues(CONVERSATION_ID_HEADER_NAME, out values)) - { - string conversationId = values.First(); - this.ConversationId = !string.IsNullOrWhiteSpace(conversationId) ? conversationId : null; - } - } + IEnumerable values; + if (responseHeaders.TryGetValues(CONVERSATION_ID_HEADER_NAME, out values)) + { + string conversationId = values.First(); + this.ConversationId = !string.IsNullOrWhiteSpace(conversationId) ? conversationId : null; + } + } - protected static Dictionary GetHttpHeadersWithRequestBody(BaseRequestV2 request, String url, Options options) - { - Dictionary headers = GetCommonHttpHeaders(request, url, options); - headers.Add(AUTHORIZATION, PrepareAuthorizationStringWithRequestBody(request, url, options)); - headers.Add(AUTHORIZATION_FALLBACK_HEADER, PrepareAuthorizationString(request, url, options)); - return headers; - } + protected static Dictionary GetHttpHeadersWithRequestBody(BaseRequestV2 request, String url, Options options) + { + Dictionary headers = GetCommonHttpHeaders(request, url, options); + headers.Add(AUTHORIZATION, PrepareAuthorizationStringWithRequestBody(request, url, options)); + headers.Add(AUTHORIZATION_FALLBACK_HEADER, PrepareAuthorizationString(request, url, options)); + return headers; + } - protected static Dictionary GetHttpHeadersWithUrlParams(BaseRequestV2 request, String url, Options options) - { - Dictionary headers = GetCommonHttpHeaders(request, url, options); - headers.Add(AUTHORIZATION, PrepareAuthorizationStringWithRequestBody(null, url, options)); - return headers; - } + protected static Dictionary GetHttpHeadersWithUrlParams(BaseRequestV2 request, String url, Options options) + { + Dictionary headers = GetCommonHttpHeaders(request, url, options); + headers.Add(AUTHORIZATION, PrepareAuthorizationStringWithRequestBody(null, url, options)); + return headers; + } - private static Dictionary GetCommonHttpHeaders(BaseRequestV2 request, String url, Options options) - { - Dictionary headers = new Dictionary(); - headers.Add("Accept", "application/json"); - headers.Add(CLIENT_VERSION_HEADER_NAME, IyzipayConstants.CLIENT_VERSION); - headers.Add(CONVERSATION_ID_HEADER_NAME, request.ConversationId); - return headers; - } + private static Dictionary GetCommonHttpHeaders(BaseRequestV2 request, String url, Options options) + { + Dictionary headers = new Dictionary(); + headers.Add("Accept", "application/json"); + headers.Add(CLIENT_VERSION_HEADER_NAME, IyzipayConstants.CLIENT_VERSION); + headers.Add(CONVERSATION_ID_HEADER_NAME, request.ConversationId); + return headers; + } - private static String PrepareAuthorizationStringWithRequestBody(BaseRequestV2 request, String url, Options options) - { - String randomKey = GenerateRandomKey(); - String uriPath = FindUriPath(url); + private static string PrepareAuthorizationStringWithRequestBody(BaseRequestV2 request, string url, Options options) + { + string randomKey = GenerateRandomKey(); + string uriPath = FindUriPath(url); + string payload = request != null ? uriPath + JsonConvert.SerializeObject(request) : uriPath; - String payload = request != null ? uriPath + JsonBuilder.SerializeObjectToPrettyJson(request) : uriPath; - String dataToEncrypt = randomKey + payload; - String hash = HashGeneratorV2.GenerateHash(options.ApiKey, options.SecretKey, randomKey, dataToEncrypt); - return IYZIWS_V2_HEADER_NAME + hash; - } - private static String PrepareAuthorizationStringWithUrlParam(BaseRequestV2 request, String url, Options options) - { - String randomKey = GenerateRandomKey(); - String uriPath = FindUriPath(url); - String dataToEncrypt = randomKey + uriPath; + string dataToEncrypt = randomKey + payload; + string hash = HashGeneratorV2.GenerateHash(options.ApiKey, options.SecretKey, randomKey, dataToEncrypt); + return IYZIWS_V2_HEADER_NAME + hash; + } - String hash = HashGeneratorV2.GenerateHash(options.ApiKey, options.SecretKey, randomKey, dataToEncrypt); - return IYZIWS_V2_HEADER_NAME + hash; - } - private static string PrepareAuthorizationString(BaseRequestV2 request, string randomString, Options options) - { - string hash = HashGenerator.GenerateHash(options.ApiKey, options.SecretKey, randomString, request); - return IYZIWS_HEADER_NAME + options.ApiKey + COLON + hash; - } - private static String GenerateRandomKey() - { - return DateTime.Now.ToString("ddMMyyyyhhmmssffff"); - } + private static String PrepareAuthorizationStringWithUrlParam(BaseRequestV2 request, String url, Options options) + { + String randomKey = GenerateRandomKey(); + String uriPath = FindUriPath(url); + String dataToEncrypt = randomKey + uriPath; - private static String FindUriPath(String url) - { - int startIndex = url.IndexOf("/v2"); - if (startIndex == -1) - { - startIndex = url.IndexOf(".com")+4; - } - int endIndex = url.IndexOf("?"); - int length = endIndex == -1 ? url.Length - startIndex : endIndex - startIndex; - return url.Substring(startIndex, length); - } - } + String hash = HashGeneratorV2.GenerateHash(options.ApiKey, options.SecretKey, randomKey, dataToEncrypt); + return IYZIWS_V2_HEADER_NAME + hash; + } + private static string PrepareAuthorizationString(BaseRequestV2 request, string randomString, Options options) + { + string hash = HashGenerator.GenerateHash(options.ApiKey, options.SecretKey, randomString, request); + return IYZIWS_HEADER_NAME + options.ApiKey + COLON + hash; + } + private static String GenerateRandomKey() + { + return DateTime.Now.ToString("ddMMyyyyhhmmssffff"); + } + + private static String FindUriPath(String url) + { + int startIndex = url.IndexOf("/v2"); + if (startIndex == -1) + { + startIndex = url.IndexOf(".com") + 4; + } + int endIndex = url.IndexOf("?"); + int length = endIndex == -1 ? url.Length - startIndex : endIndex - startIndex; + return url.Substring(startIndex, length); + } + } } diff --git a/Iyzipay/RestHttpClientV2.cs b/Iyzipay/RestHttpClientV2.cs index b8c9e82..bb6a0ec 100644 --- a/Iyzipay/RestHttpClientV2.cs +++ b/Iyzipay/RestHttpClientV2.cs @@ -6,6 +6,7 @@ using System.Net.Http.Headers; using System.Linq; using System.Threading.Tasks; +using System.Text; namespace Iyzipay { @@ -64,11 +65,12 @@ public T Post(String url, Dictionary headers, BaseRequestV2 r } public async Task PostAsync(String url, Dictionary headers, BaseRequestV2 request) where T : IyzipayResourceV2 { + var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json"); HttpRequestMessage requestMessage = new HttpRequestMessage { Method = HttpMethod.Post, RequestUri = new Uri(url), - Content = JsonBuilder.ToJsonString(request) + Content = content }; foreach (var header in headers)