stripe 4.17.0 → 5.32.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (227) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +193 -1
  3. data/CODE_OF_CONDUCT.md +77 -0
  4. data/Gemfile +10 -14
  5. data/README.md +167 -63
  6. data/Rakefile +8 -7
  7. data/VERSION +1 -1
  8. data/lib/stripe/api_operations/create.rb +1 -1
  9. data/lib/stripe/api_operations/delete.rb +7 -3
  10. data/lib/stripe/api_operations/list.rb +1 -12
  11. data/lib/stripe/api_operations/nested_resource.rb +36 -27
  12. data/lib/stripe/api_operations/request.rb +47 -8
  13. data/lib/stripe/api_operations/save.rb +8 -7
  14. data/lib/stripe/api_resource.rb +30 -5
  15. data/lib/stripe/connection_manager.rb +164 -0
  16. data/lib/stripe/error_object.rb +94 -0
  17. data/lib/stripe/errors.rb +31 -10
  18. data/lib/stripe/instrumentation.rb +82 -0
  19. data/lib/stripe/list_object.rb +41 -7
  20. data/lib/stripe/multipart_encoder.rb +131 -0
  21. data/lib/stripe/oauth.rb +8 -6
  22. data/lib/stripe/object_types.rb +102 -0
  23. data/lib/stripe/{account.rb → resources/account.rb} +44 -33
  24. data/lib/stripe/{account_link.rb → resources/account_link.rb} +2 -1
  25. data/lib/stripe/resources/alipay_account.rb +34 -0
  26. data/lib/stripe/{apple_pay_domain.rb → resources/apple_pay_domain.rb} +2 -1
  27. data/lib/stripe/resources/application_fee.rb +14 -0
  28. data/lib/stripe/resources/application_fee_refund.rb +30 -0
  29. data/lib/stripe/{balance.rb → resources/balance.rb} +2 -1
  30. data/lib/stripe/{balance_transaction.rb → resources/balance_transaction.rb} +2 -5
  31. data/lib/stripe/resources/bank_account.rb +43 -0
  32. data/lib/stripe/{issuing/dispute.rb → resources/billing_portal/configuration.rb} +4 -3
  33. data/lib/stripe/{checkout → resources/billing_portal}/session.rb +3 -2
  34. data/lib/stripe/{bitcoin_receiver.rb → resources/bitcoin_receiver.rb} +4 -3
  35. data/lib/stripe/{bitcoin_transaction.rb → resources/bitcoin_transaction.rb} +1 -1
  36. data/lib/stripe/{capability.rb → resources/capability.rb} +11 -4
  37. data/lib/stripe/{card.rb → resources/card.rb} +12 -3
  38. data/lib/stripe/resources/charge.rb +23 -0
  39. data/lib/stripe/resources/checkout/session.rb +16 -0
  40. data/lib/stripe/{country_spec.rb → resources/country_spec.rb} +2 -1
  41. data/lib/stripe/{coupon.rb → resources/coupon.rb} +2 -1
  42. data/lib/stripe/resources/credit_note.rb +33 -0
  43. data/lib/stripe/resources/credit_note_line_item.rb +7 -0
  44. data/lib/stripe/resources/customer.rb +41 -0
  45. data/lib/stripe/resources/customer_balance_transaction.rb +30 -0
  46. data/lib/stripe/{discount.rb → resources/discount.rb} +1 -1
  47. data/lib/stripe/resources/dispute.rb +22 -0
  48. data/lib/stripe/{ephemeral_key.rb → resources/ephemeral_key.rb} +6 -2
  49. data/lib/stripe/{event.rb → resources/event.rb} +2 -1
  50. data/lib/stripe/{exchange_rate.rb → resources/exchange_rate.rb} +2 -1
  51. data/lib/stripe/{file.rb → resources/file.rb} +7 -12
  52. data/lib/stripe/{file_link.rb → resources/file_link.rb} +2 -1
  53. data/lib/stripe/resources/invoice.rb +74 -0
  54. data/lib/stripe/{invoice_item.rb → resources/invoice_item.rb} +2 -1
  55. data/lib/stripe/{invoice_line_item.rb → resources/invoice_line_item.rb} +1 -1
  56. data/lib/stripe/{issuing → resources/issuing}/authorization.rb +14 -5
  57. data/lib/stripe/{issuing → resources/issuing}/card.rb +8 -3
  58. data/lib/stripe/{issuing → resources/issuing}/card_details.rb +1 -1
  59. data/lib/stripe/{issuing → resources/issuing}/cardholder.rb +2 -1
  60. data/lib/stripe/resources/issuing/dispute.rb +25 -0
  61. data/lib/stripe/{issuing → resources/issuing}/transaction.rb +2 -1
  62. data/lib/stripe/resources/line_item.rb +7 -0
  63. data/lib/stripe/resources/login_link.rb +14 -0
  64. data/lib/stripe/resources/mandate.rb +8 -0
  65. data/lib/stripe/resources/order.rb +33 -0
  66. data/lib/stripe/{order_return.rb → resources/order_return.rb} +2 -1
  67. data/lib/stripe/resources/payment_intent.rb +43 -0
  68. data/lib/stripe/{payment_method.rb → resources/payment_method.rb} +14 -5
  69. data/lib/stripe/resources/payout.rb +33 -0
  70. data/lib/stripe/{person.rb → resources/person.rb} +8 -3
  71. data/lib/stripe/{plan.rb → resources/plan.rb} +2 -1
  72. data/lib/stripe/resources/price.rb +12 -0
  73. data/lib/stripe/{product.rb → resources/product.rb} +2 -1
  74. data/lib/stripe/resources/promotion_code.rb +12 -0
  75. data/lib/stripe/resources/radar/early_fraud_warning.rb +12 -0
  76. data/lib/stripe/{radar → resources/radar}/value_list.rb +2 -1
  77. data/lib/stripe/{radar → resources/radar}/value_list_item.rb +2 -1
  78. data/lib/stripe/{recipient.rb → resources/recipient.rb} +2 -5
  79. data/lib/stripe/{recipient_transfer.rb → resources/recipient_transfer.rb} +1 -1
  80. data/lib/stripe/{refund.rb → resources/refund.rb} +2 -1
  81. data/lib/stripe/{reporting → resources/reporting}/report_run.rb +2 -1
  82. data/lib/stripe/{reporting → resources/reporting}/report_type.rb +2 -1
  83. data/lib/stripe/resources/reversal.rb +29 -0
  84. data/lib/stripe/{review.rb → resources/review.rb} +8 -3
  85. data/lib/stripe/resources/setup_attempt.rb +10 -0
  86. data/lib/stripe/resources/setup_intent.rb +33 -0
  87. data/lib/stripe/{sigma → resources/sigma}/scheduled_query_run.rb +2 -1
  88. data/lib/stripe/{sku.rb → resources/sku.rb} +2 -1
  89. data/lib/stripe/{source.rb → resources/source.rb} +22 -15
  90. data/lib/stripe/{source_transaction.rb → resources/source_transaction.rb} +1 -1
  91. data/lib/stripe/{subscription.rb → resources/subscription.rb} +12 -13
  92. data/lib/stripe/resources/subscription_item.rb +26 -0
  93. data/lib/stripe/resources/subscription_schedule.rb +33 -0
  94. data/lib/stripe/resources/tax_id.rb +26 -0
  95. data/lib/stripe/{tax_rate.rb → resources/tax_rate.rb} +2 -1
  96. data/lib/stripe/{terminal → resources/terminal}/connection_token.rb +2 -1
  97. data/lib/stripe/{terminal → resources/terminal}/location.rb +2 -1
  98. data/lib/stripe/{terminal → resources/terminal}/reader.rb +2 -1
  99. data/lib/stripe/{three_d_secure.rb → resources/three_d_secure.rb} +2 -1
  100. data/lib/stripe/{token.rb → resources/token.rb} +2 -1
  101. data/lib/stripe/{topup.rb → resources/topup.rb} +8 -3
  102. data/lib/stripe/resources/transfer.rb +27 -0
  103. data/lib/stripe/resources/usage_record.rb +7 -0
  104. data/lib/stripe/{usage_record_summary.rb → resources/usage_record_summary.rb} +1 -1
  105. data/lib/stripe/{webhook_endpoint.rb → resources/webhook_endpoint.rb} +2 -1
  106. data/lib/stripe/resources.rb +85 -0
  107. data/lib/stripe/singleton_api_resource.rb +3 -1
  108. data/lib/stripe/stripe_client.rb +576 -254
  109. data/lib/stripe/stripe_configuration.rb +194 -0
  110. data/lib/stripe/stripe_object.rb +80 -61
  111. data/lib/stripe/stripe_response.rb +53 -21
  112. data/lib/stripe/util.rb +76 -121
  113. data/lib/stripe/version.rb +1 -1
  114. data/lib/stripe/webhook.rb +43 -10
  115. data/lib/stripe.rb +42 -203
  116. data/stripe.gemspec +23 -7
  117. metadata +107 -284
  118. data/.gitattributes +0 -4
  119. data/.github/ISSUE_TEMPLATE.md +0 -5
  120. data/.gitignore +0 -8
  121. data/.rubocop.yml +0 -32
  122. data/.rubocop_todo.yml +0 -50
  123. data/.travis.yml +0 -42
  124. data/lib/stripe/alipay_account.rb +0 -27
  125. data/lib/stripe/application_fee.rb +0 -23
  126. data/lib/stripe/application_fee_refund.rb +0 -22
  127. data/lib/stripe/bank_account.rb +0 -32
  128. data/lib/stripe/charge.rb +0 -86
  129. data/lib/stripe/credit_note.rb +0 -18
  130. data/lib/stripe/customer.rb +0 -95
  131. data/lib/stripe/dispute.rb +0 -23
  132. data/lib/stripe/invoice.rb +0 -48
  133. data/lib/stripe/issuer_fraud_record.rb +0 -9
  134. data/lib/stripe/login_link.rb +0 -11
  135. data/lib/stripe/order.rb +0 -34
  136. data/lib/stripe/payment_intent.rb +0 -30
  137. data/lib/stripe/payout.rb +0 -24
  138. data/lib/stripe/reversal.rb +0 -22
  139. data/lib/stripe/subscription_item.rb +0 -17
  140. data/lib/stripe/subscription_schedule.rb +0 -35
  141. data/lib/stripe/subscription_schedule_revision.rb +0 -25
  142. data/lib/stripe/tax_id.rb +0 -22
  143. data/lib/stripe/transfer.rb +0 -25
  144. data/lib/stripe/usage_record.rb +0 -14
  145. data/test/api_stub_helpers.rb +0 -1
  146. data/test/openapi/README.md +0 -9
  147. data/test/stripe/account_link_test.rb +0 -18
  148. data/test/stripe/account_test.rb +0 -428
  149. data/test/stripe/alipay_account_test.rb +0 -37
  150. data/test/stripe/api_operations_test.rb +0 -81
  151. data/test/stripe/api_resource_test.rb +0 -526
  152. data/test/stripe/apple_pay_domain_test.rb +0 -46
  153. data/test/stripe/application_fee_refund_test.rb +0 -37
  154. data/test/stripe/application_fee_test.rb +0 -58
  155. data/test/stripe/balance_test.rb +0 -13
  156. data/test/stripe/bank_account_test.rb +0 -36
  157. data/test/stripe/capability_test.rb +0 -45
  158. data/test/stripe/charge_test.rb +0 -80
  159. data/test/stripe/checkout/session_test.rb +0 -41
  160. data/test/stripe/country_spec_test.rb +0 -20
  161. data/test/stripe/coupon_test.rb +0 -61
  162. data/test/stripe/credit_note_test.rb +0 -61
  163. data/test/stripe/customer_card_test.rb +0 -42
  164. data/test/stripe/customer_test.rb +0 -226
  165. data/test/stripe/dispute_test.rb +0 -51
  166. data/test/stripe/ephemeral_key_test.rb +0 -93
  167. data/test/stripe/errors_test.rb +0 -20
  168. data/test/stripe/exchange_rate_test.rb +0 -20
  169. data/test/stripe/file_link_test.rb +0 -41
  170. data/test/stripe/file_test.rb +0 -97
  171. data/test/stripe/file_upload_test.rb +0 -79
  172. data/test/stripe/invoice_item_test.rb +0 -66
  173. data/test/stripe/invoice_line_item_test.rb +0 -8
  174. data/test/stripe/invoice_test.rb +0 -213
  175. data/test/stripe/issuer_fraud_record_test.rb +0 -20
  176. data/test/stripe/issuing/authorization_test.rb +0 -72
  177. data/test/stripe/issuing/card_test.rb +0 -62
  178. data/test/stripe/issuing/cardholder_test.rb +0 -53
  179. data/test/stripe/issuing/dispute_test.rb +0 -45
  180. data/test/stripe/issuing/transaction_test.rb +0 -48
  181. data/test/stripe/list_object_test.rb +0 -156
  182. data/test/stripe/login_link_test.rb +0 -37
  183. data/test/stripe/oauth_test.rb +0 -88
  184. data/test/stripe/order_return_test.rb +0 -21
  185. data/test/stripe/order_test.rb +0 -75
  186. data/test/stripe/payment_intent_test.rb +0 -107
  187. data/test/stripe/payment_method_test.rb +0 -84
  188. data/test/stripe/payout_test.rb +0 -57
  189. data/test/stripe/person_test.rb +0 -46
  190. data/test/stripe/plan_test.rb +0 -98
  191. data/test/stripe/product_test.rb +0 -59
  192. data/test/stripe/radar/value_list_item_test.rb +0 -48
  193. data/test/stripe/radar/value_list_test.rb +0 -61
  194. data/test/stripe/recipient_test.rb +0 -62
  195. data/test/stripe/refund_test.rb +0 -39
  196. data/test/stripe/reporting/report_run_test.rb +0 -33
  197. data/test/stripe/reporting/report_type_test.rb +0 -22
  198. data/test/stripe/reversal_test.rb +0 -43
  199. data/test/stripe/review_test.rb +0 -27
  200. data/test/stripe/sigma/scheduled_query_run_test.rb +0 -22
  201. data/test/stripe/sku_test.rb +0 -60
  202. data/test/stripe/source_test.rb +0 -99
  203. data/test/stripe/source_transaction_test.rb +0 -19
  204. data/test/stripe/stripe_client_test.rb +0 -842
  205. data/test/stripe/stripe_object_test.rb +0 -525
  206. data/test/stripe/stripe_response_test.rb +0 -49
  207. data/test/stripe/subscription_item_test.rb +0 -63
  208. data/test/stripe/subscription_schedule_revision_test.rb +0 -37
  209. data/test/stripe/subscription_schedule_test.rb +0 -116
  210. data/test/stripe/subscription_test.rb +0 -80
  211. data/test/stripe/tax_id_test.rb +0 -31
  212. data/test/stripe/tax_rate_test.rb +0 -43
  213. data/test/stripe/terminal/connection_token_test.rb +0 -16
  214. data/test/stripe/terminal/location_test.rb +0 -68
  215. data/test/stripe/terminal/reader_test.rb +0 -62
  216. data/test/stripe/three_d_secure_test.rb +0 -23
  217. data/test/stripe/topup_test.rb +0 -62
  218. data/test/stripe/transfer_test.rb +0 -88
  219. data/test/stripe/usage_record_summary_test.rb +0 -19
  220. data/test/stripe/usage_record_test.rb +0 -28
  221. data/test/stripe/util_test.rb +0 -402
  222. data/test/stripe/webhook_endpoint_test.rb +0 -59
  223. data/test/stripe/webhook_test.rb +0 -96
  224. data/test/stripe_mock.rb +0 -77
  225. data/test/stripe_test.rb +0 -63
  226. data/test/test_data.rb +0 -61
  227. data/test/test_helper.rb +0 -73
