Module: ActsAsSubscription::Subscription::ClassMethods

Defined in:
lib/acts_as_subscription/subscription.rb

Overview

Class methods for an ActsAsSubscription::Subscription instance.

Handles validation of all subscription-related information, such as :

  • customer_code

  • email

  • first_name

  • last_name

  • zip_code

  • cc_expiration_month

  • cc_expiration_year

  • cc_verification_value

  • cc_number

These class methods should be included in ActiveRecord::Base to provide subscription validation support.

Instance Method Summary collapse

Instance Method Details

#acts_as_subscription(*args) ⇒ Object

Main entry point - any subclass of ActiveRecord::Base can include the acts_as_subscription method to activate subscription functionality.

args should be a hash containing the subscription backend account and product information :

  • backend : Name of the recurring billing service being used. Current choices are :chargify, :cheddar_getter and :recurly.

  • user : Username used to log into the billling service API.

  • password : Password used to log into the billling service API.

  • product_code : Code of your product on the backend service.

There are three ways to initialize the backend :

  1. Pass the parameters to the acts_as_subscription method from within a model :

    class Subscription < ActiveRecord::Base
      acts_as_subscription :backend      => :cheddar_getter,
                           :user         => 'my_user',
                           :password     => 'my_pass',
                           :product_code => 'my_product'
    end
    
  2. Set the params directly via the Backend module :

    ActsAsSubscription::Subscription::Backend.config = {
      :backend      => :cheddar_getter,
      :user         => 'my_user',
      :password     => 'my_pass',
      :product_code => 'my_product'
    }
    
  3. Add a config/subscription.yml file to your rails project :

    development:
      backend:      'cheddar_getter'
      user:         'my_user'
      password:     'my_pass'
      product_code: 'my_product'
    


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/acts_as_subscription/subscription.rb', line 60

def acts_as_subscription(*args)
  
  ActsAsSubscription::Subscription::Backend.config = *args
  ActsAsSubscription::Subscription::Backend.initialize
  
  validates :customer_code,
    :presence => true
  
  validates :email,
    :presence => true
  
  validates :first_name,
    :presence => true

  validates :last_name,
    :presence => true

  validates :plan_code,
    :presence => true

  validates :zip_code,
    :presence => true,
    :if       => :require_zip_code?
    
  validates :cc_expiration_month,
    :presence => true,
    :unless   => :is_free_plan? 
    
  validates :cc_expiration_year,
    :presence => true,
    :unless   => :is_free_plan? 
    
  validates :cc_verification_value,
    :presence => true,
    :if       => :require_verification_value? 
    
  validates :cc_number,
    :presence => true,
    :unless   => :is_free_plan? 
  
  validates_associated :credit_card,
    :unless => :is_free_plan? 

  before_validation :update_credit_card
    
  before_validation :assign_customer_code,    
    :on => :create
    
  validate :validate_credit_card
  
  attr_accessible :cc_expiration_month,
                  :cc_expiration_year,
                  :cc_number,
                  :cc_verification_value,
                  :customer_code,
                  :email,
                  :first_name,
                  :last_name,
                  :plan_code,
                  :zip_code
  
  # Removed this for the time being, since we often want to perform this before_save
  # on a different model (such as User), if the forms are nested. Having before_save
  # on both models causes quite a few problems. This before_save must be specified 
  # by hand in the implementing model.
  #before_save :backend_save
  
  self.send(:include, ActsAsSubscription::Subscription::SubscriptionInstanceMethods)
  
end