Up until now, New Zealand based online stores using Ruby on Rails had to use Paypal to process credit cards. Today, I’m releasing support for a new gateway to Active Merchant which uses a local online payment company called Payment Express.
Why Payment Express?
Payment express is local, which means you can hook your online transactions direct into your New Zealand bank accounts.
From Payment Express website:
In NZ we support ANZ, Westpac, National Bank, ASB and BNZ. In Australia we support ANZ, NAB, Westpac, CBA, St George and Bank of South Australia. The Maybank in Malaysia is supported and the Citibank for Singapore.
Payment Express is also very well used in the NZ industry. Here are just some of the companies using Payment Express:
- Trademe
- Sony
- Farmers
- HP
- EziBuy
Active Merchant
Active Merchant is an open source plugin for Rails. It’s an “extraction from the e-commerce system Shopify”. In short, it makes it super easy for anyone to easily and cleanly make credit card payments from their Rails application.
Show me the code!
Enough talk, here’s some example code demonstrating how you might go about using the gateway to process credit cards in your application.
First run this command from the root of your Rails application:
ruby script/plugin install http://activemerchant.googlecode.com/svn/trunk/active_merchant
Now add the appropriate files covered below…
app/views/payment/index.html
<% form_tag :action => 'process' do -%>
<label>Number</label> <%= text_field :credit_card, :number %>
<small>E.g. 4111111111111111</small><br/>
<label>First Name</label> <%= text_field :credit_card, :first_name %>
<small>E.g. David</small><br/>
<label>Last Name</label> <%= text_field :credit_card, :last_name %>
<small>E.g. Jones</small><br/>
<label>Type</label> <%= select :credit_card, :type, ['visa', 'master'] %><br/>
<%= date_select :credit_card, :expiry, :discard_day => true,
:start_year => Time.now.year,
:end_year => Time.now.year + 10 %>
<%= submit_tag 'Process' %>
<% end -%>
The view is very simple. Just gather the information about the users’ credit card…
app/controllers/payment_controller.rb
class PaymentController < ApplicationController
require 'money'
def process
creditcard = assemble_credit_card
if creditcard.valid?
gateway = ActiveMerchant::Billing::PaymentExpressGateway.new(:username => 'your_username',
:password => 'your_password')
response = gateway.authorize(1000, creditcard, {:reference => 'This weeks rent'})
if response.success?
result = gateway.capture(1000, response.authorization)
render :text => response.inspect
else
raise StandardError, response.message
end
end
end
private
def assemble_credit_card
ActiveMerchant::Billing::CreditCard.new(
:type => params[:credit_card][:type],
:number => params[:credit_card][:number],
:month => params[:credit_card]['expiry(2i)'].to_i,
:year => params[:credit_card]['expiry(1i)'].to_i,
:first_name => params[:credit_card][:first_name],
:last_name => params[:credit_card][:last_name]
)
end
end
What Now?
During the process of developing this gateway we’ve been using a Payment Express test account. If you’re looking at adding credit card support for your Rails application then I’d suggest you sign up here for a test account so you can get your application under way! Make sure you select “Development Account” on the pricing plan.
hi, where tendollar = Money.us_dollar(1000) is used in this code?
I’m searching informations how to integrate ActiveMerchant/Money class. for example, do you use composed_of :price kind method in your models or are you using rails native BigDecimal datatype? When you input a price, do you let users input cents or you do some transformations for storing data? thanks.