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

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

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



102
103
104
105
106
107
# File 'lib/chargify/client.rb', line 102

def cancel_subscription(sub_id, message="")
  raw_response = self.class.delete("/subscriptions/#{sub_id}.json", :body => {:subscription => {:cancellation_message => message} }.to_json)
  deleted      = true if raw_response.code == 200
  response     = Hashie::Mash.new(raw_response)
  (response.subscription || response).update(:success? => deleted)
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



50
51
52
53
54
# File 'lib/chargify/client.rb', line 50

def create_customer(info={})
  response = Hashie::Mash.new(self.class.post("/customers.json", :body => {:customer => info}.to_json))
  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



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

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

#customer(chargify_id) ⇒ Object



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

def customer(chargify_id)
  Hashie::Mash.new(self.class.get("/customers/lookup.json?reference=#{chargify_id}")).customer
end

#customer_subscriptions(chargify_id) ⇒ Object



71
72
73
74
# File 'lib/chargify/client.rb', line 71

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

#list_customers(options = {}) ⇒ Object

options: page



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

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

#list_productsObject



109
110
111
112
# File 'lib/chargify/client.rb', line 109

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

#product(product_id) ⇒ Object



114
115
116
# File 'lib/chargify/client.rb', line 114

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

#product_by_handle(handle) ⇒ Object



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

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

#subscription(subscription_id) ⇒ Object



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

def subscription(subscription_id)
  raw_response = self.class.get("/subscriptions/#{subscription_id}.json")
  return nil if raw_response.code != 200
  Hashie::Mash.new(raw_response).subscription
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



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

def update_customer(info={})
  info.stringify_keys!
  chargify_id = info.delete('id')
  response = Hashie::Mash.new(self.class.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



93
94
95
96
97
98
# File 'lib/chargify/client.rb', line 93

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