Friday 10 March 2023

CCAvenue Integration with C# ASP.NET MVC

 

CCAvenue Integration

CCAvenue integration with complete step-by-step code for C# ASP.NET MVC Pattern.


CCAvenue is one of the most powerful and popular payment gateways which support all types of payment methods. Integration of CCavenue with ASP.NET is quick and easy but the main problem is, Integration kit provided by the CCAvenue is implemented in the ASP.NET form application. Which is not in trend right now. ASP.NET MVC with C# is trending now. So this post will help you to implement CCAvenue with ASP.NET C# MVC pattern using visual studio 2019.


Process Login

  • Signup with CCavenue and get the CcAvenueMerchantId,CcAvenueWorkingKey, and CcAvenueAccessCode. To get this code please refer to the ccavenue documentation or use youtube because this information is available easily. So I am going to leave this to make this post smaller.
  • Download the Integration kit named ASP.Net_Kit_Version_2.0.
  • Drop an email to service@ccavenue.com with your website URL because CCAvenue entertains only register URLs sent by users by mail.
  • CCavenue provides a form with mandatory and optional parameter then encrypt user input and send it to the server.

Step 1 

  • Create an account on CCAvenue and get CcAvenueMerchantId,CcAvenueWorkingKey, and CcAvenueAccessCode.


  • Drop an email to service@ccavenue.com with your website URL because CCAvenue entertains only register URLs sent by users by mail.

Step 2

  • Download the Integration kit named ASP.Net_Kit_Version_2.0.


Step 3.


Add this code to web.config file . Change dummy keys with your live keys

<appSettings>   

  <!--================ccavenue===========-->   

  <add key="CcAvenueMerchantId" value="1432641"/>

  <add key="CcAvenueWorkingKey" value="3452UK068A50E6U92P045912705PLC99"/>

  <add key="CcAvenueAccessCode" value="MFGT50OL76BE40TPLH"/>

  <add key="CcAvenueCheckoutUrl"  value="https://secure.ccavenue.com/transaction/transaction.do?command=initiateTransaction"/>

</appSettings>


Step 4


Place this code on the checkout form and you can remove the optional fields but you will have to pass the mandatory fields.

<form method="post" novalidate action="/Controller/ActionName" >
    <table width="40%" height="100" border='1' align="center">
       <tr>
         <td>Parameter Name:</td>
         <td>Parameter Value:</td>
      </tr>
      <tr>
          <td colspan="2"> Compulsory information</td>
      </tr>

                        <tr>

                            <td>Amount</td>

                            <td><input type="text" name="amount" value="1.00" /></td>

                        </tr>

                        <tr>

                            <td colspan="2">Billing information(optional):</td>

                        </tr>

                        <tr>

                            <td>Billing Name</td>

                            <td><input type="text" name="billing_name" value="Charli" /></td>

                        </tr>

                        <tr>

                            <td>Billing Address:</td>

                            <td><input type="text" name="billing_address" value="Room no 1101, near Railway station Ambad" /></td>

                        </tr>

                        <tr>

                            <td>Billing City:</td>

                            <td><input type="text" name="billing_city" value="Indore" /></td>

                        </tr>

                        <tr>

                            <td>Billing State:</td>

                            <td><input type="text" name="billing_state" value="MP" /></td>

                        </tr>

                        <tr>

                            <td>Billing Zip:</td>

                            <td><input type="text" name="billing_zip" value="425001" /></td>

                        </tr>

                        <tr>

                            <td>Billing Country:</td>

                            <td><input type="text" name="billing_country" value="India" /></td>

                        </tr>

                        <tr>

                            <td>Billing Tel:</td>

                            <td><input type="text" name="billing_tel" value="9899622605" /></td>

                        </tr>

                        <tr>

                            <td>Billing Email:</td>

                            <td><input type="text" name="billing_email" value="test@gmail.com" /></td>

                        </tr>

                        <tr>

                            <td colspan="2">Shipping information(optional):</td>

                        </tr>

                        <tr>

                            <td>Shipping Name</td>

                            <td><input type="text" name="delivery_name" value="Chaplin" /></td>

                        </tr>

                        <tr>

                            <td>Shipping Address:</td>

                            <td><input type="text" name="delivery_address" value="room no.701 near bus stand" /></td>

                        </tr>

                        <tr>

                            <td>shipping City:</td>

                            <td><input type="text" name="delivery_city" value="Hyderabad" /></td>

                        </tr>

                        <tr>

                            <td>shipping State:</td>

                            <td><input type="text" name="delivery_state" value="Andhra" /></td>

                        </tr>

                        <tr>

                            <td>shipping Zip:</td>

                            <td><input type="text" name="delivery_zip" value="425001" /></td>

                        </tr>

                        <tr>

                            <td>shipping Country:</td>

                            <td><input type="text" name="delivery_country" value="India" /></td>

                        </tr>

                        <tr>

                            <td>Shipping Tel:</td>

                            <td><input type="text" name="delivery_tel" value="9896426054" /></td>

                        </tr>

                        <tr>

                            <td>Merchant Param1</td>

                            <td><input type="text" name="merchant_param1" value="additional Info." /></td>

                        </tr>

                        <tr>

                            <td>Merchant Param2</td>

                            <td><input type="text" name="merchant_param2" value="additional Info." /></td>

                        </tr>

                        <tr>

                            <td>Merchant Param3</td>

                            <td><input type="text" name="merchant_param3" value="additional Info." /></td>

                        </tr>

                        <tr>

                            <td>Merchant Param4</td>

                            <td><input type="text" name="merchant_param4" value="additional Info." /></td>

                        </tr>

                        <tr>

                            <td>Merchant Param5</td>

                            <td><input type="text" name="merchant_param5" value="additional Info." /></td>

                        </tr>

                        <tr>

                            <td></td>

                            <td><button type="submit" class="btn btn-success">Checkout</button></td>

                        </tr>

                    </table>

                </form>

Step 5


  • Download from ASP.Net_Kit_Version_2.0 kit from CC avenue and unzip it
  • Goto NON-SEAMLESS_KIT folder
  • Add dll file MCPG.CCA.Util.dll from SP.Net_Kit_Version_2.0\NON-SEAMLESS_KIT\libs folder . (for add dll reference goto Project>Add references)
  • Add another dll MCPG.ASP.net.2.0.Kit from location ASP.Net_Kit_Version_2.0\NON-SEAMLESS_KIT\MCPG.ASP.net.2.0.Kit\bin


Step 6 (controller Code)

Name space used in the method

using CCA.Util;

[HttpPost]

        public void CartSubmit(FormCollection formCollection)

        {

            CCACrypto ccaCrypto = new CCACrypto();

            string WorkingKey = ConfigurationManager.AppSettings["CcAvenueWorkingKey"].ToString(); //put in the 32bit alpha numeric key in the quotes provided here

            string Merchant_id = ConfigurationManager.AppSettings["CcAvenueMerchantId"].ToString();

            string CheckoutUrl = ConfigurationManager.AppSettings["CcAvenueCheckoutUrl"].ToString();

            string StrEncRequest = string.Empty;

            string ccaRequest = string.Empty;

            string StrAccessCode = ConfigurationManager.AppSettings["CcAvenueAccessCode"].ToString(); // put the access key in the

                                                                                                      // ranquotes provided here.;

            Random rnd = new Random();

            var callBackUrl = Request.Url.ToString().Substring(0,

                      Request.Url.ToString().Length - Request.Url.PathAndQuery.Length +

                      Url.Content("~").Length) + "Return/Return";

            RemotePost remotepost = new RemotePost();

            FormCollection formCollection1 = new FormCollection

            {

                ["merchant_id"] = Merchant_id,

                ["order_id"] = Session["ID"].ToString(), //Need to generate ramdom nuber for every request.

                ["currency"] = "USD",

                ["amount"] = formCollection["amount"],

                ["language"] = "en",

                ["redirect_url"] = "https://example.com/about/success",

                ["cancel_url"] = "https://example.com/about/cancel",

                ["tid"] = rnd.Next().ToString(), // uniqe number only numrics.

 

                ["billing_name"] = formCollection["billing_name"],

                ["billing_address"] = formCollection["billing_address"],

                ["billing_city"] = formCollection["billing_city"],

                ["billing_state"] = formCollection["billing_state"],

                ["billing_zip"] = formCollection["billing_zip"],

                ["billing_country"] = formCollection["billing_country"],

                ["billing_tel"] = formCollection["billing_tel"],

                ["billing_email"] = formCollection["billing_email"],

 

                ["delivery_name"] = formCollection["delivery_name"],

                ["delivery_address"] = formCollection["delivery_address"],

                ["delivery_city"] = formCollection["delivery_city"],

                ["delivery_state"] = formCollection["delivery_state"],

                ["delivery_zip"] = formCollection["delivery_zip"],

                ["delivery_country"] = formCollection["delivery_country"],

                ["delivery_tel"] = formCollection["delivery_tel"],

                ["delivery_email"] = formCollection["delivery_email"],

 

                ["merchant_param1"] = formCollection["merchant_param1"],

                ["merchant_param2"] = formCollection["merchant_param2"],

                ["merchant_param3"] = formCollection["merchant_param3"],

                ["merchant_param4"] = formCollection["merchant_param4"],

                ["merchant_param5"] = formCollection["merchant_param5"]

            };

            remotepost.Url = CheckoutUrl;

            remotepost.Add("merchant_id", formCollection1["merchant_id"]);

            remotepost.Add("order_id", formCollection1["order_id"]);

            remotepost.Add("amount", formCollection1["amount"]);

            remotepost.Add("currency", formCollection1["currency"]);

            remotepost.Add("language", "en");

            remotepost.Add("redirect_url", formCollection1["redirect_url"]);

            remotepost.Add("cancel_url", formCollection1["cancel_url"]);

            remotepost.Add("tid", formCollection1["tid"]);

            remotepost.Add("billing_name", formCollection1["billing_name"]);

            remotepost.Add("billing_address", formCollection1["billing_address"]);

            remotepost.Add("billing_city", formCollection1["billing_city"]);

            remotepost.Add("billing_state", formCollection1["billing_state"]);

            remotepost.Add("billing_zip", formCollection1["billing_zip"]);

            remotepost.Add("billing_tel", formCollection1["billing_tel"]);

            remotepost.Add("billing_email", formCollection1["billing_email"]);

 

            remotepost.Add("delivery_name", formCollection1["delivery_name"]);

            remotepost.Add("delivery_address", formCollection1["delivery_address"]);

            remotepost.Add("delivery_city", formCollection1["delivery_city"]);

            remotepost.Add("delivery_state", formCollection1["delivery_state"]);

            remotepost.Add("delivery_zip", formCollection1["delivery_zip"]);

            remotepost.Add("delivery_tel", formCollection1["delivery_tel"]);

            remotepost.Add("delivery_email", formCollection1["delivery_email"]);

 

            remotepost.Add("merchant_param1", formCollection1["merchant_param1"]);

            remotepost.Add("merchant_param2", formCollection1["merchant_param2"]);

            remotepost.Add("merchant_param3", formCollection1["merchant_param3"]);

            remotepost.Add("merchant_param4", formCollection1["merchant_param4"]);

            remotepost.Add("merchant_param5", formCollection1["merchant_param5"]);

 

            foreach (string name in formCollection1)

            {

                if (name != null)

                {

                    if (!name.StartsWith("_"))

                    {

                        ccaRequest = ccaRequest + name + "=" + formCollection1[name] + "&";

                    }

                }

            }

            StrEncRequest = ccaCrypto.Encrypt(ccaRequest, WorkingKey);

            remotepost.Add("encRequest", StrEncRequest);

            remotepost.Add("access_code", StrAccessCode);

            remotepost.Post();

 

        }

Step 7

Addd this class in seprate file

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();

        }

    }


Step 8 (optional)



Create a View called Return.cshtml. This bit of file does the trick. It redirects to the Return page on page load.

[HttpPost]

        [ValidateInput(false)]

        public ActionResult Return()

        {

            string workingKey = "";//put in the 32bit alpha numeric key in the quotes provided here

            CCACrypto ccaCrypto = new CCACrypto();

            string encResponse = ccaCrypto.Decrypt(Request.Form["encResp"], workingKey);

            NameValueCollection Params = new NameValueCollection();

            string[] segments = encResponse.Split('&');

            foreach (string seg in segments)

            {

                string[] parts = seg.Split('=');

                if (parts.Length > 0)

                {

                    string Key = parts[0].Trim();

                    string Value = parts[1].Trim();

                    Params.Add(Key, Value);

                }

            }

 

            for (int i = 0; i < Params.Count; i++)

            {

                Response.Write(Params.Keys[i] + " = " + Params[i] + "

          < br > ");

            }

        }


Code Credit


No comments:

Post a Comment