Posted by Alok Swain on July 10th, 2010

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:

    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' )

The following code makes a call to the ActiveMerchant Gateway.

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

Rest of the logic is quite simple,

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
pixelstats trackingpixel
Unique Views: 1387 Total views: 1915
Follow responses at RSS 2.0. Leave a response | Trackback.

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

  1. Rahul says:

    Perfect, just what I wanted, thanks a lot!

    Leave a Reply