stripe 4.10.0 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (225) hide show
  1. checksums.yaml +4 -4
  2. data/.editorconfig +10 -0
  3. data/.rubocop.yml +28 -4
  4. data/.rubocop_todo.yml +11 -22
  5. data/.travis.yml +3 -6
  6. data/.vscode/extensions.json +7 -0
  7. data/.vscode/settings.json +8 -0
  8. data/CHANGELOG.md +86 -1
  9. data/Gemfile +2 -10
  10. data/README.md +96 -40
  11. data/Rakefile +8 -7
  12. data/VERSION +1 -1
  13. data/lib/stripe/api_operations/delete.rb +23 -1
  14. data/lib/stripe/api_operations/list.rb +0 -6
  15. data/lib/stripe/api_operations/nested_resource.rb +14 -7
  16. data/lib/stripe/api_operations/request.rb +3 -7
  17. data/lib/stripe/api_operations/save.rb +1 -3
  18. data/lib/stripe/api_resource.rb +50 -2
  19. data/lib/stripe/connection_manager.rb +131 -0
  20. data/lib/stripe/error_object.rb +94 -0
  21. data/lib/stripe/errors.rb +22 -9
  22. data/lib/stripe/list_object.rb +11 -5
  23. data/lib/stripe/multipart_encoder.rb +131 -0
  24. data/lib/stripe/object_types.rb +94 -0
  25. data/lib/stripe/{account.rb → resources/account.rb} +49 -27
  26. data/lib/stripe/{account_link.rb → resources/account_link.rb} +1 -1
  27. data/lib/stripe/resources/alipay_account.rb +34 -0
  28. data/lib/stripe/{apple_pay_domain.rb → resources/apple_pay_domain.rb} +1 -1
  29. data/lib/stripe/resources/application_fee.rb +13 -0
  30. data/lib/stripe/resources/application_fee_refund.rb +30 -0
  31. data/lib/stripe/{balance.rb → resources/balance.rb} +1 -1
  32. data/lib/stripe/{balance_transaction.rb → resources/balance_transaction.rb} +1 -5
  33. data/lib/stripe/{bank_account.rb → resources/bank_account.rb} +14 -4
  34. data/lib/stripe/{bitcoin_receiver.rb → resources/bitcoin_receiver.rb} +3 -3
  35. data/lib/stripe/{bitcoin_transaction.rb → resources/bitcoin_transaction.rb} +1 -1
  36. data/lib/stripe/resources/capability.rb +33 -0
  37. data/lib/stripe/{card.rb → resources/card.rb} +12 -4
  38. data/lib/stripe/resources/charge.rb +22 -0
  39. data/lib/stripe/{checkout → resources/checkout}/session.rb +2 -2
  40. data/lib/stripe/{country_spec.rb → resources/country_spec.rb} +1 -1
  41. data/lib/stripe/{coupon.rb → resources/coupon.rb} +2 -2
  42. data/lib/stripe/resources/credit_note.rb +22 -0
  43. data/lib/stripe/resources/customer.rb +35 -0
  44. data/lib/stripe/resources/customer_balance_transaction.rb +30 -0
  45. data/lib/stripe/resources/discount.rb +7 -0
  46. data/lib/stripe/{dispute.rb → resources/dispute.rb} +9 -7
  47. data/lib/stripe/{ephemeral_key.rb → resources/ephemeral_key.rb} +5 -2
  48. data/lib/stripe/{event.rb → resources/event.rb} +1 -1
  49. data/lib/stripe/{exchange_rate.rb → resources/exchange_rate.rb} +1 -1
  50. data/lib/stripe/{file.rb → resources/file.rb} +8 -11
  51. data/lib/stripe/{file_link.rb → resources/file_link.rb} +2 -2
  52. data/lib/stripe/resources/invoice.rb +73 -0
  53. data/lib/stripe/{invoice_item.rb → resources/invoice_item.rb} +2 -2
  54. data/lib/stripe/{invoice_line_item.rb → resources/invoice_line_item.rb} +1 -1
  55. data/lib/stripe/resources/issuing/authorization.rb +33 -0
  56. data/lib/stripe/resources/issuing/card.rb +24 -0
  57. data/lib/stripe/{issuing → resources/issuing}/card_details.rb +1 -1
  58. data/lib/stripe/{issuing → resources/issuing}/cardholder.rb +2 -2
  59. data/lib/stripe/{issuing → resources/issuing}/dispute.rb +2 -2
  60. data/lib/stripe/{issuing → resources/issuing}/transaction.rb +2 -2
  61. data/lib/stripe/resources/login_link.rb +14 -0
  62. data/lib/stripe/resources/order.rb +32 -0
  63. data/lib/stripe/{order_return.rb → resources/order_return.rb} +1 -1
  64. data/lib/stripe/resources/payment_intent.rb +42 -0
  65. data/lib/stripe/resources/payment_method.rb +32 -0
  66. data/lib/stripe/resources/payout.rb +22 -0
  67. data/lib/stripe/{person.rb → resources/person.rb} +8 -3
  68. data/lib/stripe/{plan.rb → resources/plan.rb} +1 -1
  69. data/lib/stripe/{product.rb → resources/product.rb} +3 -3
  70. data/lib/stripe/resources/radar/early_fraud_warning.rb +11 -0
  71. data/lib/stripe/{radar → resources/radar}/value_list.rb +2 -2
  72. data/lib/stripe/{radar → resources/radar}/value_list_item.rb +2 -2
  73. data/lib/stripe/{recipient.rb → resources/recipient.rb} +2 -6
  74. data/lib/stripe/{recipient_transfer.rb → resources/recipient_transfer.rb} +1 -1
  75. data/lib/stripe/{refund.rb → resources/refund.rb} +1 -1
  76. data/lib/stripe/{reporting → resources/reporting}/report_run.rb +2 -2
  77. data/lib/stripe/{reporting → resources/reporting}/report_type.rb +2 -2
  78. data/lib/stripe/resources/reversal.rb +29 -0
  79. data/lib/stripe/resources/review.rb +20 -0
  80. data/lib/stripe/resources/setup_intent.rb +32 -0
  81. data/lib/stripe/{sigma → resources/sigma}/scheduled_query_run.rb +2 -2
  82. data/lib/stripe/{sku.rb → resources/sku.rb} +3 -3
  83. data/lib/stripe/{source.rb → resources/source.rb} +17 -15
  84. data/lib/stripe/{source_transaction.rb → resources/source_transaction.rb} +1 -1
  85. data/lib/stripe/resources/subscription.rb +25 -0
  86. data/lib/stripe/{subscription_item.rb → resources/subscription_item.rb} +5 -2
  87. data/lib/stripe/resources/subscription_schedule.rb +32 -0
  88. data/lib/stripe/resources/tax_id.rb +26 -0
  89. data/lib/stripe/resources/tax_rate.rb +11 -0
  90. data/lib/stripe/{terminal → resources/terminal}/connection_token.rb +2 -2
  91. data/lib/stripe/{terminal → resources/terminal}/location.rb +2 -2
  92. data/lib/stripe/{terminal → resources/terminal}/reader.rb +2 -2
  93. data/lib/stripe/{three_d_secure.rb → resources/three_d_secure.rb} +1 -1
  94. data/lib/stripe/{token.rb → resources/token.rb} +1 -1
  95. data/lib/stripe/resources/topup.rb +22 -0
  96. data/lib/stripe/resources/transfer.rb +26 -0
  97. data/lib/stripe/resources/usage_record.rb +7 -0
  98. data/lib/stripe/{usage_record_summary.rb → resources/usage_record_summary.rb} +1 -1
  99. data/lib/stripe/{webhook_endpoint.rb → resources/webhook_endpoint.rb} +2 -2
  100. data/lib/stripe/resources.rb +77 -0
  101. data/lib/stripe/singleton_api_resource.rb +3 -1
  102. data/lib/stripe/stripe_client.rb +337 -218
  103. data/lib/stripe/stripe_object.rb +54 -59
  104. data/lib/stripe/stripe_response.rb +53 -21
  105. data/lib/stripe/util.rb +54 -110
  106. data/lib/stripe/version.rb +1 -1
  107. data/lib/stripe/webhook.rb +5 -3
  108. data/lib/stripe.rb +64 -86
  109. data/stripe.gemspec +14 -5
  110. data/test/stripe/account_link_test.rb +1 -1
  111. data/test/stripe/account_test.rb +186 -30
  112. data/test/stripe/alipay_account_test.rb +1 -1
  113. data/test/stripe/api_operations_test.rb +3 -4
  114. data/test/stripe/api_resource_test.rb +105 -18
  115. data/test/stripe/apple_pay_domain_test.rb +18 -5
  116. data/test/stripe/application_fee_refund_test.rb +1 -1
  117. data/test/stripe/application_fee_test.rb +45 -1
  118. data/test/stripe/balance_test.rb +1 -1
  119. data/test/stripe/balance_transaction_test.rb +20 -0
  120. data/test/stripe/bank_account_test.rb +1 -1
  121. data/test/stripe/capability_test.rb +45 -0
  122. data/test/stripe/charge_test.rb +13 -8
  123. data/test/stripe/checkout/session_test.rb +1 -1
  124. data/test/stripe/connection_manager_test.rb +138 -0
  125. data/test/stripe/country_spec_test.rb +1 -1
  126. data/test/stripe/coupon_test.rb +16 -6
  127. data/test/stripe/credit_note_test.rb +61 -0
  128. data/test/stripe/customer_balance_transaction_test.rb +37 -0
  129. data/test/stripe/customer_card_test.rb +1 -1
  130. data/test/stripe/customer_test.rb +151 -40
  131. data/test/stripe/dispute_test.rb +10 -1
  132. data/test/stripe/ephemeral_key_test.rb +8 -1
  133. data/test/stripe/errors_test.rb +30 -9
  134. data/test/stripe/exchange_rate_test.rb +1 -1
  135. data/test/stripe/file_link_test.rb +1 -1
  136. data/test/stripe/file_test.rb +19 -5
  137. data/test/stripe/invoice_item_test.rb +18 -7
  138. data/test/stripe/invoice_line_item_test.rb +1 -1
  139. data/test/stripe/invoice_test.rb +77 -9
  140. data/test/stripe/issuing/authorization_test.rb +33 -11
  141. data/test/stripe/issuing/card_test.rb +15 -6
  142. data/test/stripe/issuing/cardholder_test.rb +1 -1
  143. data/test/stripe/issuing/dispute_test.rb +1 -1
  144. data/test/stripe/issuing/transaction_test.rb +1 -1
  145. data/test/stripe/list_object_test.rb +1 -17
  146. data/test/stripe/login_link_test.rb +2 -2
  147. data/test/stripe/multipart_encoder_test.rb +130 -0
  148. data/test/stripe/oauth_test.rb +1 -1
  149. data/test/stripe/order_return_test.rb +1 -1
  150. data/test/stripe/order_test.rb +28 -3
  151. data/test/stripe/payment_intent_test.rb +31 -4
  152. data/test/stripe/payment_method_test.rb +19 -1
  153. data/test/stripe/payout_test.rb +8 -1
  154. data/test/stripe/person_test.rb +1 -1
  155. data/test/stripe/plan_test.rb +26 -20
  156. data/test/stripe/product_test.rb +16 -6
  157. data/test/stripe/radar/early_fraud_warning_test.rb +22 -0
  158. data/test/stripe/radar/value_list_item_test.rb +16 -6
  159. data/test/stripe/radar/value_list_test.rb +16 -6
  160. data/test/stripe/recipient_test.rb +18 -5
  161. data/test/stripe/refund_test.rb +1 -1
  162. data/test/stripe/reporting/report_run_test.rb +1 -1
  163. data/test/stripe/reporting/report_type_test.rb +1 -1
  164. data/test/stripe/reversal_test.rb +1 -1
  165. data/test/stripe/review_test.rb +1 -1
  166. data/test/stripe/setup_intent_test.rb +84 -0
  167. data/test/stripe/sigma/scheduled_query_run_test.rb +1 -1
  168. data/test/stripe/sku_test.rb +16 -6
  169. data/test/stripe/source_test.rb +14 -19
  170. data/test/stripe/source_transaction_test.rb +1 -1
  171. data/test/stripe/stripe_client_test.rb +230 -26
  172. data/test/stripe/stripe_object_test.rb +8 -36
  173. data/test/stripe/stripe_response_test.rb +71 -25
  174. data/test/stripe/subscription_item_test.rb +28 -6
  175. data/test/stripe/subscription_schedule_test.rb +19 -1
  176. data/test/stripe/subscription_test.rb +29 -9
  177. data/test/stripe/tax_id_test.rb +31 -0
  178. data/test/stripe/tax_rate_test.rb +43 -0
  179. data/test/stripe/terminal/connection_token_test.rb +1 -1
  180. data/test/stripe/terminal/location_test.rb +16 -6
  181. data/test/stripe/terminal/reader_test.rb +16 -6
  182. data/test/stripe/three_d_secure_test.rb +1 -1
  183. data/test/stripe/topup_test.rb +9 -1
  184. data/test/stripe/transfer_test.rb +46 -1
  185. data/test/stripe/usage_record_summary_test.rb +1 -1
  186. data/test/stripe/util_test.rb +1 -1
  187. data/test/stripe/webhook_endpoint_test.rb +18 -1
  188. data/test/stripe/webhook_test.rb +4 -4
  189. data/test/stripe_mock.rb +4 -3
  190. data/test/stripe_test.rb +1 -14
  191. data/test/test_helper.rb +14 -11
  192. metadata +114 -124
  193. data/lib/stripe/alipay_account.rb +0 -27
  194. data/lib/stripe/application_fee.rb +0 -23
  195. data/lib/stripe/application_fee_refund.rb +0 -22
  196. data/lib/stripe/charge.rb +0 -84
  197. data/lib/stripe/customer.rb +0 -90
  198. data/lib/stripe/invoice.rb +0 -48
  199. data/lib/stripe/issuer_fraud_record.rb +0 -9
  200. data/lib/stripe/issuing/authorization.rb +0 -22
  201. data/lib/stripe/issuing/card.rb +0 -18
  202. data/lib/stripe/login_link.rb +0 -11
  203. data/lib/stripe/order.rb +0 -31
  204. data/lib/stripe/payment_intent.rb +0 -26
  205. data/lib/stripe/payment_method.rb +0 -23
  206. data/lib/stripe/payout.rb +0 -20
  207. data/lib/stripe/reversal.rb +0 -22
  208. data/lib/stripe/review.rb +0 -14
  209. data/lib/stripe/subscription.rb +0 -25
  210. data/lib/stripe/subscription_schedule.rb +0 -32
  211. data/lib/stripe/subscription_schedule_revision.rb +0 -25
  212. data/lib/stripe/topup.rb +0 -16
  213. data/lib/stripe/transfer.rb +0 -23
  214. data/lib/stripe/usage_record.rb +0 -14
  215. data/test/stripe/account_external_accounts_operations_test.rb +0 -69
  216. data/test/stripe/account_login_links_operations_test.rb +0 -21
  217. data/test/stripe/account_persons_operations_test.rb +0 -70
  218. data/test/stripe/application_fee_refunds_operations_test.rb +0 -56
  219. data/test/stripe/customer_sources_operations_test.rb +0 -64
  220. data/test/stripe/file_upload_test.rb +0 -76
  221. data/test/stripe/issuer_fraud_record_test.rb +0 -20
  222. data/test/stripe/subscription_schedule_revision_test.rb +0 -37
  223. data/test/stripe/subscription_schedule_revisions_operations_test.rb +0 -35
  224. data/test/stripe/transfer_reversals_operations_test.rb +0 -57
  225. data/test/stripe/usage_record_test.rb +0 -28
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require ::File.expand_path("../../../test_helper", __FILE__)
3
+ require ::File.expand_path("../../test_helper", __dir__)
4
4
 
