stripe 4.24.0 → 5.36.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (216) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +169 -0
  3. data/CODE_OF_CONDUCT.md +77 -0
  4. data/Gemfile +10 -16
  5. data/README.md +111 -44
  6. data/Rakefile +8 -7
  7. data/VERSION +1 -1
  8. data/lib/stripe.rb +39 -130
  9. data/lib/stripe/api_operations/create.rb +1 -1
  10. data/lib/stripe/api_operations/delete.rb +7 -3
  11. data/lib/stripe/api_operations/list.rb +1 -12
  12. data/lib/stripe/api_operations/nested_resource.rb +29 -26
  13. data/lib/stripe/api_operations/request.rb +82 -6
  14. data/lib/stripe/api_operations/save.rb +7 -4
  15. data/lib/stripe/api_resource.rb +12 -3
  16. data/lib/stripe/connection_manager.rb +179 -0
  17. data/lib/stripe/error_object.rb +94 -0
  18. data/lib/stripe/errors.rb +24 -3
  19. data/lib/stripe/instrumentation.rb +82 -0
  20. data/lib/stripe/list_object.rb +34 -5
  21. data/lib/stripe/multipart_encoder.rb +131 -0
  22. data/lib/stripe/oauth.rb +8 -6
  23. data/lib/stripe/object_types.rb +12 -1
  24. data/lib/stripe/resources.rb +12 -1
  25. data/lib/stripe/resources/account.rb +6 -14
  26. data/lib/stripe/resources/account_link.rb +2 -1
  27. data/lib/stripe/resources/alipay_account.rb +1 -1
  28. data/lib/stripe/resources/apple_pay_domain.rb +2 -1
  29. data/lib/stripe/resources/application_fee.rb +2 -12
  30. data/lib/stripe/resources/application_fee_refund.rb +1 -1
  31. data/lib/stripe/resources/balance.rb +2 -1
  32. data/lib/stripe/resources/balance_transaction.rb +2 -1
  33. data/lib/stripe/resources/bank_account.rb +3 -2
  34. data/lib/stripe/resources/billing_portal/configuration.rb +14 -0
  35. data/lib/stripe/resources/billing_portal/session.rb +12 -0
  36. data/lib/stripe/resources/bitcoin_receiver.rb +2 -1
  37. data/lib/stripe/resources/bitcoin_transaction.rb +1 -1
  38. data/lib/stripe/resources/capability.rb +1 -1
  39. data/lib/stripe/resources/card.rb +2 -1
  40. data/lib/stripe/resources/charge.rb +8 -69
  41. data/lib/stripe/resources/checkout/session.rb +6 -1
  42. data/lib/stripe/resources/country_spec.rb +2 -1
  43. data/lib/stripe/resources/coupon.rb +2 -1
  44. data/lib/stripe/resources/credit_note.rb +12 -1
  45. data/lib/stripe/resources/credit_note_line_item.rb +7 -0
  46. data/lib/stripe/resources/customer.rb +9 -63
  47. data/lib/stripe/resources/customer_balance_transaction.rb +1 -1
  48. data/lib/stripe/resources/discount.rb +1 -1
  49. data/lib/stripe/resources/dispute.rb +2 -7
  50. data/lib/stripe/resources/ephemeral_key.rb +2 -1
  51. data/lib/stripe/resources/event.rb +2 -1
  52. data/lib/stripe/resources/exchange_rate.rb +2 -1
  53. data/lib/stripe/resources/file.rb +6 -14
  54. data/lib/stripe/resources/file_link.rb +2 -1
  55. data/lib/stripe/resources/identity/verification_report.rb +12 -0
  56. data/lib/stripe/resources/identity/verification_session.rb +35 -0
  57. data/lib/stripe/resources/invoice.rb +8 -2
  58. data/lib/stripe/resources/invoice_item.rb +2 -1
  59. data/lib/stripe/resources/invoice_line_item.rb +1 -1
  60. data/lib/stripe/resources/issuing/authorization.rb +2 -1
  61. data/lib/stripe/resources/issuing/card.rb +2 -1
  62. data/lib/stripe/resources/issuing/card_details.rb +1 -1
  63. data/lib/stripe/resources/issuing/cardholder.rb +2 -1
  64. data/lib/stripe/resources/issuing/dispute.rb +13 -1
  65. data/lib/stripe/resources/issuing/transaction.rb +2 -1
  66. data/lib/stripe/resources/line_item.rb +7 -0
  67. data/lib/stripe/resources/login_link.rb +1 -1
  68. data/lib/stripe/resources/mandate.rb +8 -0
  69. data/lib/stripe/resources/order.rb +2 -9
  70. data/lib/stripe/resources/order_return.rb +2 -1
  71. data/lib/stripe/resources/payment_intent.rb +2 -1
  72. data/lib/stripe/resources/payment_method.rb +2 -1
  73. data/lib/stripe/resources/payout.rb +10 -5
  74. data/lib/stripe/resources/person.rb +1 -1
  75. data/lib/stripe/resources/plan.rb +2 -1
  76. data/lib/stripe/resources/price.rb +12 -0
  77. data/lib/stripe/resources/product.rb +2 -1
  78. data/lib/stripe/resources/promotion_code.rb +12 -0
  79. data/lib/stripe/resources/quote.rb +95 -0
  80. data/lib/stripe/resources/radar/early_fraud_warning.rb +2 -1
  81. data/lib/stripe/resources/radar/value_list.rb +2 -1
  82. data/lib/stripe/resources/radar/value_list_item.rb +2 -1
  83. data/lib/stripe/resources/recipient.rb +2 -5
  84. data/lib/stripe/resources/recipient_transfer.rb +1 -1
  85. data/lib/stripe/resources/refund.rb +2 -1
  86. data/lib/stripe/resources/reporting/report_run.rb +2 -1
  87. data/lib/stripe/resources/reporting/report_type.rb +2 -1
  88. data/lib/stripe/resources/reversal.rb +1 -1
  89. data/lib/stripe/resources/review.rb +2 -1
  90. data/lib/stripe/resources/setup_attempt.rb +10 -0
  91. data/lib/stripe/resources/setup_intent.rb +2 -1
  92. data/lib/stripe/resources/sigma/scheduled_query_run.rb +2 -1
  93. data/lib/stripe/resources/sku.rb +2 -1
  94. data/lib/stripe/resources/source.rb +11 -10
  95. data/lib/stripe/resources/source_transaction.rb +1 -1
  96. data/lib/stripe/resources/subscription.rb +10 -9
  97. data/lib/stripe/resources/subscription_item.rb +8 -2
  98. data/lib/stripe/resources/subscription_schedule.rb +2 -1
  99. data/lib/stripe/resources/tax_code.rb +10 -0
  100. data/lib/stripe/resources/tax_id.rb +1 -1
  101. data/lib/stripe/resources/tax_rate.rb +2 -1
  102. data/lib/stripe/resources/terminal/connection_token.rb +2 -1
  103. data/lib/stripe/resources/terminal/location.rb +2 -1
  104. data/lib/stripe/resources/terminal/reader.rb +2 -1
  105. data/lib/stripe/resources/three_d_secure.rb +2 -1
  106. data/lib/stripe/resources/token.rb +2 -1
  107. data/lib/stripe/resources/topup.rb +2 -1
  108. data/lib/stripe/resources/transfer.rb +2 -6
  109. data/lib/stripe/resources/usage_record.rb +1 -17
  110. data/lib/stripe/resources/usage_record_summary.rb +1 -1
  111. data/lib/stripe/resources/webhook_endpoint.rb +2 -1
  112. data/lib/stripe/stripe_client.rb +608 -242
  113. data/lib/stripe/stripe_configuration.rb +194 -0
  114. data/lib/stripe/stripe_object.rb +30 -25
  115. data/lib/stripe/stripe_response.rb +87 -27
  116. data/lib/stripe/util.rb +37 -18
  117. data/lib/stripe/version.rb +1 -1
  118. data/lib/stripe/webhook.rb +39 -8
  119. data/stripe.gemspec +15 -11
  120. metadata +24 -216
  121. data/.editorconfig +0 -10
  122. data/.gitattributes +0 -4
  123. data/.github/ISSUE_TEMPLATE.md +0 -5
  124. data/.gitignore +0 -8
  125. data/.rubocop.yml +0 -43
  126. data/.rubocop_todo.yml +0 -38
  127. data/.travis.yml +0 -43
  128. data/.vscode/extensions.json +0 -7
  129. data/.vscode/settings.json +0 -8
  130. data/lib/stripe/resources/issuer_fraud_record.rb +0 -9
  131. data/test/api_stub_helpers.rb +0 -1
  132. data/test/openapi/README.md +0 -9
  133. data/test/stripe/account_link_test.rb +0 -18
  134. data/test/stripe/account_test.rb +0 -428
  135. data/test/stripe/alipay_account_test.rb +0 -37
  136. data/test/stripe/api_operations_test.rb +0 -80
  137. data/test/stripe/api_resource_test.rb +0 -621
  138. data/test/stripe/apple_pay_domain_test.rb +0 -46
  139. data/test/stripe/application_fee_refund_test.rb +0 -37
  140. data/test/stripe/application_fee_test.rb +0 -58
  141. data/test/stripe/balance_test.rb +0 -13
  142. data/test/stripe/balance_transaction_test.rb +0 -20
  143. data/test/stripe/bank_account_test.rb +0 -36
  144. data/test/stripe/capability_test.rb +0 -45
  145. data/test/stripe/charge_test.rb +0 -80
  146. data/test/stripe/checkout/session_test.rb +0 -41
  147. data/test/stripe/country_spec_test.rb +0 -20
  148. data/test/stripe/coupon_test.rb +0 -61
  149. data/test/stripe/credit_note_test.rb +0 -61
  150. data/test/stripe/customer_balance_transaction_test.rb +0 -37
  151. data/test/stripe/customer_card_test.rb +0 -42
  152. data/test/stripe/customer_test.rb +0 -269
  153. data/test/stripe/dispute_test.rb +0 -51
  154. data/test/stripe/ephemeral_key_test.rb +0 -93
  155. data/test/stripe/errors_test.rb +0 -20
  156. data/test/stripe/exchange_rate_test.rb +0 -20
  157. data/test/stripe/file_link_test.rb +0 -41
  158. data/test/stripe/file_test.rb +0 -97
  159. data/test/stripe/file_upload_test.rb +0 -79
  160. data/test/stripe/invoice_item_test.rb +0 -66
  161. data/test/stripe/invoice_line_item_test.rb +0 -8
  162. data/test/stripe/invoice_test.rb +0 -213
  163. data/test/stripe/issuer_fraud_record_test.rb +0 -20
  164. data/test/stripe/issuing/authorization_test.rb +0 -72
  165. data/test/stripe/issuing/card_test.rb +0 -62
  166. data/test/stripe/issuing/cardholder_test.rb +0 -53
  167. data/test/stripe/issuing/dispute_test.rb +0 -45
  168. data/test/stripe/issuing/transaction_test.rb +0 -48
  169. data/test/stripe/list_object_test.rb +0 -156
  170. data/test/stripe/login_link_test.rb +0 -37
  171. data/test/stripe/oauth_test.rb +0 -88
  172. data/test/stripe/order_return_test.rb +0 -21
  173. data/test/stripe/order_test.rb +0 -82
  174. data/test/stripe/payment_intent_test.rb +0 -107
  175. data/test/stripe/payment_method_test.rb +0 -84
  176. data/test/stripe/payout_test.rb +0 -57
  177. data/test/stripe/person_test.rb +0 -46
  178. data/test/stripe/plan_test.rb +0 -98
  179. data/test/stripe/product_test.rb +0 -59
  180. data/test/stripe/radar/early_fraud_warning_test.rb +0 -22
  181. data/test/stripe/radar/value_list_item_test.rb +0 -48
  182. data/test/stripe/radar/value_list_test.rb +0 -61
  183. data/test/stripe/recipient_test.rb +0 -62
  184. data/test/stripe/refund_test.rb +0 -39
  185. data/test/stripe/reporting/report_run_test.rb +0 -33
  186. data/test/stripe/reporting/report_type_test.rb +0 -22
  187. data/test/stripe/reversal_test.rb +0 -43
  188. data/test/stripe/review_test.rb +0 -27
  189. data/test/stripe/setup_intent_test.rb +0 -84
  190. data/test/stripe/sigma/scheduled_query_run_test.rb +0 -22
  191. data/test/stripe/sku_test.rb +0 -60
  192. data/test/stripe/source_test.rb +0 -99
  193. data/test/stripe/source_transaction_test.rb +0 -19
  194. data/test/stripe/stripe_client_test.rb +0 -842
  195. data/test/stripe/stripe_object_test.rb +0 -525
  196. data/test/stripe/stripe_response_test.rb +0 -49
  197. data/test/stripe/subscription_item_test.rb +0 -75
  198. data/test/stripe/subscription_schedule_test.rb +0 -82
  199. data/test/stripe/subscription_test.rb +0 -80
  200. data/test/stripe/tax_id_test.rb +0 -31
  201. data/test/stripe/tax_rate_test.rb +0 -43
  202. data/test/stripe/terminal/connection_token_test.rb +0 -16
  203. data/test/stripe/terminal/location_test.rb +0 -68
  204. data/test/stripe/terminal/reader_test.rb +0 -62
  205. data/test/stripe/three_d_secure_test.rb +0 -23
  206. data/test/stripe/topup_test.rb +0 -62
  207. data/test/stripe/transfer_test.rb +0 -88
  208. data/test/stripe/usage_record_summary_test.rb +0 -19
  209. data/test/stripe/usage_record_test.rb +0 -28
  210. data/test/stripe/util_test.rb +0 -402
  211. data/test/stripe/webhook_endpoint_test.rb +0 -59
  212. data/test/stripe/webhook_test.rb +0 -96
  213. data/test/stripe_mock.rb +0 -77
  214. data/test/stripe_test.rb +0 -63
  215. data/test/test_data.rb +0 -61
  216. data/test/test_helper.rb +0 -71
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.24.0
1
+ 5.36.0
data/lib/stripe.rb CHANGED
@@ -3,15 +3,16 @@
3
3
  # Stripe Ruby bindings
4
4
  # API spec at https://stripe.com/docs/api
5
5
  require "cgi"
6
- require "faraday"
7
6
  require "json"
8
7
  require "logger"
8
+ require "net/http"
9
9
  require "openssl"
10
10
  require "rbconfig"
11
11
  require "securerandom"
12
12
  require "set"
13
13
  require "socket"
14
14
  require "uri"
15
+ require "forwardable"
15
16
 
16
17
  # Version
17
18
  require "stripe/version"
@@ -28,13 +29,17 @@ require "stripe/api_operations/save"
28
29
  require "stripe/errors"
29
30
  require "stripe/object_types"
30
31
  require "stripe/util"
32
+ require "stripe/connection_manager"
33
+ require "stripe/multipart_encoder"
31
34
  require "stripe/stripe_client"
32
35
  require "stripe/stripe_object"
33
36
  require "stripe/stripe_response"
34
37
  require "stripe/list_object"
38
+ require "stripe/error_object"
35
39
  require "stripe/api_resource"
36
40
  require "stripe/singleton_api_resource"
37
41
  require "stripe/webhook"
42
+ require "stripe/stripe_configuration"
38
43
 
39
44
  # Named API resources
40
45
  require "stripe/resources"
@@ -45,36 +50,43 @@ require "stripe/oauth"
45
50
  module Stripe
