Skip to content

Commit

Permalink
[Fix] Refactored api for multithreaded usage [2h]
Browse files Browse the repository at this point in the history
  • Loading branch information
KreativJos committed Oct 6, 2020
1 parent c3163c6 commit 330dc86
Show file tree
Hide file tree
Showing 64 changed files with 1,252 additions and 1,652 deletions.
30 changes: 10 additions & 20 deletions PAYNLFormsApp/DebugForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ private void DebugForm_Load(object sender, EventArgs e)

public void dumpPaymentmethods()
{
APISettings.InitAPI();
ClearDebug();
PAYNLSDK.API.PaymentMethod.GetAll.Request request = new PAYNLSDK.API.PaymentMethod.GetAll.Request();
InitRequestDebug(request);
Expand All @@ -41,20 +40,18 @@ public void dumpPaymentmethods()
}

public void dumpTransactionGetService()
{
APISettings.InitAPI();
{
ClearDebug();
PAYNLSDK.API.Transaction.GetService.Request request = new PAYNLSDK.API.Transaction.GetService.Request();
PAYNLSDK.API.Transaction.GetService.Request request = new PAYNLSDK.API.Transaction.GetService.Request();
InitRequestDebug(request);
APISettings.Client.PerformRequest(request);
APISettings.Client.PerformRequest(request);
DebugRawResponse(request);
tbMain.Text = request.Response.ToString();
tbMain.Text = request.Response.ToString();

}
}

