Home

Prerequisites

  • You should have a ccavenue account.

Problem

  • The second step is the problem I faced while integrating the ccavenue page. I found out that most of the people were finding the same. As we are developing something, we needed to first test it in the development environment.

Inside your ccavenue account, they have already given you the access code, merchant Id and access key for a url. But those credentials will only work if you are redirecting to the ccavenue page from that url only. For example: If url set up in your account is https://xyz.com, then only if you redirect from https://xyz.com will you be redirected to the correct page, else you will get 1002, i.e., Merchant Authentication failed.

Solution

  • Drop a mail to service@ccavenue.com along with your merchant id and test url.They will set it up for you !! Cheers! 🙂

But what if they do not respond?!! So there is another solution.

  • You can login to your account and they have a tab called support. In that support, when you enter an email id, an email comes to that mail id with some credentials. Login to that url with your credentials and raise a ticket there on an urgent basis. Your issue will be resolved 😀 (100%).

You can see the details like below screen short

CC

Now comes the very important part, i.e., the implementation.

Implementation

Request

  1. Add 4 keys to your web.config file inside. Development environment and production environment for both keys are different. We can only get the values from config files not set them. Storing them in web.config is the best place.
<add key="CcAvenueMerchantId" value="your-merchant-id"/>
<add key="CcAvenueWorkingKey" value="your-working-key"/>
<add key="CcAvenueAccessCode" value="your-access-code"/>
<add key="CcAvenueCheckoutUrl" value="your-checkout-url"/>

/*checkout url for development is : 
https://test.ccavenue.com/transaction/transaction.do?command=initiateTransaction
For production,
it is https://secure.ccavenue.com/transaction/transaction.do?command=initiateTransaction*/

Like below screen

config

2. Add a controller method called Request: 

public ActionResult SubmitRequest() { return View(); }

3. Add its view, i.e., SubmitRequest.cshtml.

Inside the view, add the following code:

<form action="/Request/SubmitRequest" method="post" novalidate>
   <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">Checkout</button></td>
   </tr>
  </table>
</form>

 

4. Add a post action for SubmitRequest

[HttpPost]
public void SubmitRequest(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 quotes provided here.;
  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"] = "132132132", //Need to generate ramdom nuber for every request.
    ["currency"] = "INR",
    ["redirect_url"] = callBackUrl,
    ["cancel_url"] = callBackUrl,
    ["tid"] = "21332132132", // 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("currency", formCollection1["currency"]);
  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();
}

 

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

 

5. 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>");
  }
}

 

That’s it and you are done. 🙂