PayPal Recurring Payments with ActiveMerchant
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:
-
config.after_initialize do
-
ActiveMerchant::Billing::Base.mode = :test
-
paypal_options = {
-
:login => "mysandboxlogingoeshere",
-
:password => "MYSANDBOXAPIPASSWORD",
-
:signature => "SANDBOXAPI"
-
}
-
::STANDARD_GATEWAY = ActiveMerchant::Billing::PaypalGateway.new(paypal_options)
-
::EXPRESS_GATEWAY = ActiveMerchant::Billing::PaypalExpressGateway.new(paypal_options)
-
::EXPRESS_RECURRING_GATEWAY = ActiveMerchant::Billing::PaypalExpressRecurringGateway.new(paypal_options)
-
end
I made a registration controller:
rails g controller index new checkout complete
-
def checkout
-
response = EXPRESS_RECURRING_GATEWAY.setup_agreement(:description => description, return_url => registration_complete_url, :cancel_return_url => registration_new_url)
-
-
redirect_to EXPRESS_RECURRING_GATEWAY.redirect_url_for(response.token)
-
end
-
-
def complete
-
token = params[:token]
-
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)
-
-
if response. success?
-
#handle success
-
else
-
#handle failure
-
end
-
end
You can figure out your own way to handle the values and details of the transaction. The Date format can be set with
-
start_date = Time.now
The amount can be just a number. Dont put it in quotes.
Thanks, mate! That was just what I was looking for. Saved me hours of work.