5
5
  module Stripe
6
6
  module Radar
@@ -27,11 +27,21 @@ module Stripe
27
27
  assert item.is_a?(Stripe::Radar::ValueListItem)
28
28
  end
29
29
 
30
- should "be deletable" do
31
- list = Stripe::Radar::ValueListItem.retrieve("rsli_123")
32
- list = list.delete
33
- assert_requested :delete, "#{Stripe.api_base}/v1/radar/value_list_items/rsli_123"
34
- assert list.is_a?(Stripe::Radar::ValueListItem)
30
+ context "#delete" do
31
+ should "be deletable" do
32
+ list = Stripe::Radar::ValueListItem.retrieve("rsli_123")
33
+ list = list.delete
34
+ assert_requested :delete, "#{Stripe.api_base}/v1/radar/value_list_items/rsli_123"
35
+ assert list.is_a?(Stripe::Radar::ValueListItem)
36
+ end
37
+ end
38
+
39
+ context ".delete" do
40
+ should "be deletable" do
41
+ list = Stripe::Radar::ValueListItem.delete("rsli_123")
42
+ assert_requested :delete, "#{Stripe.api_base}/v1/radar/value_list_items/rsli_123"
43
+ assert list.is_a?(Stripe::Radar::ValueListItem)
44
+ end
35
45
  end
