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
- 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*/
- Add a controller method called
Payment
:
public ActionResult Payment() { return View(); }
- Add its view, i.e., Payment.cshtml.
Inside the view, add the following code:
@using (Html.BeginForm(FormMethod.Post))
{ <button type="submit">Pay</button>
- Add a post action for payment.
[HttpPost]
public ActionResult Payment(){
var queryParameter = new CCACrypto();
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" }
}.Select(item => string.Format("{0}={1}", item.Key, item.Value));
return string.Join("&", queryParameters);
}
- 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)
{
var decryption = new CCACrypto();
var decryptedParameters = decryption.Decrypt(encResp,
"your-working-key-from-web.config" );
}
That's it and you are done. :)