Paypal on Rails...ActiveMerchant tips

Written by on Jun 6 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:

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.afterinitialize do ActiveMerchant::Billing::Base.gatewaymode = :test ActiveMerchant::Billing::PaypalGateway.pemfile = + ‘/config/certkeypemdev.txt’) end $PAYPALLOGIN = “ $PAYPAL_PASSWORD = ”

amount = 1000 #$10.00 creditcard = :type => ‘visa’, :number => ‘4242424242424242’, :month => 8, :year => 2009, :firstname => ‘Bob’, :lastname => ‘Bobsen’, :verificationvalue=> ‘123’ )

flash[:error] = creditcard.errors and return unless creditcard.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 =>$PAYPALLOGIN, :password=>$PAYPALPASSWORD)

res = gateway.authorize(amount, creditcard, :ip=>request.remoteip, :billingaddress=>billingaddress)

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

  • Install ActiveMerchant by following the “instructions”:, 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


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 said on October 12, 2007

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

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, creditcard, :ip => request.remoteip, :billingaddress => billingaddress)

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:
:login => ‘yourlogin’
:password => ‘your
:signature => ‘79a97ad9asetcetc’)

And completely avoid messing with .pem files!

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


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?


Steven Bristol
Steven Bristol said on November 01, 2007


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.


Hi I'm Steven,

I wrote the article you're reading... I lead the developers, write music, used to race motorcycles, and help clients find the right features to build on their product.

Get Blog Updates