stripe 1.31.0 → 1.58.0

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.
Files changed (103) hide show
  1. checksums.yaml +4 -4
  2. data/.github/ISSUE_TEMPLATE.md +5 -0
  3. data/.travis.yml +2 -12
  4. data/Gemfile +29 -4
  5. data/History.txt +168 -0
  6. data/README.md +134 -0
  7. data/Rakefile +10 -0
  8. data/VERSION +1 -1
  9. data/bin/stripe-console +12 -5
  10. data/lib/data/ca-certificates.crt +3868 -5114
  11. data/lib/stripe/account.rb +41 -21
  12. data/lib/stripe/alipay_account.rb +20 -0
  13. data/lib/stripe/api_operations/create.rb +1 -1
  14. data/lib/stripe/api_operations/delete.rb +1 -1
  15. data/lib/stripe/api_operations/list.rb +1 -2
  16. data/lib/stripe/api_operations/save.rb +87 -0
  17. data/lib/stripe/api_resource.rb +37 -4
  18. data/lib/stripe/apple_pay_domain.rb +12 -0
  19. data/lib/stripe/application_fee.rb +8 -8
  20. data/lib/stripe/application_fee_refund.rb +7 -3
  21. data/lib/stripe/balance_transaction.rb +1 -1
  22. data/lib/stripe/bank_account.rb +9 -5
  23. data/lib/stripe/bitcoin_receiver.rb +6 -6
  24. data/lib/stripe/bitcoin_transaction.rb +1 -1
  25. data/lib/stripe/card.rb +9 -5
  26. data/lib/stripe/charge.rb +30 -12
  27. data/lib/stripe/country_spec.rb +9 -0
  28. data/lib/stripe/coupon.rb +1 -1
  29. data/lib/stripe/customer.rb +6 -4
  30. data/lib/stripe/dispute.rb +2 -2
  31. data/lib/stripe/errors.rb +82 -0
  32. data/lib/stripe/file_upload.rb +1 -1
  33. data/lib/stripe/invoice.rb +3 -3
  34. data/lib/stripe/invoice_item.rb +1 -1
  35. data/lib/stripe/list_object.rb +7 -6
  36. data/lib/stripe/order.rb +10 -2
  37. data/lib/stripe/order_return.rb +9 -0
  38. data/lib/stripe/plan.rb +1 -1
  39. data/lib/stripe/product.rb +2 -10
  40. data/lib/stripe/recipient.rb +1 -1
  41. data/lib/stripe/refund.rb +1 -1
  42. data/lib/stripe/reversal.rb +7 -3
  43. data/lib/stripe/singleton_api_resource.rb +3 -3
  44. data/lib/stripe/sku.rb +2 -2
  45. data/lib/stripe/source.rb +11 -0
  46. data/lib/stripe/stripe_object.rb +167 -91
  47. data/lib/stripe/subscription.rb +15 -9
  48. data/lib/stripe/subscription_item.rb +12 -0
  49. data/lib/stripe/three_d_secure.rb +9 -0
  50. data/lib/stripe/transfer.rb +3 -4
  51. data/lib/stripe/util.rb +100 -28
  52. data/lib/stripe/version.rb +1 -1
  53. data/lib/stripe.rb +283 -140
  54. data/stripe.gemspec +5 -18
  55. data/test/stripe/account_test.rb +55 -9
  56. data/test/stripe/alipay_account_test.rb +11 -0
  57. data/test/stripe/api_operations_test.rb +31 -0
  58. data/test/stripe/api_resource_test.rb +204 -10
  59. data/test/stripe/apple_pay_domain_test.rb +34 -0
  60. data/test/stripe/application_fee_test.rb +8 -5
  61. data/test/stripe/bitcoin_receiver_test.rb +2 -2
  62. data/test/stripe/charge_refund_test.rb +12 -0
  63. data/test/stripe/charge_test.rb +32 -4
  64. data/test/stripe/country_spec_test.rb +43 -0
  65. data/test/stripe/coupon_test.rb +9 -1
  66. data/test/stripe/customer_card_test.rb +2 -2
  67. data/test/stripe/customer_test.rb +24 -1
  68. data/test/stripe/dispute_test.rb +8 -0
  69. data/test/stripe/errors_test.rb +18 -0
  70. data/test/stripe/invoice_item_test.rb +19 -0
  71. data/test/stripe/invoice_test.rb +27 -1
  72. data/test/stripe/list_object_test.rb +36 -15
  73. data/test/stripe/order_return_test.rb +25 -0
  74. data/test/stripe/order_test.rb +21 -1
  75. data/test/stripe/plan_test.rb +31 -0
  76. data/test/stripe/product_test.rb +17 -7
  77. data/test/stripe/recipient_card_test.rb +2 -2
  78. data/test/stripe/recipient_test.rb +21 -0
  79. data/test/stripe/refund_test.rb +10 -1
  80. data/test/stripe/sku_test.rb +15 -6
  81. data/test/stripe/source_test.rb +83 -0
  82. data/test/stripe/stripe_object_test.rb +180 -11
  83. data/test/stripe/subscription_item_test.rb +76 -0
  84. data/test/stripe/subscription_test.rb +161 -37
  85. data/test/stripe/three_d_secure_test.rb +22 -0
  86. data/test/stripe/transfer_test.rb +8 -0
  87. data/test/stripe/util_test.rb +48 -16
  88. data/test/stripe_test.rb +58 -0
  89. data/test/test_data.rb +337 -27
  90. data/test/test_helper.rb +7 -3
  91. metadata +47 -133
  92. data/README.rdoc +0 -68
  93. data/gemfiles/default-with-activesupport.gemfile +0 -10
  94. data/gemfiles/json.gemfile +0 -12
  95. data/gemfiles/yajl.gemfile +0 -12
  96. data/lib/stripe/api_operations/update.rb +0 -58
  97. data/lib/stripe/errors/api_connection_error.rb +0 -4
  98. data/lib/stripe/errors/api_error.rb +0 -4
  99. data/lib/stripe/errors/authentication_error.rb +0 -4
  100. data/lib/stripe/errors/card_error.rb +0 -12
  101. data/lib/stripe/errors/invalid_request_error.rb +0 -11
  102. data/lib/stripe/errors/rate_limit_error.rb +0 -4
  103. data/lib/stripe/errors/stripe_error.rb +0 -26
