From 7041c82f6e4739c66ca3f72ae30cea4eb0f369ba Mon Sep 17 00:00:00 2001 From: Adam Humpherys Date: Tue, 23 Oct 2018 09:08:44 -0600 Subject: [PATCH] custom logging --- src/Cardknox.NET/Cardknox.cs | 201 +++++++++++++++++++++----- src/Cardknox.NET/Cardknox.csproj | 4 +- src/Cardknox.NET/Extensions.cs | 27 ++++ src/Cardknox.NET/Log.cs | 8 +- src/Cardknox.NET/Operations/Events.cs | 35 +++++ 5 files changed, 228 insertions(+), 47 deletions(-) create mode 100644 src/Cardknox.NET/Extensions.cs create mode 100644 src/Cardknox.NET/Operations/Events.cs diff --git a/src/Cardknox.NET/Cardknox.cs b/src/Cardknox.NET/Cardknox.cs index 41afa4e..c3630e6 100644 --- a/src/Cardknox.NET/Cardknox.cs +++ b/src/Cardknox.NET/Cardknox.cs @@ -13,6 +13,27 @@ namespace CardknoxApi /// public class Cardknox { + /// + /// + /// + /// + /// + public delegate void RequestStartedEventHandler(object sender, CardknoxEventArgs e); + /// + /// + /// + public event RequestStartedEventHandler RequestStarted; + /// + /// + /// + /// + /// + public delegate void RequestCompletedEventHandler(object sender, CardknoxEventArgs e); + /// + /// + /// + public event RequestCompletedEventHandler RequestCompleted; + /// /// /// @@ -107,10 +128,15 @@ public CardknoxResponse CCSale(CCSale _sale, bool force = false) AddSpecialFields(_sale); - Log.LogRequest(_values); + if (RequestStarted != null) + Log.LogRequest(_values); + else RequestStarted.Invoke(this, new CardknoxEventArgs(_values)); var resp = MakeRequest(); - Log.LogResponse(resp); + if (RequestCompleted != null) + Log.LogResponse(resp); + else RequestCompleted.Invoke(this, new CardknoxEventArgs(resp)); + return new CardknoxResponse(resp); } @@ -180,10 +206,15 @@ public CardknoxResponse CCSave(CCSave _save, bool force = false) if (!IsNullOrWhiteSpace(_save.IP)) _values.Add("xIP", _save.IP); - Log.LogRequest(_values); + if (RequestStarted != null) + Log.LogRequest(_values); + else RequestStarted.Invoke(this, new CardknoxEventArgs(_values)); var resp = MakeRequest(); - Log.LogResponse(resp); + if (RequestCompleted != null) + Log.LogResponse(resp); + else RequestCompleted.Invoke(this, new CardknoxEventArgs(resp)); + return new CardknoxResponse(resp); } @@ -219,10 +250,15 @@ public CardknoxResponse CCRefund(CCRefund _refund, bool force = false) _values.Add("xRefNum", _refund.RefNum); // END required information - Log.LogRequest(_values); + if (RequestStarted != null) + Log.LogRequest(_values); + else RequestStarted.Invoke(this, new CardknoxEventArgs(_values)); var resp = MakeRequest(); - Log.LogResponse(resp); + if (RequestCompleted != null) + Log.LogResponse(resp); + else RequestCompleted.Invoke(this, new CardknoxEventArgs(resp)); + return new CardknoxResponse(resp); } @@ -296,10 +332,15 @@ public CardknoxResponse CCAuthOnly(CCAuthOnly _auth, bool force = false) AddSpecialFields(_auth); - Log.LogRequest(_values); + if (RequestStarted != null) + Log.LogRequest(_values); + else RequestStarted.Invoke(this, new CardknoxEventArgs(_values)); var resp = MakeRequest(); - Log.LogResponse(resp); + if (RequestCompleted != null) + Log.LogResponse(resp); + else RequestCompleted.Invoke(this, new CardknoxEventArgs(resp)); + return new CardknoxResponse(resp); } @@ -350,10 +391,15 @@ public CardknoxResponse CCCapture(CCCapture _capture, bool force = false) AddSpecialFields(_capture); - Log.LogRequest(_values); + if (RequestStarted != null) + Log.LogRequest(_values); + else RequestStarted.Invoke(this, new CardknoxEventArgs(_values)); var resp = MakeRequest(); - Log.LogResponse(resp); + if (RequestCompleted != null) + Log.LogResponse(resp); + else RequestCompleted.Invoke(this, new CardknoxEventArgs(resp)); + return new CardknoxResponse(resp); } @@ -427,10 +473,15 @@ public CardknoxResponse CCCredit(CCCredit _credit, bool force = false) AddSpecialFields(_credit); - Log.LogRequest(_values); + if (RequestStarted != null) + Log.LogRequest(_values); + else RequestStarted.Invoke(this, new CardknoxEventArgs(_values)); var resp = MakeRequest(); - Log.LogResponse(resp); + if (RequestCompleted != null) + Log.LogResponse(resp); + else RequestCompleted.Invoke(this, new CardknoxEventArgs(resp)); + return new CardknoxResponse(resp); } @@ -462,10 +513,15 @@ public CardknoxResponse CCVoid(CCVoid _void, bool force = false) _values.Add("xRefNum", _void.RefNum); // END required information - Log.LogRequest(_values); + if (RequestStarted != null) + Log.LogRequest(_values); + else RequestStarted.Invoke(this, new CardknoxEventArgs(_values)); var resp = MakeRequest(); - Log.LogResponse(resp); + if (RequestCompleted != null) + Log.LogResponse(resp); + else RequestCompleted.Invoke(this, new CardknoxEventArgs(resp)); + return new CardknoxResponse(resp); } @@ -512,10 +568,15 @@ public CardknoxResponse CCAdjust(CCAdjust _adjust, bool force = false) if (!IsNullOrWhiteSpace(_adjust.IP)) _values.Add("xIP", _adjust.IP); - Log.LogRequest(_values); + if (RequestStarted != null) + Log.LogRequest(_values); + else RequestStarted.Invoke(this, new CardknoxEventArgs(_values)); var resp = MakeRequest(); - Log.LogResponse(resp); + if (RequestCompleted != null) + Log.LogResponse(resp); + else RequestCompleted.Invoke(this, new CardknoxEventArgs(resp)); + return new CardknoxResponse(resp); } @@ -565,10 +626,15 @@ public CardknoxResponse CCPostAuth(CCPostAuth _auth, bool force = false) AddSpecialFields(_auth); - Log.LogRequest(_values); + if (RequestStarted != null) + Log.LogRequest(_values); + else RequestStarted.Invoke(this, new CardknoxEventArgs(_values)); var resp = MakeRequest(); - Log.LogResponse(resp); + if (RequestCompleted != null) + Log.LogResponse(resp); + else RequestCompleted.Invoke(this, new CardknoxEventArgs(resp)); + return new CardknoxResponse(resp); } @@ -600,10 +666,15 @@ public CardknoxResponse CCVoidRefund(CCVoidRefund _refund, bool force = false) _values.Add("xRefNum", _refund.RefNum); // END required information - Log.LogRequest(_values); + if (RequestStarted != null) + Log.LogRequest(_values); + else RequestStarted.Invoke(this, new CardknoxEventArgs(_values)); var resp = MakeRequest(); - Log.LogResponse(resp); + if (RequestCompleted != null) + Log.LogResponse(resp); + else RequestCompleted.Invoke(this, new CardknoxEventArgs(resp)); + return new CardknoxResponse(resp); } @@ -635,10 +706,15 @@ public CardknoxResponse CCVoidRelease(CCVoidRelease _release, bool force = false _values.Add("xRefNum", _release.RefNum); // END required information - Log.LogRequest(_values); + if (RequestStarted != null) + Log.LogRequest(_values); + else RequestStarted.Invoke(this, new CardknoxEventArgs(_values)); var resp = MakeRequest(); - Log.LogResponse(resp); + if (RequestCompleted != null) + Log.LogResponse(resp); + else RequestCompleted.Invoke(this, new CardknoxEventArgs(resp)); + return new CardknoxResponse(resp); } #endregion @@ -702,10 +778,15 @@ public CardknoxResponse CheckSale(CheckSale _sale, bool force = false) AddSpecialFields(_sale); - Log.LogRequest(_values); + if (RequestStarted != null) + Log.LogRequest(_values); + else RequestStarted.Invoke(this, new CardknoxEventArgs(_values)); var resp = MakeRequest(); - Log.LogResponse(resp); + if (RequestCompleted != null) + Log.LogResponse(resp); + else RequestCompleted.Invoke(this, new CardknoxEventArgs(resp)); + return new CardknoxResponse(resp); } @@ -766,10 +847,15 @@ public CardknoxResponse CheckCredit(CheckCredit _credit, bool force = false) AddSpecialFields(_credit); - Log.LogRequest(_values); + if (RequestStarted != null) + Log.LogRequest(_values); + else RequestStarted.Invoke(this, new CardknoxEventArgs(_values)); var resp = MakeRequest(); - Log.LogResponse(resp); + if (RequestCompleted != null) + Log.LogResponse(resp); + else RequestCompleted.Invoke(this, new CardknoxEventArgs(resp)); + return new CardknoxResponse(resp); } @@ -818,10 +904,15 @@ public CardknoxResponse CheckSave(CheckSave _save, bool force = false) if (!IsNullOrWhiteSpace(_save.IP)) _values.Add("xIP", _save.IP); - Log.LogRequest(_values); + if (RequestStarted != null) + Log.LogRequest(_values); + else RequestStarted.Invoke(this, new CardknoxEventArgs(_values)); var resp = MakeRequest(); - Log.LogResponse(resp); + if (RequestCompleted != null) + Log.LogResponse(resp); + else RequestCompleted.Invoke(this, new CardknoxEventArgs(resp)); + return new CardknoxResponse(resp); } @@ -856,10 +947,15 @@ public CardknoxResponse CheckVoid(CheckVoid _void, bool force = false) _values.Add("xRefNum", _void.RefNum); // END required information - Log.LogRequest(_values); + if (RequestStarted != null) + Log.LogRequest(_values); + else RequestStarted.Invoke(this, new CardknoxEventArgs(_values)); var resp = MakeRequest(); - Log.LogResponse(resp); + if (RequestCompleted != null) + Log.LogResponse(resp); + else RequestCompleted.Invoke(this, new CardknoxEventArgs(resp)); + return new CardknoxResponse(resp); } @@ -894,10 +990,15 @@ public CardknoxResponse CheckRefund(CheckRefund _refund, bool force = false) _values.Add("xRefNum", _refund.RefNum); // END required information - Log.LogRequest(_values); + if (RequestStarted != null) + Log.LogRequest(_values); + else RequestStarted.Invoke(this, new CardknoxEventArgs(_values)); var resp = MakeRequest(); - Log.LogResponse(resp); + if (RequestCompleted != null) + Log.LogResponse(resp); + else RequestCompleted.Invoke(this, new CardknoxEventArgs(resp)); + return new CardknoxResponse(resp); } #endregion @@ -965,10 +1066,15 @@ public CardknoxResponse EBTFSSale(EBTFSSale _sale, bool force = false) AddSpecialFields(_sale); - Log.LogRequest(_values); + if (RequestStarted != null) + Log.LogRequest(_values); + else RequestStarted.Invoke(this, new CardknoxEventArgs(_values)); var resp = MakeRequest(); - Log.LogResponse(resp); + if (RequestCompleted != null) + Log.LogResponse(resp); + else RequestCompleted.Invoke(this, new CardknoxEventArgs(resp)); + return new CardknoxResponse(resp); } /// @@ -1033,10 +1139,15 @@ public CardknoxResponse EBTFSCredit(EBTFSCredit _credit, bool force = false) AddSpecialFields(_credit); - Log.LogRequest(_values); + if (RequestStarted != null) + Log.LogRequest(_values); + else RequestStarted.Invoke(this, new CardknoxEventArgs(_values)); var resp = MakeRequest(); - Log.LogResponse(resp); + if (RequestCompleted != null) + Log.LogResponse(resp); + else RequestCompleted.Invoke(this, new CardknoxEventArgs(resp)); + return new CardknoxResponse(resp); } /// @@ -1088,10 +1199,15 @@ public CardknoxResponse EBTFSBalance(EBTFSBalance _bal, bool force = false) if (!IsNullOrWhiteSpace(_bal.IP)) _values.Add("xIP", _bal.IP); - Log.LogRequest(_values); + if (RequestStarted != null) + Log.LogRequest(_values); + else RequestStarted.Invoke(this, new CardknoxEventArgs(_values)); var resp = MakeRequest(); - Log.LogResponse(resp); + if (RequestCompleted != null) + Log.LogResponse(resp); + else RequestCompleted.Invoke(this, new CardknoxEventArgs(resp)); + return new CardknoxResponse(resp); } /// @@ -1156,10 +1272,15 @@ public CardknoxResponse EBTFSVoucher(EBTFSVoucher _voucher, bool force = false) AddSpecialFields(_voucher); - Log.LogRequest(_values); + if (RequestStarted != null) + Log.LogRequest(_values); + else RequestStarted.Invoke(this, new CardknoxEventArgs(_values)); var resp = MakeRequest(); - Log.LogResponse(resp); + if (RequestCompleted != null) + Log.LogResponse(resp); + else RequestCompleted.Invoke(this, new CardknoxEventArgs(resp)); + return new CardknoxResponse(resp); } #endregion diff --git a/src/Cardknox.NET/Cardknox.csproj b/src/Cardknox.NET/Cardknox.csproj index 7ba77ae..3085f34 100644 --- a/src/Cardknox.NET/Cardknox.csproj +++ b/src/Cardknox.NET/Cardknox.csproj @@ -8,13 +8,13 @@ Cardknox.API.Wrapper false CardknoxApi - 3.0.1-beta + 3.0.2-beta https://cardknox.adamh.us/ https://github.com/ahwm/Cardknox-API-Wrapper/blob/master/LICENSE API Wrapper for Cardknox Payment Processor written in C# Refer to https://kb.cardknox.com/api for full API reference. - Added logging + Added custom logging event handlers cardknox gateway processor payment api © 2018 Adam Humpherys 3.0.0.0 diff --git a/src/Cardknox.NET/Extensions.cs b/src/Cardknox.NET/Extensions.cs new file mode 100644 index 0000000..c17e87e --- /dev/null +++ b/src/Cardknox.NET/Extensions.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace CardknoxApi +{ + /// + /// + /// + public static class Extensions + { + /// + /// + /// + /// + /// + /// + public static string MapPath(this AppDomain domain, string path) + { + if (!path.StartsWith("~/")) + return path; + + return Path.Combine(domain.BaseDirectory, path.Replace("~/", "")); + } + } +} diff --git a/src/Cardknox.NET/Log.cs b/src/Cardknox.NET/Log.cs index eae1061..fed7c70 100644 --- a/src/Cardknox.NET/Log.cs +++ b/src/Cardknox.NET/Log.cs @@ -10,7 +10,7 @@ namespace CardknoxApi { static class Log { - readonly static string[] NoInclude = { "xCardNum", "xMagStripe", "xExp", "xCVV", "xToken", "xDUKPT", "xRouting", "xAccount", "xMICR" }; + internal readonly static string[] NoInclude = { "xCardNum", "xMagStripe", "xExp", "xCVV", "xToken", "xDUKPT", "xRouting", "xAccount", "xMICR" }; static bool EnableLogging { get @@ -39,8 +39,7 @@ public static void LogRequest(NameValueCollection _values) return; string loc = String.Format(LogLocation, DateTime.Now); - if (LogLocation.StartsWith("~")) - loc = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, loc.Replace("~/", "")); + loc = AppDomain.CurrentDomain.MapPath(loc); string body = "----" + DateTime.UtcNow.ToString("s") + "----" + Environment.NewLine; body += "Cardknox Request" + Environment.NewLine; @@ -63,8 +62,7 @@ public static void LogResponse(NameValueCollection _values) return; string loc = String.Format(LogLocation, DateTime.Now); - if (LogLocation.StartsWith("~")) - loc = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, loc.Replace("~/", "")); + loc = AppDomain.CurrentDomain.MapPath(loc); string body = "----" + DateTime.UtcNow.ToString("s") + "----" + Environment.NewLine; body += "Cardknox Response" + Environment.NewLine; diff --git a/src/Cardknox.NET/Operations/Events.cs b/src/Cardknox.NET/Operations/Events.cs new file mode 100644 index 0000000..9350458 --- /dev/null +++ b/src/Cardknox.NET/Operations/Events.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Collections.Specialized; +using System.Linq; +using System.Text; + +namespace CardknoxApi.Operations +{ + /// + /// + /// + public class CardknoxEventArgs : EventArgs + { + /// + /// + /// + public ReadOnlyDictionary Results { get { return new ReadOnlyDictionary(_results); } } + private Dictionary _results; + + /// + /// + /// + /// + public CardknoxEventArgs(NameValueCollection _values) + { + _results = new Dictionary(); + foreach (var k in _values.AllKeys) + { + if (!Log.NoInclude.Contains(k)) + _results.Add(k, _values[k]); + } + } + } +}