#144 Active Merchant Basics
Active Merchant is a great library for handling credit card transactions. In this episode I will show you the basics of using it to communicate with PayPal's gateway.
- Download:
- source codeProject Files in Zip (1.21 KB)
- mp4Full Size H.264 Video (15.4 MB)
- m4vSmaller H.264 Video (10.7 MB)
- webmFull Size VP8 Video (31.9 MB)
- ogvFull Size Theora Video (23.7 MB)
Resources
- Active Merchant
- PayPal's Developer Site
- PayPal's Website Payment Pro Guide PDF
- Full Episode Source Code
bash
sudo gem install activemerchant
sudo gem install activemerchant
ruby
require "rubygems"
require "active_merchant"
ActiveMerchant::Billing::Base.mode = :test
gateway = ActiveMerchant::Billing::PaypalGateway.new(
:login => "seller_1229899173_biz_api1.railscasts.com",
:password => "FXWU58S7KXFC6HBE",
:signature => "AGjv6SW.mTiKxtkm6L9DcSUCUgePAUDQ3L-kTdszkPG8mRfjaRZDYtSu"
)
credit_card = ActiveMerchant::Billing::CreditCard.new(
:type => "visa",
:number => "4024007148673576",
:verification_value => "123",
:month => 1,
:year => Time.now.year+1,
:first_name => "Ryan",
:last_name => "Bates"
)
if credit_card.valid?
# or gateway.purchase to do both authorize and capture
response = gateway.authorize(1000, credit_card, :ip => "127.0.0.1")
if response.success?
gateway.capture(1000, response.authorization)
puts "Purchase complete!"
else
puts "Error: #{response.message}"
end
else
puts "Error: credit card is not valid. #{credit_card.errors.full_messages.join('. ')}"
end
require "rubygems" require "active_merchant" ActiveMerchant::Billing::Base.mode = :test gateway = ActiveMerchant::Billing::PaypalGateway.new( :login => "seller_1229899173_biz_api1.railscasts.com", :password => "FXWU58S7KXFC6HBE", :signature => "AGjv6SW.mTiKxtkm6L9DcSUCUgePAUDQ3L-kTdszkPG8mRfjaRZDYtSu" ) credit_card = ActiveMerchant::Billing::CreditCard.new( :type => "visa", :number => "4024007148673576", :verification_value => "123", :month => 1, :year => Time.now.year+1, :first_name => "Ryan", :last_name => "Bates" ) if credit_card.valid? # or gateway.purchase to do both authorize and capture response = gateway.authorize(1000, credit_card, :ip => "127.0.0.1") if response.success? gateway.capture(1000, response.authorization) puts "Purchase complete!" else puts "Error: #{response.message}" end else puts "Error: credit card is not valid. #{credit_card.errors.full_messages.join('. ')}" end

