Class: ActsAsSubscription::Subscription::Backend::CheddarGetterClient

Inherits:
Object
  • Object
show all
Defined in:
lib/acts_as_subscription/backend/cheddar_getter_client.rb

Overview

A backend for the CheddarGetter recurring billing service.

cheddargetter.com

Instance Method Summary collapse

Constructor Details

#initialize(user, password, product_code) ⇒ CheddarGetterClient

Initializes the backend, and authenticates with the subscription service.

Required parameters are :

  • user : The login used to connect to the remote API.

  • password : The password used to connect to the remote API.

  • product_code : The code for the product being sold.



14
15
16
17
# File 'lib/acts_as_subscription/backend/cheddar_getter_client.rb', line 14

def initialize(user, password, product_code)
  Mousetrap.product_code = product_code
  Mousetrap.authenticate(user, password)
end

Instance 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, or false otherwise.



61
62
63
64
65
# File 'lib/acts_as_subscription/backend/cheddar_getter_client.rb', line 61

def cancel_subscription!(customer_code)
  customer = Mousetrap::Customer[customer_code]
  result = customer.try(:cancel) 
  return (result != nil)
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.



30
31
32
33
34
35
36
# File 'lib/acts_as_subscription/backend/cheddar_getter_client.rb', line 30

def create_subscription(subscription)
  customer_attributes = self.get_customer_attributes(subscription)
  Mousetrap::Customer.create customer_attributes
  return true
rescue Exception => error
  return error.message
end

#plansObject

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



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

def plans
  result = []
  Mousetrap::Plan.all.each do |plan|
    result << {
      :name              => plan.name,
      :code              => plan.code,
      :active            => (plan.active == "1"),
      :description       => plan.description,
      # CheddarGetter returns 'monthly' etc instead of 'month' - remove trailing 'ly'.
      :billing_frequency => plan.billing_frequency.sub(/ly$/i, ''), 
      :recurring_charge  => plan.recurring_charge.to_f
    }
  end
  
  return result
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.



49
50
51
52
53
54
55
56
# File 'lib/acts_as_subscription/backend/cheddar_getter_client.rb', line 49

def update_subscription(subscription)
  customer_attributes = self.get_customer_attributes(subscription)
  customer = Mousetrap::Customer.new(customer_attributes)
  customer.save
  return true
rescue Exception => error
  return error.message
end