stripe 4.9.0 → 5.28.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (251) hide show
  1. checksums.yaml +4 -4
  2. data/.editorconfig +10 -0
  3. data/.rubocop.yml +56 -8
  4. data/.rubocop_todo.yml +8 -25
  5. data/.travis.yml +4 -8
  6. data/.vscode/extensions.json +7 -0
  7. data/.vscode/settings.json +8 -0
  8. data/CHANGELOG.md +206 -2
  9. data/CODE_OF_CONDUCT.md +77 -0
  10. data/Gemfile +10 -13
  11. data/README.md +178 -58
  12. data/Rakefile +8 -7
  13. data/VERSION +1 -1
  14. data/lib/stripe.rb +41 -195
  15. data/lib/stripe/api_operations/create.rb +1 -1
  16. data/lib/stripe/api_operations/delete.rb +28 -2
  17. data/lib/stripe/api_operations/list.rb +1 -12
  18. data/lib/stripe/api_operations/nested_resource.rb +38 -28
  19. data/lib/stripe/api_operations/request.rb +47 -8
  20. data/lib/stripe/api_operations/save.rb +8 -7
  21. data/lib/stripe/api_resource.rb +52 -3
  22. data/lib/stripe/connection_manager.rb +162 -0
  23. data/lib/stripe/error_object.rb +94 -0
  24. data/lib/stripe/errors.rb +31 -10
  25. data/lib/stripe/instrumentation.rb +82 -0
  26. data/lib/stripe/list_object.rb +41 -7
  27. data/lib/stripe/multipart_encoder.rb +131 -0
  28. data/lib/stripe/oauth.rb +4 -3
  29. data/lib/stripe/object_types.rb +101 -0
  30. data/lib/stripe/resources.rb +84 -0
  31. data/lib/stripe/{account.rb → resources/account.rb} +50 -27
  32. data/lib/stripe/{account_link.rb → resources/account_link.rb} +2 -1
  33. data/lib/stripe/resources/alipay_account.rb +34 -0
  34. data/lib/stripe/{apple_pay_domain.rb → resources/apple_pay_domain.rb} +2 -1
  35. data/lib/stripe/resources/application_fee.rb +14 -0
  36. data/lib/stripe/resources/application_fee_refund.rb +30 -0
  37. data/lib/stripe/{balance.rb → resources/balance.rb} +2 -1
  38. data/lib/stripe/{balance_transaction.rb → resources/balance_transaction.rb} +2 -5
  39. data/lib/stripe/resources/bank_account.rb +43 -0
  40. data/lib/stripe/resources/billing_portal/session.rb +12 -0
  41. data/lib/stripe/{bitcoin_receiver.rb → resources/bitcoin_receiver.rb} +4 -3
  42. data/lib/stripe/{bitcoin_transaction.rb → resources/bitcoin_transaction.rb} +1 -1
  43. data/lib/stripe/resources/capability.rb +33 -0
  44. data/lib/stripe/{card.rb → resources/card.rb} +13 -4
  45. data/lib/stripe/resources/charge.rb +23 -0
  46. data/lib/stripe/resources/checkout/session.rb +16 -0
  47. data/lib/stripe/{country_spec.rb → resources/country_spec.rb} +2 -1
  48. data/lib/stripe/{coupon.rb → resources/coupon.rb} +3 -2
  49. data/lib/stripe/resources/credit_note.rb +33 -0
  50. data/lib/stripe/resources/credit_note_line_item.rb +7 -0
  51. data/lib/stripe/resources/customer.rb +36 -0
  52. data/lib/stripe/resources/customer_balance_transaction.rb +30 -0
  53. data/lib/stripe/resources/discount.rb +7 -0
  54. data/lib/stripe/resources/dispute.rb +22 -0
  55. data/lib/stripe/{ephemeral_key.rb → resources/ephemeral_key.rb} +6 -2
  56. data/lib/stripe/{event.rb → resources/event.rb} +2 -1
  57. data/lib/stripe/{exchange_rate.rb → resources/exchange_rate.rb} +2 -1
  58. data/lib/stripe/{file.rb → resources/file.rb} +9 -11
  59. data/lib/stripe/{file_link.rb → resources/file_link.rb} +3 -2
  60. data/lib/stripe/resources/invoice.rb +74 -0
  61. data/lib/stripe/{invoice_item.rb → resources/invoice_item.rb} +3 -2
  62. data/lib/stripe/{invoice_line_item.rb → resources/invoice_line_item.rb} +1 -1
  63. data/lib/stripe/resources/issuing/authorization.rb +34 -0
  64. data/lib/stripe/resources/issuing/card.rb +25 -0
  65. data/lib/stripe/{issuing → resources/issuing}/card_details.rb +1 -1
  66. data/lib/stripe/{issuing → resources/issuing}/cardholder.rb +3 -2
  67. data/lib/stripe/resources/issuing/dispute.rb +25 -0
  68. data/lib/stripe/{issuing → resources/issuing}/transaction.rb +3 -2
  69. data/lib/stripe/resources/line_item.rb +7 -0
  70. data/lib/stripe/resources/login_link.rb +14 -0
  71. data/lib/stripe/resources/mandate.rb +8 -0
  72. data/lib/stripe/resources/order.rb +33 -0
  73. data/lib/stripe/{order_return.rb → resources/order_return.rb} +2 -1
  74. data/lib/stripe/resources/payment_intent.rb +43 -0
  75. data/lib/stripe/resources/payment_method.rb +33 -0
  76. data/lib/stripe/resources/payout.rb +33 -0
  77. data/lib/stripe/{person.rb → resources/person.rb} +8 -3
  78. data/lib/stripe/{plan.rb → resources/plan.rb} +2 -1
  79. data/lib/stripe/resources/price.rb +12 -0
  80. data/lib/stripe/{product.rb → resources/product.rb} +4 -3
  81. data/lib/stripe/resources/promotion_code.rb +12 -0
  82. data/lib/stripe/resources/radar/early_fraud_warning.rb +12 -0
  83. data/lib/stripe/{radar → resources/radar}/value_list.rb +3 -2
  84. data/lib/stripe/{radar → resources/radar}/value_list_item.rb +3 -2
  85. data/lib/stripe/{recipient.rb → resources/recipient.rb} +3 -6
  86. data/lib/stripe/{recipient_transfer.rb → resources/recipient_transfer.rb} +1 -1
  87. data/lib/stripe/{refund.rb → resources/refund.rb} +2 -1
  88. data/lib/stripe/{reporting → resources/reporting}/report_run.rb +3 -2
  89. data/lib/stripe/{reporting → resources/reporting}/report_type.rb +3 -2
  90. data/lib/stripe/resources/reversal.rb +29 -0
  91. data/lib/stripe/resources/review.rb +21 -0
  92. data/lib/stripe/resources/setup_attempt.rb +10 -0
  93. data/lib/stripe/resources/setup_intent.rb +33 -0
  94. data/lib/stripe/{sigma → resources/sigma}/scheduled_query_run.rb +3 -2
  95. data/lib/stripe/{sku.rb → resources/sku.rb} +4 -3
  96. data/lib/stripe/resources/source.rb +47 -0
  97. data/lib/stripe/{source_transaction.rb → resources/source_transaction.rb} +1 -1
  98. data/lib/stripe/resources/subscription.rb +26 -0
  99. data/lib/stripe/resources/subscription_item.rb +26 -0
  100. data/lib/stripe/resources/subscription_schedule.rb +33 -0
  101. data/lib/stripe/resources/tax_id.rb +26 -0
  102. data/lib/stripe/resources/tax_rate.rb +12 -0
  103. data/lib/stripe/resources/terminal/connection_token.rb +12 -0
  104. data/lib/stripe/{terminal → resources/terminal}/location.rb +4 -2
  105. data/lib/stripe/{terminal → resources/terminal}/reader.rb +4 -2
  106. data/lib/stripe/{three_d_secure.rb → resources/three_d_secure.rb} +2 -1
  107. data/lib/stripe/{token.rb → resources/token.rb} +2 -1
  108. data/lib/stripe/resources/topup.rb +23 -0
  109. data/lib/stripe/resources/transfer.rb +27 -0
  110. data/lib/stripe/resources/usage_record.rb +7 -0
  111. data/lib/stripe/{usage_record_summary.rb → resources/usage_record_summary.rb} +1 -1
  112. data/lib/stripe/{webhook_endpoint.rb → resources/webhook_endpoint.rb} +3 -2
  113. data/lib/stripe/singleton_api_resource.rb +3 -1
  114. data/lib/stripe/stripe_client.rb +493 -236
  115. data/lib/stripe/stripe_configuration.rb +178 -0
  116. data/lib/stripe/stripe_object.rb +75 -61
  117. data/lib/stripe/stripe_response.rb +53 -21
  118. data/lib/stripe/util.rb +66 -109
  119. data/lib/stripe/version.rb +1 -1
  120. data/lib/stripe/webhook.rb +43 -10
  121. data/stripe.gemspec +14 -5
  122. data/test/stripe/account_link_test.rb +4 -4
  123. data/test/stripe/account_test.rb +193 -32
  124. data/test/stripe/alipay_account_test.rb +1 -1
  125. data/test/stripe/api_operations_test.rb +3 -4
  126. data/test/stripe/api_resource_test.rb +159 -37
  127. data/test/stripe/apple_pay_domain_test.rb +18 -5
  128. data/test/stripe/application_fee_refund_test.rb +1 -1
  129. data/test/stripe/application_fee_test.rb +45 -1
  130. data/test/stripe/balance_test.rb +1 -1
  131. data/test/stripe/balance_transaction_test.rb +20 -0
  132. data/test/stripe/bank_account_test.rb +1 -1
  133. data/test/stripe/billing_portal/session_test.rb +18 -0
  134. data/test/stripe/capability_test.rb +45 -0
  135. data/test/stripe/charge_test.rb +13 -8
  136. data/test/stripe/checkout/session_test.rb +19 -1
  137. data/test/stripe/connection_manager_test.rb +167 -0
  138. data/test/stripe/country_spec_test.rb +1 -1
  139. data/test/stripe/coupon_test.rb +16 -6
  140. data/test/stripe/credit_note_test.rb +90 -0
  141. data/test/stripe/customer_balance_transaction_test.rb +37 -0
  142. data/test/stripe/customer_card_test.rb +7 -1
  143. data/test/stripe/customer_test.rb +151 -40
  144. data/test/stripe/dispute_test.rb +10 -1
  145. data/test/stripe/ephemeral_key_test.rb +8 -1
  146. data/test/stripe/errors_test.rb +42 -9
  147. data/test/stripe/exchange_rate_test.rb +1 -1
  148. data/test/stripe/file_link_test.rb +1 -1
  149. data/test/stripe/file_test.rb +19 -5
  150. data/test/stripe/instrumentation_test.rb +74 -0
  151. data/test/stripe/invoice_item_test.rb +18 -7
  152. data/test/stripe/invoice_line_item_test.rb +1 -1
  153. data/test/stripe/invoice_test.rb +77 -9
  154. data/test/stripe/issuing/authorization_test.rb +33 -11
  155. data/test/stripe/issuing/card_test.rb +27 -6
  156. data/test/stripe/issuing/cardholder_test.rb +1 -1
  157. data/test/stripe/issuing/dispute_test.rb +23 -14
  158. data/test/stripe/issuing/transaction_test.rb +1 -1
  159. data/test/stripe/list_object_test.rb +70 -24
  160. data/test/stripe/login_link_test.rb +2 -2
  161. data/test/stripe/mandate_test.rb +14 -0
  162. data/test/stripe/multipart_encoder_test.rb +130 -0
  163. data/test/stripe/oauth_test.rb +17 -1
  164. data/test/stripe/order_return_test.rb +1 -1
  165. data/test/stripe/order_test.rb +28 -3
  166. data/test/stripe/payment_intent_test.rb +31 -4
  167. data/test/stripe/payment_method_test.rb +84 -0
  168. data/test/stripe/payout_test.rb +23 -1
  169. data/test/stripe/person_test.rb +1 -1
  170. data/test/stripe/plan_test.rb +26 -20
  171. data/test/stripe/price_test.rb +48 -0
  172. data/test/stripe/product_test.rb +17 -8
  173. data/test/stripe/promotion_code_test.rb +42 -0
  174. data/test/stripe/radar/early_fraud_warning_test.rb +22 -0
  175. data/test/stripe/radar/value_list_item_test.rb +16 -6
  176. data/test/stripe/radar/value_list_test.rb +16 -6
  177. data/test/stripe/recipient_test.rb +18 -5
  178. data/test/stripe/refund_test.rb +1 -1
  179. data/test/stripe/reporting/report_run_test.rb +1 -1
  180. data/test/stripe/reporting/report_type_test.rb +1 -1
  181. data/test/stripe/reversal_test.rb +1 -1
  182. data/test/stripe/review_test.rb +1 -1
  183. data/test/stripe/setup_attempt_test.rb +16 -0
  184. data/test/stripe/setup_intent_test.rb +84 -0
  185. data/test/stripe/sigma/scheduled_query_run_test.rb +1 -1
  186. data/test/stripe/sku_test.rb +16 -6
  187. data/test/stripe/source_test.rb +52 -19
  188. data/test/stripe/stripe_client_test.rb +546 -78
  189. data/test/stripe/stripe_configuration_test.rb +131 -0
  190. data/test/stripe/stripe_object_test.rb +16 -41
  191. data/test/stripe/stripe_response_test.rb +71 -25
  192. data/test/stripe/subscription_item_test.rb +38 -7
  193. data/test/stripe/subscription_schedule_test.rb +19 -1
  194. data/test/stripe/subscription_test.rb +29 -9
  195. data/test/stripe/tax_id_test.rb +31 -0
  196. data/test/stripe/tax_rate_test.rb +43 -0
  197. data/test/stripe/terminal/connection_token_test.rb +1 -1
  198. data/test/stripe/terminal/location_test.rb +18 -1
  199. data/test/stripe/terminal/reader_test.rb +18 -1
  200. data/test/stripe/three_d_secure_test.rb +1 -1
  201. data/test/stripe/topup_test.rb +9 -1
  202. data/test/stripe/transfer_test.rb +46 -1
  203. data/test/stripe/usage_record_summary_test.rb +15 -5
  204. data/test/stripe/util_test.rb +1 -1
  205. data/test/stripe/webhook_endpoint_test.rb +18 -1
  206. data/test/stripe/webhook_test.rb +48 -9
  207. data/test/stripe_mock.rb +4 -3
  208. data/test/stripe_test.rb +102 -33
  209. data/test/test_helper.rb +16 -12
  210. metadata +141 -129
  211. data/lib/stripe/alipay_account.rb +0 -27
  212. data/lib/stripe/application_fee.rb +0 -23
  213. data/lib/stripe/application_fee_refund.rb +0 -22
  214. data/lib/stripe/bank_account.rb +0 -32
  215. data/lib/stripe/charge.rb +0 -84
  216. data/lib/stripe/checkout/session.rb +0 -11
  217. data/lib/stripe/customer.rb +0 -90
  218. data/lib/stripe/dispute.rb +0 -19
  219. data/lib/stripe/invoice.rb +0 -48
  220. data/lib/stripe/issuer_fraud_record.rb +0 -9
  221. data/lib/stripe/issuing/authorization.rb +0 -22
  222. data/lib/stripe/issuing/card.rb +0 -18
  223. data/lib/stripe/issuing/dispute.rb +0 -13
  224. data/lib/stripe/login_link.rb +0 -11
  225. data/lib/stripe/order.rb +0 -31
  226. data/lib/stripe/payment_intent.rb +0 -26
  227. data/lib/stripe/payout.rb +0 -20
  228. data/lib/stripe/reversal.rb +0 -22
  229. data/lib/stripe/review.rb +0 -14
  230. data/lib/stripe/source.rb +0 -38
  231. data/lib/stripe/subscription.rb +0 -25
  232. data/lib/stripe/subscription_item.rb +0 -17
  233. data/lib/stripe/subscription_schedule.rb +0 -32
  234. data/lib/stripe/subscription_schedule_revision.rb +0 -25
  235. data/lib/stripe/terminal/connection_token.rb +0 -11
  236. data/lib/stripe/topup.rb +0 -16
  237. data/lib/stripe/transfer.rb +0 -23
  238. data/lib/stripe/usage_record.rb +0 -14
  239. data/test/api_stub_helpers.rb +0 -1
  240. data/test/stripe/account_external_accounts_operations_test.rb +0 -69
  241. data/test/stripe/account_login_links_operations_test.rb +0 -21
  242. data/test/stripe/account_persons_operations_test.rb +0 -70
  243. data/test/stripe/application_fee_refunds_operations_test.rb +0 -56
  244. data/test/stripe/customer_sources_operations_test.rb +0 -64
  245. data/test/stripe/file_upload_test.rb +0 -76
  246. data/test/stripe/issuer_fraud_record_test.rb +0 -20
  247. data/test/stripe/source_transaction_test.rb +0 -19
  248. data/test/stripe/subscription_schedule_revision_test.rb +0 -37
  249. data/test/stripe/subscription_schedule_revisions_operations_test.rb +0 -35
  250. data/test/stripe/transfer_reversals_operations_test.rb +0 -57
  251. data/test/stripe/usage_record_test.rb +0 -28
