Sunday, 26 June 2016

PayU india ASP.Net MVC Integration

Before implementing you need to get MERCHANT_KEYSALT  From PayU
Replace Red color variables with correct values.
BillingDetails  is Entity with only required parameters:


 public class BillingDetails
    {
        public int Id { get; set; }
        [Display(Name = "Billing Amount (₹)")]
        public double Amount { get; set; }
        public UserType UserType { get; set; }
        public int UserId { get; set; }
        [Display(Name = "Customer Name")]
        public string FirstName { get; set; }
        [Display(Name = "Customer Email")]
        public string EmailId { get; set; }
        public string Phone { get; set; }
        [Display(Name = "Product Info")]
        public string ProductInfo { get; set; }
        public string Status { get; set; }
        public string ReturnUrl { get; set; }
        public DateTime CreatedOn { get; set; }
        public int CreatedBy { get; set; }

    }


________________________________________________________________________________



using System;
using System.Net;
using System.Security.Cryptography;



[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult MakePayment(BillingDetails bd)
        {
       
            RemotePost myremotepost = new RemotePost();
            string key = "MERCHANT_KEY";
            string salt = "SALT";
            myremotepost.Url =  "https://secure.payu.in/_payment";
            myremotepost.Add("key", key);
            string txnid = Generatetxnid();
            myremotepost.Add("txnid", txnid);
            myremotepost.Add("amount", bd.Amount.ToString());
            myremotepost.Add("productinfo", bd.ProductInfo);
            myremotepost.Add("firstname", bd.FirstName);
            myremotepost.Add("phone", bd.Phone);
            myremotepost.Add("email", bd.EmailId);
            myremotepost.Add("surl","SuccessUrl");
            myremotepost.Add("furl", "FailuarURL");
            myremotepost.Add("service_provider", "payu_paisa");
            myremotepost.Add("udf1", bd.Id.ToString());
            string hashString = key + "|" + txnid + "|" + bd.Amount + "|" + bd.ProductInfo + "|" +               bd.FirstName + "|" + bd.EmailId + "|" + bd.Id + "||||||||||" + salt;
            string hash = Generatehash512(hashString);
            myremotepost.Add("hash", hash);

            myremotepost.Post();

            return View();
        }




public class RemotePost
        {
            private System.Collections.Specialized.NameValueCollection Inputs = new System.Collections.Specialized.NameValueCollection();


            public string Url = "";
            public string Method = "post";
            public string FormName = "form1";

            public void Add(string name, string value)
            {
                Inputs.Add(name, value);
            }

            public void Post()
            {
                System.Web.HttpContext.Current.Response.Clear();

                System.Web.HttpContext.Current.Response.Write("<html><head>");

                System.Web.HttpContext.Current.Response.Write(string.Format("</head><body onload=\"document.{0}.submit()\">", FormName));
                System.Web.HttpContext.Current.Response.Write(string.Format("<form name=\"{0}\" method=\"{1}\" action=\"{2}\" >", FormName, Method, Url));
                for (int i = 0; i < Inputs.Keys.Count; i++)
                {
                    System.Web.HttpContext.Current.Response.Write(string.Format("<input name=\"{0}\" type=\"hidden\" value=\"{1}\">", Inputs.Keys[i], Inputs[Inputs.Keys[i]]));
                }
                System.Web.HttpContext.Current.Response.Write("</form>");
                System.Web.HttpContext.Current.Response.Write("</body></html>");

                System.Web.HttpContext.Current.Response.End();
            }
        }


        public string Generatehash512(string text)
        {

            byte[] message = Encoding.UTF8.GetBytes(text);

            UnicodeEncoding UE = new UnicodeEncoding();
            byte[] hashValue;
            SHA512Managed hashString = new SHA512Managed();
            string hex = "";
            hashValue = hashString.ComputeHash(message);
            foreach (byte x in hashValue)
            {
                hex += String.Format("{0:x2}", x);
            }
            return hex;

        }


        public string Generatetxnid()
        {

            Random rnd = new Random();
            string strHash = Generatehash512(rnd.ToString() + DateTime.Now);
            string txnid1 = strHash.ToString().Substring(0, 20);

            return txnid1;
        }

========================================================================
If you are looking for more help write me on kalubarmea@gmail.com