opensoul.org

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:

  1. 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.
  2. It adds validations for the credit card information, and validates that the credit card processed successfully.
  3. It adds polymorphic associations from the billable class (User in this case) to Exchange (transactions) and Invoice. 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.

plugin and rails July 13, 2006

17 Comments

  1. Andrew Turner Andrew Turner July 14, 2006

    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?

  2. brandon brandon July 14, 2006

    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.

  3. Daniel Morrison Daniel Morrison July 14, 2006

    Andrew,

    Don’t use Payment. Stick to ActiveMerchant. Trust me.

  4. Jeff Jeff July 14, 2006

    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?

  5. Daniel Morrison Daniel Morrison July 14, 2006

    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.

  6. brandon brandon July 14, 2006

    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.

  7. Dan Milliron Dan Milliron July 14, 2006

    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!

  8. Cerise Cerise August 29, 2006

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

  9. Kenny Kenny September 24, 2006

    Any ETA on this great-sounding plugin? I know many people who are eagerly awaiting the release.

  10. Brandon Brandon September 24, 2006

    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! :)

  11. Prashant Chaudhary Prashant Chaudhary October 3, 2006

    Hey Brandon – Any updates on this plugin? We are all waiting anxiously,

  12. Andras Kristof Andras Kristof October 31, 2006

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

  13. Brandon Brandon December 11, 2006

    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.

    script/plugin install http://source.collectiveidea.com/public/acts_as_billable/trunk/
    

    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/depot

    Let me know what you think! I’d like to get some feedback and announce this in the next week or so.

  14. Paddy Paddy December 13, 2006

    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

  15. Brandon Brandon December 13, 2006

    Look at the README and the sample project.

    If you’re getting errors, please email them to me.

  16. Bobby Bobby January 2, 2008

    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?

  17. Ian Lotinsky Ian Lotinsky January 15, 2008

    @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!

My name is Brandon Keepers. I like to build things, usually in Ruby or JavaScript. I work at GitHub and live in Holland, MI.

Popular Posts