Thursday, 11 October 2012

Integrating the authorize.net for rails 3 application

Integrating the authorize.net for rails 3 application:

For having the Authorize.net functionality, first we should install the "Active Merchant" gem which is available at github.

Now, we should have a sandbox test account at Authorize.net site and should get the API Login ID and the Transaction Key.
For that, go through the following link.
https://developer.authorize.net/testaccount/

1) Go to the respective controller in which you are intended to integrate the authorize.net gateway and add the following method for it.

def authorization

amount_to_charge = 100
ActiveMerchant::Billing::Base.mode = :test

creditcard = ActiveMerchant::Billing::CreditCard.new(
:number => '4007000000027',
:verification_value => '123',
:month => 12,
:year => 2020,
:first_name => 'test_first_name',
:last_name => 'test_first_name',
:type => 'VISA'
)

if creditcard.valid?

gateway =ActiveMerchant::Billing::AuthorizeNetGateway.new(
:login => '<API Login ID>',
:password =>'<Transaction Key>',
:test => 'true' )

response = gateway.authorize(amount_to_charge*100 , creditcard ,
:billing_address =>{ :name =>'Mark
McBride',:address1 => '1 Show Me The Money
Lane',:city => 'San Francisco',:state =>
'CA',:country => 'US',:zip => '23456',:phone =>
'(555)555-5555'})

if response.success?
gateway.capture(amount_to_charge, response.authorization)

flash[:notice] = "Authorized #{response.inspect}"
else
render :text => 'Fail:' + response.message.to_s and return
end
else
render :text =>'Credit card not valid: ' + creditcard.validate.to_s and return
end
end

Now, edit the values of "amount_to_charge". And also edit values of "login, password" of "AuthorizeNetGateway" method.

2) Go to your particular display page from which you are giving provision for users to purchase the product. There, give the path to above method "authorization".

3) Now, open your Authorize.net test account and set the "test mode" from the path "account -> settings -> Test Mode" and click on the "Test Mode on" button.


Now, you can do authorization of users from your application and can do purchases by adding that functionality.

Transaction details cannot be seen immediately in our account, as they will be in unsettled mode if they are in process queue. So, we can find them using the "search" option. But, we will receive an email with the transaction details immediately to our email account, which was mentioned while authorize.net sandbox account registration.


No comments:

Post a Comment

Interactor in Rails

What is interactor? Interactor provides a common interface for performing complex user interactions An interactor is a simple, sin...