public void dumpTransactionGetLast()
{
APISettings.InitAPI();
ClearDebug();
PAYNLSDK.API.Transaction.GetLastTransactions.Request request = new PAYNLSDK.API.Transaction.GetLastTransactions.Request();
InitRequestDebug(request);
Expand All @@ -70,7 +67,6 @@ public void Approve(string transactionID)
try
{

APISettings.InitAPI();
ClearDebug();

if (transactionID == "")
Expand Down Expand Up @@ -104,7 +100,6 @@ public void Decline(string transactionID)
try
{

APISettings.InitAPI();
ClearDebug();

if (transactionID == "")
Expand Down Expand Up @@ -141,11 +136,10 @@ public void TransactionRefund(string transactionID, string amount, string exchan
try
{

APISettings.InitAPI();
ClearDebug();

int numValue;
bool parsed = Int32.TryParse(amount, out numValue);
bool parsed = int.TryParse(amount, out numValue);
if (!parsed || transactionID == "")
{
if (!parsed)
Expand All @@ -163,11 +157,10 @@ public void TransactionRefund(string transactionID, string amount, string exchan

else if (exchangeUrl != "")
{
APISettings.InitAPI();
AddDebug("-----");
AddDebug("Working with modified version of call");

PAYNLSDK.API.Transaction.Refund.Response response = Transaction.Refund(transactionID, null, numValue, null, exchangeUrl);
PAYNLSDK.API.Transaction.Refund.Response response = Transaction.Refund(APISettings.Client, transactionID, null, numValue, null, exchangeUrl);

tbMain.Text = response.RefundId;
}
Expand All @@ -186,7 +179,7 @@ public void TransactionRefund(string transactionID, string amount, string exchan

tbMain.Text = request.Response.RefundId;
}


}
catch (ErrorException ee)
Expand All @@ -202,11 +195,10 @@ public void RefundAdd(string bankAccountName, string bankAccountNumber, string a

try
{
APISettings.InitAPI();
ClearDebug();

int numValue;
bool parsed = Int32.TryParse(amount, out numValue);
bool parsed = int.TryParse(amount, out numValue);
if (!parsed)
{

Expand Down Expand Up @@ -241,7 +233,6 @@ public void TransactionRefundInfo(string refundID)

try
{
APISettings.InitAPI();
ClearDebug();

PAYNLSDK.API.Refund.Info.Request request = new PAYNLSDK.API.Refund.Info.Request(refundID);
Expand Down Expand Up @@ -277,14 +268,13 @@ private void AddDebug(string value)
}
private void InitRequestDebug(RequestBase request)
{
APISettings.InitAPI();
AddDebug(string.Format("Calling API {0} / {1}", request.Controller, request.Method));
AddDebug(string.Format("Requires TOKEN? {0}", request.RequiresApiToken));
AddDebug(string.Format("Requires SERVICEID? {0}", request.RequiresServiceId));
AddDebug("-----");
AddDebug("Initializing...");
AddDebug(string.Format("URL : {0}", request.Url));
AddDebug(string.Format("PARAMS : {0}", request.ToQueryString()));
AddDebug(string.Format("PARAMS : {0}", request.ToQueryString(APISettings.ApiToken, APISettings.ServiceID)));
AddDebug("-----");
}
private void DebugRawResponse(RequestBase request)
Expand Down
6 changes: 3 additions & 3 deletions PAYNLFormsApp/Fixtures/RefundInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ public class TransactionRefundInfo
{
static public PAYNLSDK.Objects.RefundInfo GetRefundInfoFixture()
{
String json = GetJsonFixture();
string json = GetJsonFixture();
PAYNLSDK.Objects.RefundInfo info = JsonConvert.DeserializeObject<PAYNLSDK.Objects.RefundInfo>(json);
return info;
}

static public String GetJsonFixture()
static public string GetJsonFixture()
{
String json = "{ \"request\": {\"result\": \"1\",\"errorId\": \"\",\"errorMessage\": \"\"},\"refund\": {\"paymentSessionId\": \"915930241\",\"amount\": \"2\",\"description\": \"EX-1234-4567-0000\",\"bankaccountHolder\": \"F. Lastname\",\"bankaccountNumber\": \"NL12RABO0123456789\",\"bankaccountBic\": \"RABONL2U\",\"statusCode\": \"316\",\"statusName\": \"Verwerkt\",\"processedDate\": \"2018-01-22\"},\"refundId\": \"RF-1234-4567-1234\"}";
string json = "{ \"request\": {\"result\": \"1\",\"errorId\": \"\",\"errorMessage\": \"\"},\"refund\": {\"paymentSessionId\": \"915930241\",\"amount\": \"2\",\"description\": \"EX-1234-4567-0000\",\"bankaccountHolder\": \"F. Lastname\",\"bankaccountNumber\": \"NL12RABO0123456789\",\"bankaccountBic\": \"RABONL2U\",\"statusCode\": \"316\",\"statusName\": \"Verwerkt\",\"processedDate\": \"2018-01-22\"},\"refundId\": \"RF-1234-4567-1234\"}";
return json;
}

Expand Down
77 changes: 34 additions & 43 deletions PAYNLFormsApp/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,13 @@ private void AddDebug(string value)
}
private void InitRequestDebug(RequestBase request)
{
APISettings.InitAPI();
AddDebug(string.Format("Calling API {0} / {1}", request.Controller, request.Method));
AddDebug(string.Format("Requires TOKEN? {0}", request.RequiresApiToken));
AddDebug(string.Format("Requires SERVICEID? {0}", request.RequiresServiceId));
AddDebug("-----");
AddDebug("Initializing...");
AddDebug(string.Format("URL : {0}", request.Url));
AddDebug(string.Format("PARAMS : {0}", request.ToQueryString()));
AddDebug(string.Format("PARAMS : {0}", request.ToQueryString(APISettings.ApiToken, APISettings.ServiceID)));
AddDebug("-----");
}
private void DebugRawResponse(RequestBase request)
Expand Down Expand Up @@ -93,7 +92,6 @@ private void dumpTransactionGetLastToolStripMenuItem_Click(object sender, EventA
private void txinfo(string id)
{
//619204633Xc4027e
APISettings.InitAPI();
ClearDebug();
PAYNLSDK.API.Transaction.Info.Request request = new PAYNLSDK.API.Transaction.Info.Request();
request.TransactionId = id;
Expand All @@ -120,59 +118,57 @@ private void xd83303CANCELToolStripMenuItem_Click(object sender, EventArgs e)

private void transActionStartToolStripMenuItem_Click(object sender, EventArgs e)
{
APISettings.InitAPI();
ClearDebug();
PAYNLSDK.API.Transaction.Start.Request fixture = TransactionStart.GetFixture();
AddDebug("Fixture loaded.");
AddDebug("JSON:");
AddDebug(fixture.ToString());
AddDebug("PARAMS:");
AddDebug(fixture.ToQueryString());
AddDebug(fixture.ToQueryString(APISettings.ApiToken, APISettings.ServiceID));
AddDebug("-----");
AddDebug("DONE");
}

private void transactionStartproductsToolStripMenuItem_Click(object sender, EventArgs e)
{
APISettings.InitAPI();
ClearDebug();
PAYNLSDK.API.Transaction.Start.Request fixture = TransactionStart.GetFixtureNoProductLines();
AddDebug("Fixture loaded.");
AddDebug("JSON:");
AddDebug(fixture.ToString());
AddDebug("PARAMS:");
string qs = fixture.ToQueryString();
string qs = fixture.ToQueryString(APISettings.ApiToken, APISettings.ServiceID);
AddDebug(qs);
NameValueCollection nvc = HttpUtility.ParseQueryString(qs);
string json = JsonConvert.SerializeObject(NvcToDictionary(nvc, true));
var parameters = HttpUtility.ParseQueryString(qs);
// string json = JsonConvert.SerializeObject(parametersToDictionary(parameters, true));
AddDebug("-----");
//AddDebug("PARAMS AS JSON");
//AddDebug(json);
DumpNvc(nvc);
Dumpparameters(parameters);
AddDebug("-----");
AddDebug("DONE");
}

void DumpNvc(NameValueCollection nvc)
void Dumpparameters(NameValueCollection parameters)
{
foreach (string key in nvc.Keys)
foreach (string key in parameters.Keys)
{
string[] values = nvc.GetValues(key);
foreach (string value in nvc.GetValues(key))
string[] values = parameters.GetValues(key);
foreach (string value in parameters.GetValues(key))
{
AddDebug(string.Format("'{0}' : '{1}'", key, value));
}
}
}

private Dictionary<string, object> NvcToDictionary(NameValueCollection nvc, bool handleMultipleValuesPerKey)
private Dictionary<string, object> parametersToDictionary(NameValueCollection parameters, bool handleMultipleValuesPerKey)
{
var result = new Dictionary<string, object>();
foreach (string key in nvc.Keys)
foreach (string key in parameters.Keys)
{
if (handleMultipleValuesPerKey)
{
string[] values = nvc.GetValues(key);
string[] values = parameters.GetValues(key);
if (values.Length == 1)
{
result.Add(key, values[0]);
Expand All @@ -184,7 +180,7 @@ private Dictionary<string, object> NvcToDictionary(NameValueCollection nvc, bool
}
else
{
result.Add(key, nvc[key]);
result.Add(key, parameters[key]);
}
}

Expand All @@ -195,11 +191,10 @@ private void startuseFixtureToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
APISettings.InitAPI();
ClearDebug();
PAYNLSDK.API.Transaction.Start.Request fixture = TransactionStart.GetFixtureNoProductLines();
InitRequestDebug(fixture);
DumpNvc(fixture.GetParameters());
Dumpparameters(fixture.GetParameters(APISettings.ApiToken, APISettings.ServiceID));

APISettings.Client.PerformRequest(fixture);
DebugRawResponse(fixture);
Expand Down Expand Up @@ -235,11 +230,11 @@ private void frm_FormClosed(object sender, FormClosedEventArgs e)
AddDebug("CANCELLED!");
return;
}
APISettings.InitAPI();

ClearDebug();
PAYNLSDK.API.Transaction.Start.Request fixture = LastRequests.LastTransactionStart;
InitRequestDebug(fixture);
DumpNvc(fixture.GetParameters());
Dumpparameters(fixture.GetParameters(APISettings.ApiToken, APISettings.ServiceID));

APISettings.Client.PerformRequest(fixture);
DebugRawResponse(fixture);
Expand Down Expand Up @@ -271,11 +266,10 @@ private void paymentProfilesToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
APISettings.InitAPI();
ClearDebug();
PAYNLSDK.API.PaymentProfile.GetAll.Request fixture = new PAYNLSDK.API.PaymentProfile.GetAll.Request();
InitRequestDebug(fixture);
DumpNvc(fixture.GetParameters());
Dumpparameters(fixture.GetParameters(APISettings.ApiToken, APISettings.ServiceID));

APISettings.Client.PerformRequest(fixture);
DebugRawResponse(fixture);
Expand All @@ -292,11 +286,10 @@ private void serviceCategoriesToolStripMenuItem_Click(object sender, EventArgs e
{
try
{
APISettings.InitAPI();
ClearDebug();
PAYNLSDK.API.Service.GetCategories.Request fixture = new PAYNLSDK.API.Service.GetCategories.Request();
InitRequestDebug(fixture);
DumpNvc(fixture.GetParameters());
Dumpparameters(fixture.GetParameters(APISettings.ApiToken, APISettings.ServiceID));

APISettings.Client.PerformRequest(fixture);
DebugRawResponse(fixture);
Expand Down Expand Up @@ -392,11 +385,11 @@ private void testDateTimeConversionToolStripMenuItem_Click(object sender, EventA
try
{
TestYMD testObj = JsonConvert.DeserializeObject<TestYMD>(dateString);
AddDebug(String.Format("Converted '{0}' to {1}.", dateString, testObj.DT.ToString()));
AddDebug(string.Format("Converted '{0}' to {1}.", dateString, testObj.DT.ToString()));
}
catch (Exception e0)
{
AddDebug(String.Format("Error converting '{0}' using YMD.", dateString));
AddDebug(string.Format("Error converting '{0}' using YMD.", dateString));
AddDebug(e0.Message);
}
}
Expand All @@ -405,11 +398,11 @@ private void testDateTimeConversionToolStripMenuItem_Click(object sender, EventA
try
{
TestDMY testObj = JsonConvert.DeserializeObject<TestDMY>(dateString);
AddDebug(String.Format("Converted '{0}' to {1}.", dateString, testObj.DT.ToString()));
AddDebug(string.Format("Converted '{0}' to {1}.", dateString, testObj.DT.ToString()));
}
catch (Exception e1)
{
AddDebug(String.Format("Error converting '{0}' using YMD.", dateString));
AddDebug(string.Format("Error converting '{0}' using YMD.", dateString));
AddDebug(e1.Message);
}
}
Expand All @@ -418,11 +411,11 @@ private void testDateTimeConversionToolStripMenuItem_Click(object sender, EventA
try
{
TestYMDHIS testObj = JsonConvert.DeserializeObject<TestYMDHIS>(dateString);
AddDebug(String.Format("Converted '{0}' to {1}.", dateString, testObj.DT.ToString()));
AddDebug(string.Format("Converted '{0}' to {1}.", dateString, testObj.DT.ToString()));
}
catch (Exception e2)
{
AddDebug(String.Format("Error converting '{0}' using YMD.", dateString));
AddDebug(string.Format("Error converting '{0}' using YMD.", dateString));
AddDebug(e2.Message);
}
}
Expand All @@ -431,50 +424,48 @@ private void testDateTimeConversionToolStripMenuItem_Click(object sender, EventA

private void refundtransactionToolStripMenuItem_Click(object sender, EventArgs e)
{
APISettings.InitAPI();
ClearDebug();
PAYNLSDK.API.Refund.Transaction.Request fixture = RefundTransaction.GetFixtureNoProductLines();
AddDebug("Fixture loaded.");
AddDebug("JSON:");
AddDebug(fixture.ToString());
AddDebug("PARAMS:");
string qs = fixture.ToQueryString();
string qs = fixture.ToQueryString(APISettings.ApiToken, APISettings.ServiceID);
AddDebug(qs);
NameValueCollection nvc = HttpUtility.ParseQueryString(qs);
string json = JsonConvert.SerializeObject(NvcToDictionary(nvc, true));
var parameters = HttpUtility.ParseQueryString(qs);
// string json = JsonConvert.SerializeObject(parametersToDictionary(parameters, true));
AddDebug("-----");
//AddDebug("PARAMS AS JSON");
//AddDebug(json);
DumpNvc(nvc);
Dumpparameters(parameters);
AddDebug("-----");
AddDebug("DONE");
}

private void refundTrasactionProductsToolStripMenuItem_Click(object sender, EventArgs e)
{
APISettings.InitAPI();
ClearDebug();
PAYNLSDK.API.Refund.Transaction.Request fixture = RefundTransaction.GetFixture();
AddDebug("Fixture loaded.");
AddDebug("JSON:");
AddDebug(fixture.ToString());
AddDebug("PARAMS:");
string qs = fixture.ToQueryString();
string qs = fixture.ToQueryString(APISettings.ApiToken, APISettings.ServiceID);
AddDebug(qs);
NameValueCollection nvc = HttpUtility.ParseQueryString(qs);
string json = JsonConvert.SerializeObject(NvcToDictionary(nvc, true));
var parameters = HttpUtility.ParseQueryString(qs);
// string json = JsonConvert.SerializeObject(parametersToDictionary(parameters, true));
AddDebug("-----");
//AddDebug("PARAMS AS JSON");
//AddDebug(json);
DumpNvc(nvc);
Dumpparameters(parameters);
AddDebug("-----");
AddDebug("DONE");
}

private void transactionRefundInofromJsonFixtureToolStripMenuItem_Click(object sender, EventArgs e)
{
ClearDebug();
String json = TransactionRefundInfo.GetJsonFixture();
string json = TransactionRefundInfo.GetJsonFixture();
PAYNLSDK.Objects.RefundInfo fixture = TransactionRefundInfo.GetRefundInfoFixture();
AddDebug("Fixture loaded.");
AddDebug("JSON:");
Expand Down
Loading

0 comments on commit 330dc86

Please sign in to comment.