0% found this document useful (0 votes)
195 views3 pages

Authorize Net (AIM) Payment Integration

This document summarizes how to integrate the Authorize.net payment gateway into a Rails application using the ActiveMerchant library. It includes instructions for setting up an Authorize.net sandbox account, adding payment and credit card forms, creating a Payment model, and processing payments in the PaymentsController. The Payment model validates credit card information and processes payments by making an API call to Authorize.net to purchase transactions.

Uploaded by

Sandip Ransing
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
195 views3 pages

Authorize Net (AIM) Payment Integration

This document summarizes how to integrate the Authorize.net payment gateway into a Rails application using the ActiveMerchant library. It includes instructions for setting up an Authorize.net sandbox account, adding payment and credit card forms, creating a Payment model, and processing payments in the PaymentsController. The Payment model validates credit card information and processes payments by making an API call to Authorize.net to purchase transactions.

Uploaded by

Sandip Ransing
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Authorize Net (AIM) payment integration with rails Authorize Net (AIM) method enables internet merchants to accept

online payments via credit card. Below post will show you how to integrate authorize net payment gateway inside rails app to accept online payments using activemerchant library. # Gemfile gem 'activemerchant', :require => 'active_merchant' Register for authorize net sandbox account <a href='https://developer.authorize.net/testaccount/'>click here</a> Payment gateway credentials # config/authorize_net.yml development: &development mode: test login: 9gdLh6T key: 67fu45xw6VP92LX1 production: <<: *development test: <<: *development Payment & creditcard form # app/views/payments/new = form_for @payment, :url => payments_url do |f| = f.text_field :amount = fields_for :creditcard, @creditcard do |cc| = cc.text_field :name = cc.text_field :number = cc.select :month, Date::ABBR_MONTHNAMES.compact.each_with_index.collect{|m, i| [m, i+1]}, {:prompt => 'Select'} = cc.select :year, Array.new(15){|i| Date.current.year+i}, {:prompt => 'Select'} = cc.text_field :verification_value = f.submit 'Pay' Payments Controller # app/controllers/payments_controller.rb class PaymentsController < ApplicationController def new @payment = Payment.new @creditcard = ActiveMerchant::Billing::CreditCard.new end

def create @payment = Payment.new(params[:payment]) @creditcard = ActiveMerchant::Billing::CreditCard.new(params[:creditcard]) @payment.valid_card = @creditcard.valid? if @payment.valid? @payment = @payment.process_payment(@creditcard) if @payment.success? @payment.save flash[:notice] = I18n.t('payment.success') redirect_to payments_url and return else flash[:error] = I18n.t('payment.failed') end end render :action => :new end end Generate & Migrate Payment Model rails g model payment status:string amount:float transaction_number:string rake db:migrate Payment Model # app/models/payment.rb class Payment < ActiveRecord::Base PROCESSING, FAILED, SUCCESS = 1, 2, 3 validates :valid_card, :inclusion => {:in => [true], :message => 'Invalid Credit Card'} validates :amount, :presence => true, :numericality => { :greater_than => 0 } def process_payment(creditcard) ActiveMerchant::Billing::Base.mode = auth['mode'].to_sym self.status = PROCESSING response = gateway.purchase(amount * 100, creditcard) if response.success? self.transaction_number = response.subscription_id self.status = SUCCESS else self.status = FAILED end return self rescue Exception => e self.status = FAILED return self end def success?

self.status == SUCCESS end private def gateway ActiveMerchant::Billing::AuthorizeNetGateway.new( :login => auth['login'], :password => auth['key']) end def auth @@auth ||= YAML.load_file("#{Rails.root}/config/authorize_net.yml")[Rails.env] end end

You might also like