stripe 4.9.0 → 5.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (224) 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 +102 -2
  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.rb +64 -85
  14. data/lib/stripe/api_operations/delete.rb +23 -1
  15. data/lib/stripe/api_operations/list.rb +0 -6
  16. data/lib/stripe/api_operations/nested_resource.rb +14 -7
  17. data/lib/stripe/api_operations/request.rb +3 -7
  18. data/lib/stripe/api_operations/save.rb +1 -3
  19. data/lib/stripe/api_resource.rb +50 -2
  20. data/lib/stripe/connection_manager.rb +141 -0
  21. data/lib/stripe/error_object.rb +94 -0
  22. data/lib/stripe/errors.rb +22 -9
  23. data/lib/stripe/list_object.rb +11 -5
  24. data/lib/stripe/multipart_encoder.rb +131 -0
  25. data/lib/stripe/object_types.rb +94 -0
  26. data/lib/stripe/resources.rb +77 -0
  27. data/lib/stripe/{account.rb → resources/account.rb} +49 -27
  28. data/lib/stripe/{account_link.rb → resources/account_link.rb} +1 -1
  29. data/lib/stripe/resources/alipay_account.rb +34 -0
  30. data/lib/stripe/{apple_pay_domain.rb → resources/apple_pay_domain.rb} +1 -1
  31. data/lib/stripe/resources/application_fee.rb +13 -0
  32. data/lib/stripe/resources/application_fee_refund.rb +30 -0
  33. data/lib/stripe/{balance.rb → resources/balance.rb} +1 -1
  34. data/lib/stripe/{balance_transaction.rb → resources/balance_transaction.rb} +1 -5
  35. data/lib/stripe/{bank_account.rb → resources/bank_account.rb} +14 -4
  36. data/lib/stripe/{bitcoin_receiver.rb → resources/bitcoin_receiver.rb} +3 -3
  37. data/lib/stripe/{bitcoin_transaction.rb → resources/bitcoin_transaction.rb} +1 -1
  38. data/lib/stripe/resources/capability.rb +33 -0
  39. data/lib/stripe/{card.rb → resources/card.rb} +12 -4
  40. data/lib/stripe/resources/charge.rb +22 -0
  41. data/lib/stripe/{checkout → resources/checkout}/session.rb +2 -2
  42. data/lib/stripe/{country_spec.rb → resources/country_spec.rb} +1 -1
  43. data/lib/stripe/{coupon.rb → resources/coupon.rb} +2 -2
  44. data/lib/stripe/resources/credit_note.rb +22 -0
  45. data/lib/stripe/resources/customer.rb +35 -0
  46. data/lib/stripe/resources/customer_balance_transaction.rb +30 -0
  47. data/lib/stripe/resources/discount.rb +7 -0
  48. data/lib/stripe/{dispute.rb → resources/dispute.rb} +9 -7
  49. data/lib/stripe/{ephemeral_key.rb → resources/ephemeral_key.rb} +5 -2
  50. data/lib/stripe/{event.rb → resources/event.rb} +1 -1
  51. data/lib/stripe/{exchange_rate.rb → resources/exchange_rate.rb} +1 -1
  52. data/lib/stripe/{file.rb → resources/file.rb} +8 -11
  53. data/lib/stripe/{file_link.rb → resources/file_link.rb} +2 -2
  54. data/lib/stripe/resources/invoice.rb +73 -0
  55. data/lib/stripe/{invoice_item.rb → resources/invoice_item.rb} +2 -2
  56. data/lib/stripe/{invoice_line_item.rb → resources/invoice_line_item.rb} +1 -1
  57. data/lib/stripe/resources/issuing/authorization.rb +33 -0
  58. data/lib/stripe/resources/issuing/card.rb +24 -0
  59. data/lib/stripe/{issuing → resources/issuing}/card_details.rb +1 -1
  60. data/lib/stripe/{issuing → resources/issuing}/cardholder.rb +2 -2
  61. data/lib/stripe/{issuing → resources/issuing}/dispute.rb +2 -2
  62. data/lib/stripe/{issuing → resources/issuing}/transaction.rb +2 -2
  63. data/lib/stripe/resources/login_link.rb +14 -0
  64. data/lib/stripe/resources/order.rb +32 -0
  65. data/lib/stripe/{order_return.rb → resources/order_return.rb} +1 -1
  66. data/lib/stripe/resources/payment_intent.rb +42 -0
  67. data/lib/stripe/resources/payment_method.rb +32 -0
  68. data/lib/stripe/resources/payout.rb +22 -0
  69. data/lib/stripe/{person.rb → resources/person.rb} +8 -3
  70. data/lib/stripe/{plan.rb → resources/plan.rb} +1 -1
  71. data/lib/stripe/{product.rb → resources/product.rb} +3 -3
  72. data/lib/stripe/resources/radar/early_fraud_warning.rb +11 -0
  73. data/lib/stripe/{radar → resources/radar}/value_list.rb +2 -2
  74. data/lib/stripe/{radar → resources/radar}/value_list_item.rb +2 -2
  75. data/lib/stripe/{recipient.rb → resources/recipient.rb} +2 -6
  76. data/lib/stripe/{recipient_transfer.rb → resources/recipient_transfer.rb} +1 -1
  77. data/lib/stripe/{refund.rb → resources/refund.rb} +1 -1
  78. data/lib/stripe/{reporting → resources/reporting}/report_run.rb +2 -2
  79. data/lib/stripe/{reporting → resources/reporting}/report_type.rb +2 -2
  80. data/lib/stripe/resources/reversal.rb +29 -0
  81. data/lib/stripe/resources/review.rb +20 -0
  82. data/lib/stripe/resources/setup_intent.rb +32 -0
  83. data/lib/stripe/{sigma → resources/sigma}/scheduled_query_run.rb +2 -2
  84. data/lib/stripe/{sku.rb → resources/sku.rb} +3 -3
  85. data/lib/stripe/{source.rb → resources/source.rb} +17 -15
  86. data/lib/stripe/{source_transaction.rb → resources/source_transaction.rb} +1 -1
  87. data/lib/stripe/resources/subscription.rb +25 -0
  88. data/lib/stripe/{subscription_item.rb → resources/subscription_item.rb} +5 -2
  89. data/lib/stripe/resources/subscription_schedule.rb +32 -0
  90. data/lib/stripe/resources/tax_id.rb +26 -0
  91. data/lib/stripe/resources/tax_rate.rb +11 -0
  92. data/lib/stripe/{terminal → resources/terminal}/connection_token.rb +2 -2
  93. data/lib/stripe/{terminal → resources/terminal}/location.rb +3 -2
  94. data/lib/stripe/{terminal → resources/terminal}/reader.rb +3 -2
  95. data/lib/stripe/{three_d_secure.rb → resources/three_d_secure.rb} +1 -1
  96. data/lib/stripe/{token.rb → resources/token.rb} +1 -1
  97. data/lib/stripe/resources/topup.rb +22 -0
  98. data/lib/stripe/resources/transfer.rb +26 -0
  99. data/lib/stripe/resources/usage_record.rb +7 -0
  100. data/lib/stripe/{usage_record_summary.rb → resources/usage_record_summary.rb} +1 -1
  101. data/lib/stripe/{webhook_endpoint.rb → resources/webhook_endpoint.rb} +2 -2
  102. data/lib/stripe/singleton_api_resource.rb +3 -1
  103. data/lib/stripe/stripe_client.rb +347 -218
  104. data/lib/stripe/stripe_object.rb +72 -59
  105. data/lib/stripe/stripe_response.rb +53 -21
  106. data/lib/stripe/util.rb +54 -109
  107. data/lib/stripe/version.rb +1 -1
  108. data/lib/stripe/webhook.rb +5 -3
  109. data/stripe.gemspec +14 -5
  110. data/test/stripe/account_link_test.rb +1 -1
  111. data/test/stripe/account_test.rb +193 -32
  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 +119 -30
  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 +7 -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 +84 -0
  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 +242 -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 +18 -1
  181. data/test/stripe/terminal/reader_test.rb +18 -1
  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 +117 -125
  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/payout.rb +0 -20
  206. data/lib/stripe/reversal.rb +0 -22
  207. data/lib/stripe/review.rb +0 -14
  208. data/lib/stripe/subscription.rb +0 -25
  209. data/lib/stripe/subscription_schedule.rb +0 -32
  210. data/lib/stripe/subscription_schedule_revision.rb +0 -25
  211. data/lib/stripe/topup.rb +0 -16
  212. data/lib/stripe/transfer.rb +0 -23
  213. data/lib/stripe/usage_record.rb +0 -14
  214. data/test/stripe/account_external_accounts_operations_test.rb +0 -69
  215. data/test/stripe/account_login_links_operations_test.rb +0 -21
  216. data/test/stripe/account_persons_operations_test.rb +0 -70
  217. data/test/stripe/application_fee_refunds_operations_test.rb +0 -56
  218. data/test/stripe/customer_sources_operations_test.rb +0 -64
  219. data/test/stripe/file_upload_test.rb +0 -76
  220. data/test/stripe/issuer_fraud_record_test.rb +0 -20
  221. data/test/stripe/subscription_schedule_revision_test.rb +0 -37
  222. data/test/stripe/subscription_schedule_revisions_operations_test.rb +0 -35
  223. data/test/stripe/transfer_reversals_operations_test.rb +0 -57
  224. data/test/stripe/usage_record_test.rb +0 -28
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.9.0
1
+ 5.1.0
@@ -3,9 +3,9 @@
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"
@@ -26,90 +26,27 @@ require "stripe/api_operations/save"
26
26
 
