by sandipransing
Authorize Net SIM gateway transaction skips merchant side creditcard details form and directs transaction to be take place on gateway server.
# Gemfile
gem 'authorize-net'
Register for authorize net sandbox account click herePayment gateway credentials
# config/gateway.yml
development: &development
mode: test
login: 9gdLh6T
key: 67fu45xw6VP92LX1
production:
<<: *development
test:
<<: *development
Generate & Migrate Payment Model
rails g model payment status:string amount:float transaction_number:string
rake db:migrate
SIM gateway methods extracted and added to payment model
# app/models/payment.rb
class Payment < ActiveRecord::Base
PROCESSING, FAILED, SUCCESS = 1, 2, 3
validates :amount, :presence => true, :numericality => { :greater_than => 0 }
def self.conf
@@gateway_conf ||= YAML.load_file(Rails.root.join('config/gateway.yml').to_s)[Rails.env]
end
def success?
self.status == SUCCESS
end
## Authorize :: SIM
def setup_transaction(options ={})
options.merge!(:link_method => AuthorizeNet::SIM::HostedReceiptPage::LinkMethod::POST)
t = AuthorizeNet::SIM::Transaction.new(
auth['login'], auth['key'], amount,
:hosted_payment_form => true,
:test => auth['mode']
)
t.set_hosted_payment_receipt(AuthorizeNet::SIM::HostedReceiptPage.new(options))
return t
end
def auth
self.class.conf
end
end
Payment routes
## Callback URL
match '/billing/:id/confirm', :to => 'billing#authorize', :as => :confirm_billing
## Request URL
match '/billing/:id', :to => 'billing#checkout', :as => :billing
match '/billing/:id/thank_you', :to => 'billing#thank_you', :as => :thank_you_billing
Billing controller
# app/controllers/billing_controller.rb
class BillingController < ApplicationController
helper :authorize_net
before_filter :get_order, :only => [:checkout, :authorize, :thank_you]
def checkout
# ASSUMPTION order is valid means amount is entered
@transaction = @order.setup_transaction(
{:link_text => 'Continue',
:link_url => confirm_billing_url(@order)})
end
## CALL BACK
def authorize
resp = AuthorizeNet::SIM::Response.new(params)
if resp.approved?
@order.status = Payment::SUCCESS
@order.transaction_num = resp.transaction_id
else
@order.status = Payment::FAILED
end
@order.save(:validate => false)
redirect_to thank_you_billing_url(@order)
end
private
def auth
Payment.conf
end
def get_order
@order = Payment.find_by_id(params[:id])
@order && @order.valid? || invalid_url
end
end
Views Forms
# app/views/billing/checkout.html.haml
= form_for :sim_transaction, :url => AuthorizeNet::SIM::Transaction::Gateway::TEST, :html => {:id => :authForm} do |f|
= sim_fields(@transaction)
:javascript
$(document).ready(function(){
$('#authForm').submit();
})
# app/views/billing/thank_you.html.haml
- if @order.success?
%p The transaction is successfully completed
- else
%p The transaction failed
Read More…
by sandipransing
Authorize Net Payment gateway provides api access to enable online payments
Gateway provides different api options to integrate-
1. Direct Post Method
In this method gateway handles all steps required in payment transaction flow securely and clean manner. To know more on this click here
2. Server Integration Method (SIM)
Here, Payment form and creditcard detail form resides on gateway site and all the steps in transaction carried out at gateway server
3. Advance Integration Method (AIM)
Provides full control of all the transaction steps at merchant server. Payment form resides on merchant side. merchnat server sends authorization and payment capture requests to gateway server where actual transaction takes place and response is sent back to merchant server to notify transaction status. To know detail integration on this click here
Prerequisites before getting started with integration
Sign up for a test account to obtain an API Login ID and Transaction Key. These keys will authenticate requests to the payment gateway.
Read More…
by sandipransing
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 click here
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
Read More…
|