Skip to content

PayPal Recurring Payments with ActiveMerchant

by admin on February 22nd, 2011

I found several pieces to the puzzle but it took me a while to put it all together. After I got it working I thought I should write it down in case I needed to remember what I did or maybe someone else would find it useful.

The first thing you should know is that ActiveMerchant does not support recurring payments with PayPal. But Raymond Law generously made a fork that fixes this. I couldnt get the fix as a gem so I just installed it as a plug in. https://github.com/rayvinly/active_merchant/

Then I went to the Railscasts on PayPal with ActiveMerchant. http://railscasts.com/episodes/146-paypal-express-checkout

In my config/environments I made a slight change from the Railscasts example:

  1. config.after_initialize do
  2.   ActiveMerchant::Billing::Base.mode = :test
  3.   paypal_options = {
  4.     :login => "mysandboxlogingoeshere",
  5.     :password => "MYSANDBOXAPIPASSWORD",
  6.     :signature => "SANDBOXAPI"
  7.   }
  8.   ::STANDARD_GATEWAY = ActiveMerchant::Billing::PaypalGateway.new(paypal_options)
  9.   ::EXPRESS_GATEWAY = ActiveMerchant::Billing::PaypalExpressGateway.new(paypal_options)
  10. ::EXPRESS_RECURRING_GATEWAY = ActiveMerchant::Billing::PaypalExpressRecurringGateway.new(paypal_options)
  11. end

I made a registration controller:
rails g controller index new checkout complete

  1. def checkout
  2.      response = EXPRESS_RECURRING_GATEWAY.setup_agreement(:description => description, return_url => registration_complete_url, :cancel_return_url => registration_new_url)
  3.      
  4.       redirect_to EXPRESS_RECURRING_GATEWAY.redirect_url_for(response.token)
  5. end
  6.  
  7. def complete
  8.      token = params[:token]
  9.      response = EXPRESS_RECURRING_GATEWAY.create_profile(token, :description => description, :start_date => start_date, :frequency => frequency_in_months, :amount => amount_in_dollars, :auto_bill_outstanding => true)
  10.  
  11.      if response. success?
  12.           #handle success
  13.      else
  14.           #handle failure
  15.      end
  16. end

You can figure out your own way to handle the values and details of the transaction. The Date format can be set with

  1. start_date = Time.now

The amount can be just a number. Dont put it in quotes.

From → PayPal, Ruby on Rails

One Comment
  1. Thanks, mate! That was just what I was looking for. Saved me hours of work.

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS