stripe 1.30.3 → 2.0.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 (122) hide show
  1. checksums.yaml +4 -4
  2. data/.gitattributes +4 -0
  3. data/.github/ISSUE_TEMPLATE.md +5 -0
  4. data/.travis.yml +3 -14
  5. data/Gemfile +28 -4
  6. data/History.txt +180 -0
  7. data/README.md +147 -0
  8. data/Rakefile +10 -0
  9. data/VERSION +1 -1
  10. data/bin/stripe-console +12 -5
  11. data/lib/data/ca-certificates.crt +3868 -5114
  12. data/lib/stripe/account.rb +43 -23
  13. data/lib/stripe/alipay_account.rb +20 -0
  14. data/lib/stripe/api_operations/create.rb +2 -2
  15. data/lib/stripe/api_operations/delete.rb +2 -2
  16. data/lib/stripe/api_operations/list.rb +2 -3
  17. data/lib/stripe/api_operations/request.rb +9 -3
  18. data/lib/stripe/api_operations/save.rb +85 -0
  19. data/lib/stripe/api_resource.rb +38 -5
  20. data/lib/stripe/apple_pay_domain.rb +12 -0
  21. data/lib/stripe/application_fee.rb +8 -8
  22. data/lib/stripe/application_fee_refund.rb +7 -3
  23. data/lib/stripe/balance_transaction.rb +1 -1
  24. data/lib/stripe/bank_account.rb +13 -4
  25. data/lib/stripe/bitcoin_receiver.rb +6 -6
  26. data/lib/stripe/bitcoin_transaction.rb +1 -1
  27. data/lib/stripe/card.rb +9 -5
  28. data/lib/stripe/charge.rb +38 -20
  29. data/lib/stripe/country_spec.rb +9 -0
  30. data/lib/stripe/coupon.rb +1 -1
  31. data/lib/stripe/customer.rb +12 -10
  32. data/lib/stripe/dispute.rb +4 -5
  33. data/lib/stripe/errors.rb +92 -0
  34. data/lib/stripe/file_upload.rb +1 -1
  35. data/lib/stripe/invoice.rb +7 -7
  36. data/lib/stripe/invoice_item.rb +1 -1
  37. data/lib/stripe/list_object.rb +8 -7
  38. data/lib/stripe/order.rb +12 -4
  39. data/lib/stripe/order_return.rb +9 -0
  40. data/lib/stripe/plan.rb +1 -1
  41. data/lib/stripe/product.rb +2 -10
  42. data/lib/stripe/recipient.rb +1 -1
  43. data/lib/stripe/refund.rb +1 -1
  44. data/lib/stripe/reversal.rb +7 -3
  45. data/lib/stripe/singleton_api_resource.rb +3 -3
  46. data/lib/stripe/sku.rb +2 -2
  47. data/lib/stripe/source.rb +11 -0
  48. data/lib/stripe/stripe_client.rb +396 -0
  49. data/lib/stripe/stripe_object.rb +167 -91
  50. data/lib/stripe/stripe_response.rb +48 -0
  51. data/lib/stripe/subscription.rb +15 -9
  52. data/lib/stripe/subscription_item.rb +12 -0
  53. data/lib/stripe/three_d_secure.rb +9 -0
  54. data/lib/stripe/transfer.rb +4 -5
  55. data/lib/stripe/util.rb +105 -33
  56. data/lib/stripe/version.rb +1 -1
  57. data/lib/stripe.rb +69 -266
  58. data/spec/fixtures.json +1409 -0
  59. data/spec/fixtures.yaml +1153 -0
  60. data/spec/spec.json +19949 -0
  61. data/spec/spec.yaml +15504 -0
  62. data/stripe.gemspec +5 -18
  63. data/test/api_fixtures.rb +29 -0
  64. data/test/api_stub_helpers.rb +125 -0
  65. data/test/stripe/account_test.rb +163 -211
  66. data/test/stripe/alipay_account_test.rb +19 -0
  67. data/test/stripe/api_operations_test.rb +31 -0
  68. data/test/stripe/api_resource_test.rb +174 -340
  69. data/test/stripe/apple_pay_domain_test.rb +33 -0
  70. data/test/stripe/application_fee_refund_test.rb +22 -31
  71. data/test/stripe/application_fee_test.rb +6 -14
  72. data/test/stripe/balance_test.rb +3 -3
  73. data/test/stripe/bank_account_test.rb +41 -0
  74. data/test/stripe/bitcoin_receiver_test.rb +51 -42
  75. data/test/stripe/bitcoin_transaction_test.rb +11 -19
  76. data/test/stripe/charge_test.rb +39 -98
  77. data/test/stripe/country_spec_test.rb +20 -0
  78. data/test/stripe/coupon_test.rb +35 -11
  79. data/test/stripe/customer_card_test.rb +25 -46
  80. data/test/stripe/customer_test.rb +89 -61
  81. data/test/stripe/dispute_test.rb +28 -31
  82. data/test/stripe/errors_test.rb +18 -0
  83. data/test/stripe/file_upload_test.rb +32 -24
  84. data/test/stripe/invoice_item_test.rb +55 -0
  85. data/test/stripe/invoice_test.rb +50 -24
  86. data/test/stripe/list_object_test.rb +57 -45
  87. data/test/stripe/order_return_test.rb +21 -0
  88. data/test/stripe/order_test.rb +41 -34
  89. data/test/stripe/plan_test.rb +52 -0
  90. data/test/stripe/product_test.rb +31 -25
  91. data/test/stripe/recipient_card_test.rb +23 -40
  92. data/test/stripe/recipient_test.rb +50 -0
  93. data/test/stripe/refund_test.rb +20 -36
  94. data/test/stripe/reversal_test.rb +27 -31
  95. data/test/stripe/sku_test.rb +39 -13
  96. data/test/stripe/source_test.rb +43 -0
  97. data/test/stripe/stripe_client_test.rb +428 -0
  98. data/test/stripe/stripe_object_test.rb +186 -13
  99. data/test/stripe/stripe_response_test.rb +46 -0
  100. data/test/stripe/subscription_item_test.rb +54 -0
  101. data/test/stripe/subscription_test.rb +40 -52
  102. data/test/stripe/three_d_secure_test.rb +23 -0
  103. data/test/stripe/transfer_test.rb +38 -13
  104. data/test/stripe/util_test.rb +48 -16
  105. data/test/stripe_test.rb +25 -0
  106. data/test/test_data.rb +5 -621
  107. data/test/test_helper.rb +24 -24
  108. metadata +60 -139
  109. data/README.rdoc +0 -68
  110. data/gemfiles/default-with-activesupport.gemfile +0 -10
  111. data/gemfiles/json.gemfile +0 -12
  112. data/gemfiles/yajl.gemfile +0 -12
  113. data/lib/stripe/api_operations/update.rb +0 -58
  114. data/lib/stripe/errors/api_connection_error.rb +0 -4
  115. data/lib/stripe/errors/api_error.rb +0 -4
  116. data/lib/stripe/errors/authentication_error.rb +0 -4
  117. data/lib/stripe/errors/card_error.rb +0 -12
  118. data/lib/stripe/errors/invalid_request_error.rb +0 -11
  119. data/lib/stripe/errors/rate_limit_error.rb +0 -4
  120. data/lib/stripe/errors/stripe_error.rb +0 -26
  121. data/test/stripe/charge_refund_test.rb +0 -55
  122. data/test/stripe/metadata_test.rb +0 -129
