Class: Chargify::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/chargify/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, subdomain) ⇒ Client

Your API key can be generated on the settings screen.



24
25
26
27
28
29
30
31
# File 'lib/chargify/client.rb', line 24

def initialize(api_key, subdomain)
  @api_key = api_key
  @subdomain = subdomain
  
  self.class.base_uri "https://#{@subdomain}.chargify.com"
  self.class.basic_auth @api_key, 'x'
  
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



21
22
23
# File 'lib/chargify/client.rb', line 21

def api_key
  @api_key
end

#subdomainObject (readonly)

Returns the value of attribute subdomain.



21
22
23
# File 'lib/chargify/client.rb', line 21

def subdomain
  @subdomain
end

Instance Method Details

#adjust_subscription(sub_id, attributes = {}) ⇒ Object



147
148
149
150
151
152
153
# File 'lib/chargify/client.rb', line 147

def adjust_subscription(sub_id, attributes = {})
  raw_response = post("/subscriptions/#{sub_id}/adjustments.json",
                      :body => { :adjustment => attributes })
  created = true if raw_response.code == 201
  response = Hashie::Mash.new(raw_response)
  (response.adjustment || response).update(:success? => created)
end

#cancel_subscription(sub_id, message = "") ⇒ Object

Returns all elements outputted by Chargify plus: response.success? -> true if response code is 200, false otherwise



115
116
117
118
119
120
# File 'lib/chargify/client.rb', line 115

def cancel_subscription(sub_id, message="")
  raw_response = delete("/subscriptions/#{sub_id}.json", :body => {:subscription => {:cancellation_message => message} })
  deleted      = true if raw_response.code == 200
  response     = Hashie::Mash.new(raw_response)
  (response.subscription || response).update(:success? => deleted)
end

#charge_subscription(sub_id, subscription_attributes = {}) ⇒ Object



129
130
131
132
133
134
135
136
137
138
# File 'lib/chargify/client.rb', line 129

def charge_subscription(sub_id, subscription_attributes={})
  raw_response = post("/subscriptions/#{sub_id}/charges.json", :body => { :charge => subscription_attributes })
  success      = raw_response.code == 201
  if raw_response.code == 404
    raw_response = {}
  end

  response = Hashie::Mash.new(raw_response)
  (response.charge || response).update(:success? => success)
end

#create_customer(info = {}) ⇒ Object

  • first_name (Required)

  • last_name (Required)

  • email (Required)

  • organization (Optional) Company/Organization name

  • reference (Optional, but encouraged) The unique identifier used within your own application for this customer



63
64
65
66
67
# File 'lib/chargify/client.rb', line 63

def create_customer(info={})
  response = Hashie::Mash.new(post("/customers.json", :body => {:customer => info}))
  return response.customer if response.customer
  response
end

#create_subscription(subscription_attributes = {}) ⇒ Object

Returns all elements outputted by Chargify plus: response.success? -> true if response code is 201, false otherwise



97
98
99
100
101
102
# File 'lib/chargify/client.rb', line 97

def create_subscription(subscription_attributes={})
  raw_response = post("/subscriptions.json", :body => {:subscription => subscription_attributes})
  created  = true if raw_response.code == 201
  response = Hashie::Mash.new(raw_response)
  (response.subscription || response).update(:success? => created)
end

#customer_by_id(chargify_id) ⇒ Object



39
40
41
42
43
44
# File 'lib/chargify/client.rb', line 39

def customer_by_id(chargify_id)
  request = get("/customers/#{chargify_id}.json")
  success = request.code == 200
  response = Hashie::Mash.new(request).customer if success
  Hashie::Mash.new(response || {}).update(:success? => success)
end

#customer_by_reference(reference_id) ⇒ Object Also known as: customer



46
47
48
49
50
51
# File 'lib/chargify/client.rb', line 46

def customer_by_reference(reference_id)
  request = get("/customers/lookup.json?reference=#{reference_id}")
  success = request.code == 200
  response = Hashie::Mash.new(request).customer if success
  Hashie::Mash.new(response || {}).update(:success? => success)
end

#customer_subscriptions(chargify_id) ⇒ Object



84
85
86
87
# File 'lib/chargify/client.rb', line 84

def customer_subscriptions(chargify_id)
  subscriptions = get("/customers/#{chargify_id}/subscriptions.json")
  subscriptions.map{|s| Hashie::Mash.new s['subscription']}
end

#list_components(subscription_id) ⇒ Object



190
191
192
193
# File 'lib/chargify/client.rb', line 190

def list_components(subscription_id)
  components = get("/subscriptions/#{subscription_id}/components.json")
  components.map{|c| Hashie::Mash.new c['component']}
end

#list_customers(options = {}) ⇒ Object

options: page



34
35
36
37
# File 'lib/chargify/client.rb', line 34

def list_customers(options={})
  customers = get("/customers.json", :query => options)
  customers.map{|c| Hashie::Mash.new c['customer']}
end

#list_productsObject



155
156
157
158
# File 'lib/chargify/client.rb', line 155

def list_products
  products = get("/products.json")
  products.map{|p| Hashie::Mash.new p['product']}
end

#list_subscription_usage(subscription_id, component_id) ⇒ Object



168
169
170
171
172
173
# File 'lib/chargify/client.rb', line 168

def list_subscription_usage(subscription_id, component_id)
  raw_response = get("/subscriptions/#{subscription_id}/components/#{component_id}/usages.json")
  success      = raw_response.code == 200
  response     = Hashie::Mash.new(raw_response)
  response.update(:success? => success)