27
27
  # API resource support classes
28
28
  require "stripe/errors"
29
+ require "stripe/object_types"
29
30
  require "stripe/util"
31
+ require "stripe/connection_manager"
32
+ require "stripe/multipart_encoder"
30
33
  require "stripe/stripe_client"
31
34
  require "stripe/stripe_object"
32
35
  require "stripe/stripe_response"
33
36
  require "stripe/list_object"
37
+ require "stripe/error_object"
34
38
  require "stripe/api_resource"
35
39
  require "stripe/singleton_api_resource"
36
40
  require "stripe/webhook"
37
41
 
38
42
  # Named API resources
39
- require "stripe/account"
40
- require "stripe/account_link"
41
- require "stripe/alipay_account"
42
- require "stripe/apple_pay_domain"
43
- require "stripe/application_fee"
44
- require "stripe/application_fee_refund"
45
- require "stripe/balance"
46
- require "stripe/balance_transaction"
47
- require "stripe/bank_account"
48
- require "stripe/bitcoin_receiver"
49
- require "stripe/bitcoin_transaction"
50
- require "stripe/card"
51
- require "stripe/charge"
52
- require "stripe/checkout/session"
53
- require "stripe/country_spec"
54
- require "stripe/coupon"
55
- require "stripe/customer"
56
- require "stripe/dispute"
57
- require "stripe/ephemeral_key"
58
- require "stripe/event"
59
- require "stripe/exchange_rate"
60
- require "stripe/file"
61
- require "stripe/file_link"
62
- require "stripe/invoice"
63
- require "stripe/invoice_item"
64
- require "stripe/invoice_line_item"
65
- require "stripe/issuer_fraud_record"
66
- require "stripe/issuing/authorization"
67
- require "stripe/issuing/card"
68
- require "stripe/issuing/card_details"
69
- require "stripe/issuing/cardholder"
70
- require "stripe/issuing/dispute"
71
- require "stripe/issuing/transaction"
72
- require "stripe/login_link"
73
- require "stripe/order"
74
- require "stripe/order_return"
75
- require "stripe/payment_intent"
76
- require "stripe/payout"
77
- require "stripe/person"
78
- require "stripe/plan"
79
- require "stripe/product"
80
- require "stripe/radar/value_list"
81
- require "stripe/radar/value_list_item"
82
- require "stripe/recipient"
83
- require "stripe/recipient_transfer"
84
- require "stripe/refund"
85
- require "stripe/reporting/report_run"
86
- require "stripe/reporting/report_type"
87
- require "stripe/reversal"
88
- require "stripe/review"
89
- require "stripe/sigma/scheduled_query_run"
90
- require "stripe/sku"
91
- require "stripe/source"
92
- require "stripe/source_transaction"
93
- require "stripe/subscription"
94
- require "stripe/subscription_item"
95
- require "stripe/subscription_schedule"
96
- require "stripe/subscription_schedule_revision"
97
- require "stripe/terminal/connection_token"
98
- require "stripe/terminal/location"
99
- require "stripe/terminal/reader"
100
- require "stripe/three_d_secure"
101
- require "stripe/token"
102
- require "stripe/topup"
103
- require "stripe/transfer"
104
- require "stripe/usage_record"
105
- require "stripe/usage_record_summary"
106
- require "stripe/webhook_endpoint"
43
+ require "stripe/resources"
107
44
 
