Skip to content

Commit

Permalink
Merge pull request #43 from TheScripters/issue42/api-update
Browse files Browse the repository at this point in the history
Issue42/api update
  • Loading branch information
ahwm authored Mar 5, 2020
2 parents afdd54f + ce01034 commit 9b71141
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 3 deletions.
153 changes: 153 additions & 0 deletions src/Cardknox.NET/Cardknox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2137,6 +2137,159 @@ public CardknoxResponse GCBalance(GCBalance _bal, bool force = false)

return new CardknoxResponse(resp);
}

/// <summary>
/// The Activate command is used to activate a Cardknox gift card.
/// </summary>
/// <param name="_activate"></param>
/// <param name="force">If <see langword="true"/> allows new command to be sent by clearing previous command entries</param>
/// <returns></returns>
public CardknoxResponse GCActivate(GCActivate _activate, bool force = false)
{
if (_values.AllKeys.Length > 4 && !force)
throw new InvalidOperationException("A new instance of Cardknox 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", _activate.Operation);
bool requiredAdded = false;
// These groups are mutually exclusive
if (!IsNullOrWhiteSpace(_activate.CardNum))
{
_values.Add("xCardNum", _activate.CardNum);
if (!IsNullOrWhiteSpace(_activate.CVV))
_values.Add("xCVV", _activate.CVV);
if (!IsNullOrWhiteSpace(_activate.Exp))
_values.Add("xExp", _activate.Exp);
requiredAdded = true;

if (IsNullOrWhiteSpace(_activate.Exp))
requiredAdded = false;
}
else if (!IsNullOrWhiteSpace(_activate.Token))
{
_values.Add("xToken", _activate.Token);
requiredAdded = true;
}
else if (!IsNullOrWhiteSpace(_activate.MagStripe))
{
_values.Add("xMagStripe", _activate.MagStripe);
requiredAdded = true;
}
if (!requiredAdded)
throw new Exception($"Missing required values. Please refer to the API documentation for the {_activate.Operation} operation.");
// END required information


// IP is optional, but is highly recommended for fraud detection
if (!IsNullOrWhiteSpace(_activate.IP))
_values.Add("xIP", _activate.IP);

AddCommonFields(_activate);

int i = 1;
foreach (string v in _activate.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 CardknoxResponse(resp);
}
/// <summary>
/// The Activate command is used to activate a Cardknox gift card.
/// </summary>
/// <param name="_deactivate"></param>
/// <param name="force">If <see langword="true"/> allows new command to be sent by clearing previous command entries</param>
/// <returns></returns>
public CardknoxResponse GCDeactivate(GCDeactivate _deactivate, bool force = false)
{
if (_values.AllKeys.Length > 4 && !force)
throw new InvalidOperationException("A new instance of Cardknox 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", _deactivate.Operation);
bool requiredAdded = false;
// These groups are mutually exclusive
if (!IsNullOrWhiteSpace(_deactivate.CardNum))
{
_values.Add("xCardNum", _deactivate.CardNum);
if (!IsNullOrWhiteSpace(_deactivate.CVV))
_values.Add("xCVV", _deactivate.CVV);
if (!IsNullOrWhiteSpace(_deactivate.Exp))
_values.Add("xExp", _deactivate.Exp);
requiredAdded = true;

if (IsNullOrWhiteSpace(_deactivate.Exp))
requiredAdded = false;
}
else if (!IsNullOrWhiteSpace(_deactivate.Token))
{
_values.Add("xToken", _deactivate.Token);
requiredAdded = true;
}
else if (!IsNullOrWhiteSpace(_deactivate.MagStripe))
{
_values.Add("xMagStripe", _deactivate.MagStripe);
requiredAdded = true;
}
if (!requiredAdded)
throw new Exception($"Missing required values. Please refer to the API documentation for the {_deactivate.Operation} operation.");
// END required information


// IP is optional, but is highly recommended for fraud detection
if (!IsNullOrWhiteSpace(_deactivate.IP))
_values.Add("xIP", _deactivate.IP);

AddCommonFields(_deactivate);

int i = 1;
foreach (string v in _deactivate.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 CardknoxResponse(resp);
}
#endregion

#region fraud
Expand Down
6 changes: 3 additions & 3 deletions src/Cardknox.NET/Cardknox.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
<Product>Cardknox.API.Wrapper</Product>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<RootNamespace>CardknoxApi</RootNamespace>
<Version>4.5.3</Version>
<Version>4.6</Version>
<PackageProjectUrl>https://github.com/TheScripters/Cardknox-API-Wrapper</PackageProjectUrl>
<Description>API Wrapper for Cardknox Payment Processor written in C#

Refer to https://kb.cardknox.com/api for full API reference or https://github.com/TheScripters/Cardknox-API-Wrapper/wiki/ for library reference</Description>
<PackageReleaseNotes>Fixed bug in response parser</PackageReleaseNotes>
<PackageReleaseNotes>Updated API version to 4.5.8, added Gift Card Activate/Deactivate methods</PackageReleaseNotes>
<PackageTags>cardknox gateway processor payment api ebt mastercard payments american express amex jcb diners club giftcard gift card discover</PackageTags>
<Copyright>© 2020 The Scripters</Copyright>
<AssemblyVersion>4.5.3.0</AssemblyVersion>
<AssemblyVersion>4.6.0.0</AssemblyVersion>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
</PropertyGroup>

Expand Down
14 changes: 14 additions & 0 deletions src/Cardknox.NET/Operations/GiftCard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,18 @@ public class GCBalance : Sale
{
internal string Operation => "gift:balance";
}
/// <summary>
/// The Balance command is used to check the available balance on a Cardknox gift card.
/// </summary>
public class GCActivate : Sale
{
internal string Operation => "gift:activate";
}
/// <summary>
/// The Balance command is used to check the available balance on a Cardknox gift card.
/// </summary>
public class GCDeactivate : Sale
{
internal string Operation => "gift:deactivate";
}
}

0 comments on commit 9b71141

Please sign in to comment.