stripe 5.1.0 → 5.55.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +241 -17
- data/CODE_OF_CONDUCT.md +77 -0
- data/Gemfile +9 -5
- data/Makefile +7 -0
- data/README.md +105 -37
- data/VERSION +1 -1
- data/lib/stripe/api_operations/create.rb +1 -1
- data/lib/stripe/api_operations/delete.rb +7 -3
- data/lib/stripe/api_operations/list.rb +1 -6
- data/lib/stripe/api_operations/nested_resource.rb +29 -26
- data/lib/stripe/api_operations/request.rb +82 -6
- data/lib/stripe/api_operations/save.rb +7 -4
- data/lib/stripe/api_operations/search.rb +19 -0
- data/lib/stripe/api_resource.rb +13 -19
- data/lib/stripe/api_resource_test_helpers.rb +47 -0
- data/lib/stripe/connection_manager.rb +85 -26
- data/lib/stripe/error_object.rb +2 -3
- data/lib/stripe/errors.rb +9 -1
- data/lib/stripe/instrumentation.rb +84 -0
- data/lib/stripe/list_object.rb +32 -4
- data/lib/stripe/oauth.rb +8 -6
- data/lib/stripe/object_types.rb +26 -1
- data/lib/stripe/resources/account.rb +5 -9
- data/lib/stripe/resources/account_link.rb +1 -0
- data/lib/stripe/resources/apple_pay_domain.rb +1 -0
- data/lib/stripe/resources/application_fee.rb +1 -0
- data/lib/stripe/resources/application_fee_refund.rb +2 -1
- data/lib/stripe/resources/balance.rb +1 -0
- data/lib/stripe/resources/balance_transaction.rb +1 -0
- data/lib/stripe/resources/bank_account.rb +2 -1
- data/lib/stripe/resources/billing_portal/configuration.rb +14 -0
- data/lib/stripe/resources/billing_portal/session.rb +12 -0
- data/lib/stripe/resources/bitcoin_receiver.rb +1 -0
- data/lib/stripe/resources/bitcoin_transaction.rb +3 -2
- data/lib/stripe/resources/capability.rb +2 -1
- data/lib/stripe/resources/card.rb +1 -0
- data/lib/stripe/resources/cash_balance.rb +22 -0
- data/lib/stripe/resources/charge.rb +10 -0
- data/lib/stripe/resources/checkout/session.rb +16 -0
- data/lib/stripe/resources/country_spec.rb +1 -0
- data/lib/stripe/resources/coupon.rb +1 -0
- data/lib/stripe/resources/credit_note.rb +11 -0
- data/lib/stripe/resources/credit_note_line_item.rb +8 -0
- data/lib/stripe/resources/customer.rb +41 -2
- data/lib/stripe/resources/customer_balance_transaction.rb +3 -2
- data/lib/stripe/resources/discount.rb +1 -0
- data/lib/stripe/resources/dispute.rb +1 -0
- data/lib/stripe/resources/ephemeral_key.rb +1 -0
- data/lib/stripe/resources/event.rb +1 -0
- data/lib/stripe/resources/exchange_rate.rb +1 -0
- data/lib/stripe/resources/file.rb +3 -1
- data/lib/stripe/resources/file_link.rb +1 -0
- data/lib/stripe/resources/financial_connections/account.rb +31 -0
- data/lib/stripe/resources/financial_connections/account_owner.rb +10 -0
- data/lib/stripe/resources/financial_connections/account_ownership.rb +10 -0
- data/lib/stripe/resources/financial_connections/session.rb +12 -0
- data/lib/stripe/resources/funding_instructions.rb +16 -0
- data/lib/stripe/resources/identity/verification_report.rb +12 -0
- data/lib/stripe/resources/identity/verification_session.rb +35 -0
- data/lib/stripe/resources/invoice.rb +12 -2
- data/lib/stripe/resources/invoice_item.rb +1 -0
- data/lib/stripe/resources/invoice_line_item.rb +1 -0
- data/lib/stripe/resources/issuing/authorization.rb +1 -0
- data/lib/stripe/resources/issuing/card.rb +1 -0
- data/lib/stripe/resources/issuing/card_details.rb +2 -1
- data/lib/stripe/resources/issuing/cardholder.rb +1 -0
- data/lib/stripe/resources/issuing/dispute.rb +12 -0
- data/lib/stripe/resources/issuing/transaction.rb +1 -0
- data/lib/stripe/resources/line_item.rb +8 -0
- data/lib/stripe/resources/login_link.rb +1 -0
- data/lib/stripe/resources/mandate.rb +8 -0
- data/lib/stripe/resources/order.rb +1 -0
- data/lib/stripe/resources/order_return.rb +1 -0
- data/lib/stripe/resources/payment_intent.rb +40 -0
- data/lib/stripe/resources/payment_link.rb +23 -0
- data/lib/stripe/resources/payment_method.rb +1 -0
- data/lib/stripe/resources/payout.rb +11 -0
- data/lib/stripe/resources/person.rb +1 -0
- data/lib/stripe/resources/plan.rb +1 -0
- data/lib/stripe/resources/price.rb +21 -0
- data/lib/stripe/resources/product.rb +10 -0
- data/lib/stripe/resources/promotion_code.rb +12 -0
- data/lib/stripe/resources/quote.rb +105 -0
- data/lib/stripe/resources/radar/early_fraud_warning.rb +1 -0
- data/lib/stripe/resources/radar/value_list.rb +1 -0
- data/lib/stripe/resources/radar/value_list_item.rb +1 -0
- data/lib/stripe/resources/recipient.rb +1 -0
- data/lib/stripe/resources/refund.rb +31 -0
- data/lib/stripe/resources/reporting/report_run.rb +1 -0
- data/lib/stripe/resources/reporting/report_type.rb +1 -0
- data/lib/stripe/resources/reversal.rb +3 -2
- data/lib/stripe/resources/review.rb +1 -0
- data/lib/stripe/resources/setup_attempt.rb +10 -0
- data/lib/stripe/resources/setup_intent.rb +11 -0
- data/lib/stripe/resources/shipping_rate.rb +12 -0
- data/lib/stripe/resources/sigma/scheduled_query_run.rb +1 -0
- data/lib/stripe/resources/sku.rb +1 -0
- data/lib/stripe/resources/source.rb +10 -3
- data/lib/stripe/resources/source_transaction.rb +1 -0
- data/lib/stripe/resources/subscription.rb +10 -0
- data/lib/stripe/resources/subscription_item.rb +7 -1
- data/lib/stripe/resources/subscription_schedule.rb +1 -0
- data/lib/stripe/resources/tax_code.rb +10 -0
- data/lib/stripe/resources/tax_id.rb +1 -0
- data/lib/stripe/resources/tax_rate.rb +1 -0
- data/lib/stripe/resources/terminal/configuration.rb +15 -0
- data/lib/stripe/resources/terminal/connection_token.rb +1 -0
- data/lib/stripe/resources/terminal/location.rb +1 -0
- data/lib/stripe/resources/terminal/reader.rb +61 -0
- data/lib/stripe/resources/test_helpers/test_clock.rb +25 -0
- data/lib/stripe/resources/three_d_secure.rb +1 -0
- data/lib/stripe/resources/token.rb +1 -0
- data/lib/stripe/resources/topup.rb +1 -0
- data/lib/stripe/resources/transfer.rb +1 -0
- data/lib/stripe/resources/usage_record.rb +1 -0
- data/lib/stripe/resources/usage_record_summary.rb +1 -0
- data/lib/stripe/resources/webhook_endpoint.rb +1 -0
- data/lib/stripe/resources.rb +23 -0
- data/lib/stripe/search_result_object.rb +86 -0
- data/lib/stripe/stripe_client.rb +412 -146
- data/lib/stripe/stripe_configuration.rb +194 -0
- data/lib/stripe/stripe_object.rb +26 -2
- data/lib/stripe/stripe_response.rb +80 -52
- data/lib/stripe/util.rb +74 -7
- data/lib/stripe/version.rb +1 -1
- data/lib/stripe/webhook.rb +38 -7
- data/lib/stripe.rb +39 -168
- data/stripe.gemspec +12 -5
- metadata +36 -186
- data/.editorconfig +0 -10
- data/.gitattributes +0 -4
- data/.github/ISSUE_TEMPLATE.md +0 -5
- data/.gitignore +0 -8
- data/.rubocop.yml +0 -56
- data/.rubocop_todo.yml +0 -39
- data/.travis.yml +0 -39
- data/.vscode/extensions.json +0 -7
- data/.vscode/settings.json +0 -8
- data/test/api_stub_helpers.rb +0 -1
- data/test/openapi/README.md +0 -9
- data/test/stripe/account_link_test.rb +0 -18
- data/test/stripe/account_test.rb +0 -412
- data/test/stripe/alipay_account_test.rb +0 -37
- data/test/stripe/api_operations_test.rb +0 -80
- data/test/stripe/api_resource_test.rb +0 -613
- data/test/stripe/apple_pay_domain_test.rb +0 -46
- data/test/stripe/application_fee_refund_test.rb +0 -37
- data/test/stripe/application_fee_test.rb +0 -58
- data/test/stripe/balance_test.rb +0 -13
- data/test/stripe/balance_transaction_test.rb +0 -20
- data/test/stripe/bank_account_test.rb +0 -36
- data/test/stripe/capability_test.rb +0 -45
- data/test/stripe/charge_test.rb +0 -64
- data/test/stripe/checkout/session_test.rb +0 -41
- data/test/stripe/connection_manager_test.rb +0 -138
- data/test/stripe/country_spec_test.rb +0 -20
- data/test/stripe/coupon_test.rb +0 -61
- data/test/stripe/credit_note_test.rb +0 -61
- data/test/stripe/customer_balance_transaction_test.rb +0 -37
- data/test/stripe/customer_card_test.rb +0 -42
- data/test/stripe/customer_test.rb +0 -226
- data/test/stripe/dispute_test.rb +0 -51
- data/test/stripe/ephemeral_key_test.rb +0 -93
- data/test/stripe/errors_test.rb +0 -41
- data/test/stripe/exchange_rate_test.rb +0 -20
- data/test/stripe/file_link_test.rb +0 -41
- data/test/stripe/file_test.rb +0 -87
- data/test/stripe/invoice_item_test.rb +0 -66
- data/test/stripe/invoice_line_item_test.rb +0 -8
- data/test/stripe/invoice_test.rb +0 -229
- data/test/stripe/issuing/authorization_test.rb +0 -72
- data/test/stripe/issuing/card_test.rb +0 -62
- data/test/stripe/issuing/cardholder_test.rb +0 -53
- data/test/stripe/issuing/dispute_test.rb +0 -45
- data/test/stripe/issuing/transaction_test.rb +0 -48
- data/test/stripe/list_object_test.rb +0 -140
- data/test/stripe/login_link_test.rb +0 -37
- data/test/stripe/multipart_encoder_test.rb +0 -130
- data/test/stripe/oauth_test.rb +0 -88
- data/test/stripe/order_return_test.rb +0 -21
- data/test/stripe/order_test.rb +0 -82
- data/test/stripe/payment_intent_test.rb +0 -107
- data/test/stripe/payment_method_test.rb +0 -84
- data/test/stripe/payout_test.rb +0 -57
- data/test/stripe/person_test.rb +0 -46
- data/test/stripe/plan_test.rb +0 -98
- data/test/stripe/product_test.rb +0 -59
- data/test/stripe/radar/early_fraud_warning_test.rb +0 -22
- data/test/stripe/radar/value_list_item_test.rb +0 -48
- data/test/stripe/radar/value_list_test.rb +0 -61
- data/test/stripe/recipient_test.rb +0 -62
- data/test/stripe/refund_test.rb +0 -39
- data/test/stripe/reporting/report_run_test.rb +0 -33
- data/test/stripe/reporting/report_type_test.rb +0 -22
- data/test/stripe/reversal_test.rb +0 -43
- data/test/stripe/review_test.rb +0 -27
- data/test/stripe/setup_intent_test.rb +0 -84
- data/test/stripe/sigma/scheduled_query_run_test.rb +0 -22
- data/test/stripe/sku_test.rb +0 -60
- data/test/stripe/source_test.rb +0 -81
- data/test/stripe/source_transaction_test.rb +0 -19
- data/test/stripe/stripe_client_test.rb +0 -1039
- data/test/stripe/stripe_object_test.rb +0 -497
- data/test/stripe/stripe_response_test.rb +0 -95
- data/test/stripe/subscription_item_test.rb +0 -75
- data/test/stripe/subscription_schedule_test.rb +0 -82
- data/test/stripe/subscription_test.rb +0 -80
- data/test/stripe/tax_id_test.rb +0 -31
- data/test/stripe/tax_rate_test.rb +0 -43
- data/test/stripe/terminal/connection_token_test.rb +0 -16
- data/test/stripe/terminal/location_test.rb +0 -68
- data/test/stripe/terminal/reader_test.rb +0 -62
- data/test/stripe/three_d_secure_test.rb +0 -23
- data/test/stripe/topup_test.rb +0 -62
- data/test/stripe/transfer_test.rb +0 -88
- data/test/stripe/usage_record_summary_test.rb +0 -19
- data/test/stripe/util_test.rb +0 -402
- data/test/stripe/webhook_endpoint_test.rb +0 -59
- data/test/stripe/webhook_test.rb +0 -96
- data/test/stripe_mock.rb +0 -78
- data/test/stripe_test.rb +0 -50
- data/test/test_data.rb +0 -61
- data/test/test_helper.rb +0 -76
@@ -0,0 +1,194 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Stripe
|
4
|
+
# Configurable options:
|
5
|
+
#
|
6
|
+
# =ca_bundle_path=
|
7
|
+
# The location of a file containing a bundle of CA certificates. By default
|
8
|
+
# the library will use an included bundle that can successfully validate
|
9
|
+
# Stripe certificates.
|
10
|
+
#
|
11
|
+
# =log_level=
|
12
|
+
# When set prompts the library to log some extra information to $stdout and
|
13
|
+
# $stderr about what it's doing. For example, it'll produce information about
|
14
|
+
# requests, responses, and errors that are received. Valid log levels are
|
15
|
+
# `debug` and `info`, with `debug` being a little more verbose in places.
|
16
|
+
#
|
17
|
+
# Use of this configuration is only useful when `.logger` is _not_ set. When
|
18
|
+
# it is, the decision what levels to print is entirely deferred to the logger.
|
19
|
+
#
|
20
|
+
# =logger=
|
21
|
+
# The logger should support the same interface as the `Logger` class that's
|
22
|
+
# part of Ruby's standard library (hint, anything in `Rails.logger` will
|
23
|
+
# likely be suitable).
|
24
|
+
#
|
25
|
+
# If `.logger` is set, the value of `.log_level` is ignored. The decision on
|
26
|
+
# what levels to print is entirely deferred to the logger.
|
27
|
+
class StripeConfiguration
|
28
|
+
attr_accessor :api_key
|
29
|
+
attr_accessor :api_version
|
30
|
+
attr_accessor :client_id
|
31
|
+
attr_accessor :enable_telemetry
|
32
|
+
attr_accessor :logger
|
33
|
+
attr_accessor :stripe_account
|
34
|
+
|
35
|
+
attr_reader :api_base
|
36
|
+
attr_reader :uploads_base
|
37
|
+
attr_reader :connect_base
|
38
|
+
attr_reader :ca_bundle_path
|
39
|
+
attr_reader :log_level
|
40
|
+
attr_reader :initial_network_retry_delay
|
41
|
+
attr_reader :max_network_retries
|
42
|
+
attr_reader :max_network_retry_delay
|
43
|
+
attr_reader :open_timeout
|
44
|
+
attr_reader :read_timeout
|
45
|
+
attr_reader :write_timeout
|
46
|
+
attr_reader :proxy
|
47
|
+
attr_reader :verify_ssl_certs
|
48
|
+
|
49
|
+
def self.setup
|
50
|
+
new.tap do |instance|
|
51
|
+
yield(instance) if block_given?
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# Create a new config based off an existing one. This is useful when the
|
56
|
+
# caller wants to override the global configuration
|
57
|
+
def reverse_duplicate_merge(hash)
|
58
|
+
dup.tap do |instance|
|
59
|
+
hash.each do |option, value|
|
60
|
+
instance.public_send("#{option}=", value)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def initialize
|
66
|
+
@ca_bundle_path = Stripe::DEFAULT_CA_BUNDLE_PATH
|
67
|
+
@enable_telemetry = true
|
68
|
+
@verify_ssl_certs = true
|
69
|
+
|
70
|
+
@max_network_retries = 0
|
71
|
+
@initial_network_retry_delay = 0.5
|
72
|
+
@max_network_retry_delay = 2
|
73
|
+
|
74
|
+
@open_timeout = 30
|
75
|
+
@read_timeout = 80
|
76
|
+
@write_timeout = 30
|
77
|
+
|
78
|
+
@api_base = "https://api.stripe.com"
|
79
|
+
@connect_base = "https://connect.stripe.com"
|
80
|
+
@uploads_base = "https://files.stripe.com"
|
81
|
+
end
|
82
|
+
|
83
|
+
def log_level=(val)
|
84
|
+
# Backwards compatibility for values that we briefly allowed
|
85
|
+
if val == "debug"
|
86
|
+
val = Stripe::LEVEL_DEBUG
|
87
|
+
elsif val == "info"
|
88
|
+
val = Stripe::LEVEL_INFO
|
89
|
+
end
|
90
|
+
|
91
|
+
levels = [Stripe::LEVEL_INFO, Stripe::LEVEL_DEBUG, Stripe::LEVEL_ERROR]
|
92
|
+
|
93
|
+
if !val.nil? && !levels.include?(val)
|
94
|
+
raise ArgumentError,
|
95
|
+
"log_level should only be set to `nil`, `debug` or `info`"
|
96
|
+
end
|
97
|
+
@log_level = val
|
98
|
+
end
|
99
|
+
|
100
|
+
def max_network_retries=(val)
|
101
|
+
@max_network_retries = val.to_i
|
102
|
+
end
|
103
|
+
|
104
|
+
def max_network_retry_delay=(val)
|
105
|
+
@max_network_retry_delay = val.to_i
|
106
|
+
end
|
107
|
+
|
108
|
+
def initial_network_retry_delay=(val)
|
109
|
+
@initial_network_retry_delay = val.to_i
|
110
|
+
end
|
111
|
+
|
112
|
+
def open_timeout=(open_timeout)
|
113
|
+
@open_timeout = open_timeout
|
114
|
+
StripeClient.clear_all_connection_managers(config: self)
|
115
|
+
end
|
116
|
+
|
117
|
+
def read_timeout=(read_timeout)
|
118
|
+
@read_timeout = read_timeout
|
119
|
+
StripeClient.clear_all_connection_managers(config: self)
|
120
|
+
end
|
121
|
+
|
122
|
+
def write_timeout=(write_timeout)
|
123
|
+
unless Net::HTTP.instance_methods.include?(:write_timeout=)
|
124
|
+
raise NotImplementedError
|
125
|
+
end
|
126
|
+
|
127
|
+
@write_timeout = write_timeout
|
128
|
+
StripeClient.clear_all_connection_managers(config: self)
|
129
|
+
end
|
130
|
+
|
131
|
+
def proxy=(proxy)
|
132
|
+
@proxy = proxy
|
133
|
+
StripeClient.clear_all_connection_managers(config: self)
|
134
|
+
end
|
135
|
+
|
136
|
+
def verify_ssl_certs=(verify_ssl_certs)
|
137
|
+
@verify_ssl_certs = verify_ssl_certs
|
138
|
+
StripeClient.clear_all_connection_managers(config: self)
|
139
|
+
end
|
140
|
+
|
141
|
+
def uploads_base=(uploads_base)
|
142
|
+
@uploads_base = uploads_base
|
143
|
+
StripeClient.clear_all_connection_managers(config: self)
|
144
|
+
end
|
145
|
+
|
146
|
+
def connect_base=(connect_base)
|
147
|
+
@connect_base = connect_base
|
148
|
+
StripeClient.clear_all_connection_managers(config: self)
|
149
|
+
end
|
150
|
+
|
151
|
+
def api_base=(api_base)
|
152
|
+
@api_base = api_base
|
153
|
+
StripeClient.clear_all_connection_managers(config: self)
|
154
|
+
end
|
155
|
+
|
156
|
+
def ca_bundle_path=(path)
|
157
|
+
@ca_bundle_path = path
|
158
|
+
|
159
|
+
# empty this field so a new store is initialized
|
160
|
+
@ca_store = nil
|
161
|
+
|
162
|
+
StripeClient.clear_all_connection_managers(config: self)
|
163
|
+
end
|
164
|
+
|
165
|
+
# A certificate store initialized from the the bundle in #ca_bundle_path and
|
166
|
+
# which is used to validate TLS on every request.
|
167
|
+
#
|
168
|
+
# This was added to the give the gem "pseudo thread safety" in that it seems
|
169
|
+
# when initiating many parallel requests marshaling the certificate store is
|
170
|
+
# the most likely point of failure (see issue #382). Any program attempting
|
171
|
+
# to leverage this pseudo safety should make a call to this method (i.e.
|
172
|
+
# `Stripe.ca_store`) in their initialization code because it marshals lazily
|
173
|
+
# and is itself not thread safe.
|
174
|
+
def ca_store
|
175
|
+
@ca_store ||= begin
|
176
|
+
store = OpenSSL::X509::Store.new
|
177
|
+
store.add_file(ca_bundle_path)
|
178
|
+
store
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
def enable_telemetry?
|
183
|
+
enable_telemetry
|
184
|
+
end
|
185
|
+
|
186
|
+
# Generates a deterministic key to identify configuration objects with
|
187
|
+
# identical configuration values.
|
188
|
+
def key
|
189
|
+
instance_variables
|
190
|
+
.collect { |variable| instance_variable_get(variable) }
|
191
|
+
.join
|
192
|
+
end
|
193
|
+
end
|
194
|
+
end
|
data/lib/stripe/stripe_object.rb
CHANGED
@@ -135,7 +135,8 @@ module Stripe
|
|
135
135
|
# ==== Attributes
|
136
136
|
#
|
137
137
|
# * +values+ - Hash of values to use to update the current attributes of
|
138
|
-
# the object.
|
138
|
+
# the object. If you are on ruby 2.7 or higher make sure to wrap in curly
|
139
|
+
# braces to be ruby 3 compatible.
|
139
140
|
# * +opts+ - Options for +StripeObject+ like an API key that will be reused
|
140
141
|
# on subsequent API calls.
|
141
142
|
#
|
@@ -266,6 +267,27 @@ module Stripe
|
|
266
267
|
[]
|
267
268
|
end
|
268
269
|
|
270
|
+
# When designing APIs, we now make a conscious effort server-side to avoid
|
271
|
+
# naming fields after important built-ins in various languages (e.g. class,
|
272
|
+
# method, etc.).
|
273
|
+
#
|
274
|
+
# However, a long time ago we made the mistake (either consciously or by
|
275
|
+
# accident) of initializing our `metadata` fields as instances of
|
276
|
+
# `StripeObject`, and metadata can have a wide range of different keys
|
277
|
+
# defined in it. This is somewhat a convenient in that it allows users to
|
278
|
+
# access data like `obj.metadata.my_field`, but is almost certainly not
|
279
|
+
# worth the cost.
|
280
|
+
#
|
281
|
+
# Naming metadata fields bad things like `class` causes `initialize_from`
|
282
|
+
# to produce strange results, so we ban known offenders here.
|
283
|
+
#
|
284
|
+
# In a future major version we should consider leaving `metadata` as a hash
|
285
|
+
# and forcing people to access it with `obj.metadata[:my_field]` because
|
286
|
+
# the potential for trouble is just too high. For now, reserve names.
|
287
|
+
RESERVED_FIELD_NAMES = [
|
288
|
+
:class,
|
289
|
+
].freeze
|
290
|
+
|
269
291
|
protected def metaclass
|
270
292
|
class << self; self; end
|
271
293
|
end
|
@@ -276,6 +298,7 @@ module Stripe
|
|
276
298
|
|
277
299
|
metaclass.instance_eval do
|
278
300
|
keys.each do |k|
|
301
|
+
next if RESERVED_FIELD_NAMES.include?(k)
|
279
302
|
next if protected_fields.include?(k)
|
280
303
|
next if @@permanent_attributes.include?(k)
|
281
304
|
|
@@ -311,6 +334,7 @@ module Stripe
|
|
311
334
|
|
312
335
|
metaclass.instance_eval do
|
313
336
|
keys.each do |k|
|
337
|
+
next if RESERVED_FIELD_NAMES.include?(k)
|
314
338
|
next if protected_fields.include?(k)
|
315
339
|
next if @@permanent_attributes.include?(k)
|
316
340
|
|
@@ -374,7 +398,7 @@ module Stripe
|
|
374
398
|
begin
|
375
399
|
super
|
376
400
|
rescue NoMethodError => e
|
377
|
-
# If we notice the accessed name
|
401
|
+
# If we notice the accessed name of our set of transient values we can
|
378
402
|
# give the user a slightly more helpful error message. If not, just
|
379
403
|
# raise right away.
|
380
404
|
raise unless @transient_values.include?(name)
|
@@ -1,63 +1,54 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Stripe
|
4
|
-
#
|
5
|
-
# the
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
#
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
def self.from_net_http(resp)
|
14
|
-
new(resp.to_hash)
|
15
|
-
end
|
4
|
+
# Headers provides an access wrapper to an API response's header data. It
|
5
|
+
# mainly exists so that we don't need to expose the entire
|
6
|
+
# `Net::HTTPResponse` object while still getting some of its benefits like
|
7
|
+
# case-insensitive access to header names and flattening of header values.
|
8
|
+
class StripeResponseHeaders
|
9
|
+
# Initializes a Headers object from a Net::HTTP::HTTPResponse object.
|
10
|
+
def self.from_net_http(resp)
|
11
|
+
new(resp.to_hash)
|
12
|
+
end
|
16
13
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
14
|
+
# `hash` is expected to be a hash mapping header names to arrays of
|
15
|
+
# header values. This is the default format generated by calling
|
16
|
+
# `#to_hash` on a `Net::HTTPResponse` object because headers can be
|
17
|
+
# repeated multiple times. Using `#[]` will collapse values down to just
|
18
|
+
# the first.
|
19
|
+
def initialize(hash)
|
20
|
+
if !hash.is_a?(Hash) ||
|
21
|
+
!hash.keys.all? { |n| n.is_a?(String) } ||
|
22
|
+
!hash.values.all? { |a| a.is_a?(Array) } ||
|
23
|
+
!hash.values.all? { |a| a.all? { |v| v.is_a?(String) } }
|
24
|
+
raise ArgumentError,
|
25
|
+
"expect hash to be a map of string header names to arrays of " \
|
26
|
+
"header values"
|
27
|
+
end
|
31
28
|
|
32
|
-
|
29
|
+
@hash = {}
|
33
30
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
end
|
31
|
+
# This shouldn't be strictly necessary because `Net::HTTPResponse` will
|
32
|
+
# produce a hash with all headers downcased, but do it anyway just in
|
33
|
+
# case an object of this class was constructed manually.
|
34
|
+
#
|
35
|
+
# Also has the effect of duplicating the hash, which is desirable for a
|
36
|
+
# little extra object safety.
|
37
|
+
hash.each do |k, v|
|
38
|
+
@hash[k.downcase] = v
|
43
39
|
end
|
40
|
+
end
|
44
41
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
end
|
50
|
-
values ? values.first : nil
|
42
|
+
def [](name)
|
43
|
+
values = @hash[name.downcase]
|
44
|
+
if values && values.count > 1
|
45
|
+
warn("Duplicate header values for `#{name}`; returning only first")
|
51
46
|
end
|
47
|
+
values ? values.first : nil
|
52
48
|
end
|
49
|
+
end
|
53
50
|
|
54
|
-
|
55
|
-
# JSON.
|
56
|
-
attr_accessor :data
|
57
|
-
|
58
|
-
# The raw HTTP body of the response.
|
59
|
-
attr_accessor :http_body
|
60
|
-
|
51
|
+
module StripeResponseBase
|
61
52
|
# A Hash of the HTTP headers of the response.
|
62
53
|
attr_accessor :http_headers
|
63
54
|
|
@@ -67,15 +58,52 @@ module Stripe
|
|
67
58
|
# The Stripe request ID of the response.
|
68
59
|
attr_accessor :request_id
|
69
60
|
|
61
|
+
def self.populate_for_net_http(resp, http_resp)
|
62
|
+
resp.http_headers = StripeResponseHeaders.from_net_http(http_resp)
|
63
|
+
resp.http_status = http_resp.code.to_i
|
64
|
+
resp.request_id = http_resp["request-id"]
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# StripeResponse encapsulates some vitals of a response that came back from
|
69
|
+
# the Stripe API.
|
70
|
+
class StripeResponse
|
71
|
+
include StripeResponseBase
|
72
|
+
# The data contained by the HTTP body of the response deserialized from
|
73
|
+
# JSON.
|
74
|
+
attr_accessor :data
|
75
|
+
|
76
|
+
# The raw HTTP body of the response.
|
77
|
+
attr_accessor :http_body
|
78
|
+
|
70
79
|
# Initializes a StripeResponse object from a Net::HTTP::HTTPResponse
|
71
80
|
# object.
|
72
81
|
def self.from_net_http(http_resp)
|
73
82
|
resp = StripeResponse.new
|
74
83
|
resp.data = JSON.parse(http_resp.body, symbolize_names: true)
|
75
84
|
resp.http_body = http_resp.body
|
76
|
-
resp
|
77
|
-
resp
|
78
|
-
|
85
|
+
StripeResponseBase.populate_for_net_http(resp, http_resp)
|
86
|
+
resp
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
# We have to alias StripeResponseHeaders to StripeResponse::Headers, as this
|
91
|
+
# class used to be embedded within StripeResponse and we want to be backwards
|
92
|
+
# compatible.
|
93
|
+
StripeResponse::Headers = StripeResponseHeaders
|
94
|
+
|
95
|
+
# StripeHeadersOnlyResponse includes only header-related vitals of the
|
96
|
+
# response. This is used for streaming requests where the response was read
|
97
|
+
# directly in a block and we explicitly don't want to store the body of the
|
98
|
+
# response in memory.
|
99
|
+
class StripeHeadersOnlyResponse
|
100
|
+
include StripeResponseBase
|
101
|
+
|
102
|
+
# Initializes a StripeHeadersOnlyResponse object from a
|
103
|
+
# Net::HTTP::HTTPResponse object.
|
104
|
+
def self.from_net_http(http_resp)
|
105
|
+
resp = StripeHeadersOnlyResponse.new
|
106
|
+
StripeResponseBase.populate_for_net_http(resp, http_resp)
|
79
107
|
resp
|
80
108
|
end
|
81
109
|
end
|
data/lib/stripe/util.rb
CHANGED
@@ -47,6 +47,53 @@ module Stripe
|
|
47
47
|
Util.object_classes[object_name] == klass
|
48
48
|
end
|
49
49
|
|
50
|
+
# Adds a custom method to a resource class. This is used to add support for
|
51
|
+
# non-CRUDL API requests, e.g. capturing charges. custom_method takes the
|
52
|
+
# following parameters:
|
53
|
+
# - name: the name of the custom method to create (as a symbol)
|
54
|
+
# - http_verb: the HTTP verb for the API request (:get, :post, or :delete)
|
55
|
+
# - http_path: the path to append to the resource's URL. If not provided,
|
56
|
+
# the name is used as the path
|
57
|
+
# - resource: the resource implementation class
|
58
|
+
# - target: the class that custom static method will be added to
|
59
|
+
#
|
60
|
+
# For example, this call:
|
61
|
+
# custom_method :capture, http_verb: post
|
62
|
+
# adds a `capture` class method to the resource class that, when called,
|
63
|
+
# will send a POST request to `/v1/<object_name>/capture`.
|
64
|
+
def self.custom_method(resource, target, name, http_verb, http_path)
|
65
|
+
unless %i[get post delete].include?(http_verb)
|
66
|
+
raise ArgumentError,
|
67
|
+
"Invalid http_verb value: #{http_verb.inspect}. Should be one " \
|
68
|
+
"of :get, :post or :delete."
|
69
|
+
end
|
70
|
+
unless target.respond_to?(:resource_url)
|
71
|
+
raise ArgumentError,
|
72
|
+
"Invalid target value: #{target}. Target class should have a " \
|
73
|
+
"`resource_url` method."
|
74
|
+
end
|
75
|
+
http_path ||= name.to_s
|
76
|
+
target.define_singleton_method(name) do |id, params = {}, opts = {}|
|
77
|
+
unless id.is_a?(String)
|
78
|
+
raise ArgumentError,
|
79
|
+
"id should be a string representing the ID of an API resource"
|
80
|
+
end
|
81
|
+
|
82
|
+
url = "#{target.resource_url}/"\
|
83
|
+
"#{CGI.escape(id)}/"\
|
84
|
+
"#{CGI.escape(http_path)}"
|
85
|
+
|
86
|
+
resp, opts = resource.execute_resource_request(
|
87
|
+
http_verb,
|
88
|
+
url,
|
89
|
+
params,
|
90
|
+
opts
|
91
|
+
)
|
92
|
+
|
93
|
+
Util.convert_to_stripe_object(resp.data, opts)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
50
97
|
# Converts a hash of fields or an array of hashes into a +StripeObject+ or
|
51
98
|
# array of +StripeObject+s. These new objects will be created as a concrete
|
52
99
|
# type as dictated by their `object` field (e.g. an `object` value of
|
@@ -76,24 +123,30 @@ module Stripe
|
|
76
123
|
end
|
77
124
|
|
78
125
|
def self.log_error(message, data = {})
|
79
|
-
|
80
|
-
|
126
|
+
config = data.delete(:config) || Stripe.config
|
127
|
+
logger = config.logger || Stripe.logger
|
128
|
+
if !logger.nil? ||
|
129
|
+
!config.log_level.nil? && config.log_level <= Stripe::LEVEL_ERROR
|
81
130
|
log_internal(message, data, color: :cyan, level: Stripe::LEVEL_ERROR,
|
82
131
|
logger: Stripe.logger, out: $stderr)
|
83
132
|
end
|
84
133
|
end
|
85
134
|
|
86
135
|
def self.log_info(message, data = {})
|
87
|
-
|
88
|
-
|
136
|
+
config = data.delete(:config) || Stripe.config
|
137
|
+
logger = config.logger || Stripe.logger
|
138
|
+
if !logger.nil? ||
|
139
|
+
!config.log_level.nil? && config.log_level <= Stripe::LEVEL_INFO
|
89
140
|
log_internal(message, data, color: :cyan, level: Stripe::LEVEL_INFO,
|
90
141
|
logger: Stripe.logger, out: $stdout)
|
91
142
|
end
|
92
143
|
end
|
93
144
|
|
94
145
|
def self.log_debug(message, data = {})
|
95
|
-
|
96
|
-
|
146
|
+
config = data.delete(:config) || Stripe.config
|
147
|
+
logger = config.logger || Stripe.logger
|
148
|
+
if !logger.nil? ||
|
149
|
+
!config.log_level.nil? && config.log_level <= Stripe::LEVEL_DEBUG
|
97
150
|
log_internal(message, data, color: :blue, level: Stripe::LEVEL_DEBUG,
|
98
151
|
logger: Stripe.logger, out: $stdout)
|
99
152
|
end
|
@@ -172,6 +225,18 @@ module Stripe
|
|
172
225
|
result
|
173
226
|
end
|
174
227
|
|
228
|
+
# `Time.now` can be unstable in cases like an administrator manually
|
229
|
+
# updating its value or a reconcilation via NTP. For this reason, prefer
|
230
|
+
# the use of the system's monotonic clock especially where comparing times
|
231
|
+
# to calculate an elapsed duration.
|
232
|
+
#
|
233
|
+
# Shortcut for getting monotonic time, mostly for purposes of line length
|
234
|
+
# and test stubbing. Returns time in seconds since the event used for
|
235
|
+
# monotonic reference purposes by the platform (e.g. system boot time).
|
236
|
+
def self.monotonic_time
|
237
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
238
|
+
end
|
239
|
+
|
175
240
|
def self.normalize_id(id)
|
176
241
|
if id.is_a?(Hash) # overloaded id
|
177
242
|
params_hash = id.dup
|
@@ -190,7 +255,9 @@ module Stripe
|
|
190
255
|
{ api_key: opts }
|
191
256
|
when Hash
|
192
257
|
check_api_key!(opts.fetch(:api_key)) if opts.key?(:api_key)
|
193
|
-
|
258
|
+
# Explicitly use dup here instead of clone to avoid preserving freeze
|
259
|
+
# state on input params.
|
260
|
+
opts.dup
|
194
261
|
else
|
195
262
|
raise TypeError, "normalize_opts expects a string or a hash"
|
196
263
|
end
|
data/lib/stripe/version.rb
CHANGED
data/lib/stripe/webhook.rb
CHANGED
@@ -24,10 +24,37 @@ module Stripe
|
|
24
24
|
module Signature
|
25
25
|
EXPECTED_SCHEME = "v1"
|
26
26
|
|
27
|
-
|
28
|
-
|
27
|
+
# Computes a webhook signature given a time (probably the current time),
|
28
|
+
# a payload, and a signing secret.
|
29
|
+
def self.compute_signature(timestamp, payload, secret)
|
30
|
+
raise ArgumentError, "timestamp should be an instance of Time" \
|
31
|
+
unless timestamp.is_a?(Time)
|
32
|
+
raise ArgumentError, "payload should be a string" \
|
33
|
+
unless payload.is_a?(String)
|
34
|
+
raise ArgumentError, "secret should be a string" \
|
35
|
+
unless secret.is_a?(String)
|
36
|
+
|
37
|
+
timestamped_payload = "#{timestamp.to_i}.#{payload}"
|
38
|
+
OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha256"), secret,
|
39
|
+
timestamped_payload)
|
40
|
+
end
|
41
|
+
|
42
|
+
# Generates a value that would be added to a `Stripe-Signature` for a
|
43
|
+
# given webhook payload.
|
44
|
+
#
|
45
|
+
# Note that this isn't needed to verify webhooks in any way, and is
|
46
|
+
# mainly here for use in test cases (those that are both within this
|
47
|
+
# project and without).
|
48
|
+
def self.generate_header(timestamp, signature, scheme: EXPECTED_SCHEME)
|
49
|
+
raise ArgumentError, "timestamp should be an instance of Time" \
|
50
|
+
unless timestamp.is_a?(Time)
|
51
|
+
raise ArgumentError, "signature should be a string" \
|
52
|
+
unless signature.is_a?(String)
|
53
|
+
raise ArgumentError, "scheme should be a string" \
|
54
|
+
unless scheme.is_a?(String)
|
55
|
+
|
56
|
+
"t=#{timestamp.to_i},#{scheme}=#{signature}"
|
29
57
|
end
|
30
|
-
private_class_method :compute_signature
|
31
58
|
|
32
59
|
# Extracts the timestamp and the signature(s) with the desired scheme
|
33
60
|
# from the header
|
@@ -35,7 +62,7 @@ module Stripe
|
|
35
62
|
list_items = header.split(/,\s*/).map { |i| i.split("=", 2) }
|
36
63
|
timestamp = Integer(list_items.select { |i| i[0] == "t" }[0][1])
|
37
64
|
signatures = list_items.select { |i| i[0] == scheme }.map { |i| i[1] }
|
38
|
-
[timestamp, signatures]
|
65
|
+
[Time.at(timestamp), signatures]
|
39
66
|
end
|
40
67
|
private_class_method :get_timestamp_and_signatures
|
41
68
|
|
@@ -53,6 +80,11 @@ module Stripe
|
|
53
80
|
begin
|
54
81
|
timestamp, signatures =
|
55
82
|
get_timestamp_and_signatures(header, EXPECTED_SCHEME)
|
83
|
+
|
84
|
+
# TODO: Try to knock over this blanket rescue as it can unintentionally
|
85
|
+
# swallow many valid errors. Instead, try to validate an incoming
|
86
|
+
# header one piece at a time, and error with a known exception class if
|
87
|
+
# any part is found to be invalid. Rescue that class here.
|
56
88
|
rescue StandardError
|
57
89
|
raise SignatureVerificationError.new(
|
58
90
|
"Unable to extract timestamp and signatures from header",
|
@@ -67,8 +99,7 @@ module Stripe
|
|
67
99
|
)
|
68
100
|
end
|
69
101
|
|
70
|
-
|
71
|
-
expected_sig = compute_signature(signed_payload, secret)
|
102
|
+
expected_sig = compute_signature(timestamp, payload, secret)
|
72
103
|
unless signatures.any? { |s| Util.secure_compare(expected_sig, s) }
|
73
104
|
raise SignatureVerificationError.new(
|
74
105
|
"No signatures found matching the expected signature for payload",
|
@@ -76,7 +107,7 @@ module Stripe
|
|
76
107
|
)
|
77
108
|
end
|
78
109
|
|
79
|
-
if tolerance && timestamp < Time.now
|
110
|
+
if tolerance && timestamp < Time.now - tolerance
|
80
111
|
raise SignatureVerificationError.new(
|
81
112
|
"Timestamp outside the tolerance zone (#{Time.at(timestamp)})",
|
82
113
|
header, http_body: payload
|