36
46
  end
37
47
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require ::File.expand_path("../../../test_helper", __FILE__)
3
+ require ::File.expand_path("../../test_helper", __dir__)
4
4
 
5
5
  module Stripe
6
6
  module Radar
@@ -40,11 +40,21 @@ module Stripe
40
40
  assert list.is_a?(Stripe::Radar::ValueList)
41
41
  end
42
42
 
43
- should "be deletable" do
44
- list = Stripe::Radar::ValueList.retrieve("rsl_123")
45
- list = list.delete
46
- assert_requested :delete, "#{Stripe.api_base}/v1/radar/value_lists/rsl_123"
47
- assert list.is_a?(Stripe::Radar::ValueList)
43
+ context "#delete" do
44
+ should "be deletable" do
45
+ list = Stripe::Radar::ValueList.retrieve("rsl_123")
46
+ list = list.delete
47
+ assert_requested :delete, "#{Stripe.api_base}/v1/radar/value_lists/rsl_123"
48
+ assert list.is_a?(Stripe::Radar::ValueList)
49
+ end
50
+ end
51
+
52
+ context ".delete" do
53
+ should "be deletable" do
54
+ list = Stripe::Radar::ValueList.delete("rsl_123")
55
+ assert_requested :delete, "#{Stripe.api_base}/v1/radar/value_lists/rsl_123"
56
+ assert list.is_a?(Stripe::Radar::ValueList)
57
+ end
48
58
  end