@@ -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
@@ -90,9 +116,13 @@ module Stripe
90
116
 
91
117
  # customer comes with a `sources` list that makes a convenient object to
92
118
  # perform tests on
93
- customer = Stripe::Customer.construct_from(make_customer, opts)
119
+ obj = Stripe::StripeObject.construct_from({
120
+ sources: [
121
+ {}
122
+ ]
123
+ }, opts)
94
124
 
95
- source = customer.sources.first
125
+ source = obj.sources.first
96
126
  # Pulling `@opts` as an instance variable here is not ideal, but it's
97
127
  # important enough argument that the test here is worth it. we should
98
128
  # consider exposing it publicly on a future pull (and possibly renaming
@@ -102,13 +132,20 @@ module Stripe
102
132
 
103
133
  should "#serialize_params on an empty object" do
104
134
  obj = Stripe::StripeObject.construct_from({})
105
- assert_equal({}, Stripe::StripeObject.serialize_params(obj))
135
+ assert_equal({}, obj.serialize_params)
136
+ end
137
+
138
+ should "#serialize_params on a new object with a subobject" do
139
+ obj = Stripe::StripeObject.new
140
+ obj.metadata = { :foo => "bar" }
141
+ assert_equal({ :metadata => { :foo => "bar" } },
142
+ obj.serialize_params)
106
143
  end
