ActiveMerchant is a very nice little Ruby library that permits connecting to payment processing gateways to charge money against credit cards. To understand how this works, you need to know that there are two separate entities, a “payment gateway” and a “merchant account” (aka a bank). The Payment Gateway receives your request over the Internet for a charge, and authorizes that charge in real-time. The Gateway then hands the authorization/transaction over to the Merchant Account, which converts that credit card transaction into money in your account.
Many Internet credit card companies will act as both Payment Gateway and Merchant Account, but you should be aware that it’s easy to de-couple these, and request that your Payment Gateway process the charge with your preferred Merchant Account. You can save money if you shop around for these services. Authorize.net is one example that seems to be popular with the Ruby/Rails community. Paypal’s Payflow Pro system is an example of a Gateway but not a Merchant Account - you can connect any participating bank to their backend for conversion to cash.
Onto some code. I noticed that many popular examples of ActiveMerchant are out of date, have typo’s or just don’t work. So Science wrote some very simple, put-it-in-a-text-file-and-run-it code that gives a couple of examples on how to process cards with ActiveMerchant and Authorize.net
First, apply for a “test account” with Authorize.net’s developer center. After you’re approved for an account, you’ll get a login_id and a transaction_key. You’ll want to enter those values into the appropriate constants at the top of the code below. Then install ActiveMerchant:
gem install ActiveMerchant -y
That’s it - run the code and you’ll see two successful transactions. Where you go from there is up to you, but hopefully this will help you get off the ground!
require 'active_merchant'
# MOST BASIC EXAMPLE
# random amount from $10 to $35 - this prevents duplicate transaction errors
charge_amount = rand(1000)+2500
LOGIN_ID = 'your_login_id_here'
TRANSACTION_KEY = 'your_trans_key_here'
ActiveMerchant::Billing::Base.mode = :test
#number = '4222222222222' #Authorize.net test card, error-producing
number = '4007000000027' #Authorize.net test card, non-error-producing
credit_card = ActiveMerchant::Billing::CreditCard.new(
:number => number,
:month => 8,
:year => 2009,
:first_name => 'Tobias',
:last_name => 'Luetke',
:type => 'visa'
)
if credit_card.valid?
gateway = ActiveMerchant::Billing::AuthorizeNetGateway.new(
:login => LOGIN_ID,
:password => TRANSACTION_KEY
)
response = gateway.purchase(charge_amount, credit_card)
if response.success?
puts "success!"
else
raise StandardError.new( response.message )
end
end
# EXAMPLE USING BILLING ADDRESS
# random amount from $10 to $35 - this prevents duplicate transaction errors
charge_amount = rand(1000)+2500
number = '4222222222222' #Authorize.net test card, error-producing
#number = '4007000000027' #Authorize.net test card, non-error-producing
billing_address = { :name => 'Mark McBride', :address1 => '1 Show Me The Money Lane',
:city => 'San Francisco', :state => 'CA',
:country => 'US', :zip => '23456', :phone => '(555)555-5555' }
creditcard = ActiveMerchant::Billing::CreditCard.new(
:number => number,
:month => 3, #for test cards, use any date in the future
:year => 2010,
:first_name => 'Mark',
:last_name => 'McBride',
:type => 'visa' #note that MasterCard is 'master'
)
if creditcard.valid?
gateway = ActiveMerchant::Billing::AuthorizeNetGateway.new(
:login => LOGIN_ID,
:password => TRANSACTION_KEY)
options = {:address => {}, :billing_address => billing_address}
response = gateway.authorize(charge_amount, creditcard, options)
if response.success?
gateway.capture(charge_amount, response.authorization)
puts "Success: " + response.message.to_s
else
puts "Fail: " + response.message.to_s
end
else
puts "Credit card not valid: " + creditcard.validate.to_s
end
ActiveMerchant::Billing::CreditCard.require_verification_value = false
For some reason they said the new activemerchant requires the code, unless you explicitly turn it off with code from above.
had to do this as suggested in activemerchant google groups to get it to work. Strange that in irb the creditcard.valid? would return true. I haven’t yet been able to find any good documentation on the gateway.purchase(total, creditcard, options) “options” parameter. Would appreciate any info…
Thanks for this!!!
I will download and run this when I have time later in the week, but I can already see that it’s very informative.
Good post! James, the reason that ActiveMerchant now requires the verification value (CVV2 code) is because it is the most secure option. Requiring the CVV2 code helps to prevent the acceptance of fraudulent payments in your application. It is forbidden to store the verification value, so thieves with card numbers stolen from online databases are not likely to be in possession of the code. For more info on CVV2 you can read http://www.visa.ca/en/merchant/fraudprevention/cvv2.cfm
Also, for more up-to-date documentation and examples, complete with sample code, you can check out my PeepCode ActiveMerchant PDF http://peepcode.com/products/activemerchant-pdf.