Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON bugfixed #145

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Iyzipay.Samples/CardManagementPageSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
178 changes: 90 additions & 88 deletions Iyzipay/IyzipayResourceV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> values;
if (responseHeaders.TryGetValues(CONVERSATION_ID_HEADER_NAME, out values))
{
string conversationId = values.First();
this.ConversationId = !string.IsNullOrWhiteSpace(conversationId) ? conversationId : null;
}
}
IEnumerable<string> values;
if (responseHeaders.TryGetValues(CONVERSATION_ID_HEADER_NAME, out values))
{
string conversationId = values.First();
this.ConversationId = !string.IsNullOrWhiteSpace(conversationId) ? conversationId : null;
}
}

protected static Dictionary<string, string> GetHttpHeadersWithRequestBody(BaseRequestV2 request, String url, Options options)
{
Dictionary<string, string> 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<string, string> GetHttpHeadersWithRequestBody(BaseRequestV2 request, String url, Options options)
{
Dictionary<string, string> 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<string, string> GetHttpHeadersWithUrlParams(BaseRequestV2 request, String url, Options options)
{
Dictionary<string, string> headers = GetCommonHttpHeaders(request, url, options);
headers.Add(AUTHORIZATION, PrepareAuthorizationStringWithRequestBody(null, url, options));
return headers;
}
protected static Dictionary<string, string> GetHttpHeadersWithUrlParams(BaseRequestV2 request, String url, Options options)
{
Dictionary<string, string> headers = GetCommonHttpHeaders(request, url, options);
headers.Add(AUTHORIZATION, PrepareAuthorizationStringWithRequestBody(null, url, options));
return headers;
}

private static Dictionary<string, string> GetCommonHttpHeaders(BaseRequestV2 request, String url, Options options)
{
Dictionary<string, string> headers = new Dictionary<string, string>();
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<string, string> GetCommonHttpHeaders(BaseRequestV2 request, String url, Options options)
{
Dictionary<string, string> headers = new Dictionary<string, string>();
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);
}
}
}
4 changes: 3 additions & 1 deletion Iyzipay/RestHttpClientV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Net.Http.Headers;
using System.Linq;
using System.Threading.Tasks;
using System.Text;

namespace Iyzipay
{
Expand Down Expand Up @@ -64,11 +65,12 @@ public T Post<T>(String url, Dictionary<string, string> headers, BaseRequestV2 r
}
public async Task<T> PostAsync<T>(String url, Dictionary<string, string> 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)
Expand Down
Loading