data/Rakefile CHANGED
@@ -13,7 +13,8 @@ RuboCop::RakeTask.new
13
13
 
14
14
  desc "Update bundled certs"
15
15
  task :update_certs do
16
- require "faraday"
16
+ require "net/http"
17
+ require "uri"
17
18
 
18
19
  fetch_file "https://curl.haxx.se/ca/cacert.pem",
19
20
  ::File.expand_path("../lib/data/ca-certificates.crt", __FILE__)
@@ -23,14 +24,14 @@ end
23
24
  # helpers
24
25
  #
25
26
 
26
- def fetch_file(url, dest)
27
+ def fetch_file(uri, dest)
27
28
  ::File.open(dest, "w") do |file|
28
- resp = Faraday.get(url)
29
- unless resp.status == 200
30
- abort("bad response when fetching: #{url}\n" \
31
- "Status #{resp.status}: #{resp.body}")
29
+ resp = Net::HTTP.get_response(URI.parse(uri))
30
+ unless resp.code.to_i == 200
31
+ abort("bad response when fetching: #{uri}\n" \
32
+ "Status #{resp.code}: #{resp.body}")
32
33
  end
33
34
  file.write(resp.body)
34
- puts "Successfully fetched: #{url}"
35
+ puts "Successfully fetched: #{uri}"
35
36
  end