108
45
  # OAuth
109
46
  require "stripe/oauth"
110
47
 
111
48
  module Stripe
112
- DEFAULT_CA_BUNDLE_PATH = ::File.dirname(__FILE__) + "/data/ca-certificates.crt"
49
+ DEFAULT_CA_BUNDLE_PATH = __dir__ + "/data/ca-certificates.crt"
113
50
 
114
51
  @app_info = nil
115
52
 
@@ -120,6 +57,8 @@ module Stripe
120
57
  @log_level = nil
121
58
  @logger = nil
122
59
 
60
+ @proxy = nil
61
+
123
62
  @max_network_retries = 0
124
63
  @max_network_retry_delay = 2
125
64
  @initial_network_retry_delay = 0.5
@@ -131,11 +70,23 @@ module Stripe
131
70
  @open_timeout = 30
132
71
  @read_timeout = 80
133
72
 
134
- @enable_telemetry = false
73
+ @enable_telemetry = true
135
74
 
136
75
  class << self
137
- attr_accessor :stripe_account, :api_key, :api_base, :verify_ssl_certs, :api_version, :client_id, :connect_base, :uploads_base,
138
- :open_timeout, :read_timeout
76
+ attr_accessor :api_key
77
+ attr_accessor :api_version
78
+ attr_accessor :client_id
79
+ attr_accessor :stripe_account
80
+
81
+ # These all get manual attribute writers so that we can reset connections
82
+ # if they change.
83
+ attr_reader :api_base
84
+ attr_reader :connect_base
85
+ attr_reader :open_timeout
86
+ attr_reader :proxy
87
+ attr_reader :read_timeout
88
+ attr_reader :uploads_base
89
+ attr_reader :verify_ssl_certs
139
90
 