107
144
 
108
145
  should "#serialize_params on a basic object" do
109
146
  obj = Stripe::StripeObject.construct_from({ :foo => nil })
110
147
  obj.update_attributes(:foo => "bar")
111
- assert_equal({ :foo => "bar" }, Stripe::StripeObject.serialize_params(obj))
148
+ assert_equal({ :foo => "bar" }, obj.serialize_params)
112
149
  end
113
150
 
114
151
  should "#serialize_params on a more complex object" do
@@ -120,7 +157,7 @@ module Stripe
120
157
  })
121
158
  obj.foo.bar = "newbar"
122
159
  assert_equal({ :foo => { :bar => "newbar" } },
123
- Stripe::StripeObject.serialize_params(obj))
160
+ obj.serialize_params)
124
161
  end
125
162
 
126
163
  should "#serialize_params on an array" do
@@ -129,7 +166,7 @@ module Stripe
129
166
  })
130
167
  obj.foo = ["new-value"]
131
168
  assert_equal({ :foo => ["new-value"] },
132
- Stripe::StripeObject.serialize_params(obj))
169
+ obj.serialize_params)
133
170
  end
134
171
 
135
172
  should "#serialize_params on an array that shortens" do
@@ -138,7 +175,7 @@ module Stripe
138
175
  })
139
176
  obj.foo = ["new-value"]
140
177
  assert_equal({ :foo => ["new-value"] },
141
- Stripe::StripeObject.serialize_params(obj))
178
+ obj.serialize_params)
142
179
  end
143
180
 
144
181
  should "#serialize_params on an array that lengthens" do
@@ -147,7 +184,7 @@ module Stripe
147
184
  })
148
185
  obj.foo = ["new-value"] * 4
149
186
  assert_equal({ :foo => ["new-value"] * 4 },
150
- Stripe::StripeObject.serialize_params(obj))
187
+ obj.serialize_params)
151
188
  end
152
189
 
153
190
  should "#serialize_params on an array of hashes" do
@@ -161,12 +198,12 @@ module Stripe
161
198
  ]
162
199
  obj.foo[0].bar = "baz"
163
200
  assert_equal({ :foo => [{ :bar => "baz" }] },
164
- Stripe::StripeObject.serialize_params(obj))
201
+ obj.serialize_params)
165
202
  end
166
203
 
167
204
  should "#serialize_params doesn't include unchanged values" do
168
205
  obj = Stripe::StripeObject.construct_from({ :foo => nil })
169
- assert_equal({}, Stripe::StripeObject.serialize_params(obj))
206
+ assert_equal({}, obj.serialize_params)
170
207
  end
171
208
 
172
209
  should "#serialize_params on an array that is unchanged" do
@@ -174,7 +211,143 @@ module Stripe
174
211
  :foo => ["0-index", "1-index", "2-index"],
175
212
  })
176
213
  obj.foo = ["0-index", "1-index", "2-index"]
177
- assert_equal({}, Stripe::StripeObject.serialize_params(obj))
214
+ assert_equal({}, obj.serialize_params)
215
+ end
216
+
217
+ should "#serialize_params with a StripeObject" do
218
+ obj = Stripe::StripeObject.construct_from({})
219
+
220
+ # using an #update_attributes will end up converting a Hash into a
221
+ # StripeObject
222
+ obj.metadata =
223
+ Stripe::StripeObject.construct_from({ :foo => 'bar' })
224
+
225
+ serialized = obj.serialize_params
226
+ assert_equal({ :foo => "bar" }, serialized[:metadata])
227
+ end
228
+
229
+ should "#serialize_params with a StripeObject that's been replaced" do
230
+ obj = Stripe::StripeObject.construct_from({
231
+ :metadata => Stripe::StripeObject.construct_from({ :bar => 'foo' })
232
+ })
233
+
234
+ # Here we replace the object wholesale which means that the client must
235
+ # be able to blank out the values that were in the old object, but which
236
+ # are no longer present in the new one.
237
+ obj.metadata =
238
+ Stripe::StripeObject.construct_from({ :baz => 'foo' })
239
+
240
+ serialized = obj.serialize_params
241
+ assert_equal({ :bar => "", :baz => 'foo' }, serialized[:metadata])
242
+ end
243
+
244
+ should "#serialize_params with an array of StripeObjects" do
245
+ obj = Stripe::StripeObject.construct_from({})
246
+ obj.metadata = [
247
+ Stripe::StripeObject.construct_from({ :foo => 'bar' })
248
+ ]
249
+
250
+ serialized = obj.serialize_params
251
+ assert_equal([{ :foo => "bar" }], serialized[:metadata])
252
+ end
253
+
254
+ should "#serialize_params and remove embedded APIResources" do
255
+ obj = Stripe::StripeObject.construct_from({
256
+ :customer => Customer.construct_from({})
257
+ })
258
+
259
+ serialized = obj.serialize_params
260
+ assert_equal({}, serialized)
261
+ end
262
+
263
+ should "#serialize_params and remove embedded APIResources unless flagged with save_with_parent" do
264
+ c = Customer.construct_from({})
265
+ c.save_with_parent = true
266
+
267
+ obj = Stripe::StripeObject.construct_from({
268
+ :customer => c,
269
+ })
270
+
271
+ serialized = obj.serialize_params
272
+ assert_equal({ :customer => {} }, serialized)
273
+ end
274
+
275
+ should "#serialize_params takes a force option" do
276
+ obj = Stripe::StripeObject.construct_from({
277
+ :id => 'id',
278
+ :metadata => Stripe::StripeObject.construct_from({ :foo => 'bar' })
279
+ })
280
+
281
+ serialized = obj.serialize_params(:force => true)
282
+ assert_equal({ :id => 'id', :metadata => { :foo => 'bar' } }, serialized)
283
+ end
284
+
285
+ should "#dirty! forces an object and its subobjects to be saved" do
286
+ obj = Stripe::StripeObject.construct_from({
287
+ :id => 'id',
288
+ :metadata => Stripe::StripeObject.construct_from({ :foo => 'bar' })
289
+ })
290
+
291
+ # note that `force` and `dirty!` are for different things, but are
292
+ # functionally equivalent
293
+ obj.dirty!
294
+
295
+ serialized = obj.serialize_params
296
+ assert_equal({ :id => 'id', :metadata => { :foo => 'bar' } }, serialized)
297
+ end
298
+
299
+ should "#to_s will call to_s for all embedded stripe objects" do
300
+ obj = Stripe::StripeObject.construct_from({
301
+ id: 'id',
302
+ #embeded list object
303
+ refunds: Stripe::ListObject.construct_from({ data: [
304
+ #embedded object in list
305
+ Stripe::StripeObject.construct_from({
306
+ id: 'id',
307
+ #embedded object in an object in a list object
308
+ metadata: Stripe::StripeObject.construct_from({
309
+ foo: 'bar',
310
+ })
311
+ })
312
+ ]}),
313
+ # embeded stripe object
314
+ metadata: Stripe::StripeObject.construct_from({
315
+ foo: 'bar',
316
+ })
317
+ })
318
+ expected = JSON.pretty_generate({
319
+ id: 'id',
320
+ refunds: {
321
+ data: [
322
+ {id: 'id', metadata: {foo: 'bar'}}
323
+ ]
324
+ },
325
+ metadata: { foo: 'bar' }
326
+ })
327
+
328
+ assert_equal(expected, obj.to_s)
329
+ end
330
+
331
+ should "warn that .serialize_params is deprecated" do
332
+ old_stderr = $stderr
333
+ $stderr = StringIO.new
334
+ begin
335
+ obj = Stripe::StripeObject.construct_from({})
336
+ Stripe::StripeObject.serialize_params(obj)
337
+ message = "NOTE: Stripe::StripeObject.serialize_params is " +
338
+ "deprecated; use #serialize_params instead"
339
+ assert_match Regexp.new(message), $stderr.string
340
+ ensure
341
+ $stderr = old_stderr
342
+ end
343
+ end
344
+
345
+ should "error on setting a property to an empty string" do
346
+ obj = Stripe::StripeObject.construct_from({ :foo => 'bar' })
347
+ e = assert_raises ArgumentError do
348
+ obj.foo = ""
349
+ end
350
+ assert_match %r{\(object\).foo = nil}, e.message
178
351
  end
