14

ActiveMerchant, Paypal and Rails

Posted on 25 March 2008

by Robin Fisher

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

Geoffrey Grosenbach

Posted on 08 April, 2008

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

Jeff Lin

Posted on 09 April, 2008

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.

Andy Gordon

Posted on 15 April, 2008

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

Don

Posted on 10 May, 2008

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)?

Matt Wigham

Posted on 17 June, 2008

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

James

Posted on 02 July, 2008

I'd love to take a gander at your implementation as well. I've found lots of good info on payment processing with AM per-say, but little regarding subscriptions.

Sol

Posted on 07 July, 2008

I would love to take a look at your code. Great article. Thanks.

Byron Bowerman

Posted on 30 July, 2008

I'd also be interested in seeing some code, I'm working on my first subscription based app at the moment as well :)

Travis Beck

Posted on 13 August, 2008

I'd like to see the code as well. I'm currently testing with the sandbox.

Adam

Posted on 22 August, 2008

You said you wouldn't mind sharing your subscription code. Would I be able to get a copy please?

Chadwick

Posted on 16 September, 2008

Are you still sharing your subscription code with folks? I'm in the middle of implementing the same thing and would love to see your code. Many thanks!

John

Posted on 19 September, 2008

Hey, Just wondering if you were able to share the code. Would be very helpful. Thanks

Bob

Posted on 13 October, 2008

I'd be interested to see how you are doing this, as I am in a similar situation, just not quite so far down this path. Thanks!

Miguel Regedor

Posted on 14 November, 2008

I'm also trying to implement a recurring payment solution in the app I'm currently working, I need a subscription system and some kind of IPN for my app, everything seems more difficult because its an UK company, and most of the gateways don't work with UK companies. Would be very helpful if you're able to share the code! thx