Skip to content

Instantly share code, notes, and snippets.

@pratik60
Created October 15, 2013 20:34
Show Gist options
  • Save pratik60/6998245 to your computer and use it in GitHub Desktop.
Save pratik60/6998245 to your computer and use it in GitHub Desktop.
ccavenue rails
Integrating CCAvenue with Ruby on Rails site
Jul 30 · 2013
At www.dealbuddie.com, we recently had the need to integrate CCAvenue payment gateway to expand our delivery network across India. While CCAvenue provides sample files for php, jsp and C#, it had nothing to offer for rails. Since DealBuddie.com is developed using RoR, I had to build the required solution myself.
Following are quick steps for anybody who wants to do the same -
1) Create the functions required to create and verify your checksum, and add it to application_helper.rb
def verifyChecksum( merchantID, orderID, amount, authDesc, workingKey, checksum)
String str = merchantID+”|”+orderID+”|”+amount+”|”+authDesc+”|”+workingKey
String newChecksum = Zlib::adler32(str).to_s
return (newChecksum.eql?(checksum)) ? true : false
end
def getChecksum( merchantID, orderID, amount, redirectUrl, workingKey)
String str = merchantID + “|” + orderID + “|” + amount + “|” + redirectUrl + “|” + workingKey;
return Zlib::adler32(str)
end
2) Build the string needed to be passed to CCAvenue in Ruby. Example code below -
orderID = @transaction.id.to_s
amount = @transaction.total_amount.to_s
redirectURL = “http://www.dealbuddie.com/transactions/”[email protected]_s+”/ccavenue_redirect”
checksum = getChecksum(CCAVENUE_MERCHANT_ID, orderID, amount, redirectURL, CCAVENUE_WORKING_KEY)
@ccaRequest =
“Merchant_Id=”+CCAVENUE_MERCHANT_ID+”&”+
“Amount=”+amount+”&”+
“Order_Id=”+orderID+”&”+
“Redirect_Url=”+redirectURL+”&”+
“billing_cust_name=”+current_user.name+”&”+
“billing_cust_address=”[email protected]_address+”&”+
“billing_cust_country=”[email protected]+”&”+
“billing_cust_tel=”[email protected]_phone+”&”+
“billing_cust_email=”+current_user.email+”&”+
“billing_cust_state=”[email protected]+”&”+
“delivery_cust_name=”+current_user.name+”&”+
“delivery_cust_address=”[email protected]_address+”&”+
“delivery_cust_country=”[email protected]+”&”+
“delivery_cust_state=”[email protected]+”&”+
“delivery_cust_tel=”[email protected]_phone+”&”+
“delivery_cust_notes=”+”Note”+”&”+
“billing_cust_city=”[email protected]+”&”+
“billing_zip_code=”[email protected]_s+”&”+
“delivery_cust_city=”[email protected]+”&”+
“delivery_zip_code=”[email protected]_s+”&”+
“Checksum=”+checksum.to_s
3) Create the encrypted string to be sent to CCAvenue. You will need java to be installed on your server for this to work.
Dir.chdir(“#{RAILS_ROOT}/public/jars/”) do
@encRequest = %x[java -jar ccavutil.jar #{CCAVENUE_WORKING_KEY} "#{@ccaRequest}" enc]
end
4) Pass the encrypted request and merchant ID to a form, that auto-submits upon loading -
<script type="text/javascript">
$(function() {
$('#redirect').submit();
});
</script>
<form id="redirect" method="post" name="redirect" action="http://www.ccavenue.com/shopzone/cc_details.jsp">
<input type=hidden name=encRequest value=<%=@encRequest%>”>
<input type=hidden name=Merchant_Id value=<%=CCAVENUE_MERCHANT_ID%>>
</form>
5) Create an action in a controller that accepts the CCAvenue returned data, decrypt the data in that action, and give user the message about whether or not the transaction succeeded.
def ccavenue_redirect
@encResponse = params[:encResponse]
@checksum = false
@authDesc = false
@p = nil
@ccaResponse = nil
if (params[:encResponse])
if @encResponse
Dir.chdir(”#{RAILS_ROOT}/public/jars/”) do
@ccaResponse = %x[java -jar ravi-ccavutil.jar #{CCAVENUE_WORKING_KEY} "#{@encResponse}" dec]
end
@p = Rack::Utils.parse_nested_query @ccaResponse
if ([email protected]? && @p[“Merchant_Id”] && @p[“Order_Id”] && @p[“Amount”] && @p[“AuthDesc”] && @p[“Checksum”])
@checksum = verifyChecksum(@p["Merchant_Id"], @p["Order_Id"], @p["Amount"], @p["AuthDesc"], CCAVENUE_WORKING_KEY, @p["Checksum"])
@authDesc = @p["AuthDesc"].eql?("Y") ? true : false
end
end
if @checksum && @authDesc
transaction = Transaction.find(@p["Order_Id"])
transaction.payment_confirmed = true
transaction.save!
message = current_buyer.user.name + ”! Thank you for your order! It will soon be at your doorsteps!”
redirect_to root_path, :flash => {:success => message}
else
if !@authDesc
message = current_buyer.user.name + ”! Your bank did not authorize the transaction. Please go to Settings > My Orders page, and click on "Pay Now" button to finish your transaction”
redirect_to root_path, :flash => {:error => message}
else
message = current_buyer.user.name + ”! Oops! There was some error in retrieving your transaction confirmation. Please drop us an email at [email protected] for order confirmation.”
redirect_to root_path, :flash => {:error => message}
end
end
else
message = current_buyer.user.name + ”! Oops! Something went wrong while processing your request. Please go to Settings > My Orders page, and click on "Pay Now" button to finish your transaction.”
redirect_to root_path, :flash => {:success => message}
end
end
@pratik60
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment