stripe 3.8.2 → 5.45.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (214) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +424 -1
  3. data/CODE_OF_CONDUCT.md +77 -0
  4. data/Gemfile +15 -20
  5. data/README.md +201 -58
  6. data/Rakefile +12 -9
  7. data/VERSION +1 -1
  8. data/bin/stripe-console +3 -1
  9. data/lib/stripe/api_operations/create.rb +3 -1
  10. data/lib/stripe/api_operations/delete.rb +30 -2
  11. data/lib/stripe/api_operations/list.rb +3 -12
  12. data/lib/stripe/api_operations/nested_resource.rb +40 -28
  13. data/lib/stripe/api_operations/request.rb +83 -9
  14. data/lib/stripe/api_operations/save.rb +19 -7
  15. data/lib/stripe/api_resource.rb +65 -4
  16. data/lib/stripe/connection_manager.rb +200 -0
  17. data/lib/stripe/error_object.rb +93 -0
  18. data/lib/stripe/errors.rb +47 -19
  19. data/lib/stripe/instrumentation.rb +84 -0
  20. data/lib/stripe/list_object.rb +43 -7
  21. data/lib/stripe/multipart_encoder.rb +131 -0
  22. data/lib/stripe/oauth.rb +14 -7
  23. data/lib/stripe/object_types.rb +110 -0
  24. data/lib/stripe/{account.rb → resources/account.rb} +60 -28
  25. data/lib/stripe/resources/account_link.rb +10 -0
  26. data/lib/stripe/resources/alipay_account.rb +34 -0
  27. data/lib/stripe/{apple_pay_domain.rb → resources/apple_pay_domain.rb} +4 -1
  28. data/lib/stripe/resources/application_fee.rb +14 -0
  29. data/lib/stripe/resources/application_fee_refund.rb +31 -0
  30. data/lib/stripe/resources/balance.rb +8 -0
  31. data/lib/stripe/resources/balance_transaction.rb +10 -0
  32. data/lib/stripe/resources/bank_account.rb +43 -0
  33. data/lib/stripe/resources/billing_portal/configuration.rb +14 -0
  34. data/lib/stripe/resources/billing_portal/session.rb +12 -0
  35. data/lib/stripe/{bitcoin_receiver.rb → resources/bitcoin_receiver.rb} +6 -6
  36. data/lib/stripe/resources/bitcoin_transaction.rb +16 -0
  37. data/lib/stripe/resources/capability.rb +34 -0
  38. data/lib/stripe/{card.rb → resources/card.rb} +15 -4
  39. data/lib/stripe/resources/charge.rb +23 -0
  40. data/lib/stripe/resources/checkout/session.rb +27 -0
  41. data/lib/stripe/resources/country_spec.rb +10 -0
  42. data/lib/stripe/{coupon.rb → resources/coupon.rb} +5 -2
  43. data/lib/stripe/resources/credit_note.rb +33 -0
  44. data/lib/stripe/resources/credit_note_line_item.rb +8 -0
  45. data/lib/stripe/resources/customer.rb +52 -0
  46. data/lib/stripe/resources/customer_balance_transaction.rb +31 -0
  47. data/lib/stripe/resources/discount.rb +8 -0
  48. data/lib/stripe/resources/dispute.rb +22 -0
  49. data/lib/stripe/resources/ephemeral_key.rb +20 -0
  50. data/lib/stripe/resources/event.rb +10 -0
  51. data/lib/stripe/resources/exchange_rate.rb +10 -0
  52. data/lib/stripe/resources/file.rb +36 -0
  53. data/lib/stripe/resources/file_link.rb +12 -0
  54. data/lib/stripe/resources/identity/verification_report.rb +12 -0
  55. data/lib/stripe/resources/identity/verification_session.rb +35 -0
  56. data/lib/stripe/resources/invoice.rb +74 -0
  57. data/lib/stripe/{invoice_item.rb → resources/invoice_item.rb} +5 -2
  58. data/lib/stripe/resources/invoice_line_item.rb +8 -0
  59. data/lib/stripe/resources/issuing/authorization.rb +34 -0
  60. data/lib/stripe/resources/issuing/card.rb +25 -0
  61. data/lib/stripe/resources/issuing/card_details.rb +10 -0
  62. data/lib/stripe/resources/issuing/cardholder.rb +14 -0
  63. data/lib/stripe/resources/issuing/dispute.rb +25 -0
  64. data/lib/stripe/resources/issuing/transaction.rb +13 -0
  65. data/lib/stripe/resources/line_item.rb +8 -0
  66. data/lib/stripe/resources/login_link.rb +15 -0
  67. data/lib/stripe/resources/mandate.rb +8 -0
  68. data/lib/stripe/resources/order.rb +33 -0
  69. data/lib/stripe/resources/order_return.rb +10 -0
  70. data/lib/stripe/resources/payment_intent.rb +53 -0
  71. data/lib/stripe/resources/payment_link.rb +23 -0
  72. data/lib/stripe/resources/payment_method.rb +33 -0
  73. data/lib/stripe/resources/payout.rb +33 -0
  74. data/lib/stripe/resources/person.rb +32 -0
  75. data/lib/stripe/{plan.rb → resources/plan.rb} +4 -1
  76. data/lib/stripe/resources/price.rb +12 -0
  77. data/lib/stripe/{product.rb → resources/product.rb} +6 -3
  78. data/lib/stripe/resources/promotion_code.rb +12 -0
  79. data/lib/stripe/resources/quote.rb +105 -0
  80. data/lib/stripe/resources/radar/early_fraud_warning.rb +12 -0
  81. data/lib/stripe/resources/radar/value_list.rb +15 -0
  82. data/lib/stripe/resources/radar/value_list_item.rb +14 -0
  83. data/lib/stripe/{recipient.rb → resources/recipient.rb} +5 -6
  84. data/lib/stripe/resources/recipient_transfer.rb +7 -0
  85. data/lib/stripe/{refund.rb → resources/refund.rb} +4 -1
  86. data/lib/stripe/resources/reporting/report_run.rb +13 -0
  87. data/lib/stripe/resources/reporting/report_type.rb +13 -0
  88. data/lib/stripe/resources/reversal.rb +30 -0
  89. data/lib/stripe/resources/review.rb +21 -0
  90. data/lib/stripe/resources/setup_attempt.rb +10 -0
  91. data/lib/stripe/resources/setup_intent.rb +43 -0
  92. data/lib/stripe/resources/shipping_rate.rb +12 -0
  93. data/lib/stripe/resources/sigma/scheduled_query_run.rb +16 -0
  94. data/lib/stripe/{sku.rb → resources/sku.rb} +6 -3
  95. data/lib/stripe/resources/source.rb +47 -0
  96. data/lib/stripe/resources/source_transaction.rb +8 -0
  97. data/lib/stripe/resources/subscription.rb +26 -0
  98. data/lib/stripe/resources/subscription_item.rb +26 -0
  99. data/lib/stripe/resources/subscription_schedule.rb +33 -0
  100. data/lib/stripe/resources/tax_code.rb +10 -0
  101. data/lib/stripe/resources/tax_id.rb +27 -0
  102. data/lib/stripe/resources/tax_rate.rb +12 -0
  103. data/lib/stripe/resources/terminal/connection_token.rb +12 -0
  104. data/lib/stripe/resources/terminal/location.rb +15 -0
  105. data/lib/stripe/resources/terminal/reader.rb +15 -0
  106. data/lib/stripe/resources/test_helpers/test_clock.rb +25 -0
  107. data/lib/stripe/{three_d_secure.rb → resources/three_d_secure.rb} +4 -1
  108. data/lib/stripe/resources/token.rb +10 -0
  109. data/lib/stripe/resources/topup.rb +23 -0
  110. data/lib/stripe/resources/transfer.rb +27 -0
  111. data/lib/stripe/resources/usage_record.rb +8 -0
  112. data/lib/stripe/resources/usage_record_summary.rb +8 -0
  113. data/lib/stripe/resources/webhook_endpoint.rb +13 -0
  114. data/lib/stripe/resources.rb +93 -0
  115. data/lib/stripe/singleton_api_resource.rb +8 -2
  116. data/lib/stripe/stripe_client.rb +712 -233
  117. data/lib/stripe/stripe_configuration.rb +194 -0
  118. data/lib/stripe/stripe_object.rb +180 -67
  119. data/lib/stripe/stripe_response.rb +89 -27
  120. data/lib/stripe/util.rb +87 -168
  121. data/lib/stripe/version.rb +3 -1
  122. data/lib/stripe/webhook.rb +45 -10
  123. data/lib/stripe.rb +47 -160
  124. data/stripe.gemspec +27 -8
  125. metadata +112 -178
  126. data/.gitattributes +0 -4
  127. data/.github/ISSUE_TEMPLATE.md +0 -5
  128. data/.gitignore +0 -6
  129. data/.rubocop.yml +0 -20
  130. data/.rubocop_todo.yml +0 -60
  131. data/.travis.yml +0 -38
  132. data/lib/stripe/alipay_account.rb +0 -25
  133. data/lib/stripe/application_fee.rb +0 -25
  134. data/lib/stripe/application_fee_refund.rb +0 -20
  135. data/lib/stripe/balance.rb +0 -5
  136. data/lib/stripe/balance_transaction.rb +0 -11
  137. data/lib/stripe/bank_account.rb +0 -30
  138. data/lib/stripe/bitcoin_transaction.rb +0 -13
  139. data/lib/stripe/charge.rb +0 -82
  140. data/lib/stripe/country_spec.rb +0 -11
  141. data/lib/stripe/customer.rb +0 -88
  142. data/lib/stripe/dispute.rb +0 -17
  143. data/lib/stripe/ephemeral_key.rb +0 -18
  144. data/lib/stripe/event.rb +0 -7
  145. data/lib/stripe/exchange_rate.rb +0 -11
  146. data/lib/stripe/file_upload.rb +0 -35
  147. data/lib/stripe/invoice.rb +0 -30
  148. data/lib/stripe/invoice_line_item.rb +0 -5
  149. data/lib/stripe/login_link.rb +0 -9
  150. data/lib/stripe/order.rb +0 -35
  151. data/lib/stripe/order_return.rb +0 -11
  152. data/lib/stripe/payout.rb +0 -18
  153. data/lib/stripe/recipient_transfer.rb +0 -5
  154. data/lib/stripe/reversal.rb +0 -20
  155. data/lib/stripe/source.rb +0 -36
  156. data/lib/stripe/source_transaction.rb +0 -5
  157. data/lib/stripe/subscription.rb +0 -43
  158. data/lib/stripe/subscription_item.rb +0 -14
  159. data/lib/stripe/token.rb +0 -7
  160. data/lib/stripe/transfer.rb +0 -21
  161. data/test/api_stub_helpers.rb +0 -0
  162. data/test/stripe/account_external_accounts_operations_test.rb +0 -66
  163. data/test/stripe/account_login_links_operations_test.rb +0 -19
  164. data/test/stripe/account_test.rb +0 -190
  165. data/test/stripe/alipay_account_test.rb +0 -35
  166. data/test/stripe/api_operations_test.rb +0 -76
  167. data/test/stripe/api_resource_test.rb +0 -522
  168. data/test/stripe/apple_pay_domain_test.rb +0 -31
  169. data/test/stripe/application_fee_refund_test.rb +0 -35
  170. data/test/stripe/application_fee_refunds_operations_test.rb +0 -54
  171. data/test/stripe/application_fee_test.rb +0 -12
  172. data/test/stripe/balance_test.rb +0 -11
  173. data/test/stripe/bank_account_test.rb +0 -34
  174. data/test/stripe/charge_test.rb +0 -57
  175. data/test/stripe/country_spec_test.rb +0 -18
  176. data/test/stripe/coupon_test.rb +0 -42
  177. data/test/stripe/customer_card_test.rb +0 -42
  178. data/test/stripe/customer_sources_operations_test.rb +0 -66
  179. data/test/stripe/customer_test.rb +0 -113
  180. data/test/stripe/dispute_test.rb +0 -40
  181. data/test/stripe/ephemeral_key_test.rb +0 -84
  182. data/test/stripe/errors_test.rb +0 -18
  183. data/test/stripe/exchange_rate_test.rb +0 -18
  184. data/test/stripe/file_upload_test.rb +0 -83
  185. data/test/stripe/invoice_item_test.rb +0 -53
  186. data/test/stripe/invoice_line_item_test.rb +0 -6
  187. data/test/stripe/invoice_test.rb +0 -111
  188. data/test/stripe/list_object_test.rb +0 -154
  189. data/test/stripe/login_link_test.rb +0 -35
  190. data/test/stripe/oauth_test.rb +0 -77
  191. data/test/stripe/order_return_test.rb +0 -19
  192. data/test/stripe/order_test.rb +0 -57
  193. data/test/stripe/payout_test.rb +0 -48
  194. data/test/stripe/plan_test.rb +0 -50
  195. data/test/stripe/product_test.rb +0 -45
  196. data/test/stripe/recipient_test.rb +0 -47
  197. data/test/stripe/refund_test.rb +0 -37
  198. data/test/stripe/reversal_test.rb +0 -41
  199. data/test/stripe/sku_test.rb +0 -48
  200. data/test/stripe/source_test.rb +0 -84
  201. data/test/stripe/source_transaction_test.rb +0 -28
  202. data/test/stripe/stripe_client_test.rb +0 -728
  203. data/test/stripe/stripe_object_test.rb +0 -448
  204. data/test/stripe/stripe_response_test.rb +0 -47
  205. data/test/stripe/subscription_item_test.rb +0 -52
  206. data/test/stripe/subscription_test.rb +0 -104
  207. data/test/stripe/three_d_secure_test.rb +0 -21
  208. data/test/stripe/transfer_reversals_operations_test.rb +0 -55
  209. data/test/stripe/transfer_test.rb +0 -41
  210. data/test/stripe/util_test.rb +0 -432
  211. data/test/stripe/webhook_test.rb +0 -94
  212. data/test/stripe_test.rb +0 -59
  213. data/test/test_data.rb +0 -59
  214. data/test/test_helper.rb +0 -60
