ActiveMerchant, Paypal and Rails
EDIT: I have followed this up with a further post, More on ActiveMerchant and Paypal.
I’ve spent most of the past week playing with ActiveMerchant and Paypal in order to implement recurring billing for my app.
After a false start with Datacash and some fumbling in the Paypal Sandbox, I got started with a test account for Website Payments Pro (UK). This has the benefit of using Paypal as your gateway service and also as your merchant account. The fees for processing are slightly higher than other gateways but I find the all in one solution worth it so far.
I should mention at this point that Cody Fauser’s ActiveMerchant PDF from PeepCode was invaluable in giving me a start to integration. Although the book is geared towards a straightforward purchase (rather than recurring billing), the code was a great starting point. Thankfully, the ActiveMerchant library is very well commented and easily understandable.
In brief, to setup the recurring billing (implemented through Subscription model), an account is created using the subdomain as account key model. If the account is a paying account, the user is redirected to a secure page to enter their credit card details and create the subscription. The credit card information, amount to be billed and frequency are passed to Paypal in the following form:
Subscription.recurring(amount, credit_card, options = {:ip => request.remote_ip}) |
This calls a method on the Subscription class which sends the data to Paypal. If a successful response is received, the response and other information is stored in the database.
In terms of updating the subscription, this is easily done with the AM library by sending the profile id (which is returned by Paypal when recurring billing is set up) along with the recurring billing request. Using this method, credit card details, payment amount and frequency can be simply changed.
I’m more than happy to share the code I’m using if you want to leave a comment.