acts_as_billable :plugin, :through => :rails
At Collective Idea, we’re developing several apps that take credit card payments. (We’ve previously blogged about sorting through this whole mine-field of credit card processing.) One of the ideas we came up with early on was using the magic of Ruby and Rails plugins to extract the billing and credit card processing logic into something that is reusable.
We started with ActiveMerchant, a handy little library developed for Shopify by JadedPixel. ActiveMerchant is great for abstracting the credit card processing API, but you still need to figure out what to charge for, how much, how often, and be able to pull up a history of payments.
Welcome to acts_as_billable, a plugin we’re working on that we think will remove some of the complexity from online billing and payments. To illustrate how it will work, here is an example.
class User < ActiveRecord::Base
belongs_to :plan
acts_as_billable :plan, :frequency => :monthly
end
class Plan < ActiveRecord::Base
has_many :users
end
When a user signs up for your super-duper-new-app, they can choose a plan. That plan determines what features they get, and how much they pay to use your app. So, the user should be billed (well charged, but chargable just doesn’t sound as nice) each month for the plan.
Another example:
class User < ActiveRecord::Base
has_many :registrations
acts_as_billable :event, :through => :registrations
end
class Registration < ActiveRecord::Base
belongs_to :user
belongs_to :event
end
class Event < ActiveRecord::Base
has_many :registrations
end
A user can register for events, which requires a payment. Users should then be billed for each event that they register for.
So what does acts_as_billable really do? Several things:
- It adds temporary fields for the credit card information and kicks off the processing through ActiveMerchant when a record is saved. So when an event is created, if the event costs money, it makes the API calls to process the credit card, returning errors if it fails.
- It adds validations for the credit card information, and validates that the credit card processed successfully.
- It adds polymorphic associations from the billable class (
Userin this case) toExchange(transactions) andInvoice. This gives you access to a record of all the user’s payments.
We’re pretty excited about what this plugin can do so far, and how easy it will make it for us to do more apps that require online payments. We intend to release this plugin as open source when we get it to a usable state. I’m sure there’s a lot we could do with it that we haven’t thought of, so we’d love some feedback.
17 Comments
Sounds really great – I’m just starting to dig in Payment and ActiveMerchant for a project I’m working on. Such a teaser – any idea about when you’ll be pushing acts_as_billable out?
Well, one of the projects that we’re using it for has to be ready for a client by July 24. So we have to have it in some form by then. I’d like to get something out in the next week or so to get some feedback and improve it.
Andrew,
Don’t use Payment. Stick to ActiveMerchant. Trust me.
Sounds cool! Can you give me an idea on what testing looks like? For example, how do I test the act_as_billable without actually sending the charges through a payment gateway?
Jeff,
Right now most of our testing is actually being done in our main app, and via ActiveMerchant. Before we release it, we’ll move that more into the plugin.
ActiveMerchant has a “Bogus Gateway” that I like to use for tests. Expect to see that in use in our tests.
Jeff,
There’s two different ways of testing. 1) ActiveMerchant includes a bogus gateway that you can use. 2) Most payment gateways will give you a test account to use. ActiveMerchant allows you to configure your payment gateway in the environment files.
Outstanding. I could use this right away. Like Andrew Turner, I am about to launch a project that requires charging, and I have put that off hoping some angel railist would drop a neat solution from the heavens. I hear wings flapping! Thanks and please hurry with a release!
This would be awesome for sites that want to offer payment plans for larger items – like paying for a car/boat/vacation… This could very easily solve the limited-duration recurring billing nightmares that anyone developing outside of a simple product + price + checkout the entire order right now scenario has to deal with. rock on.
Is it done yet is it done yet? :)
Any ETA on this great-sounding plugin? I know many people who are eagerly awaiting the release.
Kenny,
I sincerely apologize to you and everyone else waiting for this plugin for making a post about vaporware. It drives me nuts when people talk about some great peace of software but don’t release it, and here I did it myself.
We’ve been testing acts_as_billable in a project over the past month or so, but it’s somewhat tied to the implementation of that project. I will try to clean it up a bit and release it in the next couple days. I’m looking forward to getting everyones feedback and help to make it better!
Thanks for “nudging” me! :)
Hey Brandon – Any updates on this plugin? We are all waiting anxiously,
Hi Brandon,
Forgive me for not being patient enough… Are there any news on the plugin?
I need to make a decision RSN about writing one like this from scratch… That is all right, I just would like to know if there is a possibility for this plugin to get released. “No” is a perfectly understandable answer. Just please let me know :)
For those that have been waiting, I’ve rewritten acts_as_billable and have a version that I’m not ashamed of. However, it’s not ready for prime time yet, so I’d love to get some feedback.
There’s a start of a sample app (that uses rspec) that goes with it at
http://source.collectiveidea.com/public/acts_as_billable/samples/depotLet me know what you think! I’d like to get some feedback and announce this in the next week or so.
Hi,
I downloaded it and did a rake migrate and couldn’t able to proceed further and looking for a sample example on its usage.
Thank you
-Paddy
Look at the README and the sample project.
If you’re getting errors, please email them to me.
Hi. What if, when creating a new Event, you initially offer a free month, then bill X amount for the succeeding months? Is this possible with the present implementation?
@Brandon —
I’m going to be working a patch for Active Merchant to handle recurring payments (see http://code.google.com/p/activemerchant/issues/detail?id=78 for discussion).
If there is anything you have to contribute that isn’t provided in patches 78 and 54, please send it my way as soon as is feasible.
Thanks!