@@ -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.
@@ -48,15 +50,51 @@ 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 = execute_resource_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
57
94
 
58
95
  def refresh
59
- resp, opts = request(:get, resource_url, @retrieve_params)
96
+ resp, opts = execute_resource_request(:get, resource_url,
97
+ @retrieve_params)
60
98
  initialize_from(resp.data, opts)
61
99
  end
62
100
 
@@ -66,5 +104,16 @@ module Stripe
66
104
  instance.refresh
67
105
  instance
68
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
69
118
  end
70
119
  end
@@ -0,0 +1,162 @@
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
+
19
+ def initialize
20
+ @active_connections = {}
21
+ @last_used = Util.monotonic_time
22
+
23
+ # A connection manager may be accessed across threads as one thread makes
24
+ # requests on it while another is trying to clear it (either because it's
25
+ # trying to garbage collect the manager or trying to clear it because a
26
+ # configuration setting has changed). That's probably thread-safe already
27
+ # because of Ruby's GIL, but just in case the library's running on JRuby
28
+ # or the like, use a mutex to synchronize access in this connection
29
+ # manager.
30
+ @mutex = Mutex.new
31
+ end
32
+
33
+ # Finishes any active connections by closing their TCP connection and
34
+ # clears them from internal tracking.
35
+ def clear
36
+ @mutex.synchronize do
37
+ @active_connections.each do |_, connection|
38
+ connection.finish
39
+ end
40
+ @active_connections = {}
41
+ end
42
+ end
43
+
44
+ # Gets a connection for a given URI. This is for internal use only as it's
45
+ # subject to change (we've moved between HTTP client schemes in the past
46
+ # and may do it again).
47
+ #
48
+ # `uri` is expected to be a string.
49
+ def connection_for(uri)
50
+ @mutex.synchronize do
51
+ u = URI.parse(uri)
52
+ connection = @active_connections[[u.host, u.port]]
53
+
54
+ if connection.nil?
55
+ connection = create_connection(u)
56
+ connection.start
57
+
58
+ @active_connections[[u.host, u.port]] = connection
59
+ end
60
+
61
+ connection
62
+ end
63
+ end
64
+
65
+ # Executes an HTTP request to the given URI with the given method. Also
66
+ # allows a request body, headers, and query string to be specified.
67
+ def execute_request(method, uri, body: nil, headers: nil, query: nil)
68
+ # Perform some basic argument validation because it's easy to get
69
+ # confused between strings and hashes for things like body and query
70
+ # parameters.
71
+ raise ArgumentError, "method should be a symbol" \
72
+ unless method.is_a?(Symbol)
73
+ raise ArgumentError, "uri should be a string" \
74
+ unless uri.is_a?(String)
75
+ raise ArgumentError, "body should be a string" \
76
+ if body && !body.is_a?(String)
77
+ raise ArgumentError, "headers should be a hash" \
78
+ if headers && !headers.is_a?(Hash)
79
+ raise ArgumentError, "query should be a string" \
80
+ if query && !query.is_a?(String)
81
+
82
+ @last_used = Util.monotonic_time
83
+
84
+ connection = connection_for(uri)
85
+
86
+ u = URI.parse(uri)
87
+ path = if query
88
+ u.path + "?" + query
89
+ else
90
+ u.path
91
+ end
92
+
93
+ @mutex.synchronize do
94
+ connection.send_request(method.to_s.upcase, path, body, headers)
95
+ end
96
+ end
97
+
98
+ #
99
+ # private
100
+ #
101
+
102
+ # `uri` should be a parsed `URI` object.
103
+ private def create_connection(uri)
104
+ # These all come back as `nil` if no proxy is configured.
105
+ proxy_host, proxy_port, proxy_user, proxy_pass = proxy_parts
106
+
107
+ connection = Net::HTTP.new(uri.host, uri.port,
108
+ proxy_host, proxy_port,
109
+ proxy_user, proxy_pass)
110
+
111
+ # Time in seconds within which Net::HTTP will try to reuse an already
112
+ # open connection when issuing a new operation. Outside this window, Ruby
113
+ # will transparently close and re-open the connection without trying to
114
+ # reuse it.
115
+ #
116
+ # Ruby's default of 2 seconds is almost certainly too short. Here I've
117
+ # reused Go's default for `DefaultTransport`.
118
+ connection.keep_alive_timeout = 30
119
+
120
+ connection.open_timeout = Stripe.open_timeout
121
+ connection.read_timeout = Stripe.read_timeout
122
+ if connection.respond_to?(:write_timeout=)
123
+ connection.write_timeout = Stripe.write_timeout
124
+ end
125
+
126
+ connection.use_ssl = uri.scheme == "https"
127
+
128
+ if Stripe.verify_ssl_certs
129
+ connection.verify_mode = OpenSSL::SSL::VERIFY_PEER
130
+ connection.cert_store = Stripe.ca_store
131
+ else
132
+ connection.verify_mode = OpenSSL::SSL::VERIFY_NONE
133
+ warn_ssl_verify_none
134
+ end
135
+
136
+ connection
137
+ end
138
+
139
+ # `Net::HTTP` somewhat awkwardly requires each component of a proxy URI
140
+ # (host, port, etc.) rather than the URI itself. This method simply parses
141
+ # out those pieces to make passing them into a new connection a little less
142
+ # ugly.
143
+ private def proxy_parts
144
+ if Stripe.proxy.nil?
145
+ [nil, nil, nil, nil]
146
+ else
147
+ u = URI.parse(Stripe.proxy)
148
+ [u.host, u.port, u.user, u.password]
149
+ end
150
+ end
151
+
152
+ private def warn_ssl_verify_none
153
+ return if @verify_ssl_warned
154
+
155
+ @verify_ssl_warned = true
156
+ warn("WARNING: Running without SSL cert verification. " \
157
+ "You should never do this in production. " \
158
+ "Execute `Stripe.verify_ssl_certs = true` to enable " \
159
+ "verification.")
160
+ end
161
+ end
162
+ end
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Stripe
4
+ # Represents an error object as returned by the API.
5
+ #
6
+ # @see https://stripe.com/docs/api/errors
7
+ class ErrorObject < StripeObject
8
+ # Unlike other objects, we explicitly declare getter methods here. This
9
+ # is because the API doesn't return `null` values for fields on this
10
+ # object, rather the fields are omitted entirely. Not declaring the getter
11
+ # methods would cause users to run into `NoMethodError` exceptions and
12
+ # get in the way of generic error handling.
13
+
14
+ # For card errors, the ID of the failed charge.
15
+ def charge
16
+ @values[:charge]
17
+ end
18
+
19
+ # For some errors that could be handled programmatically, a short string
20
+ # indicating the error code reported.
21
+ def code
22
+ @values[:code]
23
+ end
24
+
25
+ # For card errors resulting from a card issuer decline, a short string
26
+ # indicating the card issuer's reason for the decline if they provide one.
27
+ def decline_code
28
+ @values[:decline_code]
29
+ end
30
+
31
+ # A URL to more information about the error code reported.
32
+ def doc_url
33
+ @values[:doc_url]
34
+ end
35
+
36
+ # A human-readable message providing more details about the error. For card
37
+ # errors, these messages can be shown to your users.
38
+ def message
39
+ @values[:message]
40
+ end
41
+
42
+ # If the error is parameter-specific, the parameter related to the error.
43
+ # For example, you can use this to display a message near the correct form
44
+ # field.
45
+ def param
46
+ @values[:param]
47
+ end
48
+
49
+ # The PaymentIntent object for errors returned on a request involving a
50
+ # PaymentIntent.
51
+ def payment_intent
52
+ @values[:payment_intent]
53
+ end
54
+
55
+ # The PaymentMethod object for errors returned on a request involving a
56
+ # PaymentMethod.
57
+ def payment_method
58
+ @values[:payment_method]
59
+ end
60
+
61
+ # The SetupIntent object for errors returned on a request involving a
62
+ # SetupIntent.
63
+ def setup_intent
64
+ @values[:setup_intent]
65
+ end
66
+
67
+ # The source object for errors returned on a request involving a source.
68
+ def source
69
+ @values[:source]
70
+ end
71
+
72
+ # The type of error returned. One of `api_connection_error`, `api_error`,
73
+ # `authentication_error`, `card_error`, `idempotency_error`,
74
+ # `invalid_request_error`, or `rate_limit_error`.
75
+ def type
76
+ @values[:type]
77
+ end
78
+ end
79
+
80
+ # Represents on OAuth error returned by the OAuth API.
81
+ #
82
+ # @see https://stripe.com/docs/connect/oauth-reference#post-token-errors
83
+ class OAuthErrorObject < StripeObject
84
+ # A unique error code per error type.
85
+ def error
86
+ @values[:error]
87
+ end
88
+
89
+ # A human readable description of the error.
90
+ def error_description
91
+ @values[:error_description]
92
+ end
93
+ end
94
+ end