stripe 4.9.0 → 5.28.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.editorconfig +10 -0
- data/.rubocop.yml +56 -8
- data/.rubocop_todo.yml +8 -25
- data/.travis.yml +4 -8
- data/.vscode/extensions.json +7 -0
- data/.vscode/settings.json +8 -0
- data/CHANGELOG.md +206 -2
- data/CODE_OF_CONDUCT.md +77 -0
- data/Gemfile +10 -13
- data/README.md +178 -58
- data/Rakefile +8 -7
- data/VERSION +1 -1
- data/lib/stripe.rb +41 -195
- data/lib/stripe/api_operations/create.rb +1 -1
- data/lib/stripe/api_operations/delete.rb +28 -2
- data/lib/stripe/api_operations/list.rb +1 -12
- data/lib/stripe/api_operations/nested_resource.rb +38 -28
- data/lib/stripe/api_operations/request.rb +47 -8
- data/lib/stripe/api_operations/save.rb +8 -7
- data/lib/stripe/api_resource.rb +52 -3
- data/lib/stripe/connection_manager.rb +162 -0
- data/lib/stripe/error_object.rb +94 -0
- data/lib/stripe/errors.rb +31 -10
- data/lib/stripe/instrumentation.rb +82 -0
- data/lib/stripe/list_object.rb +41 -7
- data/lib/stripe/multipart_encoder.rb +131 -0
- data/lib/stripe/oauth.rb +4 -3
- data/lib/stripe/object_types.rb +101 -0
- data/lib/stripe/resources.rb +84 -0
- data/lib/stripe/{account.rb → resources/account.rb} +50 -27
- data/lib/stripe/{account_link.rb → resources/account_link.rb} +2 -1
- data/lib/stripe/resources/alipay_account.rb +34 -0
- data/lib/stripe/{apple_pay_domain.rb → resources/apple_pay_domain.rb} +2 -1
- data/lib/stripe/resources/application_fee.rb +14 -0
- data/lib/stripe/resources/application_fee_refund.rb +30 -0
- data/lib/stripe/{balance.rb → resources/balance.rb} +2 -1
- data/lib/stripe/{balance_transaction.rb → resources/balance_transaction.rb} +2 -5
- data/lib/stripe/resources/bank_account.rb +43 -0
- data/lib/stripe/resources/billing_portal/session.rb +12 -0
- data/lib/stripe/{bitcoin_receiver.rb → resources/bitcoin_receiver.rb} +4 -3
- data/lib/stripe/{bitcoin_transaction.rb → resources/bitcoin_transaction.rb} +1 -1
- data/lib/stripe/resources/capability.rb +33 -0
- data/lib/stripe/{card.rb → resources/card.rb} +13 -4
- data/lib/stripe/resources/charge.rb +23 -0
- data/lib/stripe/resources/checkout/session.rb +16 -0
- data/lib/stripe/{country_spec.rb → resources/country_spec.rb} +2 -1
- data/lib/stripe/{coupon.rb → resources/coupon.rb} +3 -2
- data/lib/stripe/resources/credit_note.rb +33 -0
- data/lib/stripe/resources/credit_note_line_item.rb +7 -0
- data/lib/stripe/resources/customer.rb +36 -0
- data/lib/stripe/resources/customer_balance_transaction.rb +30 -0
- data/lib/stripe/resources/discount.rb +7 -0
- data/lib/stripe/resources/dispute.rb +22 -0
- data/lib/stripe/{ephemeral_key.rb → resources/ephemeral_key.rb} +6 -2
- data/lib/stripe/{event.rb → resources/event.rb} +2 -1
- data/lib/stripe/{exchange_rate.rb → resources/exchange_rate.rb} +2 -1
- data/lib/stripe/{file.rb → resources/file.rb} +9 -11
- data/lib/stripe/{file_link.rb → resources/file_link.rb} +3 -2
- data/lib/stripe/resources/invoice.rb +74 -0
- data/lib/stripe/{invoice_item.rb → resources/invoice_item.rb} +3 -2
- data/lib/stripe/{invoice_line_item.rb → resources/invoice_line_item.rb} +1 -1
- data/lib/stripe/resources/issuing/authorization.rb +34 -0
- data/lib/stripe/resources/issuing/card.rb +25 -0
- data/lib/stripe/{issuing → resources/issuing}/card_details.rb +1 -1
- data/lib/stripe/{issuing → resources/issuing}/cardholder.rb +3 -2
- data/lib/stripe/resources/issuing/dispute.rb +25 -0
- data/lib/stripe/{issuing → resources/issuing}/transaction.rb +3 -2
- data/lib/stripe/resources/line_item.rb +7 -0
- data/lib/stripe/resources/login_link.rb +14 -0
- data/lib/stripe/resources/mandate.rb +8 -0
- data/lib/stripe/resources/order.rb +33 -0
- data/lib/stripe/{order_return.rb → resources/order_return.rb} +2 -1
- data/lib/stripe/resources/payment_intent.rb +43 -0
- data/lib/stripe/resources/payment_method.rb +33 -0
- data/lib/stripe/resources/payout.rb +33 -0
- data/lib/stripe/{person.rb → resources/person.rb} +8 -3
- data/lib/stripe/{plan.rb → resources/plan.rb} +2 -1
- data/lib/stripe/resources/price.rb +12 -0
- data/lib/stripe/{product.rb → resources/product.rb} +4 -3
- data/lib/stripe/resources/promotion_code.rb +12 -0
- data/lib/stripe/resources/radar/early_fraud_warning.rb +12 -0
- data/lib/stripe/{radar → resources/radar}/value_list.rb +3 -2
- data/lib/stripe/{radar → resources/radar}/value_list_item.rb +3 -2
- data/lib/stripe/{recipient.rb → resources/recipient.rb} +3 -6
- data/lib/stripe/{recipient_transfer.rb → resources/recipient_transfer.rb} +1 -1
- data/lib/stripe/{refund.rb → resources/refund.rb} +2 -1
- data/lib/stripe/{reporting → resources/reporting}/report_run.rb +3 -2
- data/lib/stripe/{reporting → resources/reporting}/report_type.rb +3 -2
- data/lib/stripe/resources/reversal.rb +29 -0
- data/lib/stripe/resources/review.rb +21 -0
- data/lib/stripe/resources/setup_attempt.rb +10 -0
- data/lib/stripe/resources/setup_intent.rb +33 -0
- data/lib/stripe/{sigma → resources/sigma}/scheduled_query_run.rb +3 -2
- data/lib/stripe/{sku.rb → resources/sku.rb} +4 -3
- data/lib/stripe/resources/source.rb +47 -0
- data/lib/stripe/{source_transaction.rb → resources/source_transaction.rb} +1 -1
- data/lib/stripe/resources/subscription.rb +26 -0
- data/lib/stripe/resources/subscription_item.rb +26 -0
- data/lib/stripe/resources/subscription_schedule.rb +33 -0
- data/lib/stripe/resources/tax_id.rb +26 -0
- data/lib/stripe/resources/tax_rate.rb +12 -0
- data/lib/stripe/resources/terminal/connection_token.rb +12 -0
- data/lib/stripe/{terminal → resources/terminal}/location.rb +4 -2
- data/lib/stripe/{terminal → resources/terminal}/reader.rb +4 -2
- data/lib/stripe/{three_d_secure.rb → resources/three_d_secure.rb} +2 -1
- data/lib/stripe/{token.rb → resources/token.rb} +2 -1
- data/lib/stripe/resources/topup.rb +23 -0
- data/lib/stripe/resources/transfer.rb +27 -0
- data/lib/stripe/resources/usage_record.rb +7 -0
- data/lib/stripe/{usage_record_summary.rb → resources/usage_record_summary.rb} +1 -1
- data/lib/stripe/{webhook_endpoint.rb → resources/webhook_endpoint.rb} +3 -2
- data/lib/stripe/singleton_api_resource.rb +3 -1
- data/lib/stripe/stripe_client.rb +493 -236
- data/lib/stripe/stripe_configuration.rb +178 -0
- data/lib/stripe/stripe_object.rb +75 -61
- data/lib/stripe/stripe_response.rb +53 -21
- data/lib/stripe/util.rb +66 -109
- data/lib/stripe/version.rb +1 -1
- data/lib/stripe/webhook.rb +43 -10
- data/stripe.gemspec +14 -5
- data/test/stripe/account_link_test.rb +4 -4
- data/test/stripe/account_test.rb +193 -32
- data/test/stripe/alipay_account_test.rb +1 -1
- data/test/stripe/api_operations_test.rb +3 -4
- data/test/stripe/api_resource_test.rb +159 -37
- data/test/stripe/apple_pay_domain_test.rb +18 -5
- data/test/stripe/application_fee_refund_test.rb +1 -1
- data/test/stripe/application_fee_test.rb +45 -1
- data/test/stripe/balance_test.rb +1 -1
- data/test/stripe/balance_transaction_test.rb +20 -0
- data/test/stripe/bank_account_test.rb +1 -1
- data/test/stripe/billing_portal/session_test.rb +18 -0
- data/test/stripe/capability_test.rb +45 -0
- data/test/stripe/charge_test.rb +13 -8
- data/test/stripe/checkout/session_test.rb +19 -1
- data/test/stripe/connection_manager_test.rb +167 -0
- data/test/stripe/country_spec_test.rb +1 -1
- data/test/stripe/coupon_test.rb +16 -6
- data/test/stripe/credit_note_test.rb +90 -0
- data/test/stripe/customer_balance_transaction_test.rb +37 -0
- data/test/stripe/customer_card_test.rb +7 -1
- data/test/stripe/customer_test.rb +151 -40
- data/test/stripe/dispute_test.rb +10 -1
- data/test/stripe/ephemeral_key_test.rb +8 -1
- data/test/stripe/errors_test.rb +42 -9
- data/test/stripe/exchange_rate_test.rb +1 -1
- data/test/stripe/file_link_test.rb +1 -1
- data/test/stripe/file_test.rb +19 -5
- data/test/stripe/instrumentation_test.rb +74 -0
- data/test/stripe/invoice_item_test.rb +18 -7
- data/test/stripe/invoice_line_item_test.rb +1 -1
- data/test/stripe/invoice_test.rb +77 -9
- data/test/stripe/issuing/authorization_test.rb +33 -11
- data/test/stripe/issuing/card_test.rb +27 -6
- data/test/stripe/issuing/cardholder_test.rb +1 -1
- data/test/stripe/issuing/dispute_test.rb +23 -14
- data/test/stripe/issuing/transaction_test.rb +1 -1
- data/test/stripe/list_object_test.rb +70 -24
- data/test/stripe/login_link_test.rb +2 -2
- data/test/stripe/mandate_test.rb +14 -0
- data/test/stripe/multipart_encoder_test.rb +130 -0
- data/test/stripe/oauth_test.rb +17 -1
- data/test/stripe/order_return_test.rb +1 -1
- data/test/stripe/order_test.rb +28 -3
- data/test/stripe/payment_intent_test.rb +31 -4
- data/test/stripe/payment_method_test.rb +84 -0
- data/test/stripe/payout_test.rb +23 -1
- data/test/stripe/person_test.rb +1 -1
- data/test/stripe/plan_test.rb +26 -20
- data/test/stripe/price_test.rb +48 -0
- data/test/stripe/product_test.rb +17 -8
- data/test/stripe/promotion_code_test.rb +42 -0
- data/test/stripe/radar/early_fraud_warning_test.rb +22 -0
- data/test/stripe/radar/value_list_item_test.rb +16 -6
- data/test/stripe/radar/value_list_test.rb +16 -6
- data/test/stripe/recipient_test.rb +18 -5
- data/test/stripe/refund_test.rb +1 -1
- data/test/stripe/reporting/report_run_test.rb +1 -1
- data/test/stripe/reporting/report_type_test.rb +1 -1
- data/test/stripe/reversal_test.rb +1 -1
- data/test/stripe/review_test.rb +1 -1
- data/test/stripe/setup_attempt_test.rb +16 -0
- data/test/stripe/setup_intent_test.rb +84 -0
- data/test/stripe/sigma/scheduled_query_run_test.rb +1 -1
- data/test/stripe/sku_test.rb +16 -6
- data/test/stripe/source_test.rb +52 -19
- data/test/stripe/stripe_client_test.rb +546 -78
- data/test/stripe/stripe_configuration_test.rb +131 -0
- data/test/stripe/stripe_object_test.rb +16 -41
- data/test/stripe/stripe_response_test.rb +71 -25
- data/test/stripe/subscription_item_test.rb +38 -7
- data/test/stripe/subscription_schedule_test.rb +19 -1
- data/test/stripe/subscription_test.rb +29 -9
- data/test/stripe/tax_id_test.rb +31 -0
- data/test/stripe/tax_rate_test.rb +43 -0
- data/test/stripe/terminal/connection_token_test.rb +1 -1
- data/test/stripe/terminal/location_test.rb +18 -1
- data/test/stripe/terminal/reader_test.rb +18 -1
- data/test/stripe/three_d_secure_test.rb +1 -1
- data/test/stripe/topup_test.rb +9 -1
- data/test/stripe/transfer_test.rb +46 -1
- data/test/stripe/usage_record_summary_test.rb +15 -5
- data/test/stripe/util_test.rb +1 -1
- data/test/stripe/webhook_endpoint_test.rb +18 -1
- data/test/stripe/webhook_test.rb +48 -9
- data/test/stripe_mock.rb +4 -3
- data/test/stripe_test.rb +102 -33
- data/test/test_helper.rb +16 -12
- metadata +141 -129
- data/lib/stripe/alipay_account.rb +0 -27
- data/lib/stripe/application_fee.rb +0 -23
- data/lib/stripe/application_fee_refund.rb +0 -22
- data/lib/stripe/bank_account.rb +0 -32
- data/lib/stripe/charge.rb +0 -84
- data/lib/stripe/checkout/session.rb +0 -11
- data/lib/stripe/customer.rb +0 -90
- data/lib/stripe/dispute.rb +0 -19
- data/lib/stripe/invoice.rb +0 -48
- data/lib/stripe/issuer_fraud_record.rb +0 -9
- data/lib/stripe/issuing/authorization.rb +0 -22
- data/lib/stripe/issuing/card.rb +0 -18
- data/lib/stripe/issuing/dispute.rb +0 -13
- data/lib/stripe/login_link.rb +0 -11
- data/lib/stripe/order.rb +0 -31
- data/lib/stripe/payment_intent.rb +0 -26
- data/lib/stripe/payout.rb +0 -20
- data/lib/stripe/reversal.rb +0 -22
- data/lib/stripe/review.rb +0 -14
- data/lib/stripe/source.rb +0 -38
- data/lib/stripe/subscription.rb +0 -25
- data/lib/stripe/subscription_item.rb +0 -17
- data/lib/stripe/subscription_schedule.rb +0 -32
- data/lib/stripe/subscription_schedule_revision.rb +0 -25
- data/lib/stripe/terminal/connection_token.rb +0 -11
- data/lib/stripe/topup.rb +0 -16
- data/lib/stripe/transfer.rb +0 -23
- data/lib/stripe/usage_record.rb +0 -14
- data/test/api_stub_helpers.rb +0 -1
- data/test/stripe/account_external_accounts_operations_test.rb +0 -69
- data/test/stripe/account_login_links_operations_test.rb +0 -21
- data/test/stripe/account_persons_operations_test.rb +0 -70
- data/test/stripe/application_fee_refunds_operations_test.rb +0 -56
- data/test/stripe/customer_sources_operations_test.rb +0 -64
- data/test/stripe/file_upload_test.rb +0 -76
- data/test/stripe/issuer_fraud_record_test.rb +0 -20
- data/test/stripe/source_transaction_test.rb +0 -19
- data/test/stripe/subscription_schedule_revision_test.rb +0 -37
- data/test/stripe/subscription_schedule_revisions_operations_test.rb +0 -35
- data/test/stripe/transfer_reversals_operations_test.rb +0 -57
- 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
|
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
|
-
|
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
|
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
|
-
|
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.
|
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
|
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 =
|
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
|
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 =
|
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
|
data/lib/stripe/api_resource.rb
CHANGED
@@ -17,7 +17,9 @@ module Stripe
|
|
17
17
|
|
18
18
|
def self.resource_url
|
19
19
|
if self == APIResource
|
20
|
-
raise NotImplementedError,
|
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(
|
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 =
|
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
|