@@ -45,10 +45,23 @@ module Stripe
45
45
  end
46
46
 
47
47
  should "recursively call to_hash on its values" do
48
+ # deep nested hash (when contained in an array) or StripeObject
48
49
  nested_hash = { :id => 7, :foo => 'bar' }
49
50
  nested = Stripe::StripeObject.construct_from(nested_hash)
50
- obj = Stripe::StripeObject.construct_from({ :id => 1, :nested => nested, :list => [nested] })
51
- expected_hash = { :id => 1, :nested => nested_hash, :list => [nested_hash] }
51
+
52
+ obj = Stripe::StripeObject.construct_from({
53
+ :id => 1,
54
+ # simple hash that contains a StripeObject to help us test deep
55
+ # recursion
56
+ :nested => { :object => 'list', :data => [nested] },
57
+ :list => [nested]
58
+ })
59
+
60
+ expected_hash = {
61
+ :id => 1,
62
+ :nested => { :object => 'list', :data => [nested_hash] },
63
+ :list => [nested_hash]
64
+ }
52
65
  assert_equal expected_hash, obj.to_hash
53
66
  end
54
67
 
@@ -71,6 +84,19 @@ module Stripe
71
84
  assert_equal "foo", obj.unknown
72
85
  end
73
86
 
87
+ should "#update_attributes with a hash" do
88
+ obj = Stripe::StripeObject.construct_from({})
89
+ obj.update_attributes(:metadata => { :foo => 'bar' })
90
+ assert_equal Stripe::StripeObject, obj.metadata.class
91
+ end
92
+
93
+ should "create accessors when #update_attributes is called" do
94
+ obj = Stripe::StripeObject.construct_from({})
95
+ assert_equal false, obj.send(:metaclass).method_defined?(:foo)
96
+ obj.update_attributes(:foo => 'bar')
97
+ assert_equal true, obj.send(:metaclass).method_defined?(:foo)
98
+ end
99
+
74
100
  should "warn that #refresh_from is deprecated" do
75
101
  old_stderr = $stderr
76
102
  $stderr = StringIO.new
@@ -102,13 +128,20 @@ module Stripe
102
128
 
103
129
  should "#serialize_params on an empty object" do
104
130
  obj = Stripe::StripeObject.construct_from({})
105
- assert_equal({}, Stripe::StripeObject.serialize_params(obj))
131
+ assert_equal({}, obj.serialize_params)
132
+ end
133
+
134
+ should "#serialize_params on a new object with a subobject" do
135
+ obj = Stripe::StripeObject.new
136
+ obj.metadata = { :foo => "bar" }
137
+ assert_equal({ :metadata => { :foo => "bar" } },
138
+ obj.serialize_params)
106
139
  end
107
140
 
108
141
  should "#serialize_params on a basic object" do
109
142
  obj = Stripe::StripeObject.construct_from({ :foo => nil })
