lucidimagination-chargify 0.2.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/.gitignore +26 -0
- data/LICENSE +20 -0
- data/README.markdown +31 -0
- data/Rakefile +15 -0
- data/VERSION +1 -0
- data/changelog.md +21 -0
- data/lib/chargify.rb +13 -0
- data/lib/chargify/client.rb +237 -0
- data/lucidimagination-chargify.gemspec +82 -0
- data/test/chargify_test.rb +432 -0
- data/test/fixtures/customer.json +12 -0
- data/test/fixtures/customers.json +12 -0
- data/test/fixtures/deleted_subscription.json +1 -0
- data/test/fixtures/invalid_subscription.json +48 -0
- data/test/fixtures/new_customer.json +12 -0
- data/test/fixtures/product.json +17 -0
- data/test/fixtures/products.json +17 -0
- data/test/fixtures/subscription.json +49 -0
- data/test/fixtures/subscription_not_found.json +1 -0
- data/test/fixtures/subscriptions.json +49 -0
- data/test/helper.rb +52 -0
- metadata +203 -0
@@ -0,0 +1,432 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class ChargifyTest < Test::Unit::TestCase
|
4
|
+
context "Chargify API client" do
|
5
|
+
setup do
|
6
|
+
@client = Chargify::Client.new('OU812', 'pengwynn')
|
7
|
+
end
|
8
|
+
|
9
|
+
should "return a list of customers" do
|
10
|
+
stub_get "https://OU812:x@pengwynn.chargify.com/customers.json", "customers.json"
|
11
|
+
customers = @client.list_customers
|
12
|
+
customers.size.should == 1
|
13
|
+
customers.first.reference.should == 'bradleyjoyce'
|
14
|
+
customers.first.organization.should == 'Squeejee'
|
15
|
+
end
|
16
|
+
|
17
|
+
context "when finding customers" do
|
18
|
+
should "be able to be found by a <reference_id>" do
|
19
|
+
stub_get "https://OU812:x@pengwynn.chargify.com/customers/lookup.json?reference=bradleyjoyce", "customer.json"
|
20
|
+
customer = @client.customer("bradleyjoyce")
|
21
|
+
customer.success?.should == true
|
22
|
+
end
|
23
|
+
|
24
|
+
should "be able to be found by a <chargify_id>" do
|
25
|
+
stub_get "https://OU812:x@pengwynn.chargify.com/customers/16.json", "customer.json"
|
26
|
+
customer = @client.customer_by_id(16)
|
27
|
+
customer.success?.should == true
|
28
|
+
end
|
29
|
+
|
30
|
+
should "return an empty Hash with success? set to false" do
|
31
|
+
stub_get "https://OU812:x@pengwynn.chargify.com/customers/16.json", "", 404
|
32
|
+
customer = @client.customer_by_id(16)
|
33
|
+
customer.success?.should == false
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
should "create a new customer" do
|
38
|
+
stub_post "https://OU812:x@pengwynn.chargify.com/customers.json", "new_customer.json"
|
39
|
+
info = {
|
40
|
+
:first_name => "Wynn",
|
41
|
+
:last_name => "Netherland",
|
42
|
+
:email => "wynn@example.com"
|
43
|
+
}
|
44
|
+
customer = @client.create_customer(info)
|
45
|
+
customer.first_name.should == "Wynn"
|
46
|
+
end
|
47
|
+
|
48
|
+
should "update a customer" do
|
49
|
+
stub_put "https://OU812:x@pengwynn.chargify.com/customers/16.json", "customer.json"
|
50
|
+
options = {
|
51
|
+
:id => 16,
|
52
|
+
:organization => "Squeejee",
|
53
|
+
:first_name => "Bradley",
|
54
|
+
:last_name => "Joyce",
|
55
|
+
:email => "bradley@squeejee.com"
|
56
|
+
}
|
57
|
+
customer = @client.update_customer(options)
|
58
|
+
customer.first_name.should == 'Bradley'
|
59
|
+
end
|
60
|
+
|
61
|
+
should "set success? to true when customer is updated successfully" do
|
62
|
+
stub_put "https://OU812:x@pengwynn.chargify.com/customers/16.json", "customer.json", 200
|
63
|
+
options = {
|
64
|
+
:id => 16,
|
65
|
+
:organization => "Squeejee",
|
66
|
+
:first_name => "Bradley",
|
67
|
+
:last_name => "Joyce",
|
68
|
+
:email => "bradley@squeejee.com"
|
69
|
+
}
|
70
|
+
customer = @client.update_customer(options)
|
71
|
+
customer.success?.should == true
|
72
|
+
end
|
73
|
+
|
74
|
+
should "set success? to false when customer is not updated successfully" do
|
75
|
+
stub_put "https://OU812:x@pengwynn.chargify.com/customers/16.json", "customer.json", 500
|
76
|
+
options = {
|
77
|
+
:id => 16,
|
78
|
+
:organization => "Squeejee",
|
79
|
+
:first_name => "Bradley",
|
80
|
+
:last_name => "Joyce",
|
81
|
+
:email => "bradley@squeejee.com"
|
82
|
+
}
|
83
|
+
customer = @client.update_customer(options)
|
84
|
+
customer.success?.should == nil
|
85
|
+
end
|
86
|
+
|
87
|
+
# Depends on Chargify:
|
88
|
+
# should_eventually "delete a customer" do
|
89
|
+
#
|
90
|
+
# end
|
91
|
+
|
92
|
+
should "raise UnexpectedResponseError when reponse is invalid JSON" do
|
93
|
+
stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions.json", "invalid_subscription.json"
|
94
|
+
options = {
|
95
|
+
:product_handle => 'monthly',
|
96
|
+
:customer_reference => 'bradleyjoyce',
|
97
|
+
:customer_attributes => {
|
98
|
+
:first_name => "Wynn",
|
99
|
+
:last_name => "Netherland",
|
100
|
+
:email => "wynn@example.com"
|
101
|
+
}
|
102
|
+
}
|
103
|
+
assert_raise Chargify::UnexpectedResponseError do
|
104
|
+
@client.create_subscription(options)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
should "return a list of customer subscriptions" do
|
109
|
+
stub_get "https://OU812:x@pengwynn.chargify.com/customers/16/subscriptions.json", "subscriptions.json"
|
110
|
+
subscriptions = @client.customer_subscriptions(16)
|
111
|
+
subscriptions.size.should == 1
|
112
|
+
subscriptions.first.customer.reference.should == "bradleyjoyce"
|
113
|
+
end
|
114
|
+
|
115
|
+
|
116
|
+
should "return info for a subscription" do
|
117
|
+
stub_get "https://OU812:x@pengwynn.chargify.com/subscriptions/13.json", "subscription.json"
|
118
|
+
subscription = @client.subscription(13)
|
119
|
+
subscription.customer.reference.should == 'bradleyjoyce'
|
120
|
+
end
|
121
|
+
|
122
|
+
should "return nil if a subscription is not found" do
|
123
|
+
stub_get "https://OU812:x@pengwynn.chargify.com/subscriptions/18.json", "subscription_not_found.json", 404
|
124
|
+
subscription = @client.subscription(18)
|
125
|
+
subscription.should == nil
|
126
|
+
end
|
127
|
+
|
128
|
+
should "update a customer subscription" do
|
129
|
+
stub_put "https://OU812:x@pengwynn.chargify.com/subscriptions/123.json", "subscription.json"
|
130
|
+
options = {
|
131
|
+
:product_handle => 'monthly',
|
132
|
+
:customer_reference => 'bradleyjoyce',
|
133
|
+
:customer_attributes => {
|
134
|
+
:first_name => "Wynn",
|
135
|
+
:last_name => "Netherland",
|
136
|
+
:email => "wynn@example.com"
|
137
|
+
}
|
138
|
+
}
|
139
|
+
subscription = @client.update_subscription(123, options)
|
140
|
+
subscription.customer.organization.should == 'Squeejee'
|
141
|
+
end
|
142
|
+
|
143
|
+
should "set success? to true when subscription is updated successfully" do
|
144
|
+
stub_put "https://OU812:x@pengwynn.chargify.com/subscriptions/123.json", "subscription.json", 200
|
145
|
+
options = {
|
146
|
+
:product_handle => 'monthly',
|
147
|
+
:customer_reference => 'bradleyjoyce',
|
148
|
+
:customer_attributes => {
|
149
|
+
:first_name => "Wynn",
|
150
|
+
:last_name => "Netherland",
|
151
|
+
:email => "wynn@example.com"
|
152
|
+
}
|
153
|
+
}
|
154
|
+
subscription = @client.update_subscription(123, options)
|
155
|
+
subscription.success?.should == true
|
156
|
+
end
|
157
|
+
|
158
|
+
should "set success? to false when subscription is not updated successfully" do
|
159
|
+
stub_put "https://OU812:x@pengwynn.chargify.com/subscriptions/123.json", "subscription.json", 500
|
160
|
+
options = {
|
161
|
+
:product_handle => 'monthly',
|
162
|
+
:customer_reference => 'bradleyjoyce',
|
163
|
+
:customer_attributes => {
|
164
|
+
:first_name => "Wynn",
|
165
|
+
:last_name => "Netherland",
|
166
|
+
:email => "wynn@example.com"
|
167
|
+
}
|
168
|
+
}
|
169
|
+
subscription = @client.update_subscription(123, options)
|
170
|
+
subscription.success?.should == nil
|
171
|
+
end
|
172
|
+
|
173
|
+
should "create a customer subscription" do
|
174
|
+
stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions.json", "subscription.json"
|
175
|
+
options = {
|
176
|
+
:product_handle => 'monthly',
|
177
|
+
:customer_reference => 'bradleyjoyce',
|
178
|
+
:customer_attributes => {
|
179
|
+
:first_name => "Wynn",
|
180
|
+
:last_name => "Netherland",
|
181
|
+
:email => "wynn@example.com"
|
182
|
+
}
|
183
|
+
}
|
184
|
+
subscription = @client.create_subscription(options)
|
185
|
+
subscription.customer.organization.should == 'Squeejee'
|
186
|
+
end
|
187
|
+
|
188
|
+
should "create a customer subscription with a coupon code" do
|
189
|
+
stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions.json", "subscription.json"
|
190
|
+
options = {
|
191
|
+
:product_handle => 'monthly',
|
192
|
+
:customer_reference => 'bradleyjoyce',
|
193
|
+
:customer_attributes => {
|
194
|
+
:first_name => "Wynn",
|
195
|
+
:last_name => "Netherland",
|
196
|
+
:email => "wynn@example.com"
|
197
|
+
},
|
198
|
+
:coupon_code => "EARLYBIRD"
|
199
|
+
}
|
200
|
+
subscription = @client.create_subscription(options)
|
201
|
+
#subscription.coupon.should == 'Squeejee'
|
202
|
+
end
|
203
|
+
|
204
|
+
should "set success? to true when subscription is created successfully" do
|
205
|
+
stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions.json", "subscription.json", 201
|
206
|
+
options = {
|
207
|
+
:product_handle => 'monthly',
|
208
|
+
:customer_reference => 'bradleyjoyce',
|
209
|
+
:customer_attributes => {
|
210
|
+
:first_name => "Wynn",
|
211
|
+
:last_name => "Netherland",
|
212
|
+
:email => "wynn@example.com"
|
213
|
+
}
|
214
|
+
}
|
215
|
+
subscription = @client.create_subscription(options)
|
216
|
+
subscription.success?.should == true
|
217
|
+
end
|
218
|
+
|
219
|
+
should "set success? to nil when subscription is not created successfully" do
|
220
|
+
stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions.json", "subscription.json", 400
|
221
|
+
options = {
|
222
|
+
:product_handle => 'monthly',
|
223
|
+
:customer_reference => 'bradleyjoyce',
|
224
|
+
:customer_attributes => {
|
225
|
+
:first_name => "Wynn",
|
226
|
+
:last_name => "Netherland",
|
227
|
+
:email => "wynn@example.com"
|
228
|
+
}
|
229
|
+
}
|
230
|
+
subscription = @client.create_subscription(options)
|
231
|
+
subscription.success?.should == nil
|
232
|
+
end
|
233
|
+
|
234
|
+
should "reactivate a subscription" do
|
235
|
+
stub_put "https://OU812:x@pengwynn.chargify.com/subscriptions/123/reactivate.json", "subscription.json", 200
|
236
|
+
subscription = @client.reactivate_subscription(123)
|
237
|
+
|
238
|
+
subscription.state.should == "active"
|
239
|
+
end
|
240
|
+
|
241
|
+
should "set success? to nil when subscription is not reactivated successfully" do
|
242
|
+
stub_put "https://OU812:x@pengwynn.chargify.com/subscriptions/123/reactivate.json", "subscription_not_found.json", 500
|
243
|
+
subscription = @client.reactivate_subscription(123)
|
244
|
+
|
245
|
+
subscription.success?.should == nil
|
246
|
+
end
|
247
|
+
|
248
|
+
should "set success? to false when subscription is reactivated successfully" do
|
249
|
+
stub_put "https://OU812:x@pengwynn.chargify.com/subscriptions/123/reactivate.json", "subscription.json", 200
|
250
|
+
subscription = @client.reactivate_subscription(123)
|
251
|
+
|
252
|
+
subscription.success?.should == true
|
253
|
+
end
|
254
|
+
|
255
|
+
should "cancel subscription" do
|
256
|
+
stub_delete "https://OU812:x@pengwynn.chargify.com/subscriptions/123.json", "deleted_subscription.json", 200
|
257
|
+
subscription = @client.cancel_subscription(123)
|
258
|
+
|
259
|
+
subscription.state.should == "canceled"
|
260
|
+
end
|
261
|
+
|
262
|
+
should "set success? to nil when subscription is not cancelled successfully" do
|
263
|
+
stub_delete "https://OU812:x@pengwynn.chargify.com/subscriptions/123.json", "deleted_subscription.json", 500
|
264
|
+
subscription = @client.cancel_subscription(123)
|
265
|
+
|
266
|
+
subscription.success?.should == nil
|
267
|
+
end
|
268
|
+
|
269
|
+
should "set success? to true when subscription is cancelled successfully" do
|
270
|
+
stub_delete "https://OU812:x@pengwynn.chargify.com/subscriptions/123.json", "deleted_subscription.json", 200
|
271
|
+
subscription = @client.cancel_subscription(123)
|
272
|
+
|
273
|
+
subscription.success?.should == true
|
274
|
+
end
|
275
|
+
|
276
|
+
context "when creating a one-off charge for a subscription" do
|
277
|
+
setup do
|
278
|
+
stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions/123/charges.json", "charge_subscription.json", 201
|
279
|
+
@options = {
|
280
|
+
:memo => "This is the description of the one time charge.",
|
281
|
+
:amount => 1.00,
|
282
|
+
:amount_in_cents => 100
|
283
|
+
}
|
284
|
+
end
|
285
|
+
|
286
|
+
should "accept :amount as a parameter" do
|
287
|
+
subscription = @client.charge_subscription(123, @options)
|
288
|
+
|
289
|
+
subscription.amount_in_cents.should == @options[:amount]*100
|
290
|
+
subscription.success?.should == true
|
291
|
+
end
|
292
|
+
|
293
|
+
should "accept :amount_in_cents as a parameter" do
|
294
|
+
subscription = @client.charge_subscription(123, @options)
|
295
|
+
|
296
|
+
subscription.amount_in_cents.should == @options[:amount_in_cents]
|
297
|
+
subscription.success?.should == true
|
298
|
+
end
|
299
|
+
|
300
|
+
should "have success? as false if parameters are missing" do
|
301
|
+
stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions/123/charges.json", "charge_subscription_missing_parameters.json", 422
|
302
|
+
|
303
|
+
subscription = @client.charge_subscription(123, {})
|
304
|
+
subscription.success?.should == false
|
305
|
+
end
|
306
|
+
|
307
|
+
should "have success? as false if the subscription is not found" do
|
308
|
+
stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions/9999/charges.json", "", 404
|
309
|
+
|
310
|
+
subscription = @client.charge_subscription(9999, @options)
|
311
|
+
subscription.success?.should == false
|
312
|
+
end
|
313
|
+
end
|
314
|
+
|
315
|
+
context "for metered subscriptions" do
|
316
|
+
should_eventually "list usage for a subscription" do
|
317
|
+
stub_get "https://OU812:x@pengwynn.chargify.com/subscriptions/123/components/456/usages.json", "list_metered_subscriptions.json", 200
|
318
|
+
|
319
|
+
subscription = @client.list_subscription_usage(123, 456)
|
320
|
+
subscription.success?.should == true
|
321
|
+
end
|
322
|
+
|
323
|
+
should_eventually "record usage for a subscription" do
|
324
|
+
|
325
|
+
end
|
326
|
+
end
|
327
|
+
|
328
|
+
should "migrate a subscription from one product to another" do
|
329
|
+
stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions/123/migrations.json", "migrate_subscription.json"
|
330
|
+
|
331
|
+
subscription = @client.migrate_subscription(123, 354);
|
332
|
+
subscription.success?.should == true
|
333
|
+
subscription.product.id.should == 354
|
334
|
+
end
|
335
|
+
|
336
|
+
should "return a list of products" do
|
337
|
+
stub_get "https://OU812:x@pengwynn.chargify.com/products.json", "products.json"
|
338
|
+
products = @client.list_products
|
339
|
+
products.first.accounting_code.should == 'TSMO'
|
340
|
+
end
|
341
|
+
|
342
|
+
should "return info for a product" do
|
343
|
+
stub_get "https://OU812:x@pengwynn.chargify.com/products/8.json", "product.json"
|
344
|
+
product = @client.product(8)
|
345
|
+
product.accounting_code.should == 'TSMO'
|
346
|
+
end
|
347
|
+
|
348
|
+
should "return info for a product by its handle" do
|
349
|
+
stub_get "https://OU812:x@pengwynn.chargify.com/products/handle/tweetsaver.json", "product.json"
|
350
|
+
product = @client.product_by_handle('tweetsaver')
|
351
|
+
product.accounting_code.should == 'TSMO'
|
352
|
+
end
|
353
|
+
|
354
|
+
context "for quantity based components" do
|
355
|
+
should "list components" do
|
356
|
+
stub_get "https://OU812:x@pengwynn.chargify.com/subscriptions/123/components.json", "components.json"
|
357
|
+
components = @client.list_components(123)
|
358
|
+
components.first.allocated_quantity.should == 42
|
359
|
+
components.last.allocated_quantity.should == 2
|
360
|
+
end
|
361
|
+
|
362
|
+
should "show a specific component" do
|
363
|
+
stub_get "https://OU812:x@pengwynn.chargify.com/subscriptions/123/components/16.json", "component.json"
|
364
|
+
component = @client.subscription_component 123, 16
|
365
|
+
component.name.should == "Extra Rubies"
|
366
|
+
component.allocated_quantity.should == 42
|
367
|
+
end
|
368
|
+
|
369
|
+
should "update the allocated_quantity for a component" do
|
370
|
+
stub_put "https://OU812:x@pengwynn.chargify.com/subscriptions/123/components/16.json", "component.json"
|
371
|
+
response = @client.update_subscription_component_allocated_quantity 123, 16, 20_000_000
|
372
|
+
response.success?.should == true
|
373
|
+
end
|
374
|
+
end
|
375
|
+
|
376
|
+
context "for on/off components" do
|
377
|
+
should "update enabled with a 1 for component on" do
|
378
|
+
@client.expects(:put).
|
379
|
+
with("/subscriptions/123/components/16.json", :body => {:component => {:enabled => 1}}).
|
380
|
+
returns(Hashie::Mash.new(:code => 200)).at_most(3)
|
381
|
+
@client.update_subscription_component_enabled 123, 16, true
|
382
|
+
@client.update_subscription_component_enabled 123, 16, " "
|
383
|
+
@client.update_subscription_component_enabled 123, 16, 21
|
384
|
+
end
|
385
|
+
|
386
|
+
should "update enabled with a 0 for component off" do
|
387
|
+
@client.expects(:put).
|
388
|
+
with("/subscriptions/123/components/16.json", :body => {:component => {:enabled => 0}}).
|
389
|
+
returns(Hashie::Mash.new(:code => 200)).at_most(2)
|
390
|
+
@client.update_subscription_component_enabled 123, 16, false
|
391
|
+
@client.update_subscription_component_enabled 123, 16, nil
|
392
|
+
end
|
393
|
+
|
394
|
+
should "update enabled for a component" do
|
395
|
+
stub_put "https://OU812:x@pengwynn.chargify.com/subscriptions/123/components/16.json", "component.json"
|
396
|
+
response = @client.update_subscription_component_enabled 123, 16, true
|
397
|
+
response.success?.should == true
|
398
|
+
end
|
399
|
+
end
|
400
|
+
|
401
|
+
context "aliased methods" do
|
402
|
+
setup do
|
403
|
+
stub_put "https://OU812:x@pengwynn.chargify.com/subscriptions/123/components/16.json", "component.json"
|
404
|
+
end
|
405
|
+
|
406
|
+
should "expose #update_component" do
|
407
|
+
@client.update_component 123, 16, :enabled => true
|
408
|
+
@client.update_component 123, 16, :enabled => false
|
409
|
+
@client.update_component 123, 16, :funky => 'buttlovin'
|
410
|
+
end
|
411
|
+
|
412
|
+
should "expose #update_on_off_component" do
|
413
|
+
@client.update_on_off_component 123, 16, true
|
414
|
+
end
|
415
|
+
|
416
|
+
should "expose #update_component_quantity" do
|
417
|
+
@client.update_component_quantity 123, 16, 20_000
|
418
|
+
end
|
419
|
+
|
420
|
+
should "expose #update_metered_component" do
|
421
|
+
@client.update_metered_component 123, 16, 20_000
|
422
|
+
end
|
423
|
+
end
|
424
|
+
|
425
|
+
should "return a list of statements" do
|
426
|
+
stub_get "https://OU812:x@pengwynn.chargify.com/subscriptions/123/statements.json", "statements.json"
|
427
|
+
statements = @client.list_statements 123
|
428
|
+
statements.size.should == 1
|
429
|
+
statements.first.customer_last_name.should == 'Schlumm'
|
430
|
+
end
|
431
|
+
end
|
432
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
{
|
2
|
+
"customer": {
|
3
|
+
"reference": "bradleyjoyce",
|
4
|
+
"updated_at": "2009-10-07T11:10:27-04:00",
|
5
|
+
"id": 16,
|
6
|
+
"organization": "Squeejee",
|
7
|
+
"first_name": "Bradley",
|
8
|
+
"last_name": "Joyce",
|
9
|
+
"email": "bradley@squeejee.com",
|
10
|
+
"created_at": "2009-10-07T11:10:27-04:00"
|
11
|
+
}
|
12
|
+
}
|
@@ -0,0 +1,12 @@
|
|
1
|
+
[{
|
2
|
+
"customer": {
|
3
|
+
"reference": "bradleyjoyce",
|
4
|
+
"updated_at": "2009-10-07T11:10:27-04:00",
|
5
|
+
"id": 16,
|
6
|
+
"organization": "Squeejee",
|
7
|
+
"first_name": "Bradley",
|
8
|
+
"last_name": "Joyce",
|
9
|
+
"email": "bradley@squeejee.com",
|
10
|
+
"created_at": "2009-10-07T11:10:27-04:00"
|
11
|
+
}
|
12
|
+
}]
|