36
37
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 4.17.0
1
+ 5.32.0
@@ -4,7 +4,7 @@ module Stripe
4
4
  module APIOperations
5
5
  module Create
6
6
  def create(params = {}, opts = {})
7
- resp, opts = request(:post, resource_url, params, opts)
7
+ resp, opts = execute_resource_request(:post, resource_url, params, opts)
8
8
  Util.convert_to_stripe_object(resp.data, opts)
9
9
  end
10
10
  end
@@ -15,15 +15,19 @@ module Stripe
15
15
  # * +opts+ - A Hash of additional options (separate from the params /
16
16
  # object values) to be added to the request. E.g. to allow for an
17
17
  # idempotency_key to be passed in the request headers, or for the
18
- # api_key to be overwritten. See {APIOperations::Request.request}.
18
+ # api_key to be overwritten. See
19
+ # {APIOperations::Request.execute_resource_request}.
19
20
  def delete(id, params = {}, opts = {})
20
- resp, opts = request(:delete, "#{resource_url}/#{id}", params, opts)
21
+ resp, opts = execute_resource_request(:delete,
22
+ "#{resource_url}/#{id}",
23
+ params, opts)
21
24
  Util.convert_to_stripe_object(resp.data, opts)
22
25
  end
23
26
  end
24
27
 