46
51
  DEFAULT_CA_BUNDLE_PATH = __dir__ + "/data/ca-certificates.crt"
47
52
 
48
- @app_info = nil
49
-
50
- @api_base = "https://api.stripe.com"
51
- @connect_base = "https://connect.stripe.com"
52
- @uploads_base = "https://files.stripe.com"
53
-
54
- @log_level = nil
55
- @logger = nil
56
-
57
- @proxy = nil
58
-
59
- @max_network_retries = 0
60
- @max_network_retry_delay = 2
61
- @initial_network_retry_delay = 0.5
62
-
63
- @ca_bundle_path = DEFAULT_CA_BUNDLE_PATH
64
- @ca_store = nil
65
- @verify_ssl_certs = true
53
+ # map to the same values as the standard library's logger
54
+ LEVEL_DEBUG = Logger::DEBUG
55
+ LEVEL_ERROR = Logger::ERROR
56
+ LEVEL_INFO = Logger::INFO
66
57
 
67
- @open_timeout = 30
68
- @read_timeout = 80
58
+ @app_info = nil
69
59
 
70
- @enable_telemetry = true
60
+ @config = Stripe::StripeConfiguration.setup
71
61
 
72
62
  class << self
73
- attr_accessor :stripe_account, :api_key, :api_base, :verify_ssl_certs,
74
- :api_version, :client_id, :connect_base, :uploads_base,
75
- :open_timeout, :read_timeout, :proxy
76
-
77
- attr_reader :max_network_retry_delay, :initial_network_retry_delay
63
+ extend Forwardable
64
+
65
+ attr_reader :config
66
+
67
+ # User configurable options
68
+ def_delegators :@config, :api_key, :api_key=
69
+ def_delegators :@config, :api_version, :api_version=
70
+ def_delegators :@config, :stripe_account, :stripe_account=
71
+ def_delegators :@config, :api_base, :api_base=
72
+ def_delegators :@config, :uploads_base, :uploads_base=
73
+ def_delegators :@config, :connect_base, :connect_base=
74
+ def_delegators :@config, :open_timeout, :open_timeout=
75
+ def_delegators :@config, :read_timeout, :read_timeout=
76
+ def_delegators :@config, :write_timeout, :write_timeout=
77
+ def_delegators :@config, :proxy, :proxy=
78
+ def_delegators :@config, :verify_ssl_certs, :verify_ssl_certs=
79
+ def_delegators :@config, :ca_bundle_path, :ca_bundle_path=
80
+ def_delegators :@config, :log_level, :log_level=
81
+ def_delegators :@config, :logger, :logger=
82
+ def_delegators :@config, :max_network_retries, :max_network_retries=
83
+ def_delegators :@config, :enable_telemetry=, :enable_telemetry?
84
+ def_delegators :@config, :client_id=, :client_id
85
+
86
+ # Internal configurations
87
+ def_delegators :@config, :max_network_retry_delay
88
+ def_delegators :@config, :initial_network_retry_delay
89
+ def_delegators :@config, :ca_store
78
90
  end