140
91
  attr_reader :max_network_retry_delay, :initial_network_retry_delay
141
92
  end
@@ -150,6 +101,11 @@ module Stripe
150
101
  @app_info = info
151
102
  end
152
103
 
104
+ def self.api_base=(api_base)
105
+ @api_base = api_base
106
+ StripeClient.clear_all_connection_managers
107
+ end
108
+
153
109
  # The location of a file containing a bundle of CA certificates. By default
154
110
  # the library will use an included bundle that can successfully validate
155
111
  # Stripe certificates.
@@ -162,6 +118,8 @@ module Stripe
162
118
 
163
119
  # empty this field so a new store is initialized
164
120
  @ca_store = nil
121
+
122
+ StripeClient.clear_all_connection_managers
165
123
  end
166
124
 
167
125
  # A certificate store initialized from the the bundle in #ca_bundle_path and
@@ -181,6 +139,19 @@ module Stripe
181
139
  end
182
140
  end
183
141
 
142
+ def self.connection_base=(connection_base)
143
+ @connection_base = connection_base
144
+ StripeClient.clear_all_connection_managers
145
+ end
146
+
147
+ def self.enable_telemetry?
148
+ @enable_telemetry
149
+ end
150
+
151
+ def self.enable_telemetry=(val)
152
+ @enable_telemetry = val
153
+ end
154
+
184
155
  # map to the same values as the standard library's logger
185
156
  LEVEL_DEBUG = Logger::DEBUG
186
157
  LEVEL_ERROR = Logger::ERROR
@@ -206,7 +177,8 @@ module Stripe
206
177
  end
207
178
 
208
179
  if !val.nil? && ![LEVEL_DEBUG, LEVEL_ERROR, LEVEL_INFO].include?(val)
209
- raise ArgumentError, "log_level should only be set to `nil`, `debug` or `info`"
180
+ raise ArgumentError,
181
+ "log_level should only be set to `nil`, `debug` or `info`"
210
182
  end
211
183
  @log_level = val
212
184
  end
@@ -234,12 +206,19 @@ module Stripe
234
206
  @max_network_retries = val.to_i
235
207
  end
236
208
 
237
- def self.enable_telemetry?
238
- @enable_telemetry
209
+ def self.open_timeout=(open_timeout)
210
+ @open_timeout = open_timeout
211
+ StripeClient.clear_all_connection_managers
239
212
  end
240
213
 
241
- def self.enable_telemetry=(val)
242
- @enable_telemetry = val
214
+ def self.proxy=(proxy)
215
+ @proxy = proxy
216
+ StripeClient.clear_all_connection_managers
217
+ end
218
+
219
+ def self.read_timeout=(read_timeout)
220
+ @read_timeout = read_timeout
221
+ StripeClient.clear_all_connection_managers
243
222
  end
244
223
 
