Hello,
Appreciate some "Guru" advise, try to solve this problem.
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string VoidTransaction (string merchantcode, string cctransid, string amount, string currency, string signature)
{
string dbKey = "";
string dbMerchantId = "";
if (merchantcode == "")
{
return "Merchant Code is empty";
}
else if (cctransid == "")
{
return "Transaction ID is empty";
}
else if (amount == "")
{
return "Amount is empty";
}
else if (currency == "")
{
return "Currency is empty";
}
else if (signature == "")
{
return "Signature is empty";
}
else
{
string ConnectionString = "";
SqlConnection myConnection;
myConnection = new SqlConnection(ConnectionString);
myConnection.ConnectionString = WebConfigurationManager.AppSettings["WebSvcDb"];
myConnection.Open();
string sqlGetKey = ("SELECT MerchantKey, MerchantId from eMerchant where MerchantCode = '"+merchantcode+ "'");
SqlCommand cmdGet = new SqlCommand(sqlGetKey, myConnection);
SqlDataReader drGet = cmdGet.ExecuteReader();
if (drGet.Read())
{
dbKey = drGet["MerchantKey"].ToString();
dbMerchantId = drGet["MerchantId"].ToString();
}
drGet.Close();
myConnection.Close();
}
if (signature != "")
{
string tempAmount = "0.00";
tempAmount = amount.Replace(",", "").Replace(".", "");
string tempString = dbKey + merchantcode + cctransid + tempAmount + currency;
string tempSign = Security(tempString);
if (tempSign != signature)
{
return "Signature not match";
}
}
return CheckVoidFunction(merchantcode, cctransid, amount, currency, dbMerchantId);
}
private string CheckVoidFunction(string merchantcode, string transactionid, string amount, string currency, string MerId)
{
string ConnectionString = "";
string dbPayId = "";
string dbMiddetails = "";
string dbIdentityCode = "";
string dbAmount = "";
string dbComments = "";
string dbPaybank = "";
string dbCurrency = "";
string dbMerchantName = "";
string dbMerchantId = "";
SqlConnection myConnection;
myConnection = new SqlConnection(ConnectionString);
myConnection.ConnectionString = WebConfigurationManager.AppSettings["WebSvcDb"];
myConnection.Open();
string sqlGetTransactionDetail = ("SELECT p.PayId, p.MidDetails, p.IdentityCode, p.Amount, p.Comments, p.Paybank, p.Currency, p.MerchantId, m.MerchantName from ePayment p, eMerchant m where m.MerchantId = p.MerchantId and p.CCTransId = '" + transactionid + "'");
SqlCommand cmd = new SqlCommand(sqlGetTransactionDetail, myConnection);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
dbPayId = dr["MidDetails"].ToString();
dbMiddetails = dr["MidDetails"].ToString();
dbIdentityCode = dr["IdentityCode"].ToString();
dbAmount = dr["Amount"].ToString();
dbComments = dr["Comments"].ToString();
dbPaybank = dr["Paybank"].ToString();
dbCurrency = dr["Currency"].ToString();
dbMerchantId = dr["MerchantId"].ToString();
dbMerchantName = dr["MerchantName"].ToString();
dr.Close();
myConnection.Close();
if (decimal.Round(decimal.Parse(dbAmount.ToString()), 2).ToString() != amount)
{
return "Invalid Amount";
}
else if (dbCurrency != currency)
{
return "Invalid Currency";
}
else if (dbMerchantId != MerId)
{
return "Invalid Merchant Code";
}
else
{
if (dbPaybank == "44")
{
return PBBVoidFunction(merchantcode, transactionid, amount, currency, dbPayId, dbMiddetails, dbIdentityCode, dbAmount, dbComments, dbPaybank, dbMerchantName);
}
else
{
return "This transaction is not eligible for voiding";
}
}
}
else
{
dr.Close();
myConnection.Close();
return "Transaction not found";
}
}
private string PBBVoidFunction(string merchantcode, string transactionid, string amount, string currency, string dbPayId, string dbMiddetails, string dbIdentityCode, string dbAmount, string dbComments, string dbPaybank, string dbMerchantName)
{
string ConnectionString = "";
string BinNum = "";
string VisaMID = "";
string MasterMID = "";
string TID = "";
string LiveStatus = "";
string PadTransId = "";
string PadAmount = "";
string QSTRING = "";
string tempURL = "";
string merID = "";
string tempResult = "";
string sqlUpdateTransaction = "";
if (dbIdentityCode != "")
{
BinNum = dbIdentityCode.Substring(0, 6);
}
if (dbMiddetails != "")
{
string[] stringSeparators = new string[] { ":||:" };
string[] result = dbMiddetails.Split(stringSeparators, StringSplitOptions.None);
if (result.Length > 0)
{
VisaMID = result[0];
MasterMID = result[1];
TID = result[2];
LiveStatus = result[3];
}
}
if (BinNum.Substring(0, 1) == "4")
{
merID = VisaMID;
}
else if (BinNum.Substring(0, 1) == "5")
{
merID = MasterMID;
}
PadTransId = transactionid.PadLeft(20, '0');
PadAmount = decimal.Round(decimal.Parse(dbAmount.ToString()), 2).ToString().Replace(".", "").Replace(",", "").PadLeft(12, '0');
QSTRING = "transactionType=0400&merID=" + merID + "&invoiceNo=" + PadTransId + "&amount=" + PadAmount;
if (LiveStatus == "0")
{
tempURL = "https://uattds2.pbebank.com/PGW/Pay/Process";
}
else
{
tempURL = "https://ecom.pbebank.com/PGW/Pay/Process";
}
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)(0xc0 | 0x300 | 0xc00);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(tempURL);
request.Method = "POST";
byte[] byteArray = Encoding.UTF8.GetBytes(QSTRING);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
string PBBresponse = "";
string PBBauthCode = "";
string PNNinvoiceNo = "";
string PBBPAN = "";
string PBBExpDate = "";
string PBBAmount = "";
PBBresponse = responseFromServer.Substring(0, 2);
PBBauthCode = responseFromServer.Substring(2, 6);
PNNinvoiceNo = responseFromServer.Substring(8, 20);
PBBPAN = responseFromServer.Substring(28, 4);
PBBExpDate = responseFromServer.Substring(32, 4);
PBBAmount = responseFromServer.Substring(36, 12);
dbComments = dbComments + "iPay88:VoidTransaction|" +
"iPay88Date:" + DateTime.UtcNow.AddHours(8) + "|" +
"response:" + PBBresponse + "|" +
"authCode:" + PBBauthCode + "|" +
"invoiceNo:" + PNNinvoiceNo + "|" +
"PAN:" + PBBPAN + "|" +
"ExpDate:" + PBBExpDate + "|" +
"Amount:" + PBBAmount + "||";
if (PBBresponse == "00")
{
sqlUpdateTransaction = "Update ePayment set PayStatus = 17, Comments = '" + dbComments + "' where PayId = " + dbPayId + "";
}
else
{
sqlUpdateTransaction = "Update ePayment set Comments = '" + dbComments + "' where PayId = " + dbPayId + "";
}
tempResult = PBBresponse;
if (sqlUpdateTransaction != "")
{
SqlConnection myConnection;
myConnection = new SqlConnection(ConnectionString);
myConnection.ConnectionString = WebConfigurationManager.AppSettings["WebSvcDb"];
myConnection.Open();
SqlCommand cmdUpdate = new SqlCommand(sqlUpdateTransaction, myConnection);
cmdUpdate.ExecuteNonQuery();
myConnection.Close();
}
return tempResult;
}
Thank you.
Any pointers are grateful.
Thanks
Rickson
What I have tried:
ServicePointManager.SecurityProtocol = (SecurityProtocolType)(0xc0 | 0x300 | 0xc00);