From 58751ae7be9327b92222b2e206e25f62b242bb04 Mon Sep 17 00:00:00 2001 From: Adam Humpherys Date: Thu, 15 Oct 2020 16:35:38 -0600 Subject: [PATCH] completed customer commands for recurring v1 API --- src/Cardknox.NET/Cardknox.csproj | 2 +- src/Cardknox.NET/RecurringClient.cs | 100 +++++++++++++++++- .../RecurringOperations/Customer.cs | 32 ++++++ 3 files changed, 132 insertions(+), 2 deletions(-) diff --git a/src/Cardknox.NET/Cardknox.csproj b/src/Cardknox.NET/Cardknox.csproj index 98c1ccc..6c73ef5 100644 --- a/src/Cardknox.NET/Cardknox.csproj +++ b/src/Cardknox.NET/Cardknox.csproj @@ -9,7 +9,7 @@ 4.3.1 true CardknoxApi - 5.0-beta.2 + 5.0-beta.3 https://github.com/TheScripters/Cardknox-API-Wrapper API Wrapper for Cardknox Payment Processor written in C# with support for payments, recurring transactions, and reporting. Refer to https://kb.cardknox.com for full API reference or https://github.com/TheScripters/Cardknox-API-Wrapper/wiki/ for library reference Adding initial operations for adding managing customers. Renamed the Cardknox class object to CardknoxClient diff --git a/src/Cardknox.NET/RecurringClient.cs b/src/Cardknox.NET/RecurringClient.cs index f5c1ce2..92a943f 100644 --- a/src/Cardknox.NET/RecurringClient.cs +++ b/src/Cardknox.NET/RecurringClient.cs @@ -11,7 +11,7 @@ namespace CardknoxApi { /// - /// Primary object class for interacting with the Cardknox transaction API. + /// Primary object class for interacting with the Cardknox Recurring v1 API. /// public class RecurringClient : IDisposable { @@ -278,6 +278,104 @@ public RecurringResponse CustomerFind(CustomerFind _cust, bool force = false) return new RecurringResponse(resp); } + + /// + /// + /// + /// + /// + /// + public RecurringResponse CustomerTransaction(CustomerTransaction _cust, bool force = false) + { + if (Values.AllKeys.Length > 4 && !force) + throw new InvalidOperationException("A new instance of Recurring is required to perform this operation unless 'force' is set to 'true'."); + else if (force) + { + string[] toRemove = Values.AllKeys; + foreach (var v in toRemove) + Values.Remove(v); + Values.Add("xKey", Request.Key); + Values.Add("xVersion", Request.CardknoxVersion); + Values.Add("xSoftwareName", Request.Software); + Values.Add("xSoftwareVersion", Request.SoftwareVersion); + } + + // BEGIN required information + Values.Add("xCommand", _cust.Operation); + + if (IsNullOrWhiteSpace(_cust.CustomerID) && IsNullOrWhiteSpace(_cust.PaymentMethodID)) + throw new InvalidOperationException("xPaymentMethodID is required when xCustomerID is not specified."); + + if (!IsNullOrWhiteSpace(_cust.CustomerID)) + Values.Add("xCustomerID", _cust.PaymentMethodID); + if (!IsNullOrWhiteSpace(_cust.CustomerID)) + Values.Add("xPaymentMethodID", _cust.PaymentMethodID); + Values.Add("xAmount", String.Format("{0:N2}", _cust.Amount)); + Values.Add("xUseBackupPaymentMethods", _cust.UseBackupPaymentMethods.ToString()); + if (!IsNullOrWhiteSpace(_cust.Token) && IsNullOrWhiteSpace(_cust.PaymentMethodID)) + Values.Add("xToken", _cust.Token); + if (!IsNullOrWhiteSpace(_cust.TokenType)) + Values.Add("xTokenType", _cust.TokenType); + if (!IsNullOrWhiteSpace(_cust.TokenType)) + Values.Add("xTokenType", _cust.TokenType); + if (!IsNullOrWhiteSpace(_cust.Name)) + Values.Add("xName", _cust.Name); + if (!IsNullOrWhiteSpace(_cust.Street)) + Values.Add("xStreet", _cust.Street); + if (!IsNullOrWhiteSpace(_cust.Zip)) + Values.Add("xZip", _cust.Zip); + if (!IsNullOrWhiteSpace(_cust.Description)) + Values.Add("xDescription", _cust.Description); + if (!IsNullOrWhiteSpace(_cust.Invoice)) + Values.Add("xInvoice", _cust.Invoice); + if (!IsNullOrWhiteSpace(_cust.PONum)) + Values.Add("xPONum", _cust.PONum); + + if (!IsNullOrWhiteSpace(_cust.ShipFirstName)) + Values.Add("xShipFirstName", _cust.ShipFirstName); + if (!IsNullOrWhiteSpace(_cust.ShipMiddleName)) + Values.Add("xShipMiddleName", _cust.ShipMiddleName); + if (!IsNullOrWhiteSpace(_cust.ShipLastName)) + Values.Add("xShipLastName", _cust.ShipLastName); + if (!IsNullOrWhiteSpace(_cust.ShipCompany)) + Values.Add("xShipCompany", _cust.ShipCompany); + if (!IsNullOrWhiteSpace(_cust.ShipStreet)) + Values.Add("xShipStreet", _cust.ShipStreet); + if (!IsNullOrWhiteSpace(_cust.ShipStreet2)) + Values.Add("xShipStreet2", _cust.ShipStreet2); + if (!IsNullOrWhiteSpace(_cust.ShipCity)) + Values.Add("xShipCity", _cust.ShipCity); + if (!IsNullOrWhiteSpace(_cust.ShipState)) + Values.Add("xShipState", _cust.ShipState); + if (!IsNullOrWhiteSpace(_cust.ShipZip)) + Values.Add("xShipZip", _cust.ShipZip); + if (!IsNullOrWhiteSpace(_cust.ShipCountry)) + Values.Add("xShipCountry", _cust.ShipCountry); + if (!IsNullOrWhiteSpace(_cust.ShipPhone)) + Values.Add("xShipPhone", _cust.ShipPhone); + if (!IsNullOrWhiteSpace(_cust.ShipMobile)) + Values.Add("xShipMobile", _cust.ShipMobile); + if (!IsNullOrWhiteSpace(_cust.ShipEmail)) + Values.Add("xShipEmail", _cust.ShipEmail); + + int i = 1; + foreach (string v in _cust.CustomFields) + { + Values.Add($"xCustom{i:D2}", v); + i++; + } + + if (RequestStarted == null) + Log.LogRequest(Values); + else RequestStarted.Invoke(this, new CardknoxEventArgs(Values)); + + var resp = MakeRequest(); + if (RequestCompleted == null) + Log.LogResponse(resp); + else RequestCompleted.Invoke(this, new CardknoxEventArgs(resp)); + + return new RecurringResponse(resp); + } #endregion #region private methods diff --git a/src/Cardknox.NET/RecurringOperations/Customer.cs b/src/Cardknox.NET/RecurringOperations/Customer.cs index 36063dd..4f514a3 100644 --- a/src/Cardknox.NET/RecurringOperations/Customer.cs +++ b/src/Cardknox.NET/RecurringOperations/Customer.cs @@ -70,5 +70,37 @@ public class CustomerTransaction : OperationBase /// Note: This is not allowed if using a new payment method that has not been saved. /// public bool UseBackupPaymentMethods { get; set; } + + /// + /// Cardknox token that references a previously used payment method to use for the charge (if not using a payment method that has been saved with the Recurring API) + /// + public new string Token { get; set; } + + /// + /// The xToken payment type (Valid values: "CC" or "Check") + /// * Required if the xToken is passed in. + /// + public string TokenType { get; set; } + + /// + /// Name on the customer's account + /// * Required for ACH (check) transactions if the xToken is passed in. + /// + public new string Name { get; set; } + + /// + /// Additional data optionally passed along for reporting. + /// + public string Description { get; set; } + + /// + /// Customer's purchase order number for the transaction. + /// + public string PONum { get; set; } + + /// + /// Customer's shipping email address for their shipping profile + /// + public string ShipEmail { get; set; } } }