110
143
  obj.update_attributes(:foo => "bar")
111
- assert_equal({ :foo => "bar" }, Stripe::StripeObject.serialize_params(obj))
144
+ assert_equal({ :foo => "bar" }, obj.serialize_params)
112
145
  end
113
146
 
114
147
  should "#serialize_params on a more complex object" do
@@ -120,7 +153,7 @@ module Stripe
120
153
  })
121
154
  obj.foo.bar = "newbar"
122
155
  assert_equal({ :foo => { :bar => "newbar" } },
123
- Stripe::StripeObject.serialize_params(obj))
156
+ obj.serialize_params)
124
157
  end
125
158
 
126
159
  should "#serialize_params on an array" do
@@ -129,7 +162,7 @@ module Stripe
129
162
  })
130
163
  obj.foo = ["new-value"]
131
164
  assert_equal({ :foo => ["new-value"] },
132
- Stripe::StripeObject.serialize_params(obj))
165
+ obj.serialize_params)
133
166
  end
134
167
 
135
168
  should "#serialize_params on an array that shortens" do
@@ -138,7 +171,7 @@ module Stripe
138
171
  })
139
172
  obj.foo = ["new-value"]
140
173
  assert_equal({ :foo => ["new-value"] },
141
- Stripe::StripeObject.serialize_params(obj))
174
+ obj.serialize_params)
142
175
  end
143
176
 
144
177
  should "#serialize_params on an array that lengthens" do
@@ -147,7 +180,7 @@ module Stripe
147
180
  })
148
181
  obj.foo = ["new-value"] * 4
149
182
  assert_equal({ :foo => ["new-value"] * 4 },
150
- Stripe::StripeObject.serialize_params(obj))
183
+ obj.serialize_params)
151
184
  end
152
185
 
153
186
  should "#serialize_params on an array of hashes" do
@@ -161,12 +194,12 @@ module Stripe
161
194
  ]
162
195
  obj.foo[0].bar = "baz"
163
196
  assert_equal({ :foo => [{ :bar => "baz" }] },
164
- Stripe::StripeObject.serialize_params(obj))
197
+ obj.serialize_params)
165
198
  end
166
199
 
167
200
  should "#serialize_params doesn't include unchanged values" do
168
201
  obj = Stripe::StripeObject.construct_from({ :foo => nil })
169
- assert_equal({}, Stripe::StripeObject.serialize_params(obj))
202
+ assert_equal({}, obj.serialize_params)
170
203
  end
171
204
 
172
205
  should "#serialize_params on an array that is unchanged" do
@@ -174,7 +207,143 @@ module Stripe
174
207
  :foo => ["0-index", "1-index", "2-index"],
175
208
  })
176
209
  obj.foo = ["0-index", "1-index", "2-index"]
