Skip to content

Commit

Permalink
Added log ability
Browse files Browse the repository at this point in the history
  • Loading branch information
ahwm committed Oct 22, 2018
1 parent 711554f commit 8ec96e6
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/Cardknox.NET/Cardknox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ public CardknoxResponse CCSale(CCSale _sale, bool force = false)

AddSpecialFields(_sale);

Log.LogRequest(_values);

var resp = MakeRequest();
Log.LogResponse(resp);
return new CardknoxResponse(resp);
}

Expand Down Expand Up @@ -177,7 +180,10 @@ public CardknoxResponse CCSave(CCSave _save, bool force = false)
if (!IsNullOrWhiteSpace(_save.IP))
_values.Add("xIP", _save.IP);

Log.LogRequest(_values);

var resp = MakeRequest();
Log.LogResponse(resp);
return new CardknoxResponse(resp);
}

Expand Down Expand Up @@ -213,7 +219,10 @@ public CardknoxResponse CCRefund(CCRefund _refund, bool force = false)
_values.Add("xRefNum", _refund.RefNum);
// END required information

Log.LogRequest(_values);

var resp = MakeRequest();
Log.LogResponse(resp);
return new CardknoxResponse(resp);
}

Expand Down Expand Up @@ -287,7 +296,10 @@ public CardknoxResponse CCAuthOnly(CCAuthOnly _auth, bool force = false)

AddSpecialFields(_auth);

Log.LogRequest(_values);

var resp = MakeRequest();
Log.LogResponse(resp);
return new CardknoxResponse(resp);
}

Expand Down Expand Up @@ -338,7 +350,10 @@ public CardknoxResponse CCCapture(CCCapture _capture, bool force = false)

AddSpecialFields(_capture);

Log.LogRequest(_values);

var resp = MakeRequest();
Log.LogResponse(resp);
return new CardknoxResponse(resp);
}

Expand Down Expand Up @@ -412,7 +427,10 @@ public CardknoxResponse CCCredit(CCCredit _credit, bool force = false)

AddSpecialFields(_credit);

Log.LogRequest(_values);

var resp = MakeRequest();
Log.LogResponse(resp);
return new CardknoxResponse(resp);
}

Expand Down Expand Up @@ -444,7 +462,10 @@ public CardknoxResponse CCVoid(CCVoid _void, bool force = false)
_values.Add("xRefNum", _void.RefNum);
// END required information

Log.LogRequest(_values);

var resp = MakeRequest();
Log.LogResponse(resp);
return new CardknoxResponse(resp);
}

Expand Down Expand Up @@ -491,7 +512,10 @@ public CardknoxResponse CCAdjust(CCAdjust _adjust, bool force = false)
if (!IsNullOrWhiteSpace(_adjust.IP))
_values.Add("xIP", _adjust.IP);

Log.LogRequest(_values);

var resp = MakeRequest();
Log.LogResponse(resp);
return new CardknoxResponse(resp);
}

Expand Down Expand Up @@ -541,7 +565,10 @@ public CardknoxResponse CCPostAuth(CCPostAuth _auth, bool force = false)

AddSpecialFields(_auth);

Log.LogRequest(_values);

var resp = MakeRequest();
Log.LogResponse(resp);
return new CardknoxResponse(resp);
}

Expand Down Expand Up @@ -573,7 +600,10 @@ public CardknoxResponse CCVoidRefund(CCVoidRefund _refund, bool force = false)
_values.Add("xRefNum", _refund.RefNum);
// END required information

Log.LogRequest(_values);

var resp = MakeRequest();
Log.LogResponse(resp);
return new CardknoxResponse(resp);
}

Expand Down Expand Up @@ -605,7 +635,10 @@ public CardknoxResponse CCVoidRelease(CCVoidRelease _release, bool force = false
_values.Add("xRefNum", _release.RefNum);
// END required information

Log.LogRequest(_values);

var resp = MakeRequest();
Log.LogResponse(resp);
return new CardknoxResponse(resp);
}
#endregion
Expand Down Expand Up @@ -669,7 +702,10 @@ public CardknoxResponse CheckSale(CheckSale _sale, bool force = false)

AddSpecialFields(_sale);

Log.LogRequest(_values);

var resp = MakeRequest();
Log.LogResponse(resp);
return new CardknoxResponse(resp);
}

Expand Down Expand Up @@ -730,7 +766,10 @@ public CardknoxResponse CheckCredit(CheckCredit _credit, bool force = false)

AddSpecialFields(_credit);

Log.LogRequest(_values);

var resp = MakeRequest();
Log.LogResponse(resp);
return new CardknoxResponse(resp);
}