49
59
  end
50
60
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require ::File.expand_path("../../test_helper", __FILE__)
3
+ require ::File.expand_path("../test_helper", __dir__)
4
4
 
5
5
  module Stripe
6
6
  class RecipientTest < Test::Unit::TestCase
@@ -40,10 +40,23 @@ module Stripe
40
40
  end
41
41
 
42
42
  should "be deletable" do
43
- recipient = Stripe::Recipient.retrieve("rp_123")
44
- recipient = recipient.delete
45
- assert_requested :delete, "#{Stripe.api_base}/v1/recipients/#{recipient.id}"
46
- assert recipient.is_a?(Stripe::Recipient)
43
+ end
44
+
45
+ context "#delete" do
46
+ should "be deletable" do
47
+ recipient = Stripe::Recipient.retrieve("rp_123")
48
+ recipient = recipient.delete
49
+ assert_requested :delete, "#{Stripe.api_base}/v1/recipients/#{recipient.id}"
50
+ assert recipient.is_a?(Stripe::Recipient)
51
+ end
52
+ end
53
+
54
+ context ".delete" do
55
+ should "be deletable" do
56
+ recipient = Stripe::Recipient.delete("rp_123")
57
+ assert_requested :delete, "#{Stripe.api_base}/v1/recipients/rp_123"
58
+ assert recipient.is_a?(Stripe::Recipient)
59
+ end
47
60
  end
48
61
  end
49
62
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require ::File.expand_path("../../test_helper", __FILE__)
3
+ require ::File.expand_path("../test_helper", __dir__)
4
4
 
5
5
  module Stripe
6
6
  class RefundTest < Test::Unit::TestCase
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require ::File.expand_path("../../../test_helper", __FILE__)
3
+ require ::File.expand_path("../../test_helper", __dir__)
4
4
 
5
5
  module Stripe
6
6
  module Reporting
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require ::File.expand_path("../../../test_helper", __FILE__)
3
+ require ::File.expand_path("../../test_helper", __dir__)
4
4
 
5
5
  module Stripe
6
6
  module Reporting
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require ::File.expand_path("../../test_helper", __FILE__)
3
+ require ::File.expand_path("../test_helper", __dir__)
4
4
 