177
- assert_equal({}, Stripe::StripeObject.serialize_params(obj))
210
+ assert_equal({}, obj.serialize_params)
211
+ end
212
+
213
+ should "#serialize_params with a StripeObject" do
214
+ obj = Stripe::StripeObject.construct_from({})
215
+
216
+ # using an #update_attributes will end up converting a Hash into a
217
+ # StripeObject
218
+ obj.metadata =
219
+ Stripe::StripeObject.construct_from({ :foo => 'bar' })
220
+
221
+ serialized = obj.serialize_params
222
+ assert_equal({ :foo => "bar" }, serialized[:metadata])
223
+ end
224
+
225
+ should "#serialize_params with a StripeObject that's been replaced" do
226
+ obj = Stripe::StripeObject.construct_from({
227
+ :metadata => Stripe::StripeObject.construct_from({ :bar => 'foo' })
228
+ })
229
+
230
+ # Here we replace the object wholesale which means that the client must
231
+ # be able to blank out the values that were in the old object, but which
232
+ # are no longer present in the new one.
233
+ obj.metadata =
234
+ Stripe::StripeObject.construct_from({ :baz => 'foo' })
235
+
236
+ serialized = obj.serialize_params
237
+ assert_equal({ :bar => "", :baz => 'foo' }, serialized[:metadata])
238
+ end
239
+
240
+ should "#serialize_params with an array of StripeObjects" do
241
+ obj = Stripe::StripeObject.construct_from({})
242
+ obj.metadata = [
243
+ Stripe::StripeObject.construct_from({ :foo => 'bar' })
244
+ ]
245
+
246
+ serialized = obj.serialize_params
247
+ assert_equal([{ :foo => "bar" }], serialized[:metadata])
248
+ end
249
+
250
+ should "#serialize_params and remove embedded APIResources" do
251
+ obj = Stripe::StripeObject.construct_from({
252
+ :customer => Customer.construct_from({})
253
+ })
254
+
255
+ serialized = obj.serialize_params
256
+ assert_equal({}, serialized)
257
+ end
258
+
259
+ should "#serialize_params and remove embedded APIResources unless flagged with save_with_parent" do
260
+ c = Customer.construct_from({})
261
+ c.save_with_parent = true
262
+
263
+ obj = Stripe::StripeObject.construct_from({
264
+ :customer => c,
265
+ })
266
+
267
+ serialized = obj.serialize_params
268
+ assert_equal({ :customer => {} }, serialized)
269
+ end
270
+
271
+ should "#serialize_params takes a force option" do
272
+ obj = Stripe::StripeObject.construct_from({
273
+ :id => 'id',
274
+ :metadata => Stripe::StripeObject.construct_from({ :foo => 'bar' })
275
+ })
276
+
277
+ serialized = obj.serialize_params(:force => true)
278
+ assert_equal({ :id => 'id', :metadata => { :foo => 'bar' } }, serialized)
279
+ end
280
+
281
+ should "#dirty! forces an object and its subobjects to be saved" do
282
+ obj = Stripe::StripeObject.construct_from({
283
+ :id => 'id',
284
+ :metadata => Stripe::StripeObject.construct_from({ :foo => 'bar' })
285
+ })
286
+
287
+ # note that `force` and `dirty!` are for different things, but are
288
+ # functionally equivalent
289
+ obj.dirty!
290
+
291
+ serialized = obj.serialize_params
292
+ assert_equal({ :id => 'id', :metadata => { :foo => 'bar' } }, serialized)
293
+ end
294
+
295
+ should "#to_s will call to_s for all embedded stripe objects" do
296
+ obj = Stripe::StripeObject.construct_from({
297
+ id: 'id',
298
+ #embeded list object
299
+ refunds: Stripe::ListObject.construct_from({ data: [
300
+ #embedded object in list
301
+ Stripe::StripeObject.construct_from({
302
+ id: 'id',
303
+ #embedded object in an object in a list object
304
+ metadata: Stripe::StripeObject.construct_from({
305
+ foo: 'bar',
306
+ })
307
+ })
308
+ ]}),
309
+ # embeded stripe object
310
+ metadata: Stripe::StripeObject.construct_from({
311
+ foo: 'bar',
312
+ })
313
+ })
314
+ expected = JSON.pretty_generate({
315
+ id: 'id',
316
+ refunds: {
317
+ data: [
318
+ {id: 'id', metadata: {foo: 'bar'}}
319
+ ]
320
+ },
321
+ metadata: { foo: 'bar' }
322
+ })
323
+
324
+ assert_equal(expected, obj.to_s)
325
+ end
326
+
327
+ should "warn that .serialize_params is deprecated" do
328
+ old_stderr = $stderr
329
+ $stderr = StringIO.new
330
+ begin
331
+ obj = Stripe::StripeObject.construct_from({})
332
+ Stripe::StripeObject.serialize_params(obj)
333
+ message = "NOTE: Stripe::StripeObject.serialize_params is " +
334
+ "deprecated; use #serialize_params instead"
335
+ assert_match Regexp.new(message), $stderr.string
336
+ ensure
337
+ $stderr = old_stderr
338
+ end
339
+ end
340
+
341
+ should "error on setting a property to an empty string" do
342
+ obj = Stripe::StripeObject.construct_from({ :foo => 'bar' })
343
+ e = assert_raises ArgumentError do
344
+ obj.foo = ""
345
+ end
346
+ assert_match %r{\(object\).foo = nil}, e.message
178
347
  end
179
348
  end
180
349
  end