79
91
 
80
92
  # Gets the application for a plugin that's identified some. See
@@ -87,99 +99,6 @@ module Stripe
87
99
  @app_info = info
88
100
  end
89
101
 
90
- # The location of a file containing a bundle of CA certificates. By default
91
- # the library will use an included bundle that can successfully validate
92
- # Stripe certificates.
93
- def self.ca_bundle_path
94
- @ca_bundle_path
95
- end
96
-
97
- def self.ca_bundle_path=(path)
98
- @ca_bundle_path = path
99
-
100
- # empty this field so a new store is initialized
101
- @ca_store = nil
102
- end
103
-
104
- # A certificate store initialized from the the bundle in #ca_bundle_path and
105
- # which is used to validate TLS on every request.
106
- #
107
- # This was added to the give the gem "pseudo thread safety" in that it seems
108
- # when initiating many parallel requests marshaling the certificate store is
109
- # the most likely point of failure (see issue #382). Any program attempting
110
- # to leverage this pseudo safety should make a call to this method (i.e.
111
- # `Stripe.ca_store`) in their initialization code because it marshals lazily
112
- # and is itself not thread safe.
113
- def self.ca_store
114
- @ca_store ||= begin
115
- store = OpenSSL::X509::Store.new
116
- store.add_file(ca_bundle_path)
117
- store
118
- end
119
- end
120
-
121
- # map to the same values as the standard library's logger
122
- LEVEL_DEBUG = Logger::DEBUG
123
- LEVEL_ERROR = Logger::ERROR
124
- LEVEL_INFO = Logger::INFO
125
-
126
- # When set prompts the library to log some extra information to $stdout and
127
- # $stderr about what it's doing. For example, it'll produce information about
128
- # requests, responses, and errors that are received. Valid log levels are
129
- # `debug` and `info`, with `debug` being a little more verbose in places.
130
- #
131
- # Use of this configuration is only useful when `.logger` is _not_ set. When
132
- # it is, the decision what levels to print is entirely deferred to the logger.
133
- def self.log_level
134
- @log_level
135
- end
136
-
137
- def self.log_level=(val)
138
- # Backwards compatibility for values that we briefly allowed
139
- if val == "debug"
140
- val = LEVEL_DEBUG
141
- elsif val == "info"
142
- val = LEVEL_INFO
143
- end
144
-
145
- if !val.nil? && ![LEVEL_DEBUG, LEVEL_ERROR, LEVEL_INFO].include?(val)
146
- raise ArgumentError,
147
- "log_level should only be set to `nil`, `debug` or `info`"
148
- end
149
- @log_level = val
150
- end
151
-
152
- # Sets a logger to which logging output will be sent. The logger should
153
- # support the same interface as the `Logger` class that's part of Ruby's
154
- # standard library (hint, anything in `Rails.logger` will likely be
155
- # suitable).
156
- #
157
- # If `.logger` is set, the value of `.log_level` is ignored. The decision on
158
- # what levels to print is entirely deferred to the logger.
159
- def self.logger
160
- @logger
161
- end
162
-
163
- def self.logger=(val)
164
- @logger = val
165
- end
166
-
167
- def self.max_network_retries
168
- @max_network_retries
169
- end
170
-
171
- def self.max_network_retries=(val)
172
- @max_network_retries = val.to_i
173
- end
174
-
175
- def self.enable_telemetry?
176
- @enable_telemetry
177
- end
178
-
179
- def self.enable_telemetry=(val)
180
- @enable_telemetry = val
181
- end
182
-
183
102
  # Sets some basic information about the running application that's sent along
