using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Security.Cryptography;
///
/// Helper class to handle the payment integration with PayGate
///
///
public enum PaymentResults { Pending, Approved, InsufficientFunds, Declined, InvalidCard }
public class PayGate
{
public string PayGate_ID
{ get; set; }
public string PayGate_Key
{ get; set; }
public string Reference
{ get; set; }
public float Amount
{ get; set; }
public string Currency
{ get; set; }
public string Return_URL
{ get; set; }
public string UserEmail
{ get; set; }
//private static string PayGate_Server = "https://www.paygate.co.za/paywebv2/process.trans";
private static string PayGate_Server = "SendRequest.aspx";
/****************************************************************************************************/
///
/// Initializes a new instance of the class.
///
public PayGate()
{
PayGate_ID = System.Configuration.ConfigurationManager.AppSettings.Get("PayGate_ID");
PayGate_Key = System.Configuration.ConfigurationManager.AppSettings.Get("PayGate_Key");
Currency = System.Configuration.ConfigurationManager.AppSettings.Get("PayGate_Currency");
Return_URL = System.Configuration.ConfigurationManager.AppSettings.Get("PayGate_ReturnURL");
}
/****************************************************************************************************/
///
/// Initializes a new instance of the class.
///
/// The pay gate ID.
/// The pay gate encryption key.
/// The pay gate currency.
/// The pay gate return URL.
public PayGate(string PayGate_ID, string PayGate_Key, string PayGate_Currency, string PayGate_ReturnURL)
{
this.PayGate_ID = PayGate_ID;
this.PayGate_Key = PayGate_Key;
this.Currency = PayGate_Currency;
this.Return_URL = PayGate_ReturnURL;
}
/****************************************************************************************************/
///
/// Generates the PayGate MD5 checksum.
///
/// The data.
///
private string GenerateChecksum(string Data)
{
ASCIIEncoding AE = new ASCIIEncoding();
byte[] ByteSource = AE.GetBytes(Data);
MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider();
byte[] Hash = MD5.ComputeHash(ByteSource);
string Checksum = "";
for (int i = 0; i < Hash.Length; i++)
{
Checksum += Hash[i].ToString("x2").ToUpper();
}
return Checksum;
}
/****************************************************************************************************/
///
/// Starts the payment process.
///
public string StartPayment()
{
string TransDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
string TransAmnt = (Amount * 100).ToString("G0");
string ChecksumSource = PayGate_ID + "|";
ChecksumSource += Reference + "|";
ChecksumSource += TransAmnt + "|";
ChecksumSource += Currency + "|";
ChecksumSource += Return_URL + "|";
ChecksumSource += TransDate + "|";
if (!string.IsNullOrEmpty(UserEmail))
ChecksumSource += UserEmail + "|";
ChecksumSource += PayGate_Key;
//ASCIIEncoding AE = new ASCIIEncoding();
//byte[] ByteSource = AE.GetBytes(ChecksumSource);
//MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider();
//byte[] Hash = MD5.ComputeHash(ByteSource);
//string Checksum = "";
//for (int i = 0; i < Hash.Length; i++)
//{
// Checksum += Hash[i].ToString("x2").ToUpper();
//}
string Checksum = GenerateChecksum(ChecksumSource);
string PostUrl = PayGate_Server + "?";
PostUrl += "PAYGATE_ID=" + PayGate_ID;
PostUrl += "&REFERENCE=" + Reference;
PostUrl += "&AMOUNT=" + TransAmnt;
PostUrl += "&CURRENCY=" + Currency;
PostUrl += "&TRANSACTION_DATE=" + TransDate;
PostUrl += "&RETURN_URL=" + Return_URL;
if (!string.IsNullOrEmpty(UserEmail))
PostUrl += "&EMAIL=" + UserEmail;
PostUrl += "&CHECKSUM=" + Checksum;
return PostUrl;
}
/****************************************************************************************************/
///
/// Confirms the checksum result.
///
/// The rx amount.
/// The transaction_ status.
/// The result_ code.
/// The result_ desc.
/// The auth_ code.
/// The transaction_ id.
/// The risk_ indicator.
/// The checksum.
///
public bool ConfirmChecksumResult(string rxAmount, string Transaction_Status, string Result_Code, string Result_Desc, string Auth_Code, string Transaction_Id, string Risk_Indicator, string rxChecksum)
{
bool result = false;
string ChecksumSource = PayGate_ID + "|";
ChecksumSource += Reference + "|";
ChecksumSource += Transaction_Status + "|";
ChecksumSource += Result_Code + "|";
ChecksumSource += Auth_Code + "|";
ChecksumSource += rxAmount + "|";
ChecksumSource += Result_Desc + "|";
ChecksumSource += Transaction_Id + "|";
if (!string.IsNullOrEmpty(Risk_Indicator))
ChecksumSource += Risk_Indicator + "|";
ChecksumSource += PayGate_Key;
result = rxChecksum.ToUpper().Equals(GenerateChecksum(ChecksumSource));
return result;
}
/****************************************************************************************************/
public string GetChecksumResult(string rxAmount, string Transaction_Status, string Result_Code, string Result_Desc, string Auth_Code, string Transaction_Id, string Risk_Indicator, string rxChecksum)
{
string result = "";
string ChecksumSource = PayGate_ID + "|";
ChecksumSource += Reference + "|";
ChecksumSource += Transaction_Status + "|";
ChecksumSource += Result_Code + "|";
ChecksumSource += Auth_Code + "|";
ChecksumSource += rxAmount + "|";
ChecksumSource += Result_Desc + "|";
ChecksumSource += Transaction_Id + "|";
if (!string.IsNullOrEmpty(Risk_Indicator))
ChecksumSource += Risk_Indicator + "|";
ChecksumSource += PayGate_Key;
result = GenerateChecksum(ChecksumSource);
return result;
}
/****************************************************************************************************/
///
/// Gets the transaction status.
///
/// The status.
///
public string GetTransactionStatus(int Status)
{
string result = "";
switch (Status)
{
case 0:
result = "Pending";
break;
case 1:
result = "Approved";
break;
case 2:
result = "Declined";
break;
}
return result;
}
/****************************************************************************************************/
///
/// Gets the result code.
///
/// The code.
///
public string GetResultCode(int Code)
{
string result = "";
switch (Code)
{
case 0:
result = "N/A";
break;
case 990017:
result = "Approved Transaction";
break;
case 900003:
result = "Insufficient Funds";
break;
case 900007:
result = "Declined Transaction";
break;
case 900004:
result = "Invalid Card Number";
break;
case 990022:
result = "Unprocessed Transaction";
break;
}
return result;
}
/****************************************************************************************************/
///
/// Gets the risk indicator.
///
/// The status.
///
public string GetRiskIndicator(string Status)
{
string result = "";
switch (Status.ToUpper())
{
case "AX":
result = "Authenticated";
break;
case "NX":
result = "Not Authenticated";
break;
default:
result = "N/A";
break;
}
return result;
}
/****************************************************************************************************/
///
/// Return the results code as a status code.
///
/// The code.
///
public int ResultCodeAsStatus(int Code)
{
int result = 0;
switch (Code)
{
case 0:
result = (int)PaymentResults.Pending;
break;
case 990017:
result = (int)PaymentResults.Approved;
break;
case 900003:
result = (int)PaymentResults.InsufficientFunds;
break;
case 900007:
result = (int)PaymentResults.Declined;
break;
case 900004:
result = (int)PaymentResults.InvalidCard;
break;
case 990022:
result = (int)PaymentResults.Pending;
break;
}
return result;
}
/****************************************************************************************************/
}