@@ -0,0 +1,76 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ module Stripe
4
+ class SubscriptionItemTest < Test::Unit::TestCase
5
+ should "subscription items should be retrievable" do
6
+ @mock.expects(:get).once.with("#{Stripe.api_base}/v1/subscription_items/si_test_subscription_item", nil, nil).
7
+ returns(make_response(make_subscription_item))
8
+
9
+ sub_item = Stripe::SubscriptionItem.retrieve('si_test_subscription_item')
10
+
11
+ assert sub_item.kind_of?(Stripe::SubscriptionItem)
12
+ end
13
+
14
+ should "subscription items should be listable" do
15
+ @mock.expects(:get).once.with("#{Stripe.api_base}/v1/subscription_items?subscription=s_test_subscription&limit=3", nil, nil).
16
+ returns(make_response(make_subscription_item_array))
17
+ sub_items = Stripe::SubscriptionItem.list(:subscription => 's_test_subscription', :limit => 3).data
18
+
19
+ assert sub_items.kind_of? Array
20
+ assert sub_items[0].kind_of? Stripe::SubscriptionItem
21
+ end
22
+
23
+ should "subscription items should be deletable" do
24
+ @mock.expects(:get).once.returns(make_response(make_subscription_item))
25
+ sub_item = Stripe::SubscriptionItem.retrieve('si_test_subscription_item')
26
+
27
+ @mock.expects(:delete).once.with("#{Stripe.api_base}/v1/subscription_items/#{sub_item.id}", nil, nil).
28
+ returns(make_response(make_subscription_item))
29
+ sub_item.delete
30
+ end
31
+
32
+ should "subscription items should be updateable" do
33
+ sid = 'si_test_subscription_item'
34
+ @mock.expects(:post).once.with do |url, api_key, params|
35
+ url == "#{Stripe.api_base}/v1/subscription_items/#{sid}" &&
36
+ api_key.nil? &&
37
+ CGI.parse(params) == {'plan' => ['silver'], 'quantity' => ['3']}
38
+ end.returns(make_response(make_subscription_item(:plan => 'silver', :quantity => 3)))
39
+
40
+ sub_item = Stripe::SubscriptionItem.update(sid, {:plan => 'silver', :quantity => 3})
41
+
42
+ assert_equal 'silver', sub_item.plan.id
43
+ assert_equal 3, sub_item.quantity
44
+ end
45
+
46
+ should "subscription items should be saveable" do
47
+ @mock.expects(:get).once.returns(make_response(make_subscription_item))
48
+ sub_item = Stripe::SubscriptionItem.retrieve('si_test_subscription_item')
49
+
50
+ @mock.expects(:post).once.with do |url, api_key, params|
51
+ url == "#{Stripe.api_base}/v1/subscription_items/#{sub_item.id}" &&
52
+ api_key.nil? &&
53
+ CGI.parse(params) == {'plan' => ['silver'], 'quantity' => ['3']}
54
+ end.returns(make_response(make_subscription_item(:plan => 'silver', :quantity => 3)))
55
+
56
+ sub_item.plan = 'silver'
57
+ sub_item.quantity = 3
58
+ sub_item.save
59
+
60
+ assert_equal 'silver', sub_item.plan.id
61
+ assert_equal 3, sub_item.quantity
62
+ end
63
+
64
+ should "create should return a new subscription item" do
65
+ @mock.expects(:post).once.with do |url, api_key, params|
66
+ url == "#{Stripe.api_base}/v1/subscription_items" && api_key.nil? &&
67
+ CGI.parse(params) == {'plan' => ['silver'], 'quantity' => ['3']}
68
+ end.returns(make_response(make_subscription_item(:plan => 'silver', :quantity => 3)))
69
+
70
+ sub_item = Stripe::SubscriptionItem.create(:plan => 'silver', :quantity => 3)
71
+
72
+ assert_equal 'silver', sub_item.plan.id
73
+ assert_equal 3, sub_item.quantity
74
+ end
75
+ end
76
+ end
@@ -2,71 +2,195 @@ require File.expand_path('../../test_helper', __FILE__)
2
2
 
3
3
  module Stripe
4
4
  class SubscriptionTest < Test::Unit::TestCase
5
- should "subscriptions should be listable" do
5
+ should "subscriptions should be retrievable by customer" do
6
+ @mock.expects(:get).once.returns(make_response(make_customer))
7
+ customer = Stripe::Customer.retrieve('c_test_customer')
8
+
9
+ @mock.expects(:get).once.with("#{Stripe.api_base}/v1/customers/c_test_customer/subscriptions/s_test_subscription", nil, nil).returns(make_response(make_subscription(:id => 's_test_subscription')))
10
+ _ = customer.subscriptions.retrieve('s_test_subscription')
11
+ end
12
+
13
+ should "subscriptions should be listable by customer" do
6
14
  @mock.expects(:get).once.returns(make_response(make_customer))
