Friday, September 27, 2013

Paypal with Ruby 2.0 and Active Merchant



 This can be use with Ruby 2.0 or if your using for ecommerce application with rails 4.0 and it is tested with active merchant 1.29.3

1. Install active merchant
     gem install activemerchant

2. Create paypal sandbox account with business pro
3. Create a Ruby file

require "rubygems"
require "active_merchant"

ActiveMerchant::Billing::Base.mode = :test

gateway = ActiveMerchant::Billing::PaypalGateway.new(
  :login => "dwwwrwrutest1_api1.gmail.com",
  :password => "1380282787",
  :signature => "AscqxJyW-0NJcj5gJYTIXmFahhqQAtMS8.MCcHIHTVA26-3A76p.ctmP"
)

credit_card = ActiveMerchant::Billing::CreditCard.new(
  :brand              => "Visa",
  :number             => "5024007148673554",
  :verification_value => "123",
  :month              => 1,
  :year               => Time.now.year+1,
  :first_name         => "Boundry",
  :last_name          => "Unlimited"



if credit_card.valid?
  # or gateway.purchase to do both authorize and capture
  response = gateway.authorize(1200, credit_card, :ip => "127.0.0.1", :currency => "AUD", :customer => "Test Name", :email =>"suaua@hhdd.com")
  if response.success?
    gateway.capture(1200, 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

4.Run >ruby filename.rb
5. This example is extracted from Railscasts and can be useful to play around with different Paypal features