5
5
  module Stripe
6
6
  class ReversalTest < Test::Unit::TestCase
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require ::File.expand_path("../../test_helper", __FILE__)
3
+ require ::File.expand_path("../test_helper", __dir__)
4
4
 
5
5
  module Stripe
6
6
  class ReviewTest < Test::Unit::TestCase
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ require ::File.expand_path("../test_helper", __dir__)
4
+
5
+ module Stripe
6
+ class SetupIntentTest < Test::Unit::TestCase
7
+ TEST_RESOURCE_ID = "seti_123"
8
+
9
+ should "be listable" do
10
+ setup_intents = Stripe::SetupIntent.list
11
+ assert_requested :get, "#{Stripe.api_base}/v1/setup_intents"
12
+ assert setup_intents.data.is_a?(Array)
13
+ assert setup_intents.data[0].is_a?(Stripe::SetupIntent)
14
+ end
15
+
16
+ should "be retrievable" do
17
+ setup_intent = Stripe::SetupIntent.retrieve("seti_123")
18
+ assert_requested :get, "#{Stripe.api_base}/v1/setup_intents/seti_123"
19
+ assert setup_intent.is_a?(Stripe::SetupIntent)
20
+ end
21
+
22
+ should "be creatable" do
23
+ setup_intent = Stripe::SetupIntent.create(
24
+ payment_method_types: ["card"]
25
+ )
26
+ assert_requested :post, "#{Stripe.api_base}/v1/setup_intents"
27
+ assert setup_intent.is_a?(Stripe::SetupIntent)
28
+ end
29
+
30
+ should "be saveable" do
31
+ setup_intent = Stripe::SetupIntent.construct_from(id: "seti_123", object: "setup_intent", metadata: {})
32
+ setup_intent.metadata["key"] = "value"
33
+ setup_intent.save
34
+ assert_requested :post, "#{Stripe.api_base}/v1/setup_intents/#{setup_intent.id}"
35
+ end
36
+
37
+ should "be updateable" do
38
+ setup_intent = Stripe::SetupIntent.update("seti_123", metadata: { foo: "bar" })
39
+
40
+ assert_requested :post, "#{Stripe.api_base}/v1/setup_intents/seti_123"
41
+ assert setup_intent.is_a?(Stripe::SetupIntent)
42
+ end
43
+
44
+ context "#cancel" do
45
+ should "cancel a setup_intent" do
46
+ setup_intent = Stripe::SetupIntent.construct_from(id: "seti_123", object: "setup_intent")
47
+ setup_intent = setup_intent.cancel
48
+
49
+ assert_requested :post, "#{Stripe.api_base}/v1/setup_intents/seti_123/cancel"
50
+ assert setup_intent.is_a?(Stripe::SetupIntent)
51
+ end
52
+ end
53
+
54
+ context ".cancel" do
55
+ should "cancel a setup_intent" do
56
+ setup_intent = Stripe::SetupIntent.cancel("seti_123")
57
+
58
+ assert_requested :post, "#{Stripe.api_base}/v1/setup_intents/seti_123/cancel"
59
+ assert setup_intent.is_a?(Stripe::SetupIntent)
60
+ end
61
+ end
62
+
63
+ context "#confirm" do
64
+ should "confirm a setup_intent" do
65
+ setup_intent = Stripe::SetupIntent.construct_from(id: "seti_123", object: "setup_intent")
66
+ setup_intent = setup_intent.confirm(
67
+ payment_method: "pm_123"
68
+ )
69
+
70
+ assert_requested :post, "#{Stripe.api_base}/v1/setup_intents/seti_123/confirm"
71
+ assert setup_intent.is_a?(Stripe::SetupIntent)
72
+ end
73
+ end
74
+
75
+ context ".confirm" do
76
+ should "confirm a setup_intent" do
77
+ setup_intent = Stripe::SetupIntent.confirm("seti_123", payment_method: "pm_123")
78
+
79
+ assert_requested :post, "#{Stripe.api_base}/v1/setup_intents/seti_123/confirm"
80
+ assert setup_intent.is_a?(Stripe::SetupIntent)
81
+ end
82
+ end
83
+ end
84
+ end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require ::File.expand_path("../../../test_helper", __FILE__)
3
+ require ::File.expand_path("../../test_helper", __dir__)
4
4
 
5
5
  module Stripe
6
6
  module Issuing
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require ::File.expand_path("../../test_helper", __FILE__)
3
+ require ::File.expand_path("../test_helper", __dir__)
4
4
 
5
5
  module Stripe
6
6
  class SKUTest < Test::Unit::TestCase
@@ -40,11 +40,21 @@ module Stripe
40
40
  assert sku.is_a?(Stripe::SKU)
41
41
  end
42
42
 
43
- should "be deletable" do
44
- sku = Stripe::SKU.retrieve("sku_123")
45
- sku = sku.delete
46
- assert_requested :delete, "#{Stripe.api_base}/v1/skus/#{sku.id}"
47
- assert sku.is_a?(Stripe::SKU)
43
+ context "#delete" do
44
+ should "be deletable" do
45
+ sku = Stripe::SKU.retrieve("sku_123")
46
+ sku = sku.delete
47
+ assert_requested :delete, "#{Stripe.api_base}/v1/skus/#{sku.id}"
48
+ assert sku.is_a?(Stripe::SKU)
49
+ end
50
+ end
51
+
52
+ context ".delete" do
53
+ should "be deletable" do
54
+ sku = Stripe::SKU.delete("sku_123")
55
+ assert_requested :delete, "#{Stripe.api_base}/v1/skus/sku_123"
56
+ assert sku.is_a?(Stripe::SKU)
57
+ end
48
58
  end