25
28
  def delete(params = {}, opts = {})
26
- resp, opts = request(:delete, resource_url, params, opts)
29
+ resp, opts = execute_resource_request(:delete, resource_url,
30
+ params, opts)
27
31
  initialize_from(resp.data, opts)
28
32
  end
29
33
 
@@ -6,25 +6,14 @@ module Stripe
6
6
  def list(filters = {}, opts = {})
7
7
  opts = Util.normalize_opts(opts)
8
8
 
9
- resp, opts = request(:get, resource_url, filters, opts)
9
+ resp, opts = execute_resource_request(:get, resource_url, filters, opts)
10
10
  obj = ListObject.construct_from(resp.data, opts)
11
11
 
12
12
  # set filters so that we can fetch the same limit, expansions, and
13
13
  # predicates when accessing the next and previous pages
14
- #
15
- # just for general cleanliness, remove any paging options
16
14
  obj.filters = filters.dup
17
- obj.filters.delete(:ending_before)
18
- obj.filters.delete(:starting_after)
19
-
20
15
  obj
21
16
  end
22
-
23
- # The original version of #list was given the somewhat unfortunate name of
24
- # #all, and this alias allows us to maintain backward compatibility (the
25
- # choice was somewhat misleading in the way that it only returned a single
26
- # page rather than all objects).
27
- alias all list
28
17
  end