179
352
  end
180
353
  end
@@ -0,0 +1,46 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ module Stripe
4
+ class StripeResponseTest < Test::Unit::TestCase
5
+ context ".from_faraday_hash" do
6
+ should "converts to StripeResponse" do
7
+ body = '{"foo": "bar"}'
8
+ headers = { "Request-Id" => "request-id" }
9
+
10
+ http_resp = {
11
+ body: body,
12
+ headers: headers,
13
+ status: 200,
14
+ }
15
+
16
+ resp = StripeResponse.from_faraday_hash(http_resp)
17
+
18
+ assert_equal JSON.parse(body, symbolize_names: true), resp.data
19
+ assert_equal body, resp.http_body
20
+ assert_equal headers, resp.http_headers
21
+ assert_equal 200, resp.http_status
22
+ assert_equal "request-id", resp.request_id
23
+ end
24
+ end
25
+
26
+ context ".from_faraday_response" do
27
+ should "converts to StripeResponse" do
28
+ body = '{"foo": "bar"}'
29
+ headers = { "Request-Id" => "request-id" }
30
+
31
+ env = Faraday::Env.from(
32
+ :status => 200, :body => body,
33
+ :response_headers => headers)
34
+ http_resp = Faraday::Response.new(env)
35
+
36
+ resp = StripeResponse.from_faraday_response(http_resp)
37
+
38
+ assert_equal JSON.parse(body, symbolize_names: true), resp.data
39
+ assert_equal body, resp.http_body
40
+ assert_equal headers, resp.http_headers
41
+ assert_equal 200, resp.http_status
42
+ assert_equal "request-id", resp.request_id
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,54 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ module Stripe
4
+ class SubscriptionItemTest < Test::Unit::TestCase
5
+ FIXTURE = API_FIXTURES.fetch(:subscription_item)
6
+
7
+ should "be listable" do
8
+ items = Stripe::SubscriptionItem.list(
9
+ subscription: API_FIXTURES.fetch(:subscription)[:id]
10
+ )
11
+ assert_requested :get, "#{Stripe.api_base}/v1/subscription_items",
12
+ query: { subscription: API_FIXTURES.fetch(:subscription)[:id] }
13
+ assert items.data.kind_of?(Array)
14
+ assert items.data[0].kind_of?(Stripe::SubscriptionItem)
15
+ end
16
+
17
+ should "be retrievable" do
18
+ item = Stripe::SubscriptionItem.retrieve(FIXTURE[:id])
19
+ assert_requested :get, "#{Stripe.api_base}/v1/subscription_items/#{FIXTURE[:id]}"
20
+ assert item.kind_of?(Stripe::SubscriptionItem)
21
+ end
22
+
23
+ should "be creatable" do
24
+ item = Stripe::SubscriptionItem.create(
25
+ item: 'silver',
26
+ plan: API_FIXTURES.fetch(:plan)[:id],
27
+ quantity: 3,
28
+ subscription: API_FIXTURES.fetch(:subscription)[:id]
29
+ )
30
+ assert_requested :post, "#{Stripe.api_base}/v1/subscription_items"
31
+ assert item.kind_of?(Stripe::SubscriptionItem)
32
+ end
33
+
34
+ should "be saveable" do
35
+ item = Stripe::SubscriptionItem.retrieve(FIXTURE[:id])
36
+ item.quantity = 4
37
+ item.save
38
+ assert_requested :post, "#{Stripe.api_base}/v1/subscription_items/#{FIXTURE[:id]}"
39
+ end
40
+
41
+ should "be updateable" do
42
+ item = Stripe::SubscriptionItem.update(FIXTURE[:id], metadata: {foo: 'bar'})
43
+ assert_requested :post, "#{Stripe.api_base}/v1/subscription_items/#{FIXTURE[:id]}"
44
+ assert item.kind_of?(Stripe::SubscriptionItem)
45
+ end
46
+
47
+ should "be deletable" do
48
+ item = Stripe::SubscriptionItem.retrieve(FIXTURE[:id])
49
+ item = item.delete
50
+ assert_requested :delete, "#{Stripe.api_base}/v1/subscription_items/#{FIXTURE[:id]}"
51
+ assert item.kind_of?(Stripe::SubscriptionItem)
52
+ end
53
+ end
54
+ end
@@ -2,71 +2,59 @@ 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
6
- @mock.expects(:get).once.returns(make_response(make_customer))
5
+ FIXTURE = API_FIXTURES.fetch(:subscription)
7
6
 
