Emitra Payment Gateway Integration in ASP.NET Core/MVC5/ASP.NET Web forms
In this tutorial we will see that how we can integrate E Mitra payment gateway in ASP.NET aur in ASP.NET
First of all you need to contact emitra office to get a new merchant ID along with integration document. In most cases they also provide testing URL and dummy credit card details for testing purpose.
We need to sign up up an agreement document where we need to share our bank details in which we want payments. Now we need to come on technical part of payment gateway integration.
Emitra fishes also provide encryption and decryption algorithm document for integration.
But don’t worry I am also sharing with you this encryption decryption algorithm code which is developed in C# only.
using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Web; using System.Web.Script.Serialization; using System.Collections; using System.Data.SqlClient; using System.Data; using System.Configuration; public class PaymentHelper { const string MERCHANTCODE = "YourMerchantCode"; const string CHECKSUMKEY = "SharedbyEmitratoYou"; const string SUCCESSURL = "http://yourwebsiteURL/Home/paymentsuccess.aspx"; const string FAILUREURL = "http://yourwebsiteURL/Home/paymentsuccess.aspx"; const string CANCELURL = "http://yourwebsiteURL/Home/paymentsuccess.aspx"; const string RPPURL = "https://rpp.rajasthan.gov.in/payments/v1/init"; const string RPPURL_TransVeri = "https://rpp.rajasthan.gov.in/payments/v1/services/txnStatus.json"; public static bool UpdateSuccessPayment(string PRN, string REQTIMESTAMP, decimal AMOUNT, string RPPTXNID, string RPPTIMESTAMP, decimal PAYMENTAMOUNT, string STATUS, string PAYMENTMODE, string PAYMENTMODEBID, string PAYMENTMODETIMESTAMP, string RESPONSECODE, string RESPONSEMESSAGE, string UserId, string User_IP, string ProjectId, string CHECKSUM) { try { SqlConnection SqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["CSR_Portal"].ToString()); DataTable dt = new DataTable(); SqlCommand cmd = new SqlCommand(); cmd.Parameters.Clear(); cmd.CommandText = "spcsr_AddUpdatePaymentTransaction"; cmd.CommandType = CommandType.StoredProcedure; cmd.Connection = SqlCon; cmd.Parameters.Add("@PRN", SqlDbType.VarChar).Value = PRN; cmd.Parameters.Add("@REQTIMESTAMP", SqlDbType.VarChar).Value = REQTIMESTAMP; cmd.Parameters.Add("@PURPOSE", SqlDbType.VarChar).Value = ""; cmd.Parameters.Add("@AMOUNT", SqlDbType.Decimal).Value = AMOUNT; cmd.Parameters.Add("@RPPTXNID", SqlDbType.VarChar).Value = RPPTXNID; cmd.Parameters.Add("@RPPTIMESTAMP", SqlDbType.VarChar).Value = RPPTIMESTAMP; cmd.Parameters.Add("@PAYMENTAMOUNT", SqlDbType.Decimal).Value = PAYMENTAMOUNT; cmd.Parameters.Add("@STATUS", SqlDbType.VarChar).Value = STATUS; cmd.Parameters.Add("@PAYMENTMODE", SqlDbType.VarChar).Value = PAYMENTMODE; cmd.Parameters.Add("@PAYMENTMODEBID", SqlDbType.VarChar).Value = PAYMENTMODEBID; cmd.Parameters.Add("@PAYMENTMODETIMESTAMP", SqlDbType.VarChar).Value = PAYMENTMODETIMESTAMP; cmd.Parameters.Add("@RESPONSECODE", SqlDbType.VarChar).Value = RESPONSECODE; cmd.Parameters.Add("@RESPONSEMESSAGE", SqlDbType.VarChar).Value = RESPONSEMESSAGE; cmd.Parameters.Add("@USERNAME", SqlDbType.VarChar).Value = ""; cmd.Parameters.Add("@USERMOBILE", SqlDbType.VarChar).Value = ""; cmd.Parameters.Add("@USEREMAIL", SqlDbType.VarChar).Value = ""; cmd.Parameters.Add("@UserId", SqlDbType.VarChar).Value = UserId; cmd.Parameters.Add("@User_IP", SqlDbType.VarChar).Value = User_IP; cmd.Parameters.Add("@ProjectId", SqlDbType.VarChar).Value = ProjectId; cmd.Parameters.Add("@OFFICECODE", SqlDbType.VarChar).Value = ""; cmd.Parameters.Add("@REVENUEHEAD", SqlDbType.VarChar).Value = ""; cmd.Parameters.Add("@CHECKSUM", SqlDbType.VarChar).Value = ""; if (SqlCon.State == ConnectionState.Closed) SqlCon.Open(); cmd.ExecuteNonQuery(); SqlCon.Close(); return true; } catch (Exception ex) { return false; } } public static bool InsertRequest(string PRN, string MERCHANTCODE, string REQTIMESTAMP, string PURPOSE, string AmountForm, string USERNAME, string USERMOBILE, string USEREMAIL, string UserId, string UserIp, string ProjectId, string CHECKSUM, string SCHCD, string Frequency, string Remark) { try { SqlConnection SqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["CSR_Portal"].ToString()); DataTable dt = new DataTable(); SqlCommand cmd = new SqlCommand(); cmd.Parameters.Clear(); cmd.CommandText = "spcsr_AddUpdatePaymentTransaction"; cmd.CommandType = CommandType.StoredProcedure; cmd.Connection = SqlCon; cmd.Parameters.Add("@PRN", SqlDbType.VarChar).Value = PRN; cmd.Parameters.Add("@REQTIMESTAMP", SqlDbType.VarChar).Value = REQTIMESTAMP; cmd.Parameters.Add("@PURPOSE", SqlDbType.VarChar).Value = PURPOSE; cmd.Parameters.Add("@AMOUNT", SqlDbType.Decimal).Value = AmountForm; cmd.Parameters.Add("@RPPTXNID", SqlDbType.VarChar).Value = "";// RPPTXNID; cmd.Parameters.Add("@RPPTIMESTAMP", SqlDbType.VarChar).Value = "";// RPPTIMESTAMP; cmd.Parameters.Add("@PAYMENTAMOUNT", SqlDbType.Decimal).Value = 0;//PAYMENTAMOUNT; cmd.Parameters.Add("@STATUS", SqlDbType.VarChar).Value = "";//STATUS; cmd.Parameters.Add("@PAYMENTMODE", SqlDbType.VarChar).Value = "";//PAYMENTMODE; cmd.Parameters.Add("@PAYMENTMODEBID", SqlDbType.VarChar).Value = "";//PAYMENTMODEBID; cmd.Parameters.Add("@PAYMENTMODETIMESTAMP", SqlDbType.VarChar).Value = "";//PAYMENTMODETIMESTAMP; cmd.Parameters.Add("@RESPONSECODE", SqlDbType.VarChar).Value = "";//RESPONSECODE; cmd.Parameters.Add("@RESPONSEMESSAGE", SqlDbType.VarChar).Value = "";//RESPONSEMESSAGE; cmd.Parameters.Add("@USERNAME", SqlDbType.VarChar).Value = USERNAME; cmd.Parameters.Add("@USERMOBILE", SqlDbType.VarChar).Value = USERMOBILE; cmd.Parameters.Add("@USEREMAIL", SqlDbType.VarChar).Value = USEREMAIL; cmd.Parameters.Add("@UserId", SqlDbType.VarChar).Value = UserId; cmd.Parameters.Add("@User_IP", SqlDbType.VarChar).Value = UserIp; cmd.Parameters.Add("@ProjectId", SqlDbType.VarChar).Value = ProjectId; cmd.Parameters.Add("@OFFICECODE", SqlDbType.VarChar).Value = ""; cmd.Parameters.Add("@REVENUEHEAD", SqlDbType.VarChar).Value = ""; cmd.Parameters.Add("@CHECKSUM", SqlDbType.VarChar).Value = CHECKSUM; cmd.Parameters.Add("@SCHCD", SqlDbType.VarChar).Value = SCHCD; cmd.Parameters.Add("@Frequency", SqlDbType.VarChar).Value = Frequency; cmd.Parameters.Add("@Remark", SqlDbType.VarChar).Value = Remark; if (SqlCon.State == ConnectionState.Closed) SqlCon.Open(); cmd.ExecuteNonQuery(); SqlCon.Close(); return true; } catch (Exception ex) { return false; } } public static string SendRequest(decimal AMOUNT, string PURPOSE, string USERNAME, string USERMOBILE, string USEREMAIL, string UserId, string UserIp, string ProjectId, string SCHCD, string Frequency, string Remark) { Random rnd = new Random(); string PRN = rnd.Next(100000, 999999999).ToString(); string REQTIMESTAMP = DateTime.Now.ToString("yyyyMMddHHmmssfff"); string AmountForm = AMOUNT.ToString("g29"); string CHECKSUM = sha256_hash(MERCHANTCODE + "|" + PRN + "|" + AmountForm + "|" + CHECKSUMKEY); Hashtable data = new Hashtable(); data.Add("MERCHANTCODE", MERCHANTCODE); data.Add("PRN", PRN); data.Add("REQTIMESTAMP", REQTIMESTAMP); data.Add("PURPOSE", PURPOSE); data.Add("AMOUNT", AmountForm); data.Add("SUCCESSURL", SUCCESSURL); data.Add("FAILUREURL", FAILUREURL); data.Add("CANCELURL", CANCELURL); data.Add("USERNAME", USERNAME); data.Add("USERMOBILE", USERMOBILE); data.Add("USEREMAIL", USEREMAIL); data.Add("UDF1", UserId); data.Add("UDF2", UserIp); data.Add("UDF3", ProjectId); data.Add("OFFICECODE", "rmsa"); data.Add("REVENUEHEAD", "rmsa123"); data.Add("CHECKSUM", CHECKSUM); InsertRequest(PRN, MERCHANTCODE, REQTIMESTAMP, PURPOSE, AmountForm, USERNAME, USERMOBILE, USEREMAIL, UserId, UserIp, ProjectId, CHECKSUM, SCHCD, Frequency, Remark); string strForm = PreparePOSTForm(RPPURL, data); return strForm; } private static string PreparePOSTForm(string url, System.Collections.Hashtable data) // post form { //Set a name for the form string formID = "form1"; //Build the form using the specified data to be posted. StringBuilder strForm = new StringBuilder(); strForm.Append("<form id=\"" + formID + "\" name=\"" + formID + "\" action=\"" + url + "\" method=\"POST\">"); strForm.AppendFormat("<div style='float:left; width:100%; height:100%;'>"); strForm.AppendFormat("<div style='float:left; width:100%; height:100%; margin-top:10%;'> "); strForm.AppendFormat("<div style='float:left; width:100%; text-align:center; font-size:30px; color:#525252; margin:0 0 50px 0;'>Please wait while you are being redirected to <span style='font-weight:bold;'>RPP</span> Application.</div>"); strForm.AppendFormat("<div style='float:left; width:100%; text-align:center;'>"); strForm.AppendFormat("<img src='../Style_New/images/loading.gif' width='250px'/>"); strForm.AppendFormat("</div>"); foreach (System.Collections.DictionaryEntry key in data) { strForm.Append("<input type=\"hidden\" name=\"" + key.Key + "\" value=\"" + key.Value + "\">"); } strForm.AppendFormat("</div>"); strForm.AppendFormat("</div>"); strForm.Append("</form>"); //Build the JavaScript which will do the Posting operation. StringBuilder strScript = new StringBuilder(); strScript.Append("<script language='javascript'>"); strScript.Append("var v" + formID + " = document." + formID + ";"); strScript.Append("v" + formID + ".submit();"); strScript.Append("</script>"); //Return the form and the script concatenated. //(The order is important, Form then JavaScript) return strForm.ToString() + strScript.ToString(); } public static PaymentResponse GetResponse(string STATUS, string ENCDATA) { JavaScriptSerializer serializer = new JavaScriptSerializer(); //string RESPONSEJSON = AESDecrypt(ENCDATA); ResponseParameters RESPONSEPARAMS = serializer.Deserialize<ResponseParameters>(ENCDATA); string CHECKSUM = MD5HASHING(MERCHANTCODE + "|" + RESPONSEPARAMS.PRN + "|" + RESPONSEPARAMS.RPPTXNID + "|" + RESPONSEPARAMS.PAYMENTAMOUNT + "|" + CHECKSUMKEY); PaymentResponse PAYMENTRESPONSE = new PaymentResponse(); if (CHECKSUM == RESPONSEPARAMS.CHECKSUM.ToUpper()) { PAYMENTRESPONSE = new PaymentResponse { RESPONSEJSON = ENCDATA, ENCDATA = ENCDATA, RESPONSEPARAMETERS = RESPONSEPARAMS, STATUS = STATUS, CHECKSUMVALID = true }; } else { PAYMENTRESPONSE = new PaymentResponse { RESPONSEJSON = ENCDATA, ENCDATA = ENCDATA, RESPONSEPARAMETERS = RESPONSEPARAMS, STATUS = STATUS, CHECKSUMVALID = false }; } return PAYMENTRESPONSE; } public static string posttopage(string ENCDATA) { StringBuilder sb = new StringBuilder(); sb.Append("<html>"); sb.AppendFormat(@"<body style='background-color:#F0F0F0;' onload='document.forms[""form""].submit()'>"); sb.AppendFormat("<form name='form' action='{0}' method='post'>", RPPURL); sb.AppendFormat("<div style='float:left; width:100%; height:100%;'>"); sb.AppendFormat("<div style='float:left; width:100%; height:100%; margin-top:10%;'> "); sb.AppendFormat("<div style='float:left; width:100%; text-align:center; font-size:30px; color:#525252; margin:0 0 50px 0;'>Please wait while you are being redirected to <span style='font-weight:bold;'>RPP</span> Application.</div>"); sb.AppendFormat("<div style='float:left; width:100%; text-align:center;'>"); sb.AppendFormat("<img src='/images/loading.gif' width='350px'/>"); sb.AppendFormat("</div>"); sb.AppendFormat("<input type='hidden' name='MERCHANTCODE' value='{0}'>", MERCHANTCODE); sb.AppendFormat("<input type='hidden' name='ENCDATA' value='{0}'>", ENCDATA); sb.AppendFormat("</div>"); sb.AppendFormat("<div>"); sb.Append("</form>"); sb.Append("</body>"); sb.Append("</html>"); return sb.ToString(); } public static String sha256_hash(String value) { StringBuilder Sb = new StringBuilder(); using (SHA256 hash = SHA256Managed.Create()) { Encoding enc = Encoding.UTF8; Byte[] result = hash.ComputeHash(enc.GetBytes(value)); foreach (Byte b in result) Sb.Append(b.ToString("x2")); } return Sb.ToString(); } public static string AESEncrypt(string textToEncrypt) { RijndaelManaged rijndaelCipher = new RijndaelManaged(); rijndaelCipher.Mode = CipherMode.CBC; rijndaelCipher.Padding = PaddingMode.PKCS7; rijndaelCipher.KeySize = 256; rijndaelCipher.BlockSize = 128; byte[] pwdBytes = Encoding.UTF8.GetBytes("4157FE34BBAE3A958D8F58CCBFAD7"); pwdBytes = SHA256.Create().ComputeHash(pwdBytes); byte[] keyBytes = new byte[16]; int len = pwdBytes.Length; if (len > keyBytes.Length) { len = keyBytes.Length; } Array.Copy(pwdBytes, keyBytes, len); rijndaelCipher.Key = keyBytes; rijndaelCipher.IV = keyBytes; ICryptoTransform transform = rijndaelCipher.CreateEncryptor(); byte[] plainText = Encoding.UTF8.GetBytes(textToEncrypt); return Convert.ToBase64String(transform.TransformFinalBlock(plainText, 0, plainText.Length)); } public static string AESDecrypt(string textToDecrypt) { RijndaelManaged rijndaelCipher = new RijndaelManaged(); rijndaelCipher.Mode = CipherMode.CBC; rijndaelCipher.Padding = PaddingMode.PKCS7; rijndaelCipher.KeySize = 256; rijndaelCipher.BlockSize = 128; byte[] encryptedData = Convert.FromBase64String(textToDecrypt); byte[] pwdBytes = Encoding.UTF8.GetBytes("4157FE34BBAE3A958D8F58CCBFAD7"); pwdBytes = SHA256.Create().ComputeHash(pwdBytes); byte[] keyBytes = new byte[16]; int len = pwdBytes.Length; if (len > keyBytes.Length) { len = keyBytes.Length; } Array.Copy(pwdBytes, keyBytes, len); rijndaelCipher.Key = keyBytes; rijndaelCipher.IV = keyBytes; byte[] plainText = rijndaelCipher.CreateDecryptor().TransformFinalBlock(encryptedData, 0, encryptedData.Length); return Encoding.UTF8.GetString(plainText); } public static string MD5HASHING(string input) { using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create()) { byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input); byte[] hashBytes = md5.ComputeHash(inputBytes); StringBuilder sb = new StringBuilder(); for (int i = 0; i < hashBytes.Length; i++) { sb.Append(hashBytes[i].ToString("X2")); } return sb.ToString(); } } public static string SendRequest_UpdateTrans(string PRN, decimal AMOUNT) { string AmountForm = AMOUNT.ToString("g29"); Hashtable data = new Hashtable(); data.Add("MERCHANTCODE", MERCHANTCODE); data.Add("PRN", PRN); data.Add("AMOUNT", "11000.00"); // InsertRequest(PRN, MERCHANTCODE, REQTIMESTAMP, PURPOSE, AmountForm, USERNAME, USERMOBILE, USEREMAIL, UserId, UserIp, ProjectId, CHECKSUM, SCHCD, Frequency, Remark); string strForm = PreparePOSTForm(RPPURL_TransVeri, data); return strForm; } } public class RequestParameters { public string MERCHANTCODE { get; set; } public string PRN { get; set; } public string REQTIMESTAMP { get; set; } public string PURPOSE { get; set; } public decimal? AMOUNT { get; set; } public string SUCCESSURL { get; set; } public string FAILUREURL { get; set; } public string CANCELURL { get; set; } public string USERNAME { get; set; } public string USERMOBILE { get; set; } public string USEREMAIL { get; set; } public string UDF1 { get; set; } public string UDF2 { get; set; } public string UDF3 { get; set; } public string OFFICECODE { get; set; } public string REVENUEHEAD { get; set; } public string CHECKSUM { get; set; } } public class ResponseParameters { public string MERCHANTCODE { get; set; } public string REQTIMESTAMP { get; set; } public string PRN { get; set; } public decimal? AMOUNT { get; set; } public string RPPTXNID { get; set; } public string RPPTIMESTAMP { get; set; } public string PAYMENTAMOUNT { get; set; } public string STATUS { get; set; } public string PAYMENTMODE { get; set; } public string PAYMENTMODEBID { get; set; } public string PAYMENTMODETIMESTAMP { get; set; } public string RESPONSECODE { get; set; } public string RESPONSEMESSAGE { get; set; } public string UDF1 { get; set; } public string UDF2 { get; set; } public string UDF3 { get; set; } public string CHECKSUM { get; set; } } public class PaymentRequest { public string MERCHANTCODE { get; set; } public RequestParameters REQUESTPARAMETERS { get; set; } public string REQUESTJSON { get; set; } public string ENCDATA { get; set; } } public class PaymentResponse { public ResponseParameters RESPONSEPARAMETERS { get; set; } public string RESPONSEJSON { get; set; } public string STATUS { get; set; } public string ENCDATA { get; set; } public bool CHECKSUMVALID { get; set; } }