Skip to content

Commit

Permalink
added Payment Method operations, fixed .NET 5 targeting (with fix for…
Browse files Browse the repository at this point in the history
… CodeQL)
  • Loading branch information
ahwm committed Dec 15, 2020
1 parent fe9e134 commit ffa2ebb
Show file tree
Hide file tree
Showing 4 changed files with 378 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Cardknox.NET/Cardknox.csproj
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp2.1;netcoreapp3.1;netstandard2.0;net45;net50</TargetFrameworks>
<TargetFrameworks>netcoreapp2.1;netcoreapp3.1;netstandard2.0;net45;net5.0</TargetFrameworks>
<Authors>TheScripters</Authors>
<Company>The Scripters</Company>
<PackageId>Cardknox.API.Wrapper</PackageId>
<Product>Cardknox.API.Wrapper</Product>
<MinClientVersion>4.3.1</MinClientVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<RootNamespace>CardknoxApi</RootNamespace>
<Version>5.0-beta.3</Version>
<Version>5.0-beta.4</Version>
<PackageProjectUrl>https://github.com/TheScripters/Cardknox-API-Wrapper</PackageProjectUrl>
<Description>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</Description>
<PackageReleaseNotes>Customer-based operations are now completed against the Recurring v1 API</PackageReleaseNotes>
Expand Down
239 changes: 239 additions & 0 deletions src/Cardknox.NET/RecurringClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,245 @@ public RecurringResponse CustomerTransaction(CustomerTransaction _cust, bool for
}
#endregion

#region PaymentMethod

/// <summary>
/// Use this command to add a new payment method to a customer's account profile.
/// </summary>
/// <param name="_pmt"></param>
/// <param name="force"></param>
/// <returns></returns>
public RecurringResponse PaymentMethodAdd(PaymentMethodAdd _pmt, 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", _pmt.Operation);
Values.Add("xCustomerID", _pmt.CustomerID);
Values.Add("xToken", _pmt.Token);
Values.Add("xTokenType", _pmt.TokenType);
// END required information

if (!String.IsNullOrWhiteSpace(_pmt.TokenAlias))
Values.Add("xTokenAlias", _pmt.TokenAlias);

// CONDITIONALLY REQUIRED
if (!String.IsNullOrWhiteSpace(_pmt.Exp))
Values.Add("xExp", _pmt.Exp);
if (!String.IsNullOrWhiteSpace(_pmt.Routing))
Values.Add("xRouting", _pmt.Routing);
if (!String.IsNullOrWhiteSpace(_pmt.Name))
Values.Add("xName", _pmt.Name);

if (!String.IsNullOrWhiteSpace(_pmt.Street))
Values.Add("xStreet", _pmt.Street);
if (!String.IsNullOrWhiteSpace(_pmt.Zip))
Values.Add("xZip", _pmt.Zip);
if (_pmt.SetToDefault)
Values.Add("xSetToDefault", _pmt.SetToDefault.ToString());

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);
}

/// <summary>
/// <para>Use this command to update an existing payment method.</para>
/// <para>Note: All fields with values must be passed in (even the fields that are not being updated). Any fields not passed in are treated as being set to blank.</para>
/// </summary>
/// <param name="_pmt"></param>
/// <param name="force"></param>
/// <returns></returns>
public RecurringResponse PaymentMethodUpdate(PaymentMethodUpdate _pmt, 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", _pmt.Operation);
Values.Add("xCustomerID", _pmt.CustomerID);
Values.Add("xToken", _pmt.Token);
Values.Add("xTokenType", _pmt.TokenType);
// END required information

if (!String.IsNullOrWhiteSpace(_pmt.TokenAlias))
Values.Add("xTokenAlias", _pmt.TokenAlias);

// CONDITIONALLY REQUIRED
if (!String.IsNullOrWhiteSpace(_pmt.Exp))
Values.Add("xExp", _pmt.Exp);
if (!String.IsNullOrWhiteSpace(_pmt.Routing))
Values.Add("xRouting", _pmt.Routing);
if (!String.IsNullOrWhiteSpace(_pmt.Name))
Values.Add("xName", _pmt.Name);

if (!String.IsNullOrWhiteSpace(_pmt.Street))
Values.Add("xStreet", _pmt.Street);
if (!String.IsNullOrWhiteSpace(_pmt.Zip))
Values.Add("xZip", _pmt.Zip);
if (_pmt.SetToDefault)
Values.Add("xSetToDefault", _pmt.SetToDefault.ToString());

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);
}

/// <summary>
/// <para>Use this command to remove a payment method.</para>
/// <para>Note: The payment method is not completely deleted from the database; instead, the removed property is set to true. Customers with active schedules cannot be removed.</para>
/// </summary>
/// <param name="_pmt"></param>
/// <param name="force"></param>
/// <returns></returns>
public RecurringResponse PaymentMethodRemove(PaymentMethodRemove _pmt, 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", _pmt.Operation);
Values.Add("xPaymentMethodID", _pmt.PaymentMethodID);

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);
}