8
- customer = Stripe::Customer.retrieve('test_customer')
9
-
10
- assert customer.subscriptions.first.kind_of?(Stripe::Subscription)
7
+ should "be listable" do
8
+ subscriptions = Stripe::Subscription.list
9
+ assert_requested :get, "#{Stripe.api_base}/v1/subscriptions"
10
+ assert subscriptions.data.kind_of?(Array)
11
+ assert subscriptions.data[0].kind_of?(Stripe::Subscription)
11
12
  end
12
13
 
13
- should "subscriptions should be refreshable" do
14
- @mock.expects(:get).twice.returns(make_response(make_customer), make_response(make_subscription(:id => 'refreshed_subscription')))
15
-
16
- customer = Stripe::Customer.retrieve('test_customer')
17
- subscription = customer.subscriptions.first
18
- subscription.refresh
19
-
20
- assert_equal 'refreshed_subscription', subscription.id
14
+ should "be retrievable" do
15
+ subscription = Stripe::Subscription.retrieve(FIXTURE[:id])
16
+ assert_requested :get,
17
+ "#{Stripe.api_base}/v1/subscriptions/#{FIXTURE[:id]}"
18
+ assert subscription.kind_of?(Stripe::Subscription)
21
19
  end
22
20
 
23
- 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
27
-
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
30
-
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
21
+ should "be creatable" do
22
+ subscription = Stripe::Subscription.create(
23
+ customer: API_FIXTURES.fetch(:customer)[:id]
24
+ )
25
+ assert_requested :post, "#{Stripe.api_base}/v1/subscriptions"
26
+ assert subscription.kind_of?(Stripe::Subscription)
33
27
  end
34
28
 
35
- 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'})))
38
-
39
- customer = Stripe::Customer.retrieve('test_customer')
40
- subscription = customer.subscriptions.first
41
- assert_equal 'trialing', subscription.status
42
-
43
- subscription.status = 'active'
29
+ should "be saveable" do
30
+ subscription = Stripe::Subscription.retrieve(FIXTURE[:id])
31
+ subscription.metadata['key'] = 'value'
44
32
  subscription.save
45
-
46
- assert_equal 'active', subscription.status
33
+ assert_requested :post,
34
+ "#{Stripe.api_base}/v1/subscriptions/#{FIXTURE[:id]}"
47
35
  end
48
36
 
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')))
52
-
53
- customer = Stripe::Customer.retrieve('test_customer')
54
- subscription = customer.subscriptions.create(:plan => 'silver')
55
- assert_equal 'test_new_subscription', subscription.id
37
+ should "be updateable" do
38
+ subscription = Stripe::Subscription.update(FIXTURE[:id], metadata: {foo: 'bar'})
39
+ assert_requested :post,
40
+ "#{Stripe.api_base}/v1/subscriptions/#{FIXTURE[:id]}"
41
+ assert subscription.kind_of?(Stripe::Subscription)
56
42
  end
57
43
 
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')))
61
-
62
-
63
- customer = Stripe::Customer.retrieve('test_customer')
64
- subscription = customer.subscriptions.create(:plan => 'silver')
44
+ should "be deletable" do
45
+ subscription = Stripe::Subscription.retrieve(FIXTURE[:id])
46
+ subscription = subscription.delete
47
+ assert_requested :delete,
48
+ "#{Stripe.api_base}/v1/subscriptions/#{FIXTURE[:id]}"
49
+ assert subscription.kind_of?(Stripe::Subscription)
50
+ end
65
51
 
66
- url = "#{Stripe.api_base}/v1/customers/c_test_customer/subscriptions/test_new_subscription/discount"
67
- @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
52
+ context "#delete_discount" do
53
+ should "be able to delete a subscriptions's discount" do
54
+ subscription = Stripe::Subscription.retrieve(FIXTURE[:id])
55
+ subscription = subscription.delete_discount
56
+ assert subscription.kind_of?(Stripe::Subscription)
57
+ end
70
58
  end