15
+ customer = Stripe::Customer.retrieve('c_test_customer')
16
+
17
+ @mock.expects(:get).once.with("#{Stripe.api_base}/v1/customers/c_test_customer/subscriptions", nil, nil).returns(make_response(make_customer_subscription_array('c_test_customer')))
18
+ subs = customer.subscriptions.all()
19
+
20
+ assert subs.kind_of?(Stripe::ListObject)
21
+ assert subs.data.kind_of?(Array)
22
+ assert subs.data[0].kind_of? Stripe::Subscription
23
+ end
24
+
25
+ should "subscriptions should be creatable by customer" do
26
+ @mock.expects(:get).once.returns(make_response(make_customer))
27
+ customer = Stripe::Customer.retrieve('c_test_customer')
28
+
29
+ @mock.expects(:post).once.with("#{Stripe.api_base}/v1/customers/c_test_customer/subscriptions", nil, 'plan=silver').returns(make_response(make_subscription(:id => 'test_new_subscription')))
30
+ subscription = customer.subscriptions.create(:plan => 'silver')
31
+
32
+ assert_equal 'test_new_subscription', subscription.id
33
+ end
34
+
35
+ should "subscriptions should be retrievable" do
36
+ @mock.expects(:get).once.with("#{Stripe.api_base}/v1/subscriptions/s_test_subscription", nil, nil).returns(make_response(make_subscription))
37
+ sub = Stripe::Subscription.retrieve('s_test_subscription')
38
+
39
+ assert sub.kind_of?(Stripe::Subscription)
40
+ end
41
+
42
+ should "subscriptions should be listable" do
43
+ @mock.expects(:get).once.returns(make_response(make_subscription_array))
44
+ subs = Stripe::Subscription.list.data
45
+
46
+ assert subs.kind_of? Array
47
+ assert subs[0].kind_of? Stripe::Subscription
48
+ end
7
49
 
8
- customer = Stripe::Customer.retrieve('test_customer')
50
+ should "subscriptions should be listable with filters" do
51
+ @mock.expects(:get).once.with("#{Stripe.api_base}/v1/subscriptions?customer=c_test_customer&limit=3&plan=gold", nil, nil).returns(make_response(make_subscription_array))
52
+ subs = Stripe::Subscription.all(:customer => 'c_test_customer', :limit => 3, :plan => 'gold')
9
53
 
10
- assert customer.subscriptions.first.kind_of?(Stripe::Subscription)
54
+ assert subs.kind_of?(Stripe::ListObject)
55
+ assert subs.data.kind_of?(Array)
56
+ assert subs.data[0].kind_of? Stripe::Subscription
11
57
  end
12
58
 
13
59
  should "subscriptions should be refreshable" do
14
- @mock.expects(:get).twice.returns(make_response(make_customer), make_response(make_subscription(:id => 'refreshed_subscription')))
60
+ @mock.expects(:get).twice.returns(make_response(make_subscription(:id => 'refreshed_subscription')))
15
61
 
16
- customer = Stripe::Customer.retrieve('test_customer')
17
- subscription = customer.subscriptions.first
18
- subscription.refresh
62
+ sub = Stripe::Subscription.retrieve('s_test_subscription')
63
+ sub.refresh
19
64
 
20
- assert_equal 'refreshed_subscription', subscription.id
65
+ assert_equal 'refreshed_subscription', sub.id
21
66
  end
22
67
 
23
68
  should "subscriptions should be deletable" do
24
- @mock.expects(:get).once.returns(make_response(make_customer))
25
- customer = Stripe::Customer.retrieve('test_customer')
26
- subscription = customer.subscriptions.first
69
+ @mock.expects(:get).once.returns(make_response(make_subscription))
70
+ sub = Stripe::Subscription.retrieve('s_test_subscription')
27
71
 
28
- @mock.expects(:delete).once.with("#{Stripe.api_base}/v1/customers/c_test_customer/subscriptions/#{subscription.id}?at_period_end=true", nil, nil).returns(make_response(make_subscription))
29
- subscription.delete :at_period_end => true
72
+ @mock.expects(:delete).once.with("#{Stripe.api_base}/v1/subscriptions/#{sub.id}?at_period_end=true", nil, nil).returns(make_response(make_subscription))
73
+ sub.delete :at_period_end => true
30
74
 
31
- @mock.expects(:delete).once.with("#{Stripe.api_base}/v1/customers/c_test_customer/subscriptions/#{subscription.id}", nil, nil).returns(make_response(make_subscription))
32
- subscription.delete
75
+ @mock.expects(:delete).once.with("#{Stripe.api_base}/v1/subscriptions/#{sub.id}", nil, nil).returns(make_response(make_subscription))
76
+ sub.delete
33
77
  end
34
78
 
35
79
  should "subscriptions should be updateable" do
36
- @mock.expects(:get).once.returns(make_response(make_customer))
37
- @mock.expects(:post).once.returns(make_response(make_subscription({:status => 'active'})))
80
+ sid = 's_test_subscription'
81
+ @mock.expects(:post).once.with do |url, api_key, params|
82
+ url == "#{Stripe.api_base}/v1/subscriptions/#{sid}" && api_key.nil? && CGI.parse(params) == {'status' => ['active']}
83
+ end.returns(make_response(make_subscription(:status => 'active')))
38
84
 