184
103
  # with API requests. Useful for plugin authors to identify their plugin when
185
104
  # communicating with Stripe.
@@ -193,16 +112,6 @@ module Stripe
193
112
  version: version,
194
113
  }
195
114
  end
196
-
197
- # DEPRECATED. Use `Util#encode_parameters` instead.
198
- def self.uri_encode(params)
199
- Util.encode_parameters(params)
200
- end
201
- private_class_method :uri_encode
202
- class << self
203
- extend Gem::Deprecate
204
- deprecate :uri_encode, "Stripe::Util#encode_parameters", 2016, 1
205
- end
206
115
  end
207
116
 
208
117
  Stripe.log_level = ENV["STRIPE_LOG"] unless ENV["STRIPE_LOG"].nil?
@@ -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,7 +6,7 @@ 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
@@ -14,9 +14,11 @@ module Stripe
14
14
  resource_plural: nil)
15
15
  resource_plural ||= "#{resource}s"
16
16
  path ||= resource_plural
17
+
17
18
  raise ArgumentError, "operations array required" if operations.nil?
18
19
 
19
20
  resource_url_method = :"#{resource}s_url"
21
+
20
22
  define_singleton_method(resource_url_method) do |id, nested_id = nil|
21
23
  url = "#{resource_url}/#{CGI.escape(id)}/#{CGI.escape(path)}"
