captured sparks

Live from Stevenage, England
25 March, 2008

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.


Comments

08 April, 2008

Geoffrey Grosenbach says:

Thanks for the article! I'm glad you found the PeepCode book useful.

09 April, 2008

Jeff Lin pronounces:

I'd be very interested in learning more about how you implemented the recurring payments. I'm in the middle of doing something probably very similar. Sharing your solutions would be greatly appreciated, and I'd of course return the favor.

15 April, 2008

Andy Gordon states:

I am about to implement this with exactly the same set up, seeing your code would be great if possible. Much appreciated you post.

10 May, 2008

Don replies:

I'd really appreciate seeing some code that works for a subscription model. I've also been thinking about picking up that PeepCode book. (The others I've bought have been helpful.) Do you happen to know if it has enough information relevant to Paypal (US)?

17 June, 2008

Matt Wigham declares:

This is great. I'd love to check out the code as well.

Leave a comment