39
- customer = Stripe::Customer.retrieve('test_customer')
40
- subscription = customer.subscriptions.first
41
- assert_equal 'trialing', subscription.status
85
+ sub = Stripe::Subscription.update(sid, :status => 'active')
42
86
 
43
- subscription.status = 'active'
44
- subscription.save
87
+ assert_equal 'active', sub.status
88
+ end
45
89
 
46
- assert_equal 'active', subscription.status
90
+ should "subscription items should be updateable" do
91
+ sid = 's_test_subscription'
92
+ items = {:data => [{:plan => {:id =>'gold'}, :quantity => 1}, {:plan => {:id =>'silver'}, :quantity => 2}]}
93
+
94
+ @mock.expects(:post).once.with do |url, api_key, params|
95
+ url == "#{Stripe.api_base}/v1/subscriptions/#{sid}" &&
96
+ api_key.nil? &&
97
+ CGI.parse(params) == {
98
+ 'items[0][plan]' => ['gold'],
99
+ 'items[0][quantity]' => ['1'],
100
+ 'items[1][plan]' => ['silver'],
101
+ 'items[1][quantity]' => ['2'],
102
+ }
103
+ end.returns(make_response(make_subscription(:items => items)))
104
+
105
+ sub = Stripe::Subscription.update(sid, :items => [{:plan => 'gold', :quantity =>1}, {:plan => 'silver', :quantity =>2}])
106
+
107
+ assert_equal 'gold', sub.items.data[0].plan.id
108
+ assert_equal 1, sub.items.data[0].quantity
109
+ assert_equal 'silver', sub.items.data[1].plan.id
110
+ assert_equal 2, sub.items.data[1].quantity
47
111
  end
48
112
 
49
- should "create should return a new subscription" do
50
- @mock.expects(:get).once.returns(make_response(make_customer))
51
- @mock.expects(:post).once.returns(make_response(make_subscription(:id => 'test_new_subscription')))
113
+ should "subscription items should be updateable with hash" do
114
+ sid = 's_test_subscription'
115
+ items = {:data => [{:plan => {:id =>'gold'}, :quantity => 1}, {:plan => {:id =>'silver'}, :quantity => 2}]}
116
+
117
+ @mock.expects(:post).once.with do |url, api_key, params|
118
+ url == "#{Stripe.api_base}/v1/subscriptions/#{sid}" &&
119
+ api_key.nil? &&
120
+ CGI.parse(params) == {
121
+ 'items[0][plan]' => ['gold'],
122
+ 'items[0][quantity]' => ['1'],
123
+ 'items[1][plan]' => ['silver'],
124
+ 'items[1][quantity]' => ['2'],
125
+ }
126
+ end.returns(make_response(make_subscription(:items => items)))
127
+
128
+ sub = Stripe::Subscription.update(sid, :items => {'0' => {:plan => 'gold', :quantity =>1}, '1' => {:plan => 'silver', :quantity =>2}})
129
+
130
+ assert_equal 'gold', sub.items.data[0].plan.id
131
+ assert_equal 1, sub.items.data[0].quantity
132
+ assert_equal 'silver', sub.items.data[1].plan.id
133
+ assert_equal 2, sub.items.data[1].quantity
134
+ end
52
135
 
53
- customer = Stripe::Customer.retrieve('test_customer')
54
- subscription = customer.subscriptions.create(:plan => 'silver')
55
- assert_equal 'test_new_subscription', subscription.id
136
+ should "subscriptions should be saveable" do
137
+ @mock.expects(:get).once.returns(make_response(make_subscription))
138
+ sub = Stripe::Subscription.retrieve('s_test_subscription')
139
+ assert_equal 'trialing', sub.status
140
+
141
+ @mock.expects(:post).once.with do |url, api_key, params|
142
+ url == "#{Stripe.api_base}/v1/subscriptions/#{sub.id}" && api_key.nil? && CGI.parse(params) == {'status' => ['active']}
143
+ end.returns(make_response(make_subscription(:status => 'active')))
144
+
145
+ sub.status = 'active'
146
+ sub.save
147
+
148
+ assert_equal 'active', sub.status
56
149
  end
57
150
 
58
- should "be able to delete a subscriptions's discount" do
59
- @mock.expects(:get).once.returns(make_response(make_customer))
60
- @mock.expects(:post).once.returns(make_response(make_subscription(:id => 'test_new_subscription')))
151
+ should "create should return a new subscription" do
152
+ @mock.expects(:post).once.with do |url, api_key, params|
153
+ url == "#{Stripe.api_base}/v1/subscriptions" && api_key.nil? && CGI.parse(params) == {'customer' => ['c_test_customer'], 'plan' => ['gold']}
154
+ end.returns(make_response(make_subscription(:plan => 'gold', :id => 'test_new_subscription')))
61
155
 