29
18
  end
30
19
  end
@@ -6,16 +6,19 @@ module Stripe
6
6
  # that it's possible to do so from a static context (i.e. without a
7
7
  # pre-existing collection of subresources on the parent).
8
8
  #
9
- # For examle, a transfer gains the static methods for reversals so that the
9
+ # For example, a transfer gains the static methods for reversals so that the
10
10
  # methods `.create_reversal`, `.retrieve_reversal`, `.update_reversal`,
11
11
  # etc. all become available.
12
12
  module NestedResource
13
- def nested_resource_class_methods(resource, path: nil, operations: nil, resource_plural: nil)
13
+ def nested_resource_class_methods(resource, path: nil, operations: nil,
14
+ resource_plural: nil)
14
15
  resource_plural ||= "#{resource}s"
15
16
  path ||= resource_plural
17
+
16
18
  raise ArgumentError, "operations array required" if operations.nil?
17
19
 
18
20
  resource_url_method = :"#{resource}s_url"
21
+
19
22
  define_singleton_method(resource_url_method) do |id, nested_id = nil|
20
23
  url = "#{resource_url}/#{CGI.escape(id)}/#{CGI.escape(path)}"
21
24
  url += "/#{CGI.escape(nested_id)}" unless nested_id.nil?
@@ -25,35 +28,41 @@ module Stripe
25
28
  operations.each do |operation|
26
29
  case operation
27
30
  when :create
28
- define_singleton_method(:"create_#{resource}") do |id, params = {}, opts = {}|
29
- url = send(resource_url_method, id)
30
- resp, opts = request(:post, url, params, opts)
31
- Util.convert_to_stripe_object(resp.data, opts)
32
- end
31
+ define_singleton_method(:"create_#{resource}") \
32
+ do |id, params = {}, opts = {}|
33
+ url = send(resource_url_method, id)
34
+ resp, opts = execute_resource_request(:post, url, params, opts)
35
+ Util.convert_to_stripe_object(resp.data, opts)
36
+ end
33
37
  when :retrieve
34
- define_singleton_method(:"retrieve_#{resource}") do |id, nested_id, opts = {}|
35
- url = send(resource_url_method, id, nested_id)
36
- resp, opts = request(:get, url, {}, opts)
37
- Util.convert_to_stripe_object(resp.data, opts)
38
- end
38
+ define_singleton_method(:"retrieve_#{resource}") \
39
+ do |id, nested_id, opts = {}|
40
+ url = send(resource_url_method, id, nested_id)
41
+ resp, opts = execute_resource_request(:get, url, {}, opts)
42
+ Util.convert_to_stripe_object(resp.data, opts)
43
+ end
39
44
  when :update
40
- define_singleton_method(:"update_#{resource}") do |id, nested_id, params = {}, opts = {}|
41
- url = send(resource_url_method, id, nested_id)
42
- resp, opts = request(:post, url, params, opts)
43
- Util.convert_to_stripe_object(resp.data, opts)
44
- end
45
+ define_singleton_method(:"update_#{resource}") \
46
+ do |id, nested_id, params = {}, opts = {}|
47
+ url = send(resource_url_method, id, nested_id)
48
+ resp, opts = execute_resource_request(:post, url, params, opts)
49
+ Util.convert_to_stripe_object(resp.data, opts)
50
+ end
45
51
  when :delete
46
- define_singleton_method(:"delete_#{resource}") do |id, nested_id, params = {}, opts = {}|
47
- url = send(resource_url_method, id, nested_id)
48
- resp, opts = request(:delete, url, params, opts)
49
- Util.convert_to_stripe_object(resp.data, opts)
50
- end
52
+ define_singleton_method(:"delete_#{resource}") \
53
+ do |id, nested_id, params = {}, opts = {}|
54
+ url = send(resource_url_method, id, nested_id)
55
+ resp, opts = execute_resource_request(:delete, url, params,
56
+ opts)
57
+ Util.convert_to_stripe_object(resp.data, opts)
58
+ end
51
59
  when :list
52
- define_singleton_method(:"list_#{resource_plural}") do |id, params = {}, opts = {}|
53
- url = send(resource_url_method, id)
54
- resp, opts = request(:get, url, params, opts)
55
- Util.convert_to_stripe_object(resp.data, opts)
56
- end
60
+ define_singleton_method(:"list_#{resource_plural}") \
61
+ do |id, params = {}, opts = {}|
62
+ url = send(resource_url_method, id)
63
+ resp, opts = execute_resource_request(:get, url, params, opts)
64
+ Util.convert_to_stripe_object(resp.data, opts)
65
+ end
57
66
  else
58
67
  raise ArgumentError, "Unknown operation: #{operation.inspect}"
59
68
  end
@@ -4,10 +4,16 @@ module Stripe
4
4
  module APIOperations
