Class: ActsAsSubscription::Subscription::Backend::ChargifyClient
- Inherits:
-
Object
- Object
- ActsAsSubscription::Subscription::Backend::ChargifyClient
- Defined in:
- lib/acts_as_subscription/backend/chargify_client.rb
Overview
A backend for the Chargify recurring billing service.
Instance Method Summary collapse
-
#cancel_subscription!(customer_code) ⇒ Object
Cancels the customer with the given
customer_code
on the backend subscription service. -
#create_subscription(subscription) ⇒ Object
Creates a customer on the backend subscription service, using the settings from the given
subscription
instance. -
#initialize(user, password, product_code) ⇒ ChargifyClient
constructor
Initializes the backend, and authenticates with the subscription service.
-
#plans ⇒ Object
Returns a list of subscription plans registered with the backend subscription service.
-
#update_subscription(subscription) ⇒ Object
Updates a customer on the backend subscription service, using the settings from the given
subscription
instance.
Constructor Details
#initialize(user, password, product_code) ⇒ ChargifyClient
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.
16 17 18 19 20 21 |
# File 'lib/acts_as_subscription/backend/chargify_client.rb', line 16 def initialize(user, password, product_code) Chargify.configure do |c| c.subdomain = user c.api_key = password end 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.
93 94 95 96 97 98 99 100 |
# File 'lib/acts_as_subscription/backend/chargify_client.rb', line 93 def cancel_subscription!(customer_code) sub = Chargify::Subscription.find_by_customer_reference(customer_code) response = sub.cancel doc = Hpricot::XML(response.body) return (response.code == "200" and (doc/:subscription/:state).text == 'canceled') rescue Exception => error return false 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.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/acts_as_subscription/backend/chargify_client.rb', line 34 def create_subscription(subscription) customer_attributes = self.get_customer_attributes(subscription) customer = Chargify::Customer.create(customer_attributes) if customer.errors.length == 0 subscription_attributes = self.get_subscription_attributes(subscription) subscription = Chargify::Subscription.create(subscription_attributes) if subscription.errors.length > 0 return subscription.errors[:base][0] else return true end else return customer.errors[:base][0] end return 'An unknown error occurred.' rescue Exception => error return error. end |
#plans ⇒ Object
Returns a list of subscription plans registered with the backend subscription service.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/acts_as_subscription/backend/chargify_client.rb', line 103 def plans result = [] Chargify::Product.all.each do |product| result << { :name => product.name, :code => product.handle, :active => true, # Doesn't seem to be an option to make inactive. :description => product.description, :billing_frequency => product.interval_unit, :recurring_charge => product.price_in_cents.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.
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 |
# File 'lib/acts_as_subscription/backend/chargify_client.rb', line 64 def update_subscription(subscription) customer = Chargify::Customer.find_by_reference(subscription.customer_code) customer.first_name = subscription.first_name, customer.last_name = subscription.last_name, customer.email = subscription.email, customer.reference = subscription.customer_code if customer.save sub = Chargify::Subscription.find_by_customer_reference(subscription.customer_code) sub.product_handle = subscription.plan_code subscription_attributes = self.get_subscription_attributes(subscription) # If the user is down-grading to a free plan, credit_card_attributes may not be available. if subscription_attributes[:credit_card_attributes] sub.credit_card_attributes = subscription_attributes[:credit_card_attributes] end if sub.save return true else return sub.errors[:base][0] end else return customer.errors[:base][0] end rescue Exception => error return error. end |