49
59
  end
50
60
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require ::File.expand_path("../../test_helper", __FILE__)
3
+ require ::File.expand_path("../test_helper", __dir__)
4
4
 
5
5
  module Stripe
6
6
  class SourceTest < Test::Unit::TestCase
@@ -51,24 +51,6 @@ module Stripe
51
51
  end
52
52
  end
53
53
 
54
- context "#delete" do
55
- should "warn that #delete is deprecated" do
56
- old_stderr = $stderr
57
- $stderr = StringIO.new
58
- begin
59
- source = Stripe::Source.construct_from(customer: "cus_123",
60
- id: "src_123",
61
- object: "source")
62
- source.delete
63
- message = "NOTE: Stripe::Source#delete is " \
64
- "deprecated; use #detach instead"
65
- assert_match Regexp.new(message), $stderr.string
66
- ensure
67
- $stderr = old_stderr
68
- end
69
- end
70
- end
71
-
72
54
  should "not be listable" do
73
55
  assert_raises NoMethodError do
74
56
  Stripe::Source.list
@@ -79,6 +61,19 @@ module Stripe
79
61
  should "verify the source" do
80
62
  source = Stripe::Source.retrieve("src_123")
81
63
  source = source.verify(values: [1, 2])
64
+ assert_requested :post,
65
+ "#{Stripe.api_base}/v1/sources/#{source.id}/verify",
66
+ body: { values: [1, 2] }
67
+ assert source.is_a?(Stripe::Source)
68
+ end
69
+ end
70
+
71
+ context ".verify" do
72
+ should "verify the source" do
73
+ source = Stripe::Source.verify("src_123", values: [1, 2])
74
+ assert_requested :post,
75
+ "#{Stripe.api_base}/v1/sources/src_123/verify",
76
+ body: { values: [1, 2] }
82
77
  assert source.is_a?(Stripe::Source)
83
78
  end
84
79
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require ::File.expand_path("../../test_helper", __FILE__)
3
+ require ::File.expand_path("../test_helper", __dir__)
4
4
 
5
5
  module Stripe
6
6
  class SourceTransactionTest < Test::Unit::TestCase
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require ::File.expand_path("../../test_helper", __FILE__)
3
+ require ::File.expand_path("../test_helper", __dir__)
4
4
 
5
5
  module Stripe
6
6
  class StripeClientTest < Test::Unit::TestCase
@@ -17,6 +17,50 @@ module Stripe
17
17
  end
18
18
  end
19
19
 
20
+ context ".clear_all_connection_managers" do
21
+ should "clear connection managers across all threads" do
22
+ stub_request(:post, "#{Stripe.api_base}/path")
23
+ .to_return(body: JSON.generate(object: "account"))
24
+
25
+ num_threads = 3
26
+
27
+ # Poorly named class -- note this is actually a concurrent queue.
28
+ recv_queue = Queue.new
29
+ send_queue = Queue.new
30
+
31
+ threads = num_threads.times.map do |_|
32
+ Thread.start do
33
+ # Causes a connection manager to be created on this thread and a
34
+ # connection within that manager to be created for API access.
35
+ manager = StripeClient.default_connection_manager
36
+ manager.execute_request(:post, "#{Stripe.api_base}/path")
37
+
38
+ # Signal to the main thread we're ready.
39
+ recv_queue << true
40
+
41
+ # Wait for the main thread to signal continue.
42
+ send_queue.pop
43
+
44
+ # This check isn't great, but it's otherwise difficult to tell that
45
+ # anything happened with just the public-facing API.
46
+ assert_equal({}, manager.instance_variable_get(:@active_connections))
47
+ end
48
+ end
49
+
50
+ # Wait for threads to start up.
51
+ threads.each { recv_queue.pop }
52
+
53
+ # Do the clear (the method we're actually trying to test).
54
+ StripeClient.clear_all_connection_managers
55
+
56
+ # Tell threads to run their check.
57
+ threads.each { send_queue << true }
58
+
59
+ # And finally, give all threads time to perform their check.
60
+ threads.each(&:join)
61
+ end
62
+ end
63
+
20
64
  context ".default_client" do
21
65
  should "be a StripeClient" do
22
66
  assert_kind_of StripeClient, StripeClient.default_client
@@ -32,18 +76,19 @@ module Stripe
32
76
  end
33
77
  end
34
78
 
35
- context ".default_conn" do
36
- should "be a Faraday::Connection" do
37
- assert_kind_of Faraday::Connection, StripeClient.default_conn
79
+ context ".default_connection_manager" do
80
+ should "be a ConnectionManager" do
81
+ assert_kind_of ConnectionManager,
82
+ StripeClient.default_connection_manager
38
83
  end
39
84
 
40
85
  should "be a different connection on each thread" do
41
- other_thread_conn = nil
86
+ other_thread_manager = nil
42
87
  thread = Thread.new do
