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
# File 'lib/chargify/client.rb', line 24

def initialize(api_key, subdomain)
  @api_key = api_key
  @subdomain = subdomain
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



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

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



125
126
127
128
129
130
131
132
133
134
# File 'lib/chargify/client.rb', line 125

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



59
60
61
62
63
# File 'lib/chargify/client.rb', line 59

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



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

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



35
36
37
38
39
40
# File 'lib/chargify/client.rb', line 35

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



42
43
44
45
46
47
# File 'lib/chargify/client.rb', line 42

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



80
81
82
83
# File 'lib/chargify/client.rb', line 80

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



173
174
175
176
# File 'lib/chargify/client.rb', line 173

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



30
31
32
33
# File 'lib/chargify/client.rb', line 30

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

#list_productsObject



143
144
145
146
# File 'lib/chargify/client.rb', line 143

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

#list_subscription_usage(subscription_id, component_id) ⇒ Object



156
157
158
159
160
161
# File 'lib/chargify/client.rb', line 156

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



136
137
138
139
140
141
# File 'lib/chargify/client.rb', line 136

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



148
149
150
# File 'lib/chargify/client.rb', line 148

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

#product_by_handle(handle) ⇒ Object



152
153
154
# File 'lib/chargify/client.rb', line 152

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

#reactivate_subscription(sub_id) ⇒ Object



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

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



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

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

#subscription(subscription_id) ⇒ Object



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

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



178
179
180
181
# File 'lib/chargify/client.rb', line 178

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

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



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

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



72
73
74
75
76
77
78
# File 'lib/chargify/client.rb', line 72

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



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

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_allocated_quantity(subscription_id, component_id, quantity) ⇒ Object



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

def update_subscription_component_allocated_quantity(subscription_id, component_id, quantity)
  response = put("/subscriptions/#{subscription_id}/components/#{component_id}.json", :body => {:component => {:allocated_quantity => quantity}})
  response[:success?] = response.code == 200
  Hashie::Mash.new(response)
end