Run a business? Check out LessAccounting, our bookkeeping system that'll save you hours per week.

Announcing:

We built the entrepreneur's bookkeeping system, it's called LessAccounting. You'll probably love it.

Paypal on Rails...ActiveMerchant tips

written by Steven on June 06, 2007

Anyone who has looked into using ActiveMerchant and PayPal together knows it is quite a challenge. ActiveMerchant is great, but there is no documentation for using it with Paypal. Here are some tips to help you get it working:

Here is a page with some good references.

Setting up Paypal

The paypal website is a bit tough to navigate and they don't make it obvious to figure out what are all the things you need to do to get it all setup.

  • You will need a sandbox (developer) paypal account to test your set up. Sign up here
  • The sandbox does not send emails, so you will have to use the Sandbox Email Tab to validate your sandbox account email address.
  • Launch the sandbox
  • Create a sandbox business account. and go through the normal paypal verification stuff. (It does not use your real bank account.)
  • Go to the Merchant Services tab and signup for a Website Payments Pro Account.
  • Verify your merchant account. You will be asked to enter you SSN number, use 111 + six random numbers. If you get an error message, try a different six numbers, the fake ssn must be unique in the sand box.
  • Under the My Account tab and the Profile subtab and the Billing Agreements link, make sure you accept the Agreement.
  • Same for the PayPal Monthly Billing link.
  • Follow these instructions to create your API Certificate. Do not make the API Signature. You only need to do the "Step 1. Generate Certificate" part.
  • Make sure you save the username & password given and download and save the pem file.

Paypal should now be set up. You will have to go through and do all the same stuff for your real paypal account, once your development is done and working.

Setting up ActiveMerchant


config.after_initialize do
  ActiveMerchant::Billing::Base.gateway_mode = :test
  ActiveMerchant::Billing::PaypalGateway.pem_file = File.read(RAILS_ROOT + '/config/cert_key_pem_dev.txt')
end
$PAYPAL_LOGIN = ''
$PAYPAL_PASSWORD = ''


amount = 1000 #$10.00
credit_card = ActiveMerchant::Billing::CreditCard.new(
	:type       => 'visa',
	:number     => '4242424242424242',
	:month      => 8,
	:year       => 2009,
	:first_name => 'Bob',
	:last_name  => 'Bobsen',
	:verification_value=> '123'
)


flash[:error] = credit_card.errors and return unless credit_card.valid?


billing_address = {
	:name     => "John Smith",
	:address1 => '123 First St.',
	:address2 => '',
	:city     => 'Los Angeles',
	:state    => 'CA',
	:country  => 'US',
	:zip      => '90068',
	:phone    => '310-555-1234'
}


gateway = ActiveMerchant::Billing::PaypalGateway.new(:login=>$PAYPAL_LOGIN, :password=>$PAYPAL_PASSWORD)

res = gateway.authorize(amount, credit_card, :ip=>request.remote_ip, :billing_address=>billing_address)

if res.success?
	gateway.capture(amount, res.authorization)
	flash[:notice] = "Authorized"
	redirect_to somewhere_url
else
	flash[:error] = "Failure: " + res.message.to_s
end


  • Install ActiveMerchant by following the "instructions":http://activemerchant.org/, you will need to install the money gem, which ActiveMerchant requires.
  • Rename the pem file to cert_key_pem_dev.txt and put it in your config dir.
  • Add the following code to your environments/development.rb:
  • Use the actual username and password in the previous code (don't use the empty strings :)
  • Do the same for the environments/test.rb
  • Do the same for the environments/production.rb, except remove the line: ActiveMerchant::Billing::Base.gateway_mode = :test and use the cert_key_pem_prod.txt file (which you can get through your real paypal account (renaming to cert_key_pem_prod.txt ).
  • Do something like this in your code:
  • Make sure State and Country are two character code.
  • The credit card errors is actually a hash, not an array (like AR.errors), so you will need a helper to display the error.

Last Tip

When testing with paypal, you must use port 80: ./script/server -p 80

Business Owners: save hours per week with LessAccounting. It's like Quickbooks, just not total shit.

9 Comments

Thomas Fee
Thomas Fee said on June 19, 2007

Kudos to you!

Your instructions on making ActiveMerchant work with PayPay is 1000% better than anything else out there – and you got me over the hump and my app (for the Chinese school) is now working at this critical part. Thank you a bunch for sharing your tips and solution! If you provide a “send a beer by PayPal” button, I’ll click on it ;-) … I tell you, it was a sweat pounding on this stuff. PayPal’s bits are horribly scattered – and of course, ActiveMerchant is not documented.

BTW, the website is not up yet. It will go beta, then will go live July 1st.

Justin Meyer
Justin Meyer said on June 27, 2007

I agree, this is the best thing I found. I even bought a book, Beginning Ruby on Rails E-Commerce, that left out many important steps you described.

fawad
fawad said on October 12, 2007

I received an error, OPENSSL::SSL::SSLERROR while using the code. Please help me… its urgent

ali
ali said on October 15, 2007

As fawad,
i´m getting OpenSSL::PKey::RSAError in AdminController#testPay

The error is trown in this line:

res = gateway.authorize(amount, credit_card, :ip => request.remote_ip, :billing_address => billing_address)

I´m working in winXP, WEBRick 1.3.1 port=80


Should i open some port in the windows fireall?


Thank you for this tutorial, Steven!!

Alex C.
Alex C. said on October 18, 2007

Hey, just wanted to let you know that you can instantiate a PayPal gateway like this:

ActiveMerchant::Billing::PaypalGateway.new(
:login => ‘your_login’
:password => ‘your_pass’,
:signature => ‘79a97ad9asetcetc’)

And completely avoid messing with .pem files!

Fawad
Fawad said on October 24, 2007

Hi, Is there any way to send payment to other bank account or to paypal account from a paypal account. Response is highly appreciatable.

Steven A Bristol
Steven A Bristol said on October 24, 2007

Fawad,

Send payment to another bank account? I’m not sure I understand your question, but ActiveMerchant does support other banks besides paypal.

As far as transferring funds between paypal accounts, paypal does have an api that might allow for that, but I have never needed to use it.

Russ Dsa
Russ Dsa said on November 01, 2007

I don’t see any ‘Billing Agreements’ or ‘Paypal Monthly Billing’ links under Merchant Services > Profile. Is this a critical step? I DO see an ‘Accept Billing Agreement’ for step 3 of the Website Payments Pro signup process, but I can’t get to it since the ‘Confirm your Email’ step in the sandbox never sends out the confirmation email. Any advice?

Thanks!
Russ

Steven Bristol
Steven Bristol said on November 01, 2007

Russ,

Accepting the billing agreements are critical steps. Evey step is critical and it won’t work unless they are all done. The sandbox does not actually send real emails, so you have to click on the email tab inside the sandbox and look at the email messages in the sandbox application.

About Steven
Steven Bristol has written code for the past 20 years. He like green vegetables and kittens, oh and butterflies too. He loves to throw ninja stars at his enemies.

You Should...

Follow Steven on Twitter
Friend Steven on Facebook
Subscribe
LessEverything Copyright 2011 LessEverything.com
We don't like footers, they're kinda boring