71
59
  end
72
60
  end
@@ -0,0 +1,23 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ module Stripe
4
+ class ThreeDSecureTest < Test::Unit::TestCase
5
+ FIXTURE = API_FIXTURES.fetch(:three_d_secure)
6
+
7
+ should "be retrievable" do
8
+ secure = Stripe::ThreeDSecure.retrieve(FIXTURE[:id])
9
+ assert_requested :get, "#{Stripe.api_base}/v1/3d_secure/#{FIXTURE[:id]}"
10
+ assert secure.kind_of?(Stripe::ThreeDSecure)
11
+ end
12
+
13
+ should "be creatable" do
14
+ _ = Stripe::ThreeDSecure.create(
15
+ card: API_FIXTURES.fetch(:token)[:id],
16
+ amount: 1500,
17
+ currency: "usd",
18
+ return_url: "https://example.org/3d-secure-result"
19
+ )
20
+ assert_requested :post, "#{Stripe.api_base}/v1/3d_secure"
21
+ end
22
+ end
23
+ end
@@ -2,24 +2,49 @@ require File.expand_path('../../test_helper', __FILE__)
2
2
 
3
3
  module Stripe
4
4
  class TransferTest < Test::Unit::TestCase
5
- should "retrieve should retrieve transfer" do
6
- @mock.expects(:get).once.returns(make_response(make_transfer))
7
- transfer = Stripe::Transfer.retrieve('tr_test_transfer')
8
- assert_equal 'tr_test_transfer', transfer.id
5
+ FIXTURE = API_FIXTURES.fetch(:transfer)
6
+
7
+ should "be listable" do
8
+ transfers = Stripe::Transfer.list
9
+ assert_requested :get, "#{Stripe.api_base}/v1/transfers"
10
+ assert transfers.data.kind_of?(Array)
11
+ assert transfers.data[0].kind_of?(Stripe::Transfer)
12
+ end
13
+
14
+ should "be retrievable" do
15
+ transfer = Stripe::Transfer.retrieve(FIXTURE[:id])
16
+ assert_requested :get, "#{Stripe.api_base}/v1/transfers/#{FIXTURE[:id]}"
17
+ assert transfer.kind_of?(Stripe::Transfer)
9
18
  end
10
19
 
11
- should "create should create a transfer" do
12
- @mock.expects(:post).once.returns(make_response(make_transfer))
13
- transfer = Stripe::Transfer.create
14
- assert_equal "tr_test_transfer", transfer.id
20
+ should "be creatable" do
21
+ transfer = Stripe::Transfer.create(
22
+ amount: 100,
23
+ currency: "USD"
24
+ )
25
+ assert_requested :post, "#{Stripe.api_base}/v1/transfers"
26
+ assert transfer.kind_of?(Stripe::Transfer)
15
27
  end
16
28
 
17
- should "cancel should cancel a transfer" do
18
- @mock.expects(:get).once.returns(make_response(make_transfer))
19
- transfer = Stripe::Transfer.retrieve('tr_test_transfer')
29
+ should "be saveable" do
30
+ transfer = Stripe::Transfer.retrieve(FIXTURE[:id])
31
+ transfer.metadata['key'] = 'value'
32
+ transfer.save
33
+ assert_requested :post, "#{Stripe.api_base}/v1/transfers/#{FIXTURE[:id]}"
34
+ end
35
+
36
+ should "be updateable" do
37
+ transfer = Stripe::Transfer.update(FIXTURE[:id], metadata: {foo: 'bar'})
38
+ assert_requested :post, "#{Stripe.api_base}/v1/transfers/#{FIXTURE[:id]}"
39
+ assert transfer.kind_of?(Stripe::Transfer)
40
+ end
20
41
 
21
- @mock.expects(:post).once.with('https://api.stripe.com/v1/transfers/tr_test_transfer/cancel', nil, '').returns(make_response(make_canceled_transfer))
22
- transfer.cancel
42
+ context "#cancel" do
43
+ should "cancel a transfer" do
44
+ transfer = Stripe::Transfer.retrieve(FIXTURE[:id])
45
+ transfer = transfer.cancel
46
+ assert transfer.kind_of?(Stripe::Transfer)
47
+ end
23
48
  end
