Module: ActsAsSubscription::Subscription::Backend

Defined in:
lib/acts_as_subscription/backend.rb,
lib/acts_as_subscription/backend/dummy_client.rb,
lib/acts_as_subscription/backend/recurly_client.rb,
lib/acts_as_subscription/backend/chargify_client.rb,
lib/acts_as_subscription/backend/cheddar_getter_client.rb

Overview

Provides a consistent interface to the various recurring billing services.

There are three ways to initialize the backend :

  1. Pass the parameters to the acts_as_subscription method inside the 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 in rails via config/initializers/acts_as_subscription.rb :

    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'
    

Defined Under Namespace

Classes: ChargifyClient, CheddarGetterClient, DummyClient, RecurlyClient

Class Method Summary collapse

Class Method Details

.cancel_subscription!(customer_code) ⇒ Object

Cancels the customer with the given customer_code on the backend subscription service.

Returns true if the cancellation was successful, otherwise an error message.



120
121
122
# File 'lib/acts_as_subscription/backend.rb', line 120

def self.cancel_subscription!(customer_code)
  self.instance.cancel_subscription!(customer_code)
end

.config=(*args) ⇒ Object

Sets the configuration hash for the backend.

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.

If the backend configuration has already been set, args will be merged with the current settings.



53
54
55
56
57
58
59
60
61
# File 'lib/acts_as_subscription/backend.rb', line 53

def self.config=(*args)
  args = args.extract_options!
  
  if @@config
    @@config = self.config.merge(args)
  else
    @@config = args
  end
end

.create_subscription(subscription) ⇒ Object

Creates a customer on the backend subscription service, using the settings from the given subscription instance.

subscription should be a subclass of ActiveRecord::Base that implements acts_as_subscription :

class Subscription < ActiveRecord::Base
  acts_as_subscription
end

Returns true if the operation was successful, otherwise an error message.



98
99
100
# File 'lib/acts_as_subscription/backend.rb', line 98

def self.create_subscription(subscription)
  self.instance.create_subscription(subscription)
end

.initialize(*args) ⇒ Object

Validates the configuration options, and creates an instance of the requested :backend.

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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/acts_as_subscription/backend.rb', line 66

def self.initialize(*args)
  # Don't do anything if the backend has already been loaded.
  return if self.instance
  
  self.read_yaml_options
  self.config = *args        
  self.validate_options!
  
  # Make sure the backend is a valid class.
  begin
    require "acts_as_subscription/backend/#{self.config[:backend]}_client"
    class_name    = "#{self.config[:backend].to_s}_client".camelize
    klass         = ActsAsSubscription::Subscription::Backend.const_get(class_name)
    self.instance = klass.new(self.config[:user], self.config[:password], self.config[:product_code])
  rescue LoadError
    raise ActsAsSubscription::Subscription::Error::BackendError.new("Backend '#{self.config[:backend]}' could not be found") 
  rescue NameError
    raise ActsAsSubscription::Subscription::Error::BackendError.new("Backend '#{self.config[:backend]}' does not appear to implement class '#{class_name}'") 
  end
end

.plansObject

Returns a list of subscription plans registered with the backend subscription service.



125
126
127
# File 'lib/acts_as_subscription/backend.rb', line 125

def self.plans
  self.instance.plans
end

.update_subscription(subscription) ⇒ Object

Updates a customer on the backend subscription service, using the settings from the given subscription instance.

subscription should be a subclass of ActiveRecord::Base that implements acts_as_subscription :

class Subscription < ActiveRecord::Base
  acts_as_subscription
end

Returns true if the update was successful, otherwise an error message.



113
114
115
# File 'lib/acts_as_subscription/backend.rb', line 113

def self.update_subscription(subscription)
  self.instance.update_subscription(subscription)
end