You are here: Browse Railsplugins Acts As Billable
= acts_as_billable
acts_as_billable is an Active Record extension that eases the development of applications that process credit card transactions.
RequirementsFirst you must define a default Credit Card gateway in environment.rb. Typically you want different settings in development and production, so you may want something like this:
acts_as_billable uses four tables:
To create these tables, run the billable_migration generator:
script/generate billable_migration add_billable_tables
And run:
rake db:migrate
=== Declare your billable models
class User < ActiveRecord::Base
acts_as_billable
end
=== Declare your sellable models
class Product < ActiveRecord::Base
acts_as_sellable
end
See the notes for acts_as_sellable[link:classes/CollectiveIdea/Acts/Sellable/ClassMethods.html#M000005]
Usage
@order = @user.purchase Product.find(1)
@order.pay ActiveMerchant::Billing::CreditCard.new :credit_card => {
:type => 'visa',
:number => '1234567812345678,
:month => '10',
:year => '2007',
:verification_value => '678',
:first_name => 'First',
:last_name => 'Last'
}
= Controllers
def create
@credit_card = ActiveMerchant::Billing::CreditCard.new(params[:credit_card])
if @credit_card.valid?
@order = current_user.purchase(@cart.items)
@payment = @order.pay(@credit_card)
end
rescue Payment::AuthorizationError => error
@order.destroy
flash[:notice] = error.message
render :action => ‘new’
end
Running the specs
Tests are written using RSpec in a sample app. To run the specs, check out http://source.collectiveidea.com/public/acts_as_billable and, after configuring your database, run "rake spec". It would be appreciated if all patches have specs to go with them.
To DoNOTE: This description has been extracted from the Plugin README and so the formatting may need updating to make browser friendly