245
224
  # Sets some basic information about the running application that's sent along
@@ -256,14 +235,14 @@ module Stripe
256
235
  }
257
236
  end
258
237
 
259
- # DEPRECATED. Use `Util#encode_parameters` instead.
260
- def self.uri_encode(params)
261
- Util.encode_parameters(params)
238
+ def self.uploads_base=(uploads_base)
239
+ @uploads_base = uploads_base
240
+ StripeClient.clear_all_connection_managers
262
241
  end
263
- private_class_method :uri_encode
264
- class << self
265
- extend Gem::Deprecate
266
- deprecate :uri_encode, "Stripe::Util#encode_parameters", 2016, 1
242
+
243
+ def self.verify_ssl_certs=(verify_ssl_certs)
244
+ @verify_ssl_certs = verify_ssl_certs
245
+ StripeClient.clear_all_connection_managers
267
246
  end
268
247
  end
269
248
 
@@ -3,11 +3,33 @@
3
3
  module Stripe
4
4
  module APIOperations
5
5
  module Delete
6
+ module ClassMethods
7
+ # Deletes an API resource
8
+ #
9
+ # Deletes the identified resource with the passed in parameters.
10
+ #
11
+ # ==== Attributes
12
+ #
13
+ # * +id+ - ID of the resource to delete.
14
+ # * +params+ - A hash of parameters to pass to the API
15
+ # * +opts+ - A Hash of additional options (separate from the params /
16
+ # object values) to be added to the request. E.g. to allow for an
17
+ # idempotency_key to be passed in the request headers, or for the
18
+ # api_key to be overwritten. See {APIOperations::Request.request}.
19
+ def delete(id, params = {}, opts = {})
20
+ resp, opts = request(:delete, "#{resource_url}/#{id}", params, opts)
21
+ Util.convert_to_stripe_object(resp.data, opts)
22
+ end
23
+ end
24
+
6
25
  def delete(params = {}, opts = {})
7
- opts = Util.normalize_opts(opts)
8
26
  resp, opts = request(:delete, resource_url, params, opts)
9
27
  initialize_from(resp.data, opts)
10
28
  end
29
+
30
+ def self.included(base)
31
+ base.extend(ClassMethods)
32
+ end
11
33
  end
12
34
  end
13
35
  end
@@ -19,12 +19,6 @@ module Stripe
19
19
 
20
20
  obj
21
21
  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
22
  end
29
23
  end
30
24
  end
@@ -10,8 +10,10 @@ module Stripe
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)
14
- path ||= "#{resource}s"
13
+ def nested_resource_class_methods(resource, path: nil, operations: nil,
14
+ resource_plural: nil)
15
+ resource_plural ||= "#{resource}s"
16
+ path ||= resource_plural
15
17
  raise ArgumentError, "operations array required" if operations.nil?
16
18
 
17
19
  resource_url_method = :"#{resource}s_url"
@@ -24,31 +26,36 @@ module Stripe
24
26
  operations.each do |operation|
25
27
  case operation
26
28
  when :create
27
- define_singleton_method(:"create_#{resource}") do |id, params = {}, opts = {}|
29
+ define_singleton_method(:"create_#{resource}") \
30
+ do |id, params = {}, opts = {}|
28
31
  url = send(resource_url_method, id)
29
32
  resp, opts = request(:post, url, params, opts)
30
33
  Util.convert_to_stripe_object(resp.data, opts)
31
34
  end
32
35
  when :retrieve
33
- define_singleton_method(:"retrieve_#{resource}") do |id, nested_id, opts = {}|
36
+ define_singleton_method(:"retrieve_#{resource}") \
37
+ do |id, nested_id, opts = {}|
34
38
  url = send(resource_url_method, id, nested_id)
35
39
  resp, opts = request(:get, url, {}, opts)
36
40
  Util.convert_to_stripe_object(resp.data, opts)
37
41
  end
38
42
  when :update
39
- define_singleton_method(:"update_#{resource}") do |id, nested_id, params = {}, opts = {}|
43
+ define_singleton_method(:"update_#{resource}") \
44
+ do |id, nested_id, params = {}, opts = {}|
40
45
  url = send(resource_url_method, id, nested_id)
41
46
  resp, opts = request(:post, url, params, opts)
42
47
  Util.convert_to_stripe_object(resp.data, opts)
43
48
  end
44
49
  when :delete