5
5
  module Request
6
6
  module ClassMethods
7
- def request(method, url, params = {}, opts = {})
7
+ def execute_resource_request(method, url,
8
+ params = {}, opts = {})
9
+ params ||= {}
10
+
11
+ error_on_invalid_params(params)
8
12
  warn_on_opts_in_params(params)
9
13
 
10
14
  opts = Util.normalize_opts(opts)
15
+ error_on_non_string_user_opts(opts)
16
+
11
17
  opts[:client] ||= StripeClient.active_client
12
18
 
13
19
  headers = opts.clone
@@ -31,12 +37,43 @@ module Stripe
31
37
  [resp, opts_to_persist]
32
38
  end
33
39
 
34
- private
40
+ # This method used to be called `request`, but it's such a short name
41
+ # that it eventually conflicted with the name of a field on an API
42
+ # resource (specifically, `Event#request`), so it was renamed to
43
+ # something more unique.
44
+ #
45
+ # The former name had been around for just about forever though, and
46
+ # although all internal uses have been renamed, I've left this alias in
47
+ # place for backwards compatibility. Consider removing it on the next
48
+ # major.
49
+ alias request execute_resource_request
35
50
 
36
- def warn_on_opts_in_params(params)
51
+ private def error_on_non_string_user_opts(opts)
52
+ Util::OPTS_USER_SPECIFIED.each do |opt|
53
+ next unless opts.key?(opt)
54
+
55
+ val = opts[opt]
56
+ next if val.nil?
57
+ next if val.is_a?(String)
58
+
59
+ raise ArgumentError,
60
+ "request option '#{opt}' should be a string value " \
61
+ "(was a #{val.class})"
62
+ end
63
+ end
64
+
65
+ private def error_on_invalid_params(params)
66
+ return if params.nil? || params.is_a?(Hash)
67
+
68
+ raise ArgumentError,
69
+ "request params should be either a Hash or nil " \
70
+ "(was a #{params.class})"
71
+ end
72
+
73
+ private def warn_on_opts_in_params(params)
37
74
  Util::OPTS_USER_SPECIFIED.each do |opt|
38
75
  if params.key?(opt)
39
- $stderr.puts("WARNING: #{opt} should be in opts instead of params.")
76
+ warn("WARNING: '#{opt}' should be in opts instead of params.")
40
77
  end
41
78
  end
42
79
  end
@@ -46,12 +83,14 @@ module Stripe
46
83
  base.extend(ClassMethods)
47
84
  end
48
85
 
49
- protected
50
-
51
- def request(method, url, params = {}, opts = {})
86
+ protected def execute_resource_request(method, url,
87
+ params = {}, opts = {})
52
88
  opts = @opts.merge(Util.normalize_opts(opts))
53
- self.class.request(method, url, params, opts)
89
+ self.class.execute_resource_request(method, url, params, opts)
54
90
  end
91
+
92
+ # See notes on `alias` above.
93
+ alias request execute_resource_request
55
94
  end
56
95
  end
57
96
  end
@@ -15,7 +15,8 @@ module Stripe
15
15
  # * +opts+ - A Hash of additional options (separate from the params /
16
16
  # object values) to be added to the request. E.g. to allow for an
17
17
  # idempotency_key to be passed in the request headers, or for the
18
- # api_key to be overwritten. See {APIOperations::Request.request}.
18
+ # api_key to be overwritten. See
19
+ # {APIOperations::Request.execute_resource_request}.
19
20
  def update(id, params = {}, opts = {})
20
21
  params.each_key do |k|
21
22
  if protected_fields.include?(k)
@@ -23,7 +24,8 @@ module Stripe
23
24
  end
24
25
  end
25
26
 
26
- resp, opts = request(:post, "#{resource_url}/#{id}", params, opts)
27
+ resp, opts = execute_resource_request(:post, "#{resource_url}/#{id}",
28
+ params, opts)
27
29
  Util.convert_to_stripe_object(resp.data, opts)
28
30
  end
29
31
  end
@@ -43,7 +45,8 @@ module Stripe
43
45
  # * +opts+ - A Hash of additional options (separate from the params /
44
46
  # object values) to be added to the request. E.g. to allow for an
45
47
  # idempotency_key to be passed in the request headers, or for the
46
- # api_key to be overwritten. See {APIOperations::Request.request}.
48
+ # api_key to be overwritten. See
49
+ # {APIOperations::Request.execute_resource_request}.
47
50
  def save(params = {}, opts = {})
48
51
  # We started unintentionally (sort of) allowing attributes sent to
49
52
  # +save+ to override values used during the update. So as not to break
@@ -59,7 +62,7 @@ module Stripe
59
62
  # generated a uri for this object with an identifier baked in
60
63
  values.delete(:id)
61
64
 
62
- resp, opts = request(:post, save_url, values, opts)
65
+ resp, opts = execute_resource_request(:post, save_url, values, opts)
63
66
  initialize_from(resp.data, opts)
64
67
  end
65
68
 
@@ -76,9 +79,7 @@ module Stripe
76
79
  base.extend(ClassMethods)
77
80
  end
78
81
 
79
- private
80
-
81
- def save_url
82
+ private def save_url
82
83
  # This switch essentially allows us "upsert"-like functionality. If the
83
84
  # API resource doesn't have an ID set (suggesting that it's new) and
84
85
  # its class responds to .create (which comes from
@@ -17,7 +17,9 @@ module Stripe
17
17
 
18
18
  def self.resource_url
19
19
  if self == APIResource
20
- raise NotImplementedError, "APIResource is an abstract class. You should perform actions on its subclasses (Charge, Customer, etc.)"
20
+ raise NotImplementedError,
21
+ "APIResource is an abstract class. You should perform actions " \
22
+ "on its subclasses (Charge, Customer, etc.)"
21
23
  end
22
24
  # Namespaces are separated in object names with periods (.) and in URLs
23
25
  # with forward slashes (/), so replace the former with the latter.
@@ -62,25 +64,37 @@ module Stripe
62
64
  # will send a POST request to `/v1/<object_name>/capture`.
63
65
  def self.custom_method(name, http_verb:, http_path: nil)
64
66
  unless %i[get post delete].include?(http_verb)
65
- raise ArgumentError, "Invalid http_verb value: #{http_verb.inspect}. Should be one of :get, :post or :delete."
67
+ raise ArgumentError,
68
+ "Invalid http_verb value: #{http_verb.inspect}. Should be one " \
69
+ "of :get, :post or :delete."
66
70
  end
67
71
  http_path ||= name.to_s
68
72
  define_singleton_method(name) do |id, params = {}, opts = {}|
73
+ unless id.is_a?(String)
74
+ raise ArgumentError,
75
+ "id should be a string representing the ID of an API resource"
76
+ end
77
+
69
78
  url = "#{resource_url}/#{CGI.escape(id)}/#{CGI.escape(http_path)}"
70
- resp, opts = request(http_verb, url, params, opts)
79
+ resp, opts = execute_resource_request(http_verb, url, params, opts)
71
80
  Util.convert_to_stripe_object(resp.data, opts)
72
81
  end
73
82
  end
74
83
 
75
84
  def resource_url
76
85
  unless (id = self["id"])
77
- raise InvalidRequestError.new("Could not determine which URL to request: #{self.class} instance has invalid ID: #{id.inspect}", "id")
86
+ raise InvalidRequestError.new(
87
+ "Could not determine which URL to request: #{self.class} instance " \
88
+ "has invalid ID: #{id.inspect}",
89
+ "id"
90
+ )
78
91
  end
79
92
  "#{self.class.resource_url}/#{CGI.escape(id)}"
80
93
  end
81
94
 
82
95
  def refresh
83
- resp, opts = request(:get, resource_url, @retrieve_params)
96
+ resp, opts = execute_resource_request(:get, resource_url,
97
+ @retrieve_params)
84
98
  initialize_from(resp.data, opts)
85
99
  end
86
100
 
@@ -90,5 +104,16 @@ module Stripe
90
104
  instance.refresh
91
105
  instance
92
106
  end
107
+
108
+ protected def request_stripe_object(method:, path:, params:, opts: {})
109
+ resp, opts = execute_resource_request(method, path, params, opts)
110
+
111
+ # If we're getting back this thing, update; otherwise, instantiate.
112
+ if Util.object_name_matches_class?(resp.data[:object], self.class)
113
+ initialize_from(resp.data, opts)
114
+ else
115
+ Util.convert_to_stripe_object(resp.data, opts)
116
+ end
117
+ end
93
118
  end
94
119
  end
@@ -0,0 +1,164 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Stripe
4
+ # Manages connections across multiple hosts which is useful because the
5
+ # library may connect to multiple hosts during a typical session (main API,
6
+ # Connect, Uploads). Ruby doesn't provide an easy way to make this happen
7
+ # easily, so this class is designed to track what we're connected to and
8
+ # manage the lifecycle of those connections.
9
+ #
10
+ # Note that this class in itself is *not* thread safe. We expect it to be
11
+ # instantiated once per thread.
12
+ class ConnectionManager
13
+ # Timestamp (in seconds procured from the system's monotonic clock)
14
+ # indicating when the connection manager last made a request. This is used
15
+ # by `StripeClient` to determine whether a connection manager should be
16
+ # garbage collected or not.
17
+ attr_reader :last_used
18
+ attr_reader :config
19
+
20
+ def initialize(config = Stripe.config)
21
+ @config = config
22
+ @active_connections = {}
23
+ @last_used = Util.monotonic_time
24
+
25
+ # A connection manager may be accessed across threads as one thread makes
26
+ # requests on it while another is trying to clear it (either because it's
27
+ # trying to garbage collect the manager or trying to clear it because a
28
+ # configuration setting has changed). That's probably thread-safe already
29
+ # because of Ruby's GIL, but just in case the library's running on JRuby
30
+ # or the like, use a mutex to synchronize access in this connection
31
+ # manager.
32
+ @mutex = Mutex.new
33
+ end
34
+
35
+ # Finishes any active connections by closing their TCP connection and
36
+ # clears them from internal tracking.
37
+ def clear
38
+ @mutex.synchronize do
39
+ @active_connections.each do |_, connection|
40
+ connection.finish
41
+ end
42
+ @active_connections = {}
43
+ end
44
+ end
45
+
46
+ # Gets a connection for a given URI. This is for internal use only as it's
47
+ # subject to change (we've moved between HTTP client schemes in the past
48
+ # and may do it again).
49
+ #
50
+ # `uri` is expected to be a string.
51
+ def connection_for(uri)
52
+ @mutex.synchronize do
53
+ u = URI.parse(uri)
54
+ connection = @active_connections[[u.host, u.port]]
55
+
56
+ if connection.nil?
57
+ connection = create_connection(u)
58
+ connection.start
59
+
60
+ @active_connections[[u.host, u.port]] = connection
61
+ end
62
+
63
+ connection
64
+ end
65
+ end
66
+
67
+ # Executes an HTTP request to the given URI with the given method. Also
68
+ # allows a request body, headers, and query string to be specified.
69
+ def execute_request(method, uri, body: nil, headers: nil, query: nil)
70
+ # Perform some basic argument validation because it's easy to get
71
+ # confused between strings and hashes for things like body and query
72
+ # parameters.
73
+ raise ArgumentError, "method should be a symbol" \
74
+ unless method.is_a?(Symbol)
75
+ raise ArgumentError, "uri should be a string" \
76
+ unless uri.is_a?(String)
77
+ raise ArgumentError, "body should be a string" \
78
+ if body && !body.is_a?(String)
79
+ raise ArgumentError, "headers should be a hash" \
80
+ if headers && !headers.is_a?(Hash)
81
+ raise ArgumentError, "query should be a string" \
82
+ if query && !query.is_a?(String)
83
+
84
+ @last_used = Util.monotonic_time
85
+
86
+ connection = connection_for(uri)
87
+
88
+ u = URI.parse(uri)
89
+ path = if query
90
+ u.path + "?" + query
91
+ else
92
+ u.path
93
+ end
94
+
95
+ @mutex.synchronize do
96
+ connection.send_request(method.to_s.upcase, path, body, headers)
97
+ end
98
+ end
99
+
100
+ #
101
+ # private
102
+ #
103
+
104
+ # `uri` should be a parsed `URI` object.
105
+ private def create_connection(uri)
106
+ # These all come back as `nil` if no proxy is configured.
107
+ proxy_host, proxy_port, proxy_user, proxy_pass = proxy_parts
108
+
109
+ connection = Net::HTTP.new(uri.host, uri.port,
110
+ proxy_host, proxy_port,
111
+ proxy_user, proxy_pass)
112
+
113
+ # Time in seconds within which Net::HTTP will try to reuse an already
114
+ # open connection when issuing a new operation. Outside this window, Ruby
115
+ # will transparently close and re-open the connection without trying to
116
+ # reuse it.
117
+ #
118
+ # Ruby's default of 2 seconds is almost certainly too short. Here I've
119
+ # reused Go's default for `DefaultTransport`.
120
+ connection.keep_alive_timeout = 30
121
+
122
+ connection.open_timeout = config.open_timeout
123
+ connection.read_timeout = config.read_timeout
124
+ if connection.respond_to?(:write_timeout=)
125
+ connection.write_timeout = config.write_timeout
126
+ end
127
+
128
+ connection.use_ssl = uri.scheme == "https"
129
+
130
+ if config.verify_ssl_certs
131
+ connection.verify_mode = OpenSSL::SSL::VERIFY_PEER
132
+ connection.cert_store = config.ca_store
133
+ else
134
+ connection.verify_mode = OpenSSL::SSL::VERIFY_NONE
135
+ warn_ssl_verify_none
136
+ end
137
+
138
+ connection
139
+ end
140
+
141
+ # `Net::HTTP` somewhat awkwardly requires each component of a proxy URI
142
+ # (host, port, etc.) rather than the URI itself. This method simply parses
143
+ # out those pieces to make passing them into a new connection a little less
144
+ # ugly.
145
+ private def proxy_parts
146
+ if config.proxy.nil?
147
+ [nil, nil, nil, nil]
148
+ else
149
+ u = URI.parse(config.proxy)
150
+ [u.host, u.port, u.user, u.password]
151
+ end
152
+ end
153
+
154
+ private def warn_ssl_verify_none
155
+ return if @verify_ssl_warned
156
+
157
+ @verify_ssl_warned = true
158
+ warn("WARNING: Running without SSL cert verification. " \
159
+ "You should never do this in production. " \
160
+ "Execute `Stripe.verify_ssl_certs = true` to enable " \
161
+ "verification.")
162
+ end
163
+ end
164
+ end