@@ -1,728 +0,0 @@
1
- require File.expand_path("../../test_helper", __FILE__)
2
-
3
- module Stripe
4
- class StripeClientTest < Test::Unit::TestCase
5
- context ".active_client" do
6
- should "be .default_client outside of #request" do
7
- assert_equal StripeClient.default_client, StripeClient.active_client
8
- end
9
-
10
- should "be active client inside of #request" do
11
- client = StripeClient.new
12
- client.request do
13
- assert_equal client, StripeClient.active_client
14
- end
15
- end
16
- end
17
-
18
- context ".default_client" do
19
- should "be a StripeClient" do
20
- assert_kind_of StripeClient, StripeClient.default_client
21
- end
22
-
23
- should "be a different client on each thread" do
24
- other_thread_client = nil
25
- thread = Thread.new do
26
- other_thread_client = StripeClient.default_client
27
- end
28
- thread.join
29
- refute_equal StripeClient.default_client, other_thread_client
30
- end
31
- end
32
-
33
- context ".default_conn" do
34
- should "be a Faraday::Connection" do
35
- assert_kind_of Faraday::Connection, StripeClient.default_conn
36
- end
37
-
38
- should "be a different connection on each thread" do
39
- other_thread_conn = nil
40
- thread = Thread.new do
41
- other_thread_conn = StripeClient.default_conn
42
- end
43
- thread.join
44
- refute_equal StripeClient.default_conn, other_thread_conn
45
- end
46
- end
47
-
48
- context ".should_retry?" do
49
- setup do
50
- Stripe.stubs(:max_network_retries).returns(2)
51
- end
52
-
53
- should "retry on timeout" do
54
- assert StripeClient.should_retry?(Faraday::TimeoutError.new(""), 0)
55
- end
56
-
57
- should "retry on a failed connection" do
58
- assert StripeClient.should_retry?(Faraday::ConnectionFailed.new(""), 0)
59
- end
60
-
61
- should "retry on a conflict" do
62
- error = make_rate_limit_error
63
- e = Faraday::ClientError.new(error[:error][:message], status: 409)
64
- assert StripeClient.should_retry?(e, 0)
65
- end
66
-
67
- should "not retry at maximum count" do
68
- refute StripeClient.should_retry?(RuntimeError.new, Stripe.max_network_retries)
69
- end
70
-
71
- should "not retry on a certificate validation error" do
72
- refute StripeClient.should_retry?(Faraday::SSLError.new(""), 0)
73
- end
74
- end
75
-
76
- context ".sleep_time" do
77
- should "should grow exponentially" do
78
- StripeClient.stubs(:rand).returns(1)
79
- Stripe.stubs(:max_network_retry_delay).returns(999)
80
- assert_equal(Stripe.initial_network_retry_delay, StripeClient.sleep_time(1))
81
- assert_equal(Stripe.initial_network_retry_delay * 2, StripeClient.sleep_time(2))
82
- assert_equal(Stripe.initial_network_retry_delay * 4, StripeClient.sleep_time(3))
83
- assert_equal(Stripe.initial_network_retry_delay * 8, StripeClient.sleep_time(4))
84
- end
85
-
86
- should "enforce the max_network_retry_delay" do
87
- StripeClient.stubs(:rand).returns(1)
88
- Stripe.stubs(:initial_network_retry_delay).returns(1)
89
- Stripe.stubs(:max_network_retry_delay).returns(2)
90
- assert_equal(1, StripeClient.sleep_time(1))
91
- assert_equal(2, StripeClient.sleep_time(2))
92
- assert_equal(2, StripeClient.sleep_time(3))
93
- assert_equal(2, StripeClient.sleep_time(4))
94
- end
95
-
96
- should "add some randomness" do
97
- random_value = 0.8
98
- StripeClient.stubs(:rand).returns(random_value)
99
- Stripe.stubs(:initial_network_retry_delay).returns(1)
100
- Stripe.stubs(:max_network_retry_delay).returns(8)
101
-
102
- base_value = Stripe.initial_network_retry_delay * (0.5 * (1 + random_value))
103
-
104
- # the initial value cannot be smaller than the base,
105
- # so the randomness is ignored
106
- assert_equal(Stripe.initial_network_retry_delay, StripeClient.sleep_time(1))
107
-
108
- # after the first one, the randomness is applied
109
- assert_equal(base_value * 2, StripeClient.sleep_time(2))
110
- assert_equal(base_value * 4, StripeClient.sleep_time(3))
111
- assert_equal(base_value * 8, StripeClient.sleep_time(4))
112
- end
113
- end
114
-
115
- context "#initialize" do
116
- should "set Stripe.default_conn" do
117
- client = StripeClient.new
118
- assert_equal StripeClient.default_conn, client.conn
119
- end
120
-
121
- should "set a different connection if one was specified" do
122
- conn = Faraday.new
123
- client = StripeClient.new(conn)
124
- assert_equal conn, client.conn
125
- end
126
- end
127
-
128
- context "#execute_request" do
129
- context "headers" do
130
- should "support literal headers" do
131
- stub_request(:post, "#{Stripe.api_base}/v1/account")
132
- .with(headers: { "Stripe-Account" => "bar" })
133
- .to_return(body: JSON.generate(object: "account"))
134
-
135
- client = StripeClient.new
136
- client.execute_request(:post, "/v1/account",
137
- headers: { "Stripe-Account" => "bar" })
138
- end
139
-
140
- should "support RestClient-style header keys" do
141
- stub_request(:post, "#{Stripe.api_base}/v1/account")
142
- .with(headers: { "Stripe-Account" => "bar" })
143
- .to_return(body: JSON.generate(object: "account"))
144
-
145
- client = StripeClient.new
146
- client.execute_request(:post, "/v1/account",
147
- headers: { stripe_account: "bar" })
148
- end
149
- end
150
-
151
- context "logging" do
152
- setup do
153
- # Freeze time for the purposes of the `elapsed` parameter that we
154
- # emit for responses. I didn't want to bring in a new dependency for
155
- # this, but Mocha's `anything` parameter can't match inside of a hash
156
- # and is therefore not useful for this purpose. If we switch over to
157
- # rspec-mocks at some point, we can probably remove Timecop from the
158
- # project.
159
- Timecop.freeze(Time.local(1990))
160
- end
161
-
162
- teardown do
163
- Timecop.return
164
- end
165
-
166
- should "produce appropriate logging" do
167
- body = JSON.generate(object: "account")
168
-
169
- Util.expects(:log_info).with("Request to Stripe API",
170
- account: "acct_123",
171
- api_version: "2010-11-12",
172
- idempotency_key: "abc",
173
- method: :post,
174
- num_retries: 0,
175
- path: "/v1/account")
176
- Util.expects(:log_debug).with("Request details",
177
- body: "",
178
- idempotency_key: "abc",
179
- query_params: nil)
180
-
181
- Util.expects(:log_info).with("Response from Stripe API",
182
- account: "acct_123",
183
- api_version: "2010-11-12",
184
- elapsed: 0.0,
185
- idempotency_key: "abc",
186
- method: :post,
187
- path: "/v1/account",
188
- request_id: "req_123",
189
- status: 200)
190
- Util.expects(:log_debug).with("Response details",
191
- body: body,
192
- idempotency_key: "abc",
193
- request_id: "req_123")
194
- Util.expects(:log_debug).with("Dashboard link for request",
195
- idempotency_key: "abc",
196
- request_id: "req_123",
197
- url: Util.request_id_dashboard_url("req_123", Stripe.api_key))
198
-
199
- stub_request(:post, "#{Stripe.api_base}/v1/account")
200
- .to_return(
201
- body: body,
202
- headers: {
203
- "Idempotency-Key" => "abc",
204
- "Request-Id" => "req_123",
205
- "Stripe-Account" => "acct_123",
206
- "Stripe-Version" => "2010-11-12",
207
- }
208
- )
209
-
210
- client = StripeClient.new
211
- client.execute_request(:post, "/v1/account",
212
- headers: {
213
- "Idempotency-Key" => "abc",
214
- "Stripe-Account" => "acct_123",
215
- "Stripe-Version" => "2010-11-12",
216
- })
217
- end
218
-
219
- should "produce logging on API error" do
220
- Util.expects(:log_info).with("Request to Stripe API",
221
- account: nil,
222
- api_version: nil,
223
- idempotency_key: nil,
224
- method: :post,
225
- num_retries: 0,
226
- path: "/v1/account")
227
- Util.expects(:log_info).with("Response from Stripe API",
228
- account: nil,
229
- api_version: nil,
230
- elapsed: 0.0,
231
- idempotency_key: nil,
232
- method: :post,
233
- path: "/v1/account",
234
- request_id: nil,
235
- status: 500)
236
-
237
- error = {
238
- code: "code",
239
- message: "message",
240
- param: "param",
241
- type: "type",
242
- }
243
- Util.expects(:log_error).with("Stripe API error",
244
- status: 500,
245
- error_code: error["code"],
246
- error_message: error["message"],
247
- error_param: error["param"],
248
- error_type: error["type"],
249
- idempotency_key: nil,
250
- request_id: nil)
251
-
252
- stub_request(:post, "#{Stripe.api_base}/v1/account")
253
- .to_return(
254
- body: JSON.generate(error: error),
255
- status: 500
256
- )
257
-
258
- client = StripeClient.new
259
- assert_raises Stripe::APIError do
260
- client.execute_request(:post, "/v1/account")
261
- end
262
- end
263
-
264
- should "produce logging on OAuth error" do
265
- Util.expects(:log_info).with("Request to Stripe API",
266
- account: nil,
267
- api_version: nil,
268
- idempotency_key: nil,
269
- method: :post,
270
- num_retries: 0,
271
- path: "/oauth/token")
272
- Util.expects(:log_info).with("Response from Stripe API",
273
- account: nil,
274
- api_version: nil,
275
- elapsed: 0.0,
276
- idempotency_key: nil,
277
- method: :post,
278
- path: "/oauth/token",
279
- request_id: nil,
280
- status: 400)
281
-
282
- Util.expects(:log_error).with("Stripe OAuth error",
283
- status: 400,
284
- error_code: "invalid_request",
285
- error_description: "No grant type specified",
286
- idempotency_key: nil,
287
- request_id: nil)
288
-
289
- stub_request(:post, "#{Stripe.connect_base}/oauth/token")
290
- .to_return(body: JSON.generate(error: "invalid_request",
291
- error_description: "No grant type specified"), status: 400)
292
-
293
- client = StripeClient.new
294
- opts = { api_base: Stripe.connect_base }
295
- assert_raises Stripe::OAuth::InvalidRequestError do
296
- client.execute_request(:post, "/oauth/token", opts)
297
- end
298
- end
299
- end
300
-
301
- context "Stripe-Account header" do
302
- should "use a globally set header" do
303
- begin
304
- old = Stripe.stripe_account
305
- Stripe.stripe_account = "acct_1234"
306
-
307
- stub_request(:post, "#{Stripe.api_base}/v1/account")
308
- .with(headers: { "Stripe-Account" => Stripe.stripe_account })
309
- .to_return(body: JSON.generate(object: "account"))
310
-
311
- client = StripeClient.new
312
- client.execute_request(:post, "/v1/account")
313
- ensure
314
- Stripe.stripe_account = old
315
- end
316
- end
317
-
318
- should "use a locally set header" do
319
- stripe_account = "acct_0000"
320
- stub_request(:post, "#{Stripe.api_base}/v1/account")
321
- .with(headers: { "Stripe-Account" => stripe_account })
322
- .to_return(body: JSON.generate(object: "account"))
323
-
324
- client = StripeClient.new
325
- client.execute_request(:post, "/v1/account",
326
- headers: { stripe_account: stripe_account })
327
- end
328
-
329
- should "not send it otherwise" do
330
- stub_request(:post, "#{Stripe.api_base}/v1/account")
331
- .with do |req|
332
- req.headers["Stripe-Account"].nil?
333
- end.to_return(body: JSON.generate(object: "account"))
334
-
335
- client = StripeClient.new
336
- client.execute_request(:post, "/v1/account")
337
- end
338
- end
339
-
340
- context "app_info" do
341
- should "send app_info if set" do
342
- begin
343
- old = Stripe.app_info
344
- Stripe.set_app_info(
345
- "MyAwesomePlugin",
346
- url: "https://myawesomeplugin.info",
347
- version: "1.2.34"
348
- )
349
-
350
- stub_request(:post, "#{Stripe.api_base}/v1/account")
351
- .with do |req|
352
- assert_equal \
353
- "Stripe/v1 RubyBindings/#{Stripe::VERSION} " \
354
- "MyAwesomePlugin/1.2.34 (https://myawesomeplugin.info)",
355
- req.headers["User-Agent"]
356
-
357
- data = JSON.parse(req.headers["X-Stripe-Client-User-Agent"],
358
- symbolize_names: true)
359
-
360
- assert_equal({
361
- name: "MyAwesomePlugin",
362
- url: "https://myawesomeplugin.info",
363
- version: "1.2.34",
364
- }, data[:application])
365
-
366
- true
367
- end.to_return(body: JSON.generate(object: "account"))
368
-
369
- client = StripeClient.new
370
- client.execute_request(:post, "/v1/account")
371
- ensure
372
- Stripe.app_info = old
373
- end
374
- end
375
- end
376
-
377
- context "error handling" do
378
- should "handle error response with empty body" do
379
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
380
- .to_return(body: "", status: 500)
381
-
382
- client = StripeClient.new
383
- e = assert_raises Stripe::APIError do
384
- client.execute_request(:post, "/v1/charges")
385
- end
386
-
387
- assert_equal 'Invalid response object from API: "" (HTTP response code was 500)', e.message
388
- end
389
-
390
- should "handle success response with empty body" do
391
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
392
- .to_return(body: "", status: 200)
393
-
394
- client = StripeClient.new
395
- e = assert_raises Stripe::APIError do
396
- client.execute_request(:post, "/v1/charges")
397
- end
398
-
399
- assert_equal 'Invalid response object from API: "" (HTTP response code was 200)', e.message
400
- end
401
-
402
- should "handle error response with unknown value" do
403
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
404
- .to_return(body: JSON.generate(bar: "foo"), status: 500)
405
-
406
- client = StripeClient.new
407
- e = assert_raises Stripe::APIError do
408
- client.execute_request(:post, "/v1/charges")
409
- end
410
-
411
- assert_equal 'Invalid response object from API: "{\"bar\":\"foo\"}" (HTTP response code was 500)', e.message
412
- end
413
-
414
- should "raise InvalidRequestError on 400" do
415
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
416
- .to_return(body: JSON.generate(make_missing_id_error), status: 400)
417
- client = StripeClient.new
418
- begin
419
- client.execute_request(:post, "/v1/charges")
420
- rescue Stripe::InvalidRequestError => e
421
- assert_equal(400, e.http_status)
422
- assert_equal(true, e.json_body.is_a?(Hash))
423
- end
424
- end
425
-
426
- should "raise AuthenticationError on 401" do
427
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
428
- .to_return(body: JSON.generate(make_missing_id_error), status: 401)
429
- client = StripeClient.new
430
- begin
431
- client.execute_request(:post, "/v1/charges")
432
- rescue Stripe::AuthenticationError => e
433
- assert_equal(401, e.http_status)
434
- assert_equal(true, e.json_body.is_a?(Hash))
435
- end
436
- end
437
-
438
- should "raise CardError on 402" do
439
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
440
- .to_return(body: JSON.generate(make_missing_id_error), status: 402)
441
- client = StripeClient.new
442
- begin
443
- client.execute_request(:post, "/v1/charges")
444
- rescue Stripe::CardError => e
445
- assert_equal(402, e.http_status)
446
- assert_equal(true, e.json_body.is_a?(Hash))
447
- end
448
- end
449
-
450
- should "raise PermissionError on 403" do
451
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
452
- .to_return(body: JSON.generate(make_missing_id_error), status: 403)
453
- client = StripeClient.new
454
- begin
455
- client.execute_request(:post, "/v1/charges")
456
- rescue Stripe::PermissionError => e
457
- assert_equal(403, e.http_status)
458
- assert_equal(true, e.json_body.is_a?(Hash))
459
- end
460
- end
461
-
462
- should "raise InvalidRequestError on 404" do
463
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
464
- .to_return(body: JSON.generate(make_missing_id_error), status: 404)
465
- client = StripeClient.new
466
- begin
467
- client.execute_request(:post, "/v1/charges")
468
- rescue Stripe::InvalidRequestError => e
469
- assert_equal(404, e.http_status)
470
- assert_equal(true, e.json_body.is_a?(Hash))
471
- end
472
- end
473
-
474
- should "raise RateLimitError on 429" do
475
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
476
- .to_return(body: JSON.generate(make_rate_limit_error), status: 429)
477
- client = StripeClient.new
478
- begin
479
- client.execute_request(:post, "/v1/charges")
480
- rescue Stripe::RateLimitError => e
481
- assert_equal(429, e.http_status)
482
- assert_equal(true, e.json_body.is_a?(Hash))
483
- end
484
- end
485
-
486
- should "raise OAuth::InvalidRequestError when error is a string with value 'invalid_request'" do
487
- stub_request(:post, "#{Stripe.connect_base}/oauth/token")
488
- .to_return(body: JSON.generate(error: "invalid_request",
489
- error_description: "No grant type specified"), status: 400)
490
-
491
- client = StripeClient.new
492
- opts = { api_base: Stripe.connect_base }
493
- e = assert_raises Stripe::OAuth::InvalidRequestError do
494
- client.execute_request(:post, "/oauth/token", opts)
495
- end
496
-
497
- assert_equal(400, e.http_status)
498
- assert_equal("No grant type specified", e.message)
499
- end
500
-
501
- should "raise OAuth::InvalidGrantError when error is a string with value 'invalid_grant'" do
502
- stub_request(:post, "#{Stripe.connect_base}/oauth/token")
503
- .to_return(body: JSON.generate(error: "invalid_grant",
504
- error_description: "This authorization code has already been used. All tokens issued with this code have been revoked."), status: 400)
505
-
506
- client = StripeClient.new
507
- opts = { api_base: Stripe.connect_base }
508
- e = assert_raises Stripe::OAuth::InvalidGrantError do
509
- client.execute_request(:post, "/oauth/token", opts)
510
- end
511
-
512
- assert_equal(400, e.http_status)
513
- assert_equal("invalid_grant", e.code)
514
- assert_equal("This authorization code has already been used. All tokens issued with this code have been revoked.", e.message)
515
- end
516
-
517
- should "raise OAuth::InvalidClientError when error is a string with value 'invalid_client'" do
518
- stub_request(:post, "#{Stripe.connect_base}/oauth/deauthorize")
519
- .to_return(body: JSON.generate(error: "invalid_client",
520
- error_description: "This application is not connected to stripe account acct_19tLK7DSlTMT26Mk, or that account does not exist."), status: 401)
521
-
522
- client = StripeClient.new
523
- opts = { api_base: Stripe.connect_base }
524
- e = assert_raises Stripe::OAuth::InvalidClientError do
525
- client.execute_request(:post, "/oauth/deauthorize", opts)
526
- end
527
-
528
- assert_equal(401, e.http_status)
529
- assert_equal("invalid_client", e.code)
530
- assert_equal("This application is not connected to stripe account acct_19tLK7DSlTMT26Mk, or that account does not exist.", e.message)
531
- end
532
-
533
- should "raise Stripe::OAuthError on indeterminate OAuth error" do
534
- stub_request(:post, "#{Stripe.connect_base}/oauth/deauthorize")
535
- .to_return(body: JSON.generate(error: "new_code_not_recognized",
536
- error_description: "Something."), status: 401)
537
-
538
- client = StripeClient.new
539
- opts = { api_base: Stripe.connect_base }
540
- e = assert_raises Stripe::OAuth::OAuthError do
541
- client.execute_request(:post, "/oauth/deauthorize", opts)
542
- end
543
-
544
- assert_equal(401, e.http_status)
545
- assert_equal("new_code_not_recognized", e.code)
546
- assert_equal("Something.", e.message)
547
- end
548
- end
549
-
550
- context "idempotency keys" do
551
- setup do
552
- Stripe.stubs(:max_network_retries).returns(2)
553
- end
554
-
555
- should "not add an idempotency key to GET requests" do
556
- SecureRandom.expects(:uuid).times(0)
557
- stub_request(:get, "#{Stripe.api_base}/v1/charges/ch_123")
558
- .with do |req|
559
- req.headers["Idempotency-Key"].nil?
560
- end.to_return(body: JSON.generate(object: "charge"))
561
- client = StripeClient.new
562
- client.execute_request(:get, "/v1/charges/ch_123")
563
- end
564
-
565
- should "ensure there is always an idempotency_key on POST requests" do
566
- SecureRandom.expects(:uuid).at_least_once.returns("random_key")
567
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
568
- .with(headers: { "Idempotency-Key" => "random_key" })
569
- .to_return(body: JSON.generate(object: "charge"))
570
- client = StripeClient.new
571
- client.execute_request(:post, "/v1/charges")
572
- end
573
-
574
- should "ensure there is always an idempotency_key on DELETE requests" do
575
- SecureRandom.expects(:uuid).at_least_once.returns("random_key")
576
- stub_request(:delete, "#{Stripe.api_base}/v1/charges/ch_123")
577
- .with(headers: { "Idempotency-Key" => "random_key" })
578
- .to_return(body: JSON.generate(object: "charge"))
579
- client = StripeClient.new
580
- client.execute_request(:delete, "/v1/charges/ch_123")
581
- end
582
-
583
- should "not override a provided idempotency_key" do
584
- # Note that this expectation looks like `:idempotency_key` instead of
585
- # the header `Idempotency-Key` because it's user provided as seen
586
- # below. The ones injected by the library itself look like headers
587
- # (`Idempotency-Key`), but rest-client does allow this symbol
588
- # formatting and will properly override the system generated one as
589
- # expected.
590
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
591
- .with(headers: { "Idempotency-Key" => "provided_key" })
592
- .to_return(body: JSON.generate(object: "charge"))
593
-
594
- client = StripeClient.new
595
- client.execute_request(:post, "/v1/charges",
596
- headers: { idempotency_key: "provided_key" })
597
- end
598
- end
599
-
600
- context "retry logic" do
601
- setup do
602
- Stripe.stubs(:max_network_retries).returns(2)
603
- end
604
-
605
- should "retry failed requests and raise if error persists" do
606
- StripeClient.expects(:sleep_time).at_least_once.returns(0)
607
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
608
- .to_raise(Errno::ECONNREFUSED.new)
609
-
610
- client = StripeClient.new
611
- err = assert_raises Stripe::APIConnectionError do
612
- client.execute_request(:post, "/v1/charges")
613
- end
614
- assert_match(/Request was retried 2 times/, err.message)
615
- end
616
-
617
- should "retry failed requests and return successful response" do
618
- StripeClient.expects(:sleep_time).at_least_once.returns(0)
619
-
620
- i = 0
621
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
622
- .to_return do |_|
623
- if i < 2
624
- i += 1
625
- raise Errno::ECONNREFUSED
626
- else
627
- { body: JSON.generate("id" => "myid") }
628
- end
629
- end
630
-
631
- client = StripeClient.new
632
- client.execute_request(:post, "/v1/charges")
633
- end
634
- end
635
-
636
- context "params serialization" do
637
- should "allows empty strings in params" do
638
- client = StripeClient.new
639
- client.execute_request(:get, "/v1/invoices/upcoming", params: {
640
- customer: "cus_123",
641
- coupon: "",
642
- })
643
- assert_requested(
644
- :get,
645
- "#{Stripe.api_base}/v1/invoices/upcoming?",
646
- query: {
647
- customer: "cus_123",
648
- coupon: "",
649
- }
650
- )
651
- end
652
-
653
- should "filter nils in params" do
654
- client = StripeClient.new
655
- client.execute_request(:get, "/v1/invoices/upcoming", params: {
656
- customer: "cus_123",
657
- coupon: nil,
658
- })
659
- assert_requested(
660
- :get,
661
- "#{Stripe.api_base}/v1/invoices/upcoming?",
662
- query: {
663
- customer: "cus_123",
664
- }
665
- )
666
- end
667
- end
668
- end
669
-
670
- context "#request" do
671
- should "return a result and response object" do
672
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
673
- .to_return(body: JSON.generate(object: "charge"))
674
-
675
- client = StripeClient.new
676
- charge, resp = client.request { Charge.create }
677
-
678
- assert charge.is_a?(Charge)
679
- assert resp.is_a?(StripeResponse)
680
- assert_equal 200, resp.http_status
681
- end
682
-
683
- should "return the value of given block" do
684
- client = StripeClient.new
685
- ret, = client.request { 7 }
686
- assert_equal 7, ret
687
- end
688
-
689
- should "reset local thread state after a call" do
690
- begin
691
- Thread.current[:stripe_client] = :stripe_client
692
-
693
- client = StripeClient.new
694
- client.request {}
695
-
696
- assert_equal :stripe_client, Thread.current[:stripe_client]
697
- ensure
698
- Thread.current[:stripe_client] = nil
699
- end
700
- end
701
- end
702
- end
703
-
704
- class SystemProfilerTest < Test::Unit::TestCase
705
- context "#uname" do
706
- should "run without failure" do
707
- # Don't actually check the result because we try a variety of different
708
- # strategies that will have different results depending on where this
709
- # test and running. We're mostly making sure that no exception is thrown.
710
- _ = StripeClient::SystemProfiler.uname
711
- end
712
- end
713
-
714
- context "#uname_from_system" do
715
- should "run without failure" do
716
- # as above, just verify that an exception is not thrown
717
- _ = StripeClient::SystemProfiler.uname_from_system
718
- end
719
- end
720
-
721
- context "#uname_from_system_ver" do
722
- should "run without failure" do
723
- # as above, just verify that an exception is not thrown
724
- _ = StripeClient::SystemProfiler.uname_from_system_ver
725
- end
726
- end
727
- end
728
- end