45
- define_singleton_method(:"delete_#{resource}") do |id, nested_id, params = {}, opts = {}|
50
+ define_singleton_method(:"delete_#{resource}") \
51
+ do |id, nested_id, params = {}, opts = {}|
46
52
  url = send(resource_url_method, id, nested_id)
47
53
  resp, opts = request(:delete, url, params, opts)
48
54
  Util.convert_to_stripe_object(resp.data, opts)
49
55
  end
50
56
  when :list
51
- define_singleton_method(:"list_#{resource}s") do |id, params = {}, opts = {}|
57
+ define_singleton_method(:"list_#{resource_plural}") \
58
+ do |id, params = {}, opts = {}|
52
59
  url = send(resource_url_method, id)
53
60
  resp, opts = request(:get, url, params, opts)
54
61
  Util.convert_to_stripe_object(resp.data, opts)
@@ -31,12 +31,10 @@ module Stripe
31
31
  [resp, opts_to_persist]
32
32
  end
33
33
 
34
- private
35
-
36
- def warn_on_opts_in_params(params)
34
+ private def warn_on_opts_in_params(params)
37
35
  Util::OPTS_USER_SPECIFIED.each do |opt|
38
36
  if params.key?(opt)
39
- $stderr.puts("WARNING: #{opt} should be in opts instead of params.")
37
+ warn("WARNING: #{opt} should be in opts instead of params.")
40
38
  end
41
39
  end
42
40
  end
@@ -46,9 +44,7 @@ module Stripe
46
44
  base.extend(ClassMethods)
47
45
  end
48
46
 
49
- protected
50
-
51
- def request(method, url, params = {}, opts = {})
47
+ protected def request(method, url, params = {}, opts = {})
52
48
  opts = @opts.merge(Util.normalize_opts(opts))
53
49
  self.class.request(method, url, params, opts)
54
50
  end
@@ -76,9 +76,7 @@ module Stripe
76
76
  base.extend(ClassMethods)
77
77
  end
78
78
 
79
- private
80
-
81
- def save_url
79
+ private def save_url
82
80
  # This switch essentially allows us "upsert"-like functionality. If the
83
81
  # API resource doesn't have an ID set (suggesting that it's new) and
84
82
  # 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.
@@ -48,9 +50,44 @@ module Stripe
48
50
  end
49
51
  end
50
52
 
53
+ # Adds a custom method to a resource class. This is used to add support for
54
+ # non-CRUDL API requests, e.g. capturing charges. custom_method takes the
55
+ # following parameters:
56
+ # - name: the name of the custom method to create (as a symbol)
57
+ # - http_verb: the HTTP verb for the API request (:get, :post, or :delete)
58
+ # - http_path: the path to append to the resource's URL. If not provided,
59
+ # the name is used as the path
60
+ #
61
+ # For example, this call:
62
+ # custom_method :capture, http_verb: post
63
+ # adds a `capture` class method to the resource class that, when called,
64
+ # will send a POST request to `/v1/<object_name>/capture`.
65
+ def self.custom_method(name, http_verb:, http_path: nil)
66
+ unless %i[get post delete].include?(http_verb)
67
+ raise ArgumentError,
68
+ "Invalid http_verb value: #{http_verb.inspect}. Should be one " \
69
+ "of :get, :post or :delete."
70
+ end
71
+ http_path ||= name.to_s
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
+
78
+ url = "#{resource_url}/#{CGI.escape(id)}/#{CGI.escape(http_path)}"
79
+ resp, opts = request(http_verb, url, params, opts)
80
+ Util.convert_to_stripe_object(resp.data, opts)
81
+ end
82
+ end
83
+
51
84
  def resource_url
52
85
  unless (id = self["id"])
53
- 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
+ )
54
91
  end
55
92
  "#{self.class.resource_url}/#{CGI.escape(id)}"
56
93
  end
@@ -66,5 +103,16 @@ module Stripe
66
103
  instance.refresh
67
104
  instance
68
105
  end
106
+
107
+ protected def request_stripe_object(method:, path:, params:, opts: {})
108
+ resp, opts = request(method, path, params, opts)
109
+
110
+ # If we're getting back this thing, update; otherwise, instantiate.
111
+ if Util.object_name_matches_class?(resp.data[:object], self.class)
112
+ initialize_from(resp.data, opts)
113
+ else
114
+ Util.convert_to_stripe_object(resp.data, opts)
115
+ end
116
+ end
69
117
  end
70
118
  end