156
+ sub = Stripe::Subscription.create(:plan => 'gold', :customer => 'c_test_customer')
62
157
 
63
- customer = Stripe::Customer.retrieve('test_customer')
64
- subscription = customer.subscriptions.create(:plan => 'silver')
158
+ assert_equal 'test_new_subscription', sub.id
159
+ assert_equal 'gold', sub.plan.identifier
160
+ end
161
+
162
+ should "create with items should return a new subscription" do
163
+ items = {:data => [{:plan => {:id =>'gold'}, :quantity => 1}, {:plan => {:id =>'silver'}, :quantity => 2}]}
164
+
165
+ @mock.expects(:post).once.with do |url, api_key, params|
166
+ url == "#{Stripe.api_base}/v1/subscriptions" &&
167
+ api_key.nil? &&
168
+ CGI.parse(params) == {
169
+ 'customer' => ['c_test_customer'],
170
+ 'items[0][plan]' => ['gold'],
171
+ 'items[0][quantity]' => ['1'],
172
+ 'items[1][plan]' => ['silver'],
173
+ 'items[1][quantity]' => ['2'],
174
+ }
175
+ end.returns(make_response(make_subscription(:items => items, :id => 'test_new_subscription')))
176
+
177
+ sub = Stripe::Subscription.create(:customer => 'c_test_customer', :items => [{:plan => 'gold', :quantity =>1}, {:plan => 'silver', :quantity =>2}])
178
+
179
+ assert_equal 'test_new_subscription', sub.id
180
+ assert_equal 'gold', sub.items.data[0].plan.id
181
+ assert_equal 1, sub.items.data[0].quantity
182
+ assert_equal 'silver', sub.items.data[1].plan.id
183
+ assert_equal 2, sub.items.data[1].quantity
184
+ end
185
+
186
+ should "be able to delete a subscriptions's discount" do
187
+ @mock.expects(:post).once.returns(make_response(make_subscription))
188
+ sub = Stripe::Subscription.create(:plan => 'gold', :customer => 'c_test_customer', coupon: 'forever')
65
189
 
66
- url = "#{Stripe.api_base}/v1/customers/c_test_customer/subscriptions/test_new_subscription/discount"
190
+ url = "#{Stripe.api_base}/v1/subscriptions/#{sub.id}/discount"
67
191
  @mock.expects(:delete).once.with(url, nil, nil).returns(make_response(make_delete_discount_response))
68
- subscription.delete_discount
69
- assert_equal nil, subscription.discount
192
+ sub.delete_discount
193
+ assert_equal nil, sub.discount
70
194
  end
71
195
  end
72
196
  end
@@ -0,0 +1,22 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ module Stripe
4
+ class ThreeDSecureTest < Test::Unit::TestCase
5
+ should "retrieve an existing 3D Secure object" do
6
+ @mock.expects(:get).once.returns(make_response(make_three_d_secure))
7
+ tds = Stripe::ThreeDSecure.retrieve("tdsrc_test")
8
+ assert_equal "tdsrc_test", tds.id
9
+ end
10
+
11
+ should "create should return a new 3D Secure object" do
12
+ @mock.expects(:post).once.returns(make_response(make_three_d_secure))
13
+ tds = Stripe::ThreeDSecure.create(
14
+ :card => "tok_test",
15
+ :amount => 1500,
16
+ :currency => "usd",
17
+ :return_url => "https://example.org/3d-secure-result"
18
+ )
19
+ assert_equal "tdsrc_test", tds.id
20
+ end
21
+ end
22
+ end
@@ -14,6 +14,14 @@ module Stripe
14
14
  assert_equal "tr_test_transfer", transfer.id
15
15
  end
16
16
 
17
+ should "create should update a transfer" do
18
+ @mock.expects(:post).once.
19
+ with("#{Stripe.api_base}/v1/transfers/test_transfer", nil, "metadata[foo]=bar").
20
+ returns(make_response(make_transfer(metadata: {foo: 'bar'})))
21
+ transfer = Stripe::Transfer.update("test_transfer", metadata: {foo: 'bar'})
22
+ assert_equal "bar", transfer.metadata['foo']
23
+ end
24
+
17
25
  should "cancel should cancel a transfer" do
18
26
  @mock.expects(:get).once.returns(make_response(make_transfer))
19
27
  transfer = Stripe::Transfer.retrieve('tr_test_transfer')