43
- other_thread_conn = StripeClient.default_conn
88
+ other_thread_manager = StripeClient.default_connection_manager
44
89
  end
45
90
  thread.join
46
- refute_equal StripeClient.default_conn, other_thread_conn
91
+ refute_equal StripeClient.default_connection_manager, other_thread_manager
47
92
  end
48
93
  end
49
94
 
@@ -52,26 +97,54 @@ module Stripe
52
97
  Stripe.stubs(:max_network_retries).returns(2)
53
98
  end
54
99
 
55
- should "retry on timeout" do
56
- assert StripeClient.should_retry?(Faraday::TimeoutError.new(""), 0)
100
+ should "retry on Errno::ECONNREFUSED" do
101
+ assert StripeClient.should_retry?(Errno::ECONNREFUSED.new,
102
+ method: :post, num_retries: 0)
103
+ end
104
+
105
+ should "retry on Net::OpenTimeout" do
106
+ assert StripeClient.should_retry?(Net::OpenTimeout.new,
107
+ method: :post, num_retries: 0)
108
+ end
109
+
110
+ should "retry on Net::ReadTimeout" do
111
+ assert StripeClient.should_retry?(Net::ReadTimeout.new,
112
+ method: :post, num_retries: 0)
57
113
  end
58
114
 
59
- should "retry on a failed connection" do
60
- assert StripeClient.should_retry?(Faraday::ConnectionFailed.new(""), 0)
115
+ should "retry on SocketError" do
116
+ assert StripeClient.should_retry?(SocketError.new,
117
+ method: :post, num_retries: 0)
61
118
  end
62
119
 
63
- should "retry on a conflict" do
64
- error = make_rate_limit_error
65
- e = Faraday::ClientError.new(error[:error][:message], status: 409)
66
- assert StripeClient.should_retry?(e, 0)
120
+ should "retry on a 409 Conflict" do
121
+ assert StripeClient.should_retry?(Stripe::StripeError.new(http_status: 409),
122
+ method: :post, num_retries: 0)
123
+ end
124
+
125
+ should "retry on a 500 Internal Server Error when non-POST" do
126
+ assert StripeClient.should_retry?(Stripe::StripeError.new(http_status: 500),
127
+ method: :get, num_retries: 0)
128
+ end
129
+
130
+ should "retry on a 503 Service Unavailable" do
131
+ assert StripeClient.should_retry?(Stripe::StripeError.new(http_status: 503),
132
+ method: :post, num_retries: 0)
67
133
  end
68
134
 
69
135
  should "not retry at maximum count" do
70
- refute StripeClient.should_retry?(RuntimeError.new, Stripe.max_network_retries)
136
+ refute StripeClient.should_retry?(RuntimeError.new,
137
+ method: :post, num_retries: Stripe.max_network_retries)
71
138
  end
72
139
 
73
140
  should "not retry on a certificate validation error" do
74
- refute StripeClient.should_retry?(Faraday::SSLError.new(""), 0)
141
+ refute StripeClient.should_retry?(OpenSSL::SSL::SSLError.new,
142
+ method: :post, num_retries: 0)
143
+ end
144
+
145
+ should "not retry on a 500 Internal Server Error when POST" do
146
+ refute StripeClient.should_retry?(Stripe::StripeError.new(http_status: 500),
147
+ method: :post, num_retries: 0)
75
148
  end
76
149
  end
77
150
 
@@ -115,15 +188,16 @@ module Stripe
115
188
  end
116
189
 
117
190
  context "#initialize" do
118
- should "set Stripe.default_conn" do
191
+ should "set Stripe.default_connection_manager" do
119
192
  client = StripeClient.new
120
- assert_equal StripeClient.default_conn, client.conn
193
+ assert_equal StripeClient.default_connection_manager,
194
+ client.connection_manager
121
195
  end
122
196
 
123
197
  should "set a different connection if one was specified" do
124
- conn = Faraday.new
125
- client = StripeClient.new(conn)
126
- assert_equal conn, client.conn
198
+ connection_manager = ConnectionManager.new
199
+ client = StripeClient.new(connection_manager)
200
+ assert_equal connection_manager, client.connection_manager
127
201
  end
128
202
  end
129
203
 
@@ -178,7 +252,7 @@ module Stripe
178
252
  Util.expects(:log_debug).with("Request details",
179
253
  body: "",
180
254
  idempotency_key: "abc",
181
- query_params: nil)
255
+ query: nil)
182
256
 