Expand Down Expand Up @@ -779,7 +818,10 @@ public CardknoxResponse CheckSave(CheckSave _save, bool force = false)
if (!IsNullOrWhiteSpace(_save.IP))
_values.Add("xIP", _save.IP);

Log.LogRequest(_values);

var resp = MakeRequest();
Log.LogResponse(resp);
return new CardknoxResponse(resp);
}

Expand Down Expand Up @@ -814,7 +856,10 @@ public CardknoxResponse CheckVoid(CheckVoid _void, bool force = false)
_values.Add("xRefNum", _void.RefNum);
// END required information

Log.LogRequest(_values);

var resp = MakeRequest();
Log.LogResponse(resp);
return new CardknoxResponse(resp);
}

Expand Down Expand Up @@ -849,7 +894,10 @@ public CardknoxResponse CheckRefund(CheckRefund _refund, bool force = false)
_values.Add("xRefNum", _refund.RefNum);
// END required information

Log.LogRequest(_values);

var resp = MakeRequest();
Log.LogResponse(resp);
return new CardknoxResponse(resp);
}
#endregion
Expand Down Expand Up @@ -917,7 +965,10 @@ public CardknoxResponse EBTFSSale(EBTFSSale _sale, bool force = false)

AddSpecialFields(_sale);

Log.LogRequest(_values);

var resp = MakeRequest();
Log.LogResponse(resp);
return new CardknoxResponse(resp);
}
/// <summary>
Expand Down Expand Up @@ -982,7 +1033,10 @@ public CardknoxResponse EBTFSCredit(EBTFSCredit _credit, bool force = false)

AddSpecialFields(_credit);

Log.LogRequest(_values);

var resp = MakeRequest();
Log.LogResponse(resp);
return new CardknoxResponse(resp);
}
/// <summary>
Expand Down Expand Up @@ -1034,7 +1088,10 @@ public CardknoxResponse EBTFSBalance(EBTFSBalance _bal, bool force = false)
if (!IsNullOrWhiteSpace(_bal.IP))
_values.Add("xIP", _bal.IP);

Log.LogRequest(_values);

var resp = MakeRequest();
Log.LogResponse(resp);
return new CardknoxResponse(resp);
}
/// <summary>
Expand Down Expand Up @@ -1099,7 +1156,10 @@ public CardknoxResponse EBTFSVoucher(EBTFSVoucher _voucher, bool force = false)

AddSpecialFields(_voucher);

Log.LogRequest(_values);

var resp = MakeRequest();
Log.LogResponse(resp);
return new CardknoxResponse(resp);
}
#endregion
Expand Down
85 changes: 85 additions & 0 deletions src/Cardknox.NET/Log.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;

namespace CardknoxApi
{
static class Log
{
readonly static string[] NoInclude = { "xCardNum", "xMagStripe", "xExp", "xCVV", "xToken", "xDUKPT", "xRouting", "xAccount", "xMICR" };
static bool EnableLogging
{
get
{
try
{
return ConfigurationManager.AppSettings["Cardknox.Logging"] == "enabled";
}
catch { return false; }
}
}
static string LogLocation
{
get
{
try
{
return ConfigurationManager.AppSettings["Cardknox.LogLocation"];
}
catch { return ""; }
}
}
public static void LogRequest(NameValueCollection _values)
{
if (!EnableLogging || LogLocation == "")
return;

string loc = LogLocation;
if (LogLocation.StartsWith("~"))
loc = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, loc);

string body = "----" + DateTime.UtcNow.ToString("s") + "----" + Environment.NewLine;
body += "Cardknox Request" + Environment.NewLine;
body += "-----------------" + Environment.NewLine;
foreach (var k in _values.AllKeys)
{
if (!NoInclude.Contains(k))
body += k + " = " + _values[k] + Environment.NewLine;
}

byte[] data = Encoding.UTF8.GetBytes(body);
using (FileStream fs = new FileStream(loc, FileMode.Append))
{
fs.Write(data, 0, data.Length);
}
}
public static void LogResponse(NameValueCollection _values)
{
if (!EnableLogging || LogLocation == "")
return;

string loc = LogLocation;
if (LogLocation.StartsWith("~"))
loc = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, loc);

string body = "----" + DateTime.UtcNow.ToString("s") + "----" + Environment.NewLine;
body += "Cardknox Response" + Environment.NewLine;
body += "-----------------" + Environment.NewLine;
foreach (var k in _values.AllKeys)
{
body += k + " = " + _values[k] + Environment.NewLine;
}
body += Environment.NewLine; // add second newline at end of response for better separation

byte[] data = Encoding.UTF8.GetBytes(body);
using (FileStream fs = new FileStream(loc, FileMode.Append))
{
fs.Write(data, 0, data.Length);
}
}
}
}

0 comments on commit 8ec96e6

Please sign in to comment.