/// <summary>
/// Use this command to retrieve details of a payment method.
/// </summary>
/// <param name="_pmt"></param>
/// <param name="force"></param>
/// <returns></returns>
public RecurringResponse PaymentMethodGet(PaymentMethodGet _pmt, 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", _pmt.Operation);
Values.Add("xPaymentMethodID", _pmt.PaymentMethodID);
Values.Add("xRemoved", _pmt.Removed.ToString());

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);
}

/// <summary>
/// Use this command to find payment methods using specific search parameters.
/// </summary>
/// <param name="_pmt"></param>
/// <param name="force"></param>
/// <returns></returns>
public RecurringResponse PaymentMethodFind(PaymentMethodFind _pmt, 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", _pmt.Operation);

if (!String.IsNullOrWhiteSpace(_pmt.CustomerID))
Values.Add("xCustomerID", _pmt.CustomerID);
if (!String.IsNullOrWhiteSpace(_pmt.Name))
Values.Add("xName", _pmt.Name);
Values.Add("xRemoved", _pmt.Removed.ToString());

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
private NameValueCollection MakeRequest()
{
Expand Down
131 changes: 131 additions & 0 deletions src/Cardknox.NET/RecurringOperations/PaymentMethod.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace CardknoxApi.RecurringOperations
{
/// <summary>
/// Use this command to add a new payment method to a customer's account profile.
/// </summary>
public class PaymentMethodAdd
{
internal string Operation => "paymentmethod:add";

/// <summary>
/// Customer's unique ID number
/// </summary>
public string CustomerID { get; set; }
/// <summary>
/// Cardknox token that references a previously used payment method to use for the charge
/// </summary>
public string Token { get; set; }
/// <summary>
/// <para>The xToken payment type</para>
/// <para>Valid Values:</para>
/// <list type="bullet">
/// <item>CC (Credit Card)</item>
/// <item>Check (Check)</item>
/// <item>Ach</item>
/// </list>
/// </summary>
public string TokenType { get; set; }
/// <summary>
/// Custom name for the xToken
/// </summary>
public string TokenAlias { get; set; }
/// <summary>
/// Credit card expiration date
/// </summary>
public string Exp { get; set; }
/// <summary>
/// ACH payment routing number
/// </summary>
public string Routing { get; set; }
/// <summary>
/// Name on the customer's account
/// </summary>
public string Name { get; set; }
/// <summary>
/// The billing street address of the cardholder
/// </summary>
public string Street { get; set; }
/// <summary>
/// The billing zip code of the cardholder
/// </summary>
public string Zip { get; set; }
/// <summary>
/// <para>Sets this payment as the default payment method</para>
/// <para>Note: If there are no other payment methods, this method is automatically set as the default</para>
/// </summary>
public bool SetToDefault { get; set; }
}

/// <summary>
/// <para>Use this command to update an existing payment method.</para>
/// <para>Note: All fields with values must be passed in (even the fields that are not being updated). Any fields not passed in are treated as being set to blank.</para>
/// </summary>
public class PaymentMethodUpdate : PaymentMethodAdd
{
internal new string Operation => "paymentmethod:update";

/// <summary>
/// Payment method's unique ID
/// </summary>
public string PaymentMethodID { get; set; }
}

/// <summary>
/// <para>Use this command to remove a payment method.</para>
/// <para>Note: The payment method is not completely deleted from the database; instead, the removed property is set to true. Customers with active schedules cannot be removed.</para>
/// </summary>
public class PaymentMethodRemove
{
internal string Operation => "paymentmethod:remove";

/// <summary>
/// Payment method's unique ID
/// </summary>
public string PaymentMethodID { get; set; }
}

/// <summary>
/// Use this command to retrieve details of a payment method.
/// </summary>
public class PaymentMethodGet
{
internal string Operation => "report:paymentmethod";

/// <summary>
/// Payment method's unique ID
/// </summary>
public string PaymentMethodID { get; set; }

/// <summary>
/// Indicates whether to return deleted rows.
/// </summary>
public bool Removed { get; set; } = false;
}

/// <summary>
/// Use this command to find payment methods using specific search parameters.
/// </summary>
public class PaymentMethodFind
{
internal string Operation => "report:paymentmethods";

/// <summary>
/// Customer's unique ID number
/// </summary>
public string CustomerID { get; set; }

/// <summary>
/// Name on the customer's account
/// </summary>
public string Name { get; set; }

/// <summary>
/// Indicates whether to return deleted rows.
/// </summary>
public bool Removed { get; set; } = false;
}
}
Loading

0 comments on commit ffa2ebb

Please sign in to comment.