Credit Card Payments using ActiveMerchant and Authorize.net

56 sec read

If you want to integrate Credit Card Payments for a web-applications, check out this blog on working with ActiveMerchant and Authorize.net payment gateways.

The first requirement is to obtain a test-account from Authorize.net from http://developer.authorize.net/testaccount. For integration on Credit-Card Payments made through website, a No Card Account is required. When you register for a test account Authorize.net sends you the API Key and Transaction Key for authentication at the gateways.
Create a credit card object that will be used later for the billing process as following:
[source language=”ruby”]
credit_card = ActiveMerchant::Billing::CreditCard.new(
:number => ‘Test Credit Card Number’,
:verification_value => ‘123’,
:month => 8, #for test cards, use any date in the future
:year => 2018,
:first_name => ‘test’,
:last_name => ‘user’,
:type => ‘Test Credit Card Type’ )
[/source]
The following code makes a call to the ActiveMerchant Gateway.
[source language=”ruby”]
gateway = ActiveMerchant::Billing::Base.gateway(:authorized_net).new(
:login =>Your API Login ID’, :password =>’Transaction Key’, :test => true)
[/source]
Rest of the logic is quite simple,
[source language=”ruby”]
if credit_card.valid?
response = gateway.authorize(amount_to_charge*100 , credit_card, :billing_address =>
{ :name =>’Mark McBride’,
:address1 => ’33 New Montgomery St.’,
:city => ‘San Francisco’,
:state => ‘CA’,
:country => ‘US’,
:zip => ‘760001’,
:phone => ‘(555)555-5555′} )
if response.success?
gateway.capture(amount_to_charge, response.authorization)
render :text =>’Success:’ + response.message.to_s and return
else
render :text => ‘Fail:’ + response.message.to_s and return
end
else
render :text =>’Credit card not valid: ‘ + credit_card.validate.to_s and return
end
[/source]

Related posts:

One Reply to “Credit Card Payments using ActiveMerchant and Authorize.net”

Leave a Reply

Your email address will not be published. Required fields are marked *