From 8ec96e616348a934f1ab6181f6a5a5171fdd87cd Mon Sep 17 00:00:00 2001 From: Adam Humpherys Date: Mon, 22 Oct 2018 15:40:13 -0600 Subject: [PATCH] Added log ability --- src/Cardknox.NET/Cardknox.cs | 60 +++++++++++++++++++++++++ src/Cardknox.NET/Log.cs | 85 ++++++++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 src/Cardknox.NET/Log.cs diff --git a/src/Cardknox.NET/Cardknox.cs b/src/Cardknox.NET/Cardknox.cs index fe9ee5e..41afa4e 100644 --- a/src/Cardknox.NET/Cardknox.cs +++ b/src/Cardknox.NET/Cardknox.cs @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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 @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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 @@ -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); } /// @@ -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); } /// @@ -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); } /// @@ -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 diff --git a/src/Cardknox.NET/Log.cs b/src/Cardknox.NET/Log.cs new file mode 100644 index 0000000..2055e47 --- /dev/null +++ b/src/Cardknox.NET/Log.cs @@ -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); + } + } + } +}