24
49
  end
25
50
  end
@@ -3,23 +3,51 @@ require File.expand_path('../../test_helper', __FILE__)
3
3
  module Stripe
4
4
  class UtilTest < Test::Unit::TestCase
5
5
  should "#encode_parameters should prepare parameters for an HTTP request" do
6
- # use array instead of hash for 1.8.7 ordering
7
- params = [
8
- [:a, 3],
9
- [:b, "+foo?"],
10
- [:c, "bar&baz"],
11
- [:d, { :a => "a", :b => "b" }],
12
- [:e, [0, 1]],
6
+ params = {
7
+ :a => 3,
8
+ :b => "+foo?",
9
+ :c => "bar&baz",
10
+ :d => { :a => "a", :b => "b" },
11
+ :e => [0, 1],
12
+ :f => "",
13
13
 
14
14
  # note the empty hash won't even show up in the request
15
- [:f, []]
16
- ]
15
+ :g => [],
16
+ }
17
17
  assert_equal(
18
- "a=3&b=%2Bfoo%3F&c=bar%26baz&d[a]=a&d[b]=b&e[]=0&e[]=1",
18
+ "a=3&b=%2Bfoo%3F&c=bar%26baz&d[a]=a&d[b]=b&e[]=0&e[]=1&f=",
19
19
  Stripe::Util.encode_parameters(params)
20
20
  )
21
21
  end
22
22
 
23
+ should "#encode_params should throw an error on an array of maps that cannot be encoded" do
24
+ params = {
25
+ :a => [
26
+ { :a => 1, :b => 2 },
27
+ { :c => 3, :a => 4 },
28
+ ]
29
+ }
30
+ e = assert_raises(ArgumentError) do
31
+ Stripe::Util.encode_parameters(params)
32
+ end
33
+ expected = "All maps nested in an array should start with the same key " +
34
+ "(expected starting key 'a', got 'c')"
35
+ assert_equal expected, e.message
36
+
37
+ # Make sure the check is recursive by taking our original params and
38
+ # nesting it into yet another map and array. Should throw exactly the
39
+ # same error because it's still the in inner array of maps that's wrong.
40
+ params = {
41
+ :x => [
42
+ params
43
+ ]
44
+ }
45
+ e = assert_raises(ArgumentError) do
46
+ Stripe::Util.encode_parameters(params)
47
+ end
48
+ assert_equal expected, e.message
49
+ end
50
+
23
51
  should "#url_encode should prepare strings for HTTP" do
24
52
  assert_equal "foo", Stripe::Util.url_encode("foo")
25
53
  assert_equal "foo", Stripe::Util.url_encode(:foo)
@@ -36,8 +64,8 @@ module Stripe
36
64
  [:d, { :a => "a", :b => "b" }],
37
65
  [:e, [0, 1]],
38
66
  [:f, [
39
- { :bar => "1", :foo => "2" },
40
- { :baz => "3", :foo => "4" },
67
+ { :foo => "1", :ghi => "2" },
68
+ { :foo => "3", :bar => "4" },
41
69
  ]],
42
70
  ]
43
71
  assert_equal([
@@ -52,10 +80,10 @@ module Stripe
52
80
  # *The key here is the order*. In order to be properly interpreted as
53
81
  # an array of hashes on the server, everything from a single hash must
54
82
  # come in at once. A duplicate key in an array triggers a new element.
55
- ["f[][bar]", "1"],
56
- ["f[][foo]", "2"],
57
- ["f[][baz]", "3"],
58
- ["f[][foo]", "4"],
83
+ ["f[][foo]", "1"],
84
+ ["f[][ghi]", "2"],
85
+ ["f[][foo]", "3"],
86
+ ["f[][bar]", "4"],
59
87
  ], Stripe::Util.flatten_params(params))
60
88
  end
61
89
 
@@ -113,5 +141,9 @@ module Stripe
113
141
  obj = Util.convert_to_stripe_object([1, 2, 3], {})
114
142
  assert_equal [1, 2, 3], obj
115
143
  end
144
+
145
+ should "#array_to_hash should convert an array into a hash with integer keys" do
146
+ assert_equal({"0" => 1, "1" => 2, "2" => 3}, Util.array_to_hash([1, 2, 3]))
147
+ end
116
148
  end
117
149
  end