end

#migrate_subscription(sub_id, product_id) ⇒ Object



140
141
142
143
144
145
# File 'lib/chargify/client.rb', line 140

def migrate_subscription(sub_id, product_id)
  raw_response = post("/subscriptions/#{sub_id}/migrations.json", :body => {:product_id => product_id })
  success      = true if raw_response.code == 200
  response     = Hashie::Mash.new(raw_response)
  (response.subscription || {}).update(:success? => success)
end

#product(product_id) ⇒ Object



160
161
162
# File 'lib/chargify/client.rb', line 160

def product(product_id)
  Hashie::Mash.new( get("/products/#{product_id}.json")).product
end

#product_by_handle(handle) ⇒ Object



164
165
166
# File 'lib/chargify/client.rb', line 164

def product_by_handle(handle)
  Hashie::Mash.new(get("/products/handle/#{handle}.json")).product
end

#reactivate_subscription(sub_id) ⇒ Object



122
123
124
125
126
127
# File 'lib/chargify/client.rb', line 122

def reactivate_subscription(sub_id)
  raw_response = put("/subscriptions/#{sub_id}/reactivate.json", :body => "")
  reactivated  = true if raw_response.code == 200
  response     = Hashie::Mash.new(raw_response) rescue Hashie::Mash.new
  (response.subscription || response).update(:success? => reactivated)
end

#site_transactions(options = {}) ⇒ Object



185
186
187
188
# File 'lib/chargify/client.rb', line 185

def site_transactions(options={})
  transactions = get("/transactions.json", :query => options)
  transactions.map{|t| Hashie::Mash.new t['transaction']}
end

#subscription(subscription_id) ⇒ Object



89
90
91
92
93
# File 'lib/chargify/client.rb', line 89

def subscription(subscription_id)
  raw_response = get("/subscriptions/#{subscription_id}.json")
  return nil if raw_response.code != 200
  Hashie::Mash.new(raw_response).subscription
end

#subscription_component(subscription_id, component_id) ⇒ Object



195
196
197
198
# File 'lib/chargify/client.rb', line 195

def subscription_component(subscription_id, component_id)
  response = get("/subscriptions/#{subscription_id}/components/#{component_id}.json")
  Hashie::Mash.new(response).component
end

#subscription_statements(sub_id, options = {}) ⇒ Object



180
181
182
183
# File 'lib/chargify/client.rb', line 180

def subscription_statements(sub_id, options={})
  statements = get("/subscriptions/#{sub_id}/statements.json", :query => options)
  statements.map{|t| Hashie::Mash.new t['statement']}
end

#subscription_transactions(sub_id, options = {}) ⇒ Object



175
176
177
178
# File 'lib/chargify/client.rb', line 175

def subscription_transactions(sub_id, options={})
  transactions = get("/subscriptions/#{sub_id}/transactions.json", :query => options)
  transactions.map{|t| Hashie::Mash.new t['transaction']}
end

#update_customer(info = {}) ⇒ Object

  • first_name (Required)

  • last_name (Required)

  • email (Required)

  • organization (Optional) Company/Organization name

  • reference (Optional, but encouraged) The unique identifier used within your own application for this customer



76
77
78
79
80
81
82
# File 'lib/chargify/client.rb', line 76

def update_customer(info={})
  info.stringify_keys!
  chargify_id = info.delete('id')
  response = Hashie::Mash.new(put("/customers/#{chargify_id}.json", :body => {:customer => info}))
  return response.customer unless response.customer.to_a.empty?
  response
end

#update_subscription(sub_id, subscription_attributes = {}) ⇒ Object

Returns all elements outputted by Chargify plus: response.success? -> true if response code is 200, false otherwise



106
107
108
109
110
111
# File 'lib/chargify/client.rb', line 106

def update_subscription(sub_id, subscription_attributes = {})
  raw_response = put("/subscriptions/#{sub_id}.json", :body => {:subscription => subscription_attributes})
  updated      = true if raw_response.code == 200
  response     = Hashie::Mash.new(raw_response)
  (response.subscription || response).update(:success? => updated)
end

#update_subscription_component(subscription_id, component_id, component = {}) ⇒ Object Also known as: update_component



208
209
210
211
212
213
214
# File 'lib/chargify/client.rb', line 208

def update_subscription_component(subscription_id, component_id, component = {})
  component[:enabled] = (component[:enabled] ? 1 : 0) if component.keys.include?(:enabled)
  response = put("/subscriptions/#{subscription_id}/components/#{component_id}.json", 
                :body => {:component => component})
  response[:success?] = response.code == 200
  Hashie::Mash.new(response)
end

#update_subscription_component_allocated_quantity(subscription_id, component_id, quantity) ⇒ Object Also known as: update_metered_component, update_component_quantity



200
201
202
# File 'lib/chargify/client.rb', line 200

def update_subscription_component_allocated_quantity(subscription_id, component_id, quantity)
  update_subscription_component(subscription_id, component_id, :allocated_quantity => quantity)
end

#update_subscription_component_enabled(subscription_id, component_id, enabled) ⇒ Object Also known as: update_on_off_component



204
205
206
# File 'lib/chargify/client.rb', line 204

def update_subscription_component_enabled(subscription_id, component_id, enabled)
  update_subscription_component(subscription_id, component_id, :enabled => enabled)
end