183
257
  Util.expects(:log_info).with("Response from Stripe API",
184
258
  account: "acct_123",
@@ -403,6 +477,20 @@ module Stripe
403
477
  assert_equal 'Invalid response object from API: "" (HTTP response code was 200)', e.message
404
478
  end
405
479
 
480
+ should "handle low level error" do
481
+ stub_request(:post, "#{Stripe.api_base}/v1/charges")
482
+ .to_raise(Errno::ECONNREFUSED.new)
483
+
484
+ client = StripeClient.new
485
+ e = assert_raises Stripe::APIConnectionError do
486
+ client.execute_request(:post, "/v1/charges")
487
+ end
488
+
489
+ assert_equal StripeClient::ERROR_MESSAGE_CONNECTION % Stripe.api_base +
490
+ "\n\n(Network error: Connection refused)",
491
+ e.message
492
+ end
493
+
406
494
  should "handle error response with unknown value" do
407
495
  stub_request(:post, "#{Stripe.api_base}/v1/charges")
408
496
  .to_return(body: JSON.generate(bar: "foo"), status: 500)
@@ -738,14 +826,130 @@ module Stripe
738
826
 
739
827
  should "reset local thread state after a call" do
740
828
  begin
741
- Thread.current[:stripe_client] = :stripe_client
829
+ StripeClient.current_thread_context.active_client = :stripe_client
830
+
831
+ client = StripeClient.new
832
+ client.request {}
833
+
834
+ assert_equal :stripe_client,
835
+ StripeClient.current_thread_context.active_client
836
+ ensure
837
+ StripeClient.current_thread_context.active_client = nil
838
+ end
839
+ end
840
+
841
+ should "correctly return last responses despite multiple clients" do
842
+ charge_resp = { object: "charge" }
843
+ coupon_resp = { object: "coupon" }
844
+
845
+ stub_request(:post, "#{Stripe.api_base}/v1/charges")
846
+ .to_return(body: JSON.generate(charge_resp))
847
+ stub_request(:post, "#{Stripe.api_base}/v1/coupons")
848
+ .to_return(body: JSON.generate(coupon_resp))
849
+
850
+ client1 = StripeClient.new
851
+ client2 = StripeClient.new
852
+
853
+ client2_resp = nil
854
+ _charge, client1_resp = client1.request do
855
+ Charge.create
856
+
857
+ # This is contrived, but we run one client nested in the `request`
858
+ # block of another one just to ensure that the parent is still
859
+ # unwinding when this goes through. If the parent's last response
860
+ # were to be overridden by this client (through a bug), then it would
861
+ # happen here.
862
+ _coupon, client2_resp = client2.request do
863
+ Coupon.create
864
+ end
865
+ end
866
+
867
+ assert_equal charge_resp, client1_resp.data
868
+ assert_equal coupon_resp, client2_resp.data
869
+ end
870
+
871
+ should "correctly return last responses despite multiple threads" do
872
+ charge_resp = { object: "charge" }
873
+ coupon_resp = { object: "coupon" }
874
+
875
+ stub_request(:post, "#{Stripe.api_base}/v1/charges")
876
+ .to_return(body: JSON.generate(charge_resp))
877
+ stub_request(:post, "#{Stripe.api_base}/v1/coupons")
878
+ .to_return(body: JSON.generate(coupon_resp))
879
+
880
+ client = StripeClient.new
881
+
882
+ # Poorly named class -- note this is actually a concurrent queue.
883
+ recv_queue = Queue.new
884
+ send_queue = Queue.new
885
+
886
+ # Start a thread, make an API request, but then idle in the `request`
887
+ # block until the main thread has been able to make its own API request
888
+ # and signal that it's done. If this thread's last response were to be
889
+ # overridden by the main thread (through a bug), then this routine
890
+ # should suss it out.
891
+ resp1 = nil
892
+ thread = Thread.start do
893
+ _charge, resp1 = client.request do
894
+ Charge.create
895
+
896
+ # Idle in `request` block until main thread signals.
897
+ send_queue.pop
898
+ end
899
+
900
+ # Signal main thread that we're done and it can run its checks.
901
+ recv_queue << true
902
+ end
903
+
904
+ # Make an API request.
905
+ _coupon, resp2 = client.request do
906
+ Coupon.create
907
+ end
908
+
909
+ # Tell background thread to finish `request`, then wait for it to
910
+ # signal back to us that it's ready.
911
+ send_queue << true
912
+ recv_queue.pop
913
+
914
+ assert_equal charge_resp, resp1.data
915
+ assert_equal coupon_resp, resp2.data
916
+
917
+ # And for maximum hygiene, make sure that our thread rejoins.
918
+ thread.join
919
+ end
920
+
921
+ should "error if calls to #request are nested on the same thread" do
922
+ client = StripeClient.new
923
+ client.request do
924
+ e = assert_raises(RuntimeError) do
925
+ client.request {}
926
+ end
927
+ assert_equal "calls to StripeClient#request cannot be nested within a thread",
928
+ e.message
929
+ end
930
+ end
931
+ end
932
+
933
+ context "#proxy" do
934
+ should "run the request through the proxy" do
935
+ begin
936
+ StripeClient.current_thread_context.default_connection_manager = nil
937
+
938
+ Stripe.proxy = "http://user:pass@localhost:8080"
742
939
 
743
940
  client = StripeClient.new
744
941
  client.request {}
745
942
 
746
- assert_equal :stripe_client, Thread.current[:stripe_client]
943
+ connection = Stripe::StripeClient.default_connection_manager.connection_for(Stripe.api_base)
944
+
945
+ assert_equal "localhost", connection.proxy_address
946
+ assert_equal 8080, connection.proxy_port
947
+ assert_equal "user", connection.proxy_user
948
+ assert_equal "pass", connection.proxy_pass
747
949
  ensure
748
- Thread.current[:stripe_client] = nil
950
+ Stripe.proxy = nil
951
+
952
+ StripeClient.current_thread_context.default_connection_manager = nil
749
953
  end
750
954
  end
751
955
  end