Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Integrate CcAvenue Payment Gateway in ASP.NET MVC

0.00/5 (No votes)
24 Jan 2017 1  
Step by step solution to integrate Ccavenue payment gateway in ASP.NET MVC

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 :D (100%).

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*/
  2. Add a controller method called Payment:
    public ActionResult Payment() { return View(); }
    
  3. Add its view, i.e., Payment.cshtml.

    Inside the view, add the following code:

    @using (Html.BeginForm(FormMethod.Post)) 
    { <button type="submit">Pay</button>
  4. Add a post action for payment.
     [HttpPost] 
     public ActionResult Payment(){
    
     var queryParameter = new CCACrypto();
    
    //CCACrypto is the dll you get when you download the ASP.NET 3.5 integration kit from 
    //ccavenue account.
    
     return View("CcAvenue", new CcAvenueViewModel(queryParameter.Encrypt
    (BuildCcAvenueRequestParameters(),your-working-key),
     _your-access-code,
     your-checkout-url));
    }
    private string BuildCcAvenueRequestParameters(string orderId, string amount) {
    
    var queryParameters = new Dictionary<string, string>
    {
    {"order_id", orderId},
    {"merchant_id", "your-merchant-id"},
    {"amount", amount},
    {"currency","INR" },
    {"tid",orderId },
    {"redirect_url","/payment-successful" },
    {"cancel_url","/payment-cancelled"},
    {"request_type","JSON" },
    {"response_type","JSON" },
    {"version","1.1" }
    //add other key value pairs here.
    }.Select(item => string.Format("{0}={1}", item.Key, item.Value));
    return string.Join("&", queryParameters);
    
    }
  5. Create a View called ccAvenue.cshtml. This bit of file does the trick. It redirects to the ccAvenue page on page load.
    @model CcAvenueViewModel
    <form method="post" id="paymentForm" 
    action="@Model.CheckoutUrl">
    <input type="hidden" id="encRequest" 
    name="encRequest" value="@Model.EncryptionRequest" />
    <input type="hidden" id="access_code" 
    name="access_code" value="@Model.AccessCode" />
    </form>
    <div class="row" style="text-align:center">
    <div class="small-12 large-12">
    <h2 class="text-center">Processing...</h2>
    </div>
    </div>
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script>
    
     $("#paymentForm").submit();
    
    </script>

Response

Create an Action method to which you will redirect after success.

[HttpPost]
public ActionResult PaymentSuccessful(string encResp)
{
/*Do not change the encResp.If you change it you will not get any response.
Decode the encResp*/
var decryption = new CCACrypto();
var decryptedParameters = decryption.Decrypt(encResp,
"your-working-key-from-web.config" );
/*split the decryptedParameters by & and then by = and save your values*/
}

That's it and you are done. :)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here