Jan 3, 2008

Rails Application Using Active Merchant

Hi All
I think i write this post after a long time. Sorry pals, i was in ATG project. Hmm... Ok Lets start now to create an application using Active Merchant. What is Active Merchant. Its an extraction from the e-commerce system Shopify. Shopify's requirements for a simple and unified API to access dozens of different payment gateways with very different internal APIs was the chief principle in designing the library. Now we are going to know how to create an application using active merchant API with payment gateway authorize.net. The Authorize.net is a very famous and effective gateway that i know. Ok, Come on lets start

Create a rails application named as creditcard

$ rails creditcard

Before enter into next step make sure the plugin active merchant is installed in your application's vendor/plugins directory. Then install the gem money too.Next,Create a model creditcard

$ ruby script/generate model creditcard

and place the following code in the migration file

create_table :creditcards do |t|
t.column :first_name, :string
t.column :last_name, :string
t.column :card_type, :string
t.column :card_number, :string
t.column :expiration_month, :integer
t.column :expiration_year, :integer
t.column :created_at, :date
t.column :updated_at, :date
end

Create a controller called home by

$ ruby script/generate controller home index check_balance result


Copy and paste the following code in your home controller

def index
return unless request.post?
@creditcard = Creditcard.new(params[:creditcard])
creditcard = ActiveMerchant::Billing::CreditCard.new(
:type => params[:creditcard][:card_type],
:number => params[:creditcard][:card_number],
:month => params[:creditcard][:expiration_month],
:year => params[:creditcard][:expiration_year],
:first_name => params[:creditcard][:first_name],
:last_name => params[:creditcard][:last_name]
)
if creditcard.valid?
if @creditcard.save
flash[:message] ="Creditcard Info saved!"
redirect_to :action => 'check_balance', :id=> @creditcard
else
flash[:message] = "Error in server. Try again later!"
redirect_to :action => 'index'
end
else
flash[:message] = "Error in credit card info!"
redirect_to :action => index
end
end

def check_balance
return unless request.post?
@creditcard = Creditcard.find(params[:id])
gateway = ActiveMerchant::Billing::AuthorizeNetGateway.new(:test => true, :login => "your authorize.net login id", :password=> "your authorize.net password")
amount_to_charge = Money.ca_dollar(params[:user][:balance].to_i) #1000 = ten US dollars
creditcard = ActiveMerchant::Billing::CreditCard.new(
:type => @creditcard.card_type,
:number => @creditcard.card_number,
:month => @creditcard.expiration_month,
:year => @creditcard.expiration_year,
:first_name => @creditcard.first_name,
:last_name => @creditcard.last_name
)
response = gateway.authorize(amount_to_charge,creditcard)
if response.success?
flash[:message] ="You have enough balance to shop!"
redirect_to :action => 'result'
else
flash[:message] = "Please check your Balance!"
redirect_to :action => 'check_balance'
end
end

Your view file index.rhtml should be

<% if flash[:message] %>
<%=flash[:message]%>
<%end%>
<%form_tag({:action => 'index'}, :name => 'frmnew')do%>

Card Holder First Name

<%= text_field 'creditcard', 'first_name', :size => '40' %>
Card Holder Last Name

<%= text_field 'creditcard', 'last_name', :size => '40' %>
Card Type
<%=cards("creditcard[card_type]", "creditcard_card_type")%>
Card Number
<%= text_field 'creditcard', 'card_number', 'value' => "", :size => '40' %>
Expiry Month <%=expiry_month%>
Expiry Year <%=expiry_year%>
<%= submit_tag "SignUp" %>
<%end%>

Then , copy and paste the following code into your check_balance.rhtml

<% if flash[:message] %>
<%=flash[:message]%>
<%end%>
<%form_tag({:action => 'check_balance'})do%>

Enter Your Balance in account:
<%= text_field 'user', 'balance', :size => '40' %>

<%= submit_tag "Check Availability" %>
<%end%>

Finally, Your result.rhtml should like

<% if flash[:message] %>
<%=flash[:message]%>
<%end%>
Creditcard Validated!


Finaly you have to add some code in your application helper as like below. Its for the expiry_month and expiry_date date.Add all the codes in following three pictures to your applicatio_helper.rb

Click Here to view large






















Click Here to view large






















Click Here to view large






















Now configure your application Database and create table using

$ rake db:migrate

Now you run the application. Check with my code and comment me please if any error occurs.
Your can capture the response also by
gateway.capture(1000, response.authorization)

You can get login and password in the check_balance action by registering with
http://www.authorize.net