22
24
  url += "/#{CGI.escape(nested_id)}" unless nested_id.nil?
@@ -27,39 +29,40 @@ module Stripe
27
29
  case operation
28
30
  when :create
29
31
  define_singleton_method(:"create_#{resource}") \
30
- do |id, params = {}, opts = {}|
31
- url = send(resource_url_method, id)
32
- resp, opts = request(:post, url, params, opts)
33
- Util.convert_to_stripe_object(resp.data, opts)
34
- end
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
35
37
  when :retrieve
36
38
  define_singleton_method(:"retrieve_#{resource}") \
37
- do |id, nested_id, opts = {}|
38
- url = send(resource_url_method, id, nested_id)
39
- resp, opts = request(:get, url, {}, opts)
40
- Util.convert_to_stripe_object(resp.data, opts)
41
- end
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
42
44
  when :update
43
45
  define_singleton_method(:"update_#{resource}") \
44
- do |id, nested_id, params = {}, opts = {}|
45
- url = send(resource_url_method, id, nested_id)
46
- resp, opts = request(:post, url, params, opts)
47
- Util.convert_to_stripe_object(resp.data, opts)
48
- end
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
49
51
  when :delete
50
52
  define_singleton_method(:"delete_#{resource}") \
51
- do |id, nested_id, params = {}, opts = {}|
52
- url = send(resource_url_method, id, nested_id)
53
- resp, opts = request(:delete, url, params, opts)
54
- Util.convert_to_stripe_object(resp.data, opts)
55
- end
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
56
59
  when :list
57
60
  define_singleton_method(:"list_#{resource_plural}") \
58
- do |id, params = {}, opts = {}|
59
- url = send(resource_url_method, id)
60
- resp, opts = request(:get, url, params, opts)
61
- Util.convert_to_stripe_object(resp.data, opts)
62
- end
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
63
66
  else
64
67
  raise ArgumentError, "Unknown operation: #{operation.inspect}"
65
68
  end
@@ -4,10 +4,38 @@ 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
+ execute_resource_request_internal(
10
+ :execute_request, method, url, params, opts
11
+ )
12
+ end
13
+
14
+ def execute_resource_request_stream(method, url,
15
+ params = {}, opts = {},
16
+ &read_body_chunk_block)
17
+ execute_resource_request_internal(
18
+ :execute_request_stream,
19
+ method,
20
+ url,
21
+ params,
22
+ opts,
23
+ &read_body_chunk_block
24
+ )
25
+ end
26
+
27
+ private def execute_resource_request_internal(client_request_method_sym,
28
+ method, url,
29
+ params, opts,
30
+ &read_body_chunk_block)
31
+ params ||= {}
32
+
33
+ error_on_invalid_params(params)
8
34
  warn_on_opts_in_params(params)
9
35
 
10
36
  opts = Util.normalize_opts(opts)
37
+ error_on_non_string_user_opts(opts)
38
+
11
39
  opts[:client] ||= StripeClient.active_client
12
40
 
13
41
  headers = opts.clone
@@ -16,10 +44,12 @@ module Stripe
16
44
  client = headers.delete(:client)
17
45
  # Assume all remaining opts must be headers
18
46
 
19
- resp, opts[:api_key] = client.execute_request(
47
+ resp, opts[:api_key] = client.send(
48
+ client_request_method_sym,
20
49
  method, url,
21
50
  api_base: api_base, api_key: api_key,
22
- headers: headers, params: params
51
+ headers: headers, params: params,
52
+ &read_body_chunk_block
23
53
  )
24
54
 
25
55
  # Hash#select returns an array before 1.9
@@ -31,10 +61,43 @@ module Stripe
31
61
  [resp, opts_to_persist]
32
62
  end
33
63
 
64
+ # This method used to be called `request`, but it's such a short name
65
+ # that it eventually conflicted with the name of a field on an API
66
+ # resource (specifically, `Event#request`), so it was renamed to
67
+ # something more unique.
68
+ #
69
+ # The former name had been around for just about forever though, and
70
+ # although all internal uses have been renamed, I've left this alias in
71
+ # place for backwards compatibility. Consider removing it on the next
72
+ # major.
73
+ alias request execute_resource_request
74
+
75
+ private def error_on_non_string_user_opts(opts)
76
+ Util::OPTS_USER_SPECIFIED.each do |opt|
77
+ next unless opts.key?(opt)
78
+
79
+ val = opts[opt]
80
+ next if val.nil?
81
+ next if val.is_a?(String)
82
+
83
+ raise ArgumentError,
84
+ "request option '#{opt}' should be a string value " \
85
+ "(was a #{val.class})"
86
+ end
87
+ end
88
+
89
+ private def error_on_invalid_params(params)
90
+ return if params.nil? || params.is_a?(Hash)
91
+
92
+ raise ArgumentError,
93
+ "request params should be either a Hash or nil " \
94
+ "(was a #{params.class})"
95
+ end
96
+
34
97
  private def warn_on_opts_in_params(params)
35
98
  Util::OPTS_USER_SPECIFIED.each do |opt|
36
99
  if params.key?(opt)
37
- warn("WARNING: #{opt} should be in opts instead of params.")
100
+ warn("WARNING: '#{opt}' should be in opts instead of params.")
38
101
  end
39
102
  end
40
103
  end
@@ -44,10 +107,23 @@ module Stripe
44
107
  base.extend(ClassMethods)
45
108
  end
46
109
 
47
- protected def request(method, url, params = {}, opts = {})
110
+ protected def execute_resource_request(method, url,
111
+ params = {}, opts = {})
48
112
  opts = @opts.merge(Util.normalize_opts(opts))
49
- self.class.request(method, url, params, opts)
113
+ self.class.execute_resource_request(method, url, params, opts)
50
114
  end
115
+
116
+ protected def execute_resource_request_stream(method, url,
117
+ params = {}, opts = {},
118
+ &read_body_chunk_block)
119
+ opts = @opts.merge(Util.normalize_opts(opts))
120
+ self.class.execute_resource_request_stream(
121
+ method, url, params, opts, &read_body_chunk_block
122
+ )
123
+ end
124
+
125
+ # See notes on `alias` above.
126
+ alias request execute_resource_request
51
127
  end
52
128
  end
53
129
  end