stripe 4.17.0 → 5.32.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 +193 -1
- data/CODE_OF_CONDUCT.md +77 -0
- data/Gemfile +10 -14
- data/README.md +167 -63
- data/Rakefile +8 -7
- 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 -12
- data/lib/stripe/api_operations/nested_resource.rb +36 -27
- data/lib/stripe/api_operations/request.rb +47 -8
- data/lib/stripe/api_operations/save.rb +8 -7
- data/lib/stripe/api_resource.rb +30 -5
- data/lib/stripe/connection_manager.rb +164 -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 +8 -6
- data/lib/stripe/object_types.rb +102 -0
- data/lib/stripe/{account.rb → resources/account.rb} +44 -33
- 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/{issuing/dispute.rb → resources/billing_portal/configuration.rb} +4 -3
- data/lib/stripe/{checkout → resources/billing_portal}/session.rb +3 -2
- 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/{capability.rb → resources/capability.rb} +11 -4
- data/lib/stripe/{card.rb → resources/card.rb} +12 -3
- 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} +2 -1
- 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 +41 -0
- data/lib/stripe/resources/customer_balance_transaction.rb +30 -0
- data/lib/stripe/{discount.rb → resources/discount.rb} +1 -1
- 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} +7 -12
- data/lib/stripe/{file_link.rb → resources/file_link.rb} +2 -1
- data/lib/stripe/resources/invoice.rb +74 -0
- data/lib/stripe/{invoice_item.rb → resources/invoice_item.rb} +2 -1
- data/lib/stripe/{invoice_line_item.rb → resources/invoice_line_item.rb} +1 -1
- data/lib/stripe/{issuing → resources/issuing}/authorization.rb +14 -5
- data/lib/stripe/{issuing → resources/issuing}/card.rb +8 -3
- data/lib/stripe/{issuing → resources/issuing}/card_details.rb +1 -1
- data/lib/stripe/{issuing → resources/issuing}/cardholder.rb +2 -1
- data/lib/stripe/resources/issuing/dispute.rb +25 -0
- data/lib/stripe/{issuing → resources/issuing}/transaction.rb +2 -1
- 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/{payment_method.rb → resources/payment_method.rb} +14 -5
- 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} +2 -1
- 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 +2 -1
- data/lib/stripe/{radar → resources/radar}/value_list_item.rb +2 -1
- data/lib/stripe/{recipient.rb → resources/recipient.rb} +2 -5
- 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 +2 -1
- data/lib/stripe/{reporting → resources/reporting}/report_type.rb +2 -1
- data/lib/stripe/resources/reversal.rb +29 -0
- data/lib/stripe/{review.rb → resources/review.rb} +8 -3
- 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 +2 -1
- data/lib/stripe/{sku.rb → resources/sku.rb} +2 -1
- data/lib/stripe/{source.rb → resources/source.rb} +22 -15
- data/lib/stripe/{source_transaction.rb → resources/source_transaction.rb} +1 -1
- data/lib/stripe/{subscription.rb → resources/subscription.rb} +12 -13
- 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/{tax_rate.rb → resources/tax_rate.rb} +2 -1
- data/lib/stripe/{terminal → resources/terminal}/connection_token.rb +2 -1
- data/lib/stripe/{terminal → resources/terminal}/location.rb +2 -1
- data/lib/stripe/{terminal → resources/terminal}/reader.rb +2 -1
- 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/{topup.rb → resources/topup.rb} +8 -3
- 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} +2 -1
- data/lib/stripe/resources.rb +85 -0
- data/lib/stripe/singleton_api_resource.rb +3 -1
- data/lib/stripe/stripe_client.rb +576 -254
- data/lib/stripe/stripe_configuration.rb +194 -0
- data/lib/stripe/stripe_object.rb +80 -61
- data/lib/stripe/stripe_response.rb +53 -21
- data/lib/stripe/util.rb +76 -121
- data/lib/stripe/version.rb +1 -1
- data/lib/stripe/webhook.rb +43 -10
- data/lib/stripe.rb +42 -203
- data/stripe.gemspec +23 -7
- metadata +107 -284
- data/.gitattributes +0 -4
- data/.github/ISSUE_TEMPLATE.md +0 -5
- data/.gitignore +0 -8
- data/.rubocop.yml +0 -32
- data/.rubocop_todo.yml +0 -50
- data/.travis.yml +0 -42
- 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 -86
- data/lib/stripe/credit_note.rb +0 -18
- data/lib/stripe/customer.rb +0 -95
- data/lib/stripe/dispute.rb +0 -23
- data/lib/stripe/invoice.rb +0 -48
- data/lib/stripe/issuer_fraud_record.rb +0 -9
- data/lib/stripe/login_link.rb +0 -11
- data/lib/stripe/order.rb +0 -34
- data/lib/stripe/payment_intent.rb +0 -30
- data/lib/stripe/payout.rb +0 -24
- data/lib/stripe/reversal.rb +0 -22
- data/lib/stripe/subscription_item.rb +0 -17
- data/lib/stripe/subscription_schedule.rb +0 -35
- data/lib/stripe/subscription_schedule_revision.rb +0 -25
- data/lib/stripe/tax_id.rb +0 -22
- data/lib/stripe/transfer.rb +0 -25
- data/lib/stripe/usage_record.rb +0 -14
- 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 -428
- data/test/stripe/alipay_account_test.rb +0 -37
- data/test/stripe/api_operations_test.rb +0 -81
- data/test/stripe/api_resource_test.rb +0 -526
- 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/bank_account_test.rb +0 -36
- data/test/stripe/capability_test.rb +0 -45
- data/test/stripe/charge_test.rb +0 -80
- data/test/stripe/checkout/session_test.rb +0 -41
- 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_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 -20
- 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 -97
- data/test/stripe/file_upload_test.rb +0 -79
- 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 -213
- data/test/stripe/issuer_fraud_record_test.rb +0 -20
- 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 -156
- data/test/stripe/login_link_test.rb +0 -37
- data/test/stripe/oauth_test.rb +0 -88
- data/test/stripe/order_return_test.rb +0 -21
- data/test/stripe/order_test.rb +0 -75
- 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/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/sigma/scheduled_query_run_test.rb +0 -22
- data/test/stripe/sku_test.rb +0 -60
- data/test/stripe/source_test.rb +0 -99
- data/test/stripe/source_transaction_test.rb +0 -19
- data/test/stripe/stripe_client_test.rb +0 -842
- data/test/stripe/stripe_object_test.rb +0 -525
- data/test/stripe/stripe_response_test.rb +0 -49
- data/test/stripe/subscription_item_test.rb +0 -63
- data/test/stripe/subscription_schedule_revision_test.rb +0 -37
- data/test/stripe/subscription_schedule_test.rb +0 -116
- 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/usage_record_test.rb +0 -28
- 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 -77
- data/test/stripe_test.rb +0 -63
- data/test/test_data.rb +0 -61
- data/test/test_helper.rb +0 -73
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Stripe
|
4
|
+
class TaxId < APIResource
|
5
|
+
include Stripe::APIOperations::Delete
|
6
|
+
extend Stripe::APIOperations::List
|
7
|
+
|
8
|
+
OBJECT_NAME = "tax_id"
|
9
|
+
|
10
|
+
def resource_url
|
11
|
+
if !respond_to?(:customer) || customer.nil?
|
12
|
+
raise NotImplementedError,
|
13
|
+
"Tax IDs cannot be accessed without a customer ID."
|
14
|
+
end
|
15
|
+
"#{Customer.resource_url}/#{CGI.escape(customer)}/tax_ids" \
|
16
|
+
"/#{CGI.escape(id)}"
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.retrieve(_id, _opts = {})
|
20
|
+
raise NotImplementedError,
|
21
|
+
"Tax IDs cannot be retrieved without a customer ID. Retrieve a " \
|
22
|
+
"tax ID using `Customer.retrieve_tax_id('customer_id', " \
|
23
|
+
"'tax_id_id')`"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -1,3 +1,4 @@
|
|
1
|
+
# File generated from our OpenAPI spec
|
1
2
|
# frozen_string_literal: true
|
2
3
|
|
3
4
|
module Stripe
|
@@ -6,6 +7,6 @@ module Stripe
|
|
6
7
|
extend Stripe::APIOperations::List
|
7
8
|
include Stripe::APIOperations::Save
|
8
9
|
|
9
|
-
OBJECT_NAME = "tax_rate"
|
10
|
+
OBJECT_NAME = "tax_rate"
|
10
11
|
end
|
11
12
|
end
|
@@ -1,3 +1,4 @@
|
|
1
|
+
# File generated from our OpenAPI spec
|
1
2
|
# frozen_string_literal: true
|
2
3
|
|
3
4
|
module Stripe
|
@@ -5,7 +6,7 @@ module Stripe
|
|
5
6
|
class ConnectionToken < APIResource
|
6
7
|
extend Stripe::APIOperations::Create
|
7
8
|
|
8
|
-
OBJECT_NAME = "terminal.connection_token"
|
9
|
+
OBJECT_NAME = "terminal.connection_token"
|
9
10
|
end
|
10
11
|
end
|
11
12
|
end
|
@@ -1,3 +1,4 @@
|
|
1
|
+
# File generated from our OpenAPI spec
|
1
2
|
# frozen_string_literal: true
|
2
3
|
|
3
4
|
module Stripe
|
@@ -8,7 +9,7 @@ module Stripe
|
|
8
9
|
extend Stripe::APIOperations::List
|
9
10
|
include Stripe::APIOperations::Save
|
10
11
|
|
11
|
-
OBJECT_NAME = "terminal.location"
|
12
|
+
OBJECT_NAME = "terminal.location"
|
12
13
|
end
|
13
14
|
end
|
14
15
|
end
|
@@ -1,3 +1,4 @@
|
|
1
|
+
# File generated from our OpenAPI spec
|
1
2
|
# frozen_string_literal: true
|
2
3
|
|
3
4
|
module Stripe
|
@@ -8,7 +9,7 @@ module Stripe
|
|
8
9
|
extend Stripe::APIOperations::List
|
9
10
|
include Stripe::APIOperations::Save
|
10
11
|
|
11
|
-
OBJECT_NAME = "terminal.reader"
|
12
|
+
OBJECT_NAME = "terminal.reader"
|
12
13
|
end
|
13
14
|
end
|
14
15
|
end
|
@@ -1,10 +1,11 @@
|
|
1
|
+
# File generated from our OpenAPI spec
|
1
2
|
# frozen_string_literal: true
|
2
3
|
|
3
4
|
module Stripe
|
4
5
|
class ThreeDSecure < APIResource
|
5
6
|
extend Stripe::APIOperations::Create
|
6
7
|
|
7
|
-
OBJECT_NAME = "three_d_secure"
|
8
|
+
OBJECT_NAME = "three_d_secure"
|
8
9
|
|
9
10
|
def self.resource_url
|
10
11
|
"/v1/3d_secure"
|
@@ -1,3 +1,4 @@
|
|
1
|
+
# File generated from our OpenAPI spec
|
1
2
|
# frozen_string_literal: true
|
2
3
|
|
3
4
|
module Stripe
|
@@ -6,13 +7,17 @@ module Stripe
|
|
6
7
|
extend Stripe::APIOperations::List
|
7
8
|
include Stripe::APIOperations::Save
|
8
9
|
|
9
|
-
OBJECT_NAME = "topup"
|
10
|
+
OBJECT_NAME = "topup"
|
10
11
|
|
11
12
|
custom_method :cancel, http_verb: :post
|
12
13
|
|
13
14
|
def cancel(params = {}, opts = {})
|
14
|
-
|
15
|
-
|
15
|
+
request_stripe_object(
|
16
|
+
method: :post,
|
17
|
+
path: resource_url + "/cancel",
|
18
|
+
params: params,
|
19
|
+
opts: opts
|
20
|
+
)
|
16
21
|
end
|
17
22
|
end
|
18
23
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# File generated from our OpenAPI spec
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
module Stripe
|
5
|
+
class Transfer < APIResource
|
6
|
+
extend Stripe::APIOperations::Create
|
7
|
+
extend Stripe::APIOperations::List
|
8
|
+
include Stripe::APIOperations::Save
|
9
|
+
extend Stripe::APIOperations::NestedResource
|
10
|
+
|
11
|
+
OBJECT_NAME = "transfer"
|
12
|
+
|
13
|
+
custom_method :cancel, http_verb: :post
|
14
|
+
|
15
|
+
nested_resource_class_methods :reversal,
|
16
|
+
operations: %i[create retrieve update list]
|
17
|
+
|
18
|
+
def cancel(params = {}, opts = {})
|
19
|
+
request_stripe_object(
|
20
|
+
method: :post,
|
21
|
+
path: resource_url + "/cancel",
|
22
|
+
params: params,
|
23
|
+
opts: opts
|
24
|
+
)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -1,3 +1,4 @@
|
|
1
|
+
# File generated from our OpenAPI spec
|
1
2
|
# frozen_string_literal: true
|
2
3
|
|
3
4
|
module Stripe
|
@@ -7,6 +8,6 @@ module Stripe
|
|
7
8
|
extend Stripe::APIOperations::List
|
8
9
|
include Stripe::APIOperations::Save
|
9
10
|
|
10
|
-
OBJECT_NAME = "webhook_endpoint"
|
11
|
+
OBJECT_NAME = "webhook_endpoint"
|
11
12
|
end
|
12
13
|
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "stripe/resources/account"
|
4
|
+
require "stripe/resources/account_link"
|
5
|
+
require "stripe/resources/alipay_account"
|
6
|
+
require "stripe/resources/apple_pay_domain"
|
7
|
+
require "stripe/resources/application_fee"
|
8
|
+
require "stripe/resources/application_fee_refund"
|
9
|
+
require "stripe/resources/balance"
|
10
|
+
require "stripe/resources/balance_transaction"
|
11
|
+
require "stripe/resources/bank_account"
|
12
|
+
require "stripe/resources/billing_portal/configuration"
|
13
|
+
require "stripe/resources/billing_portal/session"
|
14
|
+
require "stripe/resources/bitcoin_receiver"
|
15
|
+
require "stripe/resources/bitcoin_transaction"
|
16
|
+
require "stripe/resources/capability"
|
17
|
+
require "stripe/resources/card"
|
18
|
+
require "stripe/resources/charge"
|
19
|
+
require "stripe/resources/checkout/session"
|
20
|
+
require "stripe/resources/country_spec"
|
21
|
+
require "stripe/resources/coupon"
|
22
|
+
require "stripe/resources/credit_note"
|
23
|
+
require "stripe/resources/credit_note_line_item"
|
24
|
+
require "stripe/resources/customer"
|
25
|
+
require "stripe/resources/customer_balance_transaction"
|
26
|
+
require "stripe/resources/discount"
|
27
|
+
require "stripe/resources/dispute"
|
28
|
+
require "stripe/resources/ephemeral_key"
|
29
|
+
require "stripe/resources/event"
|
30
|
+
require "stripe/resources/exchange_rate"
|
31
|
+
require "stripe/resources/file"
|
32
|
+
require "stripe/resources/file_link"
|
33
|
+
require "stripe/resources/invoice"
|
34
|
+
require "stripe/resources/invoice_item"
|
35
|
+
require "stripe/resources/invoice_line_item"
|
36
|
+
require "stripe/resources/issuing/authorization"
|
37
|
+
require "stripe/resources/issuing/card"
|
38
|
+
require "stripe/resources/issuing/card_details"
|
39
|
+
require "stripe/resources/issuing/cardholder"
|
40
|
+
require "stripe/resources/issuing/dispute"
|
41
|
+
require "stripe/resources/issuing/transaction"
|
42
|
+
require "stripe/resources/line_item"
|
43
|
+
require "stripe/resources/login_link"
|
44
|
+
require "stripe/resources/mandate"
|
45
|
+
require "stripe/resources/order"
|
46
|
+
require "stripe/resources/order_return"
|
47
|
+
require "stripe/resources/payment_intent"
|
48
|
+
require "stripe/resources/payment_method"
|
49
|
+
require "stripe/resources/payout"
|
50
|
+
require "stripe/resources/person"
|
51
|
+
require "stripe/resources/plan"
|
52
|
+
require "stripe/resources/price"
|
53
|
+
require "stripe/resources/product"
|
54
|
+
require "stripe/resources/promotion_code"
|
55
|
+
require "stripe/resources/radar/early_fraud_warning"
|
56
|
+
require "stripe/resources/radar/value_list"
|
57
|
+
require "stripe/resources/radar/value_list_item"
|
58
|
+
require "stripe/resources/recipient"
|
59
|
+
require "stripe/resources/recipient_transfer"
|
60
|
+
require "stripe/resources/refund"
|
61
|
+
require "stripe/resources/reporting/report_run"
|
62
|
+
require "stripe/resources/reporting/report_type"
|
63
|
+
require "stripe/resources/reversal"
|
64
|
+
require "stripe/resources/review"
|
65
|
+
require "stripe/resources/setup_attempt"
|
66
|
+
require "stripe/resources/setup_intent"
|
67
|
+
require "stripe/resources/sigma/scheduled_query_run"
|
68
|
+
require "stripe/resources/sku"
|
69
|
+
require "stripe/resources/source"
|
70
|
+
require "stripe/resources/source_transaction"
|
71
|
+
require "stripe/resources/subscription"
|
72
|
+
require "stripe/resources/subscription_item"
|
73
|
+
require "stripe/resources/subscription_schedule"
|
74
|
+
require "stripe/resources/tax_id"
|
75
|
+
require "stripe/resources/tax_rate"
|
76
|
+
require "stripe/resources/terminal/connection_token"
|
77
|
+
require "stripe/resources/terminal/location"
|
78
|
+
require "stripe/resources/terminal/reader"
|
79
|
+
require "stripe/resources/three_d_secure"
|
80
|
+
require "stripe/resources/token"
|
81
|
+
require "stripe/resources/topup"
|
82
|
+
require "stripe/resources/transfer"
|
83
|
+
require "stripe/resources/usage_record"
|
84
|
+
require "stripe/resources/usage_record_summary"
|
85
|
+
require "stripe/resources/webhook_endpoint"
|
@@ -4,7 +4,9 @@ module Stripe
|
|
4
4
|
class SingletonAPIResource < APIResource
|
5
5
|
def self.resource_url
|
6
6
|
if self == SingletonAPIResource
|
7
|
-
raise NotImplementedError,
|
7
|
+
raise NotImplementedError,
|
8
|
+
"SingletonAPIResource is an abstract class. You should " \
|
9
|
+
"perform actions on its subclasses (Balance, etc.)"
|
8
10
|
end
|
9
11
|
# Namespaces are separated in object names with periods (.) and in URLs
|
10
12
|
# with forward slashes (/), so replace the former with the latter.
|
data/lib/stripe/stripe_client.rb
CHANGED
@@ -1,107 +1,190 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "stripe/instrumentation"
|
4
|
+
|
3
5
|
module Stripe
|
4
6
|
# StripeClient executes requests against the Stripe API and allows a user to
|
5
7
|
# recover both a resource a call returns as well as a response object that
|
6
8
|
# contains information on the HTTP call.
|
7
9
|
class StripeClient
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
10
|
+
# A set of all known thread contexts across all threads and a mutex to
|
11
|
+
# synchronize global access to them.
|
12
|
+
@thread_contexts_with_connection_managers = Set.new
|
13
|
+
@thread_contexts_with_connection_managers_mutex = Mutex.new
|
14
|
+
@last_connection_manager_gc = Util.monotonic_time
|
15
|
+
|
16
|
+
# Initializes a new StripeClient
|
17
|
+
def initialize(config_arg = {})
|
14
18
|
@system_profiler = SystemProfiler.new
|
15
19
|
@last_request_metrics = nil
|
20
|
+
|
21
|
+
@config = case config_arg
|
22
|
+
when Hash
|
23
|
+
Stripe.config.reverse_duplicate_merge(config_arg)
|
24
|
+
when Stripe::ConnectionManager
|
25
|
+
# Supports accepting a connection manager object for backwards
|
26
|
+
# compatibility only, and that use is DEPRECATED.
|
27
|
+
Stripe.config.dup
|
28
|
+
when Stripe::StripeConfiguration
|
29
|
+
config_arg
|
30
|
+
when String
|
31
|
+
Stripe.config.reverse_duplicate_merge(
|
32
|
+
{ api_key: config_arg }
|
33
|
+
)
|
34
|
+
else
|
35
|
+
raise ArgumentError, "Can't handle argument: #{config_arg}"
|
36
|
+
end
|
16
37
|
end
|
17
38
|
|
39
|
+
attr_reader :config
|
40
|
+
attr_reader :options
|
41
|
+
|
42
|
+
# Gets a currently active `StripeClient`. Set for the current thread when
|
43
|
+
# `StripeClient#request` is being run so that API operations being executed
|
44
|
+
# inside of that block can find the currently active client. It's reset to
|
45
|
+
# the original value (hopefully `nil`) after the block ends.
|
46
|
+
#
|
47
|
+
# For internal use only. Does not provide a stable API and may be broken
|
48
|
+
# with future non-major changes.
|
18
49
|
def self.active_client
|
19
|
-
|
50
|
+
current_thread_context.active_client || default_client
|
20
51
|
end
|
21
52
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
#
|
27
|
-
#
|
28
|
-
#
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
#
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
53
|
+
# Finishes any active connections by closing their TCP connection and
|
54
|
+
# clears them from internal tracking in all connection managers across all
|
55
|
+
# threads.
|
56
|
+
#
|
57
|
+
# If passed a `config` object, only clear connection managers for that
|
58
|
+
# particular configuration.
|
59
|
+
#
|
60
|
+
# For internal use only. Does not provide a stable API and may be broken
|
61
|
+
# with future non-major changes.
|
62
|
+
def self.clear_all_connection_managers(config: nil)
|
63
|
+
# Just a quick path for when configuration is being set for the first
|
64
|
+
# time before any connections have been opened. There is technically some
|
65
|
+
# potential for thread raciness here, but not in a practical sense.
|
66
|
+
return if @thread_contexts_with_connection_managers.empty?
|
67
|
+
|
68
|
+
@thread_contexts_with_connection_managers_mutex.synchronize do
|
69
|
+
pruned_contexts = Set.new
|
70
|
+
|
71
|
+
@thread_contexts_with_connection_managers.each do |thread_context|
|
72
|
+
# Note that the thread context itself is not destroyed, but we clear
|
73
|
+
# its connection manager and remove our reference to it. If it ever
|
74
|
+
# makes a new request we'll give it a new connection manager and
|
75
|
+
# it'll go back into `@thread_contexts_with_connection_managers`.
|
76
|
+
thread_context.default_connection_managers.reject! do |cm_config, cm|
|
77
|
+
if config.nil? || config.key == cm_config
|
78
|
+
cm.clear
|
79
|
+
true
|
80
|
+
end
|
45
81
|
end
|
46
|
-
end
|
47
|
-
|
48
|
-
conn.proxy = Stripe.proxy if Stripe.proxy
|
49
|
-
|
50
|
-
if Stripe.verify_ssl_certs
|
51
|
-
conn.ssl.verify = true
|
52
|
-
conn.ssl.cert_store = Stripe.ca_store
|
53
|
-
else
|
54
|
-
conn.ssl.verify = false
|
55
82
|
|
56
|
-
|
57
|
-
|
58
|
-
$stderr.puts("WARNING: Running without SSL cert verification. " \
|
59
|
-
"You should never do this in production. " \
|
60
|
-
"Execute 'Stripe.verify_ssl_certs = true' to enable verification.")
|
83
|
+
if thread_context.default_connection_managers.empty?
|
84
|
+
pruned_contexts << thread_context
|
61
85
|
end
|
62
86
|
end
|
63
87
|
|
64
|
-
|
88
|
+
@thread_contexts_with_connection_managers.subtract(pruned_contexts)
|
65
89
|
end
|
66
90
|
end
|
67
91
|
|
68
|
-
#
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
return false if num_retries >= Stripe.max_network_retries
|
92
|
+
# A default client for the current thread.
|
93
|
+
def self.default_client
|
94
|
+
current_thread_context.default_client ||= StripeClient.new(Stripe.config)
|
95
|
+
end
|
73
96
|
|
74
|
-
|
75
|
-
|
97
|
+
# A default connection manager for the current thread scoped to the
|
98
|
+
# configuration object that may be provided.
|
99
|
+
def self.default_connection_manager(config = Stripe.config)
|
100
|
+
current_thread_context.default_connection_managers[config.key] ||= begin
|
101
|
+
connection_manager = ConnectionManager.new(config)
|
76
102
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
103
|
+
@thread_contexts_with_connection_managers_mutex.synchronize do
|
104
|
+
maybe_gc_connection_managers
|
105
|
+
@thread_contexts_with_connection_managers << current_thread_context
|
106
|
+
end
|
81
107
|
|
82
|
-
|
83
|
-
# 409 conflict
|
84
|
-
return true if e.response[:status] == 409
|
108
|
+
connection_manager
|
85
109
|
end
|
110
|
+
end
|
86
111
|
|
87
|
-
|
112
|
+
# Checks if an error is a problem that we should retry on. This includes
|
113
|
+
# both socket errors that may represent an intermittent problem and some
|
114
|
+
# special HTTP statuses.
|
115
|
+
def self.should_retry?(error,
|
116
|
+
method:, num_retries:, config: Stripe.config)
|
117
|
+
return false if num_retries >= config.max_network_retries
|
118
|
+
|
119
|
+
case error
|
120
|
+
when Net::OpenTimeout, Net::ReadTimeout
|
121
|
+
# Retry on timeout-related problems (either on open or read).
|
122
|
+
true
|
123
|
+
when EOFError, Errno::ECONNREFUSED, Errno::ECONNRESET,
|
124
|
+
Errno::EHOSTUNREACH, Errno::ETIMEDOUT, SocketError
|
125
|
+
# Destination refused the connection, the connection was reset, or a
|
126
|
+
# variety of other connection failures. This could occur from a single
|
127
|
+
# saturated server, so retry in case it's intermittent.
|
128
|
+
true
|
129
|
+
when Stripe::StripeError
|
130
|
+
# The API may ask us not to retry (e.g. if doing so would be a no-op),
|
131
|
+
# or advise us to retry (e.g. in cases of lock timeouts). Defer to
|
132
|
+
# those instructions if given.
|
133
|
+
return false if error.http_headers["stripe-should-retry"] == "false"
|
134
|
+
return true if error.http_headers["stripe-should-retry"] == "true"
|
135
|
+
|
136
|
+
# 409 Conflict
|
137
|
+
return true if error.http_status == 409
|
138
|
+
|
139
|
+
# 429 Too Many Requests
|
140
|
+
#
|
141
|
+
# There are a few different problems that can lead to a 429. The most
|
142
|
+
# common is rate limiting, on which we *don't* want to retry because
|
143
|
+
# that'd likely contribute to more contention problems. However, some
|
144
|
+
# 429s are lock timeouts, which is when a request conflicted with
|
145
|
+
# another request or an internal process on some particular object.
|
146
|
+
# These 429s are safe to retry.
|
147
|
+
return true if error.http_status == 429 && error.code == "lock_timeout"
|
148
|
+
|
149
|
+
# 500 Internal Server Error
|
150
|
+
#
|
151
|
+
# We only bother retrying these for non-POST requests. POSTs end up
|
152
|
+
# being cached by the idempotency layer so there's no purpose in
|
153
|
+
# retrying them.
|
154
|
+
return true if error.http_status == 500 && method != :post
|
155
|
+
|
156
|
+
# 503 Service Unavailable
|
157
|
+
error.http_status == 503
|
158
|
+
else
|
159
|
+
false
|
160
|
+
end
|
88
161
|
end
|
89
162
|
|
90
|
-
def self.sleep_time(num_retries)
|
163
|
+
def self.sleep_time(num_retries, config: Stripe.config)
|
91
164
|
# Apply exponential backoff with initial_network_retry_delay on the
|
92
|
-
# number of num_retries so far as inputs. Do not allow the number to
|
93
|
-
# max_network_retry_delay.
|
94
|
-
sleep_seconds = [
|
95
|
-
|
96
|
-
|
97
|
-
|
165
|
+
# number of num_retries so far as inputs. Do not allow the number to
|
166
|
+
# exceed max_network_retry_delay.
|
167
|
+
sleep_seconds = [
|
168
|
+
config.initial_network_retry_delay * (2**(num_retries - 1)),
|
169
|
+
config.max_network_retry_delay,
|
170
|
+
].min
|
171
|
+
|
172
|
+
# Apply some jitter by randomizing the value in the range of
|
173
|
+
# (sleep_seconds / 2) to (sleep_seconds).
|
98
174
|
sleep_seconds *= (0.5 * (1 + rand))
|
99
175
|
|
100
176
|
# But never sleep less than the base sleep seconds.
|
101
|
-
|
177
|
+
[config.initial_network_retry_delay, sleep_seconds].max
|
178
|
+
end
|
102
179
|
|
103
|
-
|
180
|
+
# Gets the connection manager in use for the current `StripeClient`.
|
181
|
+
#
|
182
|
+
# This method is DEPRECATED and for backwards compatibility only.
|
183
|
+
def connection_manager
|
184
|
+
self.class.default_connection_manager
|
104
185
|
end
|
186
|
+
extend Gem::Deprecate
|
187
|
+
deprecate :connection_manager, :none, 2020, 9
|
105
188
|
|
106
189
|
# Executes the API call within the given block. Usage looks like:
|
107
190
|
#
|
@@ -109,139 +192,276 @@ module Stripe
|
|
109
192
|
# charge, resp = client.request { Charge.create }
|
110
193
|
#
|
111
194
|
def request
|
112
|
-
|
113
|
-
|
114
|
-
|
195
|
+
old_stripe_client = self.class.current_thread_context.active_client
|
196
|
+
self.class.current_thread_context.active_client = self
|
197
|
+
|
198
|
+
if self.class.current_thread_context.last_responses&.key?(object_id)
|
199
|
+
raise "calls to StripeClient#request cannot be nested within a thread"
|
200
|
+
end
|
201
|
+
|
202
|
+
self.class.current_thread_context.last_responses ||= {}
|
203
|
+
self.class.current_thread_context.last_responses[object_id] = nil
|
115
204
|
|
116
205
|
begin
|
117
206
|
res = yield
|
118
|
-
[res,
|
207
|
+
[res, self.class.current_thread_context.last_responses[object_id]]
|
119
208
|
ensure
|
120
|
-
|
209
|
+
self.class.current_thread_context.active_client = old_stripe_client
|
210
|
+
self.class.current_thread_context.last_responses.delete(object_id)
|
121
211
|
end
|
122
212
|
end
|
123
213
|
|
124
214
|
def execute_request(method, path,
|
125
215
|
api_base: nil, api_key: nil, headers: {}, params: {})
|
126
|
-
|
127
|
-
|
216
|
+
raise ArgumentError, "method should be a symbol" \
|
217
|
+
unless method.is_a?(Symbol)
|
218
|
+
raise ArgumentError, "path should be a string" \
|
219
|
+
unless path.is_a?(String)
|
220
|
+
|
221
|
+
api_base ||= config.api_base
|
222
|
+
api_key ||= config.api_key
|
128
223
|
params = Util.objects_to_ids(params)
|
129
224
|
|
130
225
|
check_api_key!(api_key)
|
131
226
|
|
132
|
-
|
227
|
+
body_params = nil
|
133
228
|
query_params = nil
|
134
|
-
case method
|
229
|
+
case method
|
135
230
|
when :get, :head, :delete
|
136
231
|
query_params = params
|
137
232
|
else
|
138
|
-
|
233
|
+
body_params = params
|
139
234
|
end
|
140
235
|
|
141
|
-
|
142
|
-
# parameters in `query_params` and query parameters that are appended
|
143
|
-
# onto the end of the given path. In this case, Faraday will silently
|
144
|
-
# discard the URL's parameters which may break a request.
|
145
|
-
#
|
146
|
-
# Here we decode any parameters that were added onto the end of a path
|
147
|
-
# and add them to `query_params` so that all parameters end up in one
|
148
|
-
# place and all of them are correctly included in the final request.
|
149
|
-
u = URI.parse(path)
|
150
|
-
unless u.query.nil?
|
151
|
-
query_params ||= {}
|
152
|
-
query_params = Hash[URI.decode_www_form(u.query)].merge(query_params)
|
153
|
-
|
154
|
-
# Reset the path minus any query parameters that were specified.
|
155
|
-
path = u.path
|
156
|
-
end
|
236
|
+
query_params, path = merge_query_params(query_params, path)
|
157
237
|
|
158
238
|
headers = request_headers(api_key, method)
|
159
239
|
.update(Util.normalize_headers(headers))
|
160
|
-
params_encoder = FaradayStripeEncoder.new
|
161
240
|
url = api_url(path, api_base)
|
162
241
|
|
242
|
+
# Merge given query parameters with any already encoded in the path.
|
243
|
+
query = query_params ? Util.encode_parameters(query_params) : nil
|
244
|
+
|
245
|
+
# Encoding body parameters is a little more complex because we may have
|
246
|
+
# to send a multipart-encoded body. `body_log` is produced separately as
|
247
|
+
# a log-friendly variant of the encoded form. File objects are displayed
|
248
|
+
# as such instead of as their file contents.
|
249
|
+
body, body_log =
|
250
|
+
body_params ? encode_body(body_params, headers) : [nil, nil]
|
251
|
+
|
163
252
|
# stores information on the request we're about to make so that we don't
|
164
253
|
# have to pass as many parameters around for logging.
|
165
254
|
context = RequestLogContext.new
|
166
255
|
context.account = headers["Stripe-Account"]
|
167
256
|
context.api_key = api_key
|
168
257
|
context.api_version = headers["Stripe-Version"]
|
169
|
-
context.body =
|
258
|
+
context.body = body_log
|
170
259
|
context.idempotency_key = headers["Idempotency-Key"]
|
171
260
|
context.method = method
|
172
261
|
context.path = path
|
173
|
-
context.
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
req.params = query_params unless query_params.nil?
|
183
|
-
end
|
262
|
+
context.query = query
|
263
|
+
|
264
|
+
http_resp = execute_request_with_rescues(method, api_base, context) do
|
265
|
+
self.class
|
266
|
+
.default_connection_manager(config)
|
267
|
+
.execute_request(method, url,
|
268
|
+
body: body,
|
269
|
+
headers: headers,
|
270
|
+
query: query)
|
184
271
|
end
|
185
272
|
|
186
273
|
begin
|
187
|
-
resp = StripeResponse.
|
274
|
+
resp = StripeResponse.from_net_http(http_resp)
|
188
275
|
rescue JSON::ParserError
|
189
|
-
raise general_api_error(http_resp.
|
276
|
+
raise general_api_error(http_resp.code.to_i, http_resp.body)
|
190
277
|
end
|
191
278
|
|
192
|
-
#
|
193
|
-
|
279
|
+
# If being called from `StripeClient#request`, put the last response in
|
280
|
+
# thread-local memory so that it can be returned to the user. Don't store
|
281
|
+
# anything otherwise so that we don't leak memory.
|
282
|
+
store_last_response(object_id, resp)
|
283
|
+
|
194
284
|
[resp, api_key]
|
195
285
|
end
|
196
286
|
|
197
|
-
|
287
|
+
def store_last_response(object_id, resp)
|
288
|
+
return unless last_response_has_key?(object_id)
|
289
|
+
|
290
|
+
self.class.current_thread_context.last_responses[object_id] = resp
|
291
|
+
end
|
292
|
+
|
293
|
+
def last_response_has_key?(object_id)
|
294
|
+
self.class.current_thread_context.last_responses&.key?(object_id)
|
295
|
+
end
|
198
296
|
|
199
|
-
# Used to workaround buggy behavior in Faraday: the library will try to
|
200
|
-
# reshape anything that we pass to `req.params` with one of its default
|
201
|
-
# encoders. I don't think this process is supposed to be lossy, but it is
|
202
|
-
# -- in particular when we send our integer-indexed maps (i.e. arrays),
|
203
|
-
# Faraday ends up stripping out the integer indexes.
|
204
297
|
#
|
205
|
-
#
|
206
|
-
# telling Faraday to use that.
|
298
|
+
# private
|
207
299
|
#
|
208
|
-
|
209
|
-
#
|
210
|
-
# for
|
300
|
+
|
301
|
+
# Time (in seconds) that a connection manager has not been used before it's
|
302
|
+
# eligible for garbage collection.
|
303
|
+
CONNECTION_MANAGER_GC_LAST_USED_EXPIRY = 120
|
304
|
+
|
305
|
+
# How often to check (in seconds) for connection managers that haven't been
|
306
|
+
# used in a long time and which should be garbage collected.
|
307
|
+
CONNECTION_MANAGER_GC_PERIOD = 60
|
308
|
+
|
309
|
+
ERROR_MESSAGE_CONNECTION =
|
310
|
+
"Unexpected error communicating when trying to connect to " \
|
311
|
+
"Stripe (%s). You may be seeing this message because your DNS is not " \
|
312
|
+
"working or you don't have an internet connection. To check, try " \
|
313
|
+
"running `host stripe.com` from the command line."
|
314
|
+
ERROR_MESSAGE_SSL =
|
315
|
+
"Could not establish a secure connection to Stripe (%s), you " \
|
316
|
+
"may need to upgrade your OpenSSL version. To check, try running " \
|
317
|
+
"`openssl s_client -connect api.stripe.com:443` from the command " \
|
318
|
+
"line."
|
319
|
+
|
320
|
+
# Common error suffix sared by both connect and read timeout messages.
|
321
|
+
ERROR_MESSAGE_TIMEOUT_SUFFIX =
|
322
|
+
"Please check your internet connection and try again. " \
|
323
|
+
"If this problem persists, you should check Stripe's service " \
|
324
|
+
"status at https://status.stripe.com, or let us know at " \
|
325
|
+
"support@stripe.com."
|
326
|
+
|
327
|
+
ERROR_MESSAGE_TIMEOUT_CONNECT = (
|
328
|
+
"Timed out connecting to Stripe (%s). " +
|
329
|
+
ERROR_MESSAGE_TIMEOUT_SUFFIX
|
330
|
+
).freeze
|
331
|
+
|
332
|
+
ERROR_MESSAGE_TIMEOUT_READ = (
|
333
|
+
"Timed out communicating with Stripe (%s). " +
|
334
|
+
ERROR_MESSAGE_TIMEOUT_SUFFIX
|
335
|
+
).freeze
|
336
|
+
|
337
|
+
# Maps types of exceptions that we're likely to see during a network
|
338
|
+
# request to more user-friendly messages that we put in front of people.
|
339
|
+
# The original error message is also appended onto the final exception for
|
340
|
+
# full transparency.
|
341
|
+
NETWORK_ERROR_MESSAGES_MAP = {
|
342
|
+
EOFError => ERROR_MESSAGE_CONNECTION,
|
343
|
+
Errno::ECONNREFUSED => ERROR_MESSAGE_CONNECTION,
|
344
|
+
Errno::ECONNRESET => ERROR_MESSAGE_CONNECTION,
|
345
|
+
Errno::EHOSTUNREACH => ERROR_MESSAGE_CONNECTION,
|
346
|
+
Errno::ETIMEDOUT => ERROR_MESSAGE_TIMEOUT_CONNECT,
|
347
|
+
SocketError => ERROR_MESSAGE_CONNECTION,
|
348
|
+
|
349
|
+
Net::OpenTimeout => ERROR_MESSAGE_TIMEOUT_CONNECT,
|
350
|
+
Net::ReadTimeout => ERROR_MESSAGE_TIMEOUT_READ,
|
351
|
+
|
352
|
+
OpenSSL::SSL::SSLError => ERROR_MESSAGE_SSL,
|
353
|
+
}.freeze
|
354
|
+
private_constant :NETWORK_ERROR_MESSAGES_MAP
|
355
|
+
|
356
|
+
# A record representing any data that `StripeClient` puts into
|
357
|
+
# `Thread.current`. Making it a class likes this gives us a little extra
|
358
|
+
# type safety and lets us document what each field does.
|
211
359
|
#
|
212
|
-
#
|
213
|
-
#
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
360
|
+
# For internal use only. Does not provide a stable API and may be broken
|
361
|
+
# with future non-major changes.
|
362
|
+
class ThreadContext
|
363
|
+
# A `StripeClient` that's been flagged as currently active within a
|
364
|
+
# thread by `StripeClient#request`. A client stays active until the
|
365
|
+
# completion of the request block.
|
366
|
+
attr_accessor :active_client
|
367
|
+
|
368
|
+
# A default `StripeClient` object for the thread. Used in all cases where
|
369
|
+
# the user hasn't specified their own.
|
370
|
+
attr_accessor :default_client
|
371
|
+
|
372
|
+
# A temporary map of object IDs to responses from last executed API
|
373
|
+
# calls. Used to return a responses from calls to `StripeClient#request`.
|
374
|
+
#
|
375
|
+
# Stored in the thread data to make the use of a single `StripeClient`
|
376
|
+
# object safe across multiple threads. Stored as a map so that multiple
|
377
|
+
# `StripeClient` objects can run concurrently on the same thread.
|
378
|
+
#
|
379
|
+
# Responses are only left in as long as they're needed, which means
|
380
|
+
# they're removed as soon as a call leaves `StripeClient#request`, and
|
381
|
+
# because that's wrapped in an `ensure` block, they should never leave
|
382
|
+
# garbage in `Thread.current`.
|
383
|
+
attr_accessor :last_responses
|
384
|
+
|
385
|
+
# A map of connection mangers for the thread. Normally shared between
|
386
|
+
# all `StripeClient` objects on a particular thread, and created so as to
|
387
|
+
# minimize the number of open connections that an application needs.
|
388
|
+
def default_connection_managers
|
389
|
+
@default_connection_managers ||= {}
|
218
390
|
end
|
219
391
|
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
392
|
+
def reset_connection_managers
|
393
|
+
@default_connection_managers = {}
|
394
|
+
end
|
395
|
+
end
|
396
|
+
|
397
|
+
# Access data stored for `StripeClient` within the thread's current
|
398
|
+
# context. Returns `ThreadContext`.
|
399
|
+
#
|
400
|
+
# For internal use only. Does not provide a stable API and may be broken
|
401
|
+
# with future non-major changes.
|
402
|
+
def self.current_thread_context
|
403
|
+
Thread.current[:stripe_client__internal_use_only] ||= ThreadContext.new
|
404
|
+
end
|
405
|
+
|
406
|
+
# Garbage collects connection managers that haven't been used in some time,
|
407
|
+
# with the idea being that we want to remove old connection managers that
|
408
|
+
# belong to dead threads and the like.
|
409
|
+
#
|
410
|
+
# Prefixed with `maybe_` because garbage collection will only run
|
411
|
+
# periodically so that we're not constantly engaged in busy work. If
|
412
|
+
# connection managers live a little passed their useful age it's not
|
413
|
+
# harmful, so it's not necessary to get them right away.
|
414
|
+
#
|
415
|
+
# For testability, returns `nil` if it didn't run and the number of
|
416
|
+
# connection managers that were garbage collected otherwise.
|
417
|
+
#
|
418
|
+
# IMPORTANT: This method is not thread-safe and expects to be called inside
|
419
|
+
# a lock on `@thread_contexts_with_connection_managers_mutex`.
|
420
|
+
#
|
421
|
+
# For internal use only. Does not provide a stable API and may be broken
|
422
|
+
# with future non-major changes.
|
423
|
+
def self.maybe_gc_connection_managers
|
424
|
+
next_gc_time = @last_connection_manager_gc + CONNECTION_MANAGER_GC_PERIOD
|
425
|
+
return nil if next_gc_time > Util.monotonic_time
|
426
|
+
|
427
|
+
last_used_threshold =
|
428
|
+
Util.monotonic_time - CONNECTION_MANAGER_GC_LAST_USED_EXPIRY
|
429
|
+
|
430
|
+
pruned_contexts = []
|
431
|
+
@thread_contexts_with_connection_managers.each do |thread_context|
|
432
|
+
thread_context
|
433
|
+
.default_connection_managers
|
434
|
+
.each do |config_key, connection_manager|
|
435
|
+
next if connection_manager.last_used > last_used_threshold
|
436
|
+
|
437
|
+
connection_manager.clear
|
438
|
+
thread_context.default_connection_managers.delete(config_key)
|
439
|
+
end
|
226
440
|
end
|
227
441
|
|
228
|
-
|
229
|
-
|
230
|
-
|
442
|
+
@thread_contexts_with_connection_managers.each do |thread_context|
|
443
|
+
next unless thread_context.default_connection_managers.empty?
|
444
|
+
|
445
|
+
pruned_contexts << thread_context
|
231
446
|
end
|
447
|
+
|
448
|
+
@thread_contexts_with_connection_managers -= pruned_contexts
|
449
|
+
@last_connection_manager_gc = Util.monotonic_time
|
450
|
+
|
451
|
+
pruned_contexts.count
|
232
452
|
end
|
233
453
|
|
234
|
-
def api_url(url = "", api_base = nil)
|
235
|
-
(api_base ||
|
454
|
+
private def api_url(url = "", api_base = nil)
|
455
|
+
(api_base || config.api_base) + url
|
236
456
|
end
|
237
457
|
|
238
|
-
def check_api_key!(api_key)
|
458
|
+
private def check_api_key!(api_key)
|
239
459
|
unless api_key
|
240
460
|
raise AuthenticationError, "No API key provided. " \
|
241
461
|
'Set your API key using "Stripe.api_key = <API-KEY>". ' \
|
242
462
|
"You can generate API keys from the Stripe web interface. " \
|
243
|
-
"See https://stripe.com/api for details, or email
|
244
|
-
"if you have any questions."
|
463
|
+
"See https://stripe.com/api for details, or email " \
|
464
|
+
"support@stripe.com if you have any questions."
|
245
465
|
end
|
246
466
|
|
247
467
|
return unless api_key =~ /\s/
|
@@ -252,49 +472,101 @@ module Stripe
|
|
252
472
|
"email support@stripe.com if you have any questions.)"
|
253
473
|
end
|
254
474
|
|
255
|
-
|
475
|
+
# Encodes a set of body parameters using multipart if `Content-Type` is set
|
476
|
+
# for that, or standard form-encoding otherwise. Returns the encoded body
|
477
|
+
# and a version of the encoded body that's safe to be logged.
|
478
|
+
private def encode_body(body_params, headers)
|
479
|
+
body = nil
|
480
|
+
flattened_params = Util.flatten_params(body_params)
|
481
|
+
|
482
|
+
if headers["Content-Type"] == MultipartEncoder::MULTIPART_FORM_DATA
|
483
|
+
body, content_type = MultipartEncoder.encode(flattened_params)
|
484
|
+
|
485
|
+
# Set a new content type that also includes the multipart boundary.
|
486
|
+
# See `MultipartEncoder` for details.
|
487
|
+
headers["Content-Type"] = content_type
|
488
|
+
|
489
|
+
# `#to_s` any complex objects like files and the like to build output
|
490
|
+
# that's more condusive to logging.
|
491
|
+
flattened_params =
|
492
|
+
flattened_params.map { |k, v| [k, v.is_a?(String) ? v : v.to_s] }.to_h
|
493
|
+
else
|
494
|
+
body = Util.encode_parameters(body_params)
|
495
|
+
end
|
496
|
+
|
497
|
+
# We don't use `Util.encode_parameters` partly as an optimization (to not
|
498
|
+
# redo work we've already done), and partly because the encoded forms of
|
499
|
+
# certain characters introduce a lot of visual noise and it's nice to
|
500
|
+
# have a clearer format for logs.
|
501
|
+
body_log = flattened_params.map { |k, v| "#{k}=#{v}" }.join("&")
|
502
|
+
|
503
|
+
[body, body_log]
|
504
|
+
end
|
505
|
+
|
506
|
+
private def execute_request_with_rescues(method, api_base, context)
|
256
507
|
num_retries = 0
|
508
|
+
|
257
509
|
begin
|
258
|
-
request_start =
|
510
|
+
request_start = nil
|
511
|
+
user_data = nil
|
512
|
+
|
259
513
|
log_request(context, num_retries)
|
514
|
+
user_data = notify_request_begin(context)
|
515
|
+
|
516
|
+
request_start = Util.monotonic_time
|
260
517
|
resp = yield
|
261
|
-
|
262
|
-
log_response(context, request_start, resp.status, resp.body)
|
518
|
+
request_duration = Util.monotonic_time - request_start
|
263
519
|
|
264
|
-
|
265
|
-
|
266
|
-
|
520
|
+
http_status = resp.code.to_i
|
521
|
+
context = context.dup_from_response_headers(resp)
|
522
|
+
|
523
|
+
handle_error_response(resp, context) if http_status >= 400
|
524
|
+
|
525
|
+
log_response(context, request_start, http_status, resp.body)
|
526
|
+
notify_request_end(context, request_duration, http_status,
|
527
|
+
num_retries, user_data)
|
528
|
+
|
529
|
+
if config.enable_telemetry? && context.request_id
|
530
|
+
request_duration_ms = (request_duration * 1000).to_i
|
531
|
+
@last_request_metrics =
|
532
|
+
StripeRequestMetrics.new(context.request_id, request_duration_ms)
|
267
533
|
end
|
268
534
|
|
269
535
|
# We rescue all exceptions from a request so that we have an easy spot to
|
270
|
-
# implement our retry logic across the board. We'll re-raise if it's a
|
271
|
-
# of exception that we didn't expect to handle.
|
536
|
+
# implement our retry logic across the board. We'll re-raise if it's a
|
537
|
+
# type of exception that we didn't expect to handle.
|
272
538
|
rescue StandardError => e
|
273
539
|
# If we modify context we copy it into a new variable so as not to
|
274
540
|
# taint the original on a retry.
|
275
541
|
error_context = context
|
542
|
+
http_status = nil
|
543
|
+
request_duration = Util.monotonic_time - request_start if request_start
|
276
544
|
|
277
|
-
if e.
|
278
|
-
error_context = context.
|
545
|
+
if e.is_a?(Stripe::StripeError)
|
546
|
+
error_context = context.dup_from_response_headers(e.http_headers)
|
547
|
+
http_status = resp.code.to_i
|
279
548
|
log_response(error_context, request_start,
|
280
|
-
e.
|
549
|
+
e.http_status, e.http_body)
|
281
550
|
else
|
282
551
|
log_response_error(error_context, request_start, e)
|
283
552
|
end
|
553
|
+
notify_request_end(context, request_duration, http_status, num_retries,
|
554
|
+
user_data)
|
284
555
|
|
285
|
-
if self.class.should_retry?(e,
|
556
|
+
if self.class.should_retry?(e,
|
557
|
+
method: method,
|
558
|
+
num_retries: num_retries,
|
559
|
+
config: config)
|
286
560
|
num_retries += 1
|
287
|
-
sleep self.class.sleep_time(num_retries)
|
561
|
+
sleep self.class.sleep_time(num_retries, config: config)
|
288
562
|
retry
|
289
563
|
end
|
290
564
|
|
291
565
|
case e
|
292
|
-
when
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
handle_network_error(e, error_context, num_retries, api_base)
|
297
|
-
end
|
566
|
+
when Stripe::StripeError
|
567
|
+
raise
|
568
|
+
when *NETWORK_ERROR_MESSAGES_MAP.keys
|
569
|
+
handle_network_error(e, error_context, num_retries, api_base)
|
298
570
|
|
299
571
|
# Only handle errors when we know we can do so, and re-raise otherwise.
|
300
572
|
# This should be pretty infrequent.
|
@@ -306,7 +578,42 @@ module Stripe
|
|
306
578
|
resp
|
307
579
|
end
|
308
580
|
|
309
|
-
def
|
581
|
+
private def notify_request_begin(context)
|
582
|
+
return unless Instrumentation.any_subscribers?(:request_begin)
|
583
|
+
|
584
|
+
event = Instrumentation::RequestBeginEvent.new(
|
585
|
+
method: context.method,
|
586
|
+
path: context.path,
|
587
|
+
user_data: {}
|
588
|
+
)
|
589
|
+
Stripe::Instrumentation.notify(:request_begin, event)
|
590
|
+
|
591
|
+
# This field may be set in the `request_begin` callback. If so, we'll
|
592
|
+
# forward it onto `request_end`.
|
593
|
+
event.user_data
|
594
|
+
end
|
595
|
+
|
596
|
+
private def notify_request_end(context, duration, http_status, num_retries,
|
597
|
+
user_data)
|
598
|
+
return if !Instrumentation.any_subscribers?(:request_end) &&
|
599
|
+
!Instrumentation.any_subscribers?(:request)
|
600
|
+
|
601
|
+
event = Instrumentation::RequestEndEvent.new(
|
602
|
+
duration: duration,
|
603
|
+
http_status: http_status,
|
604
|
+
method: context.method,
|
605
|
+
num_retries: num_retries,
|
606
|
+
path: context.path,
|
607
|
+
user_data: user_data || {}
|
608
|
+
)
|
609
|
+
Stripe::Instrumentation.notify(:request_end, event)
|
610
|
+
|
611
|
+
# The name before `request_begin` was also added. Provided for backwards
|
612
|
+
# compatibility.
|
613
|
+
Stripe::Instrumentation.notify(:request, event)
|
614
|
+
end
|
615
|
+
|
616
|
+
private def general_api_error(status, body)
|
310
617
|
APIError.new("Invalid response object from API: #{body.inspect} " \
|
311
618
|
"(HTTP response code was #{status})",
|
312
619
|
http_status: status, http_body: body)
|
@@ -316,21 +623,21 @@ module Stripe
|
|
316
623
|
# end of a User-Agent string where it'll be fairly prominent in places like
|
317
624
|
# the Dashboard. Note that this formatting has been implemented to match
|
318
625
|
# other libraries, and shouldn't be changed without universal consensus.
|
319
|
-
def format_app_info(info)
|
626
|
+
private def format_app_info(info)
|
320
627
|
str = info[:name]
|
321
628
|
str = "#{str}/#{info[:version]}" unless info[:version].nil?
|
322
629
|
str = "#{str} (#{info[:url]})" unless info[:url].nil?
|
323
630
|
str
|
324
631
|
end
|
325
632
|
|
326
|
-
def handle_error_response(http_resp, context)
|
633
|
+
private def handle_error_response(http_resp, context)
|
327
634
|
begin
|
328
|
-
resp = StripeResponse.
|
635
|
+
resp = StripeResponse.from_net_http(http_resp)
|
329
636
|
error_data = resp.data[:error]
|
330
637
|
|
331
638
|
raise StripeError, "Indeterminate error" unless error_data
|
332
639
|
rescue JSON::ParserError, StripeError
|
333
|
-
raise general_api_error(http_resp
|
640
|
+
raise general_api_error(http_resp.code.to_i, http_resp.body)
|
334
641
|
end
|
335
642
|
|
336
643
|
error = if error_data.is_a?(String)
|
@@ -343,7 +650,29 @@ module Stripe
|
|
343
650
|
raise(error)
|
344
651
|
end
|
345
652
|
|
346
|
-
|
653
|
+
# Works around an edge case where we end up with both query parameters from
|
654
|
+
# parameteers and query parameters that were appended onto the end of the
|
655
|
+
# given path.
|
656
|
+
#
|
657
|
+
# Decode any parameters that were added onto the end of a path and add them
|
658
|
+
# to a unified query parameter hash so that all parameters end up in one
|
659
|
+
# place and all of them are correctly included in the final request.
|
660
|
+
private def merge_query_params(query_params, path)
|
661
|
+
u = URI.parse(path)
|
662
|
+
|
663
|
+
# Return original results if there was nothing to be found.
|
664
|
+
return query_params, path if u.query.nil?
|
665
|
+
|
666
|
+
query_params ||= {}
|
667
|
+
query_params = Hash[URI.decode_www_form(u.query)].merge(query_params)
|
668
|
+
|
669
|
+
# Reset the path minus any query parameters that were specified.
|
670
|
+
path = u.path
|
671
|
+
|
672
|
+
[query_params, path]
|
673
|
+
end
|
674
|
+
|
675
|
+
private def specific_api_error(resp, error_data, context)
|
347
676
|
Util.log_error("Stripe API error",
|
348
677
|
status: resp.http_status,
|
349
678
|
error_code: error_data[:code],
|
@@ -351,7 +680,8 @@ module Stripe
|
|
351
680
|
error_param: error_data[:param],
|
352
681
|
error_type: error_data[:type],
|
353
682
|
idempotency_key: context.idempotency_key,
|
354
|
-
request_id: context.request_id
|
683
|
+
request_id: context.request_id,
|
684
|
+
config: config)
|
355
685
|
|
356
686
|
# The standard set of arguments that can be used to initialize most of
|
357
687
|
# the exceptions.
|
@@ -367,35 +697,32 @@ module Stripe
|
|
367
697
|
when 400, 404
|
368
698
|
case error_data[:type]
|
369
699
|
when "idempotency_error"
|
370
|
-
IdempotencyError.new(error_data[:message], opts)
|
700
|
+
IdempotencyError.new(error_data[:message], **opts)
|
371
701
|
else
|
372
702
|
InvalidRequestError.new(
|
373
703
|
error_data[:message], error_data[:param],
|
374
|
-
opts
|
704
|
+
**opts
|
375
705
|
)
|
376
706
|
end
|
377
707
|
when 401
|
378
|
-
AuthenticationError.new(error_data[:message], opts)
|
708
|
+
AuthenticationError.new(error_data[:message], **opts)
|
379
709
|
when 402
|
380
|
-
# TODO: modify CardError constructor to make code a keyword argument
|
381
|
-
# so we don't have to delete it from opts
|
382
|
-
opts.delete(:code)
|
383
710
|
CardError.new(
|
384
|
-
error_data[:message], error_data[:param],
|
385
|
-
opts
|
711
|
+
error_data[:message], error_data[:param],
|
712
|
+
**opts
|
386
713
|
)
|
387
714
|
when 403
|
388
|
-
PermissionError.new(error_data[:message], opts)
|
715
|
+
PermissionError.new(error_data[:message], **opts)
|
389
716
|
when 429
|
390
|
-
RateLimitError.new(error_data[:message], opts)
|
717
|
+
RateLimitError.new(error_data[:message], **opts)
|
391
718
|
else
|
392
|
-
APIError.new(error_data[:message], opts)
|
719
|
+
APIError.new(error_data[:message], **opts)
|
393
720
|
end
|
394
721
|
end
|
395
722
|
|
396
723
|
# Attempts to look at a response's error code and return an OAuth error if
|
397
724
|
# one matches. Will return `nil` if the code isn't recognized.
|
398
|
-
def specific_oauth_error(resp, error_code, context)
|
725
|
+
private def specific_oauth_error(resp, error_code, context)
|
399
726
|
description = resp.data[:error_description] || error_code
|
400
727
|
|
401
728
|
Util.log_error("Stripe OAuth error",
|
@@ -403,64 +730,61 @@ module Stripe
|
|
403
730
|
error_code: error_code,
|
404
731
|
error_description: description,
|
405
732
|
idempotency_key: context.idempotency_key,
|
406
|
-
request_id: context.request_id
|
733
|
+
request_id: context.request_id,
|
734
|
+
config: config)
|
407
735
|
|
408
|
-
args =
|
736
|
+
args = {
|
409
737
|
http_status: resp.http_status, http_body: resp.http_body,
|
410
738
|
json_body: resp.data, http_headers: resp.http_headers,
|
411
|
-
}
|
739
|
+
}
|
412
740
|
|
413
741
|
case error_code
|
414
|
-
when "invalid_client"
|
415
|
-
|
416
|
-
when "
|
417
|
-
|
418
|
-
when "
|
419
|
-
|
742
|
+
when "invalid_client"
|
743
|
+
OAuth::InvalidClientError.new(error_code, description, **args)
|
744
|
+
when "invalid_grant"
|
745
|
+
OAuth::InvalidGrantError.new(error_code, description, **args)
|
746
|
+
when "invalid_request"
|
747
|
+
OAuth::InvalidRequestError.new(error_code, description, **args)
|
748
|
+
when "invalid_scope"
|
749
|
+
OAuth::InvalidScopeError.new(error_code, description, **args)
|
750
|
+
when "unsupported_grant_type"
|
751
|
+
OAuth::UnsupportedGrantTypeError.new(error_code, description, **args)
|
752
|
+
when "unsupported_response_type"
|
753
|
+
OAuth::UnsupportedResponseTypeError.new(error_code, description, **args)
|
420
754
|
else
|
421
755
|
# We'd prefer that all errors are typed, but we create a generic
|
422
756
|
# OAuthError in case we run into a code that we don't recognize.
|
423
|
-
OAuth::OAuthError.new(
|
757
|
+
OAuth::OAuthError.new(error_code, description, **args)
|
424
758
|
end
|
425
759
|
end
|
426
760
|
|
427
|
-
def handle_network_error(
|
761
|
+
private def handle_network_error(error, context, num_retries,
|
762
|
+
api_base = nil)
|
428
763
|
Util.log_error("Stripe network error",
|
429
|
-
error_message:
|
764
|
+
error_message: error.message,
|
430
765
|
idempotency_key: context.idempotency_key,
|
431
|
-
request_id: context.request_id
|
432
|
-
|
433
|
-
case e
|
434
|
-
when Faraday::ConnectionFailed
|
435
|
-
message = "Unexpected error communicating when trying to connect to Stripe. " \
|
436
|
-
"You may be seeing this message because your DNS is not working. " \
|
437
|
-
"To check, try running 'host stripe.com' from the command line."
|
438
|
-
|
439
|
-
when Faraday::SSLError
|
440
|
-
message = "Could not establish a secure connection to Stripe, you may " \
|
441
|
-
"need to upgrade your OpenSSL version. To check, try running " \
|
442
|
-
"'openssl s_client -connect api.stripe.com:443' from the " \
|
443
|
-
"command line."
|
444
|
-
|
445
|
-
when Faraday::TimeoutError
|
446
|
-
api_base ||= Stripe.api_base
|
447
|
-
message = "Could not connect to Stripe (#{api_base}). " \
|
448
|
-
"Please check your internet connection and try again. " \
|
449
|
-
"If this problem persists, you should check Stripe's service status at " \
|
450
|
-
"https://twitter.com/stripestatus, or let us know at support@stripe.com."
|
766
|
+
request_id: context.request_id,
|
767
|
+
config: config)
|
451
768
|
|
452
|
-
|
453
|
-
|
454
|
-
|
769
|
+
errors, message = NETWORK_ERROR_MESSAGES_MAP.detect do |(e, _)|
|
770
|
+
error.is_a?(e)
|
771
|
+
end
|
455
772
|
|
773
|
+
if errors.nil?
|
774
|
+
message = "Unexpected error #{error.class.name} communicating " \
|
775
|
+
"with Stripe. Please let us know at support@stripe.com."
|
456
776
|
end
|
457
777
|
|
778
|
+
api_base ||= config.api_base
|
779
|
+
message = message % api_base
|
780
|
+
|
458
781
|
message += " Request was retried #{num_retries} times." if num_retries > 0
|
459
782
|
|
460
|
-
raise APIConnectionError,
|
783
|
+
raise APIConnectionError,
|
784
|
+
message + "\n\n(Network error: #{error.message})"
|
461
785
|
end
|
462
786
|
|
463
|
-
def request_headers(api_key, method)
|
787
|
+
private def request_headers(api_key, method)
|
464
788
|
user_agent = "Stripe/v1 RubyBindings/#{Stripe::VERSION}"
|
465
789
|
unless Stripe.app_info.nil?
|
466
790
|
user_agent += " " + format_app_info(Stripe.app_info)
|
@@ -472,18 +796,20 @@ module Stripe
|
|
472
796
|
"Content-Type" => "application/x-www-form-urlencoded",
|
473
797
|
}
|
474
798
|
|
475
|
-
if
|
476
|
-
headers["X-Stripe-Client-Telemetry"] = JSON.generate(
|
799
|
+
if config.enable_telemetry? && !@last_request_metrics.nil?
|
800
|
+
headers["X-Stripe-Client-Telemetry"] = JSON.generate(
|
801
|
+
last_request_metrics: @last_request_metrics.payload
|
802
|
+
)
|
477
803
|
end
|
478
804
|
|
479
805
|
# It is only safe to retry network failures on post and delete
|
480
806
|
# requests if we add an Idempotency-Key header
|
481
|
-
if %i[post delete].include?(method) &&
|
807
|
+
if %i[post delete].include?(method) && config.max_network_retries > 0
|
482
808
|
headers["Idempotency-Key"] ||= SecureRandom.uuid
|
483
809
|
end
|
484
810
|
|
485
|
-
headers["Stripe-Version"] =
|
486
|
-
headers["Stripe-Account"] =
|
811
|
+
headers["Stripe-Version"] = config.api_version if config.api_version
|
812
|
+
headers["Stripe-Account"] = config.stripe_account if config.stripe_account
|
487
813
|
|
488
814
|
user_agent = @system_profiler.user_agent
|
489
815
|
begin
|
@@ -500,54 +826,59 @@ module Stripe
|
|
500
826
|
headers
|
501
827
|
end
|
502
828
|
|
503
|
-
def log_request(context, num_retries)
|
829
|
+
private def log_request(context, num_retries)
|
504
830
|
Util.log_info("Request to Stripe API",
|
505
831
|
account: context.account,
|
506
832
|
api_version: context.api_version,
|
507
833
|
idempotency_key: context.idempotency_key,
|
508
834
|
method: context.method,
|
509
835
|
num_retries: num_retries,
|
510
|
-
path: context.path
|
836
|
+
path: context.path,
|
837
|
+
config: config)
|
511
838
|
Util.log_debug("Request details",
|
512
839
|
body: context.body,
|
513
840
|
idempotency_key: context.idempotency_key,
|
514
|
-
|
841
|
+
query: context.query,
|
842
|
+
config: config)
|
515
843
|
end
|
516
|
-
private :log_request
|
517
844
|
|
518
|
-
def log_response(context, request_start, status, body)
|
845
|
+
private def log_response(context, request_start, status, body)
|
519
846
|
Util.log_info("Response from Stripe API",
|
520
847
|
account: context.account,
|
521
848
|
api_version: context.api_version,
|
522
|
-
elapsed:
|
849
|
+
elapsed: Util.monotonic_time - request_start,
|
523
850
|
idempotency_key: context.idempotency_key,
|
524
851
|
method: context.method,
|
525
852
|
path: context.path,
|
526
853
|
request_id: context.request_id,
|
527
|
-
status: status
|
854
|
+
status: status,
|
855
|
+
config: config)
|
528
856
|
Util.log_debug("Response details",
|
529
857
|
body: body,
|
530
858
|
idempotency_key: context.idempotency_key,
|
531
|
-
request_id: context.request_id
|
859
|
+
request_id: context.request_id,
|
860
|
+
config: config)
|
532
861
|
|
533
862
|
return unless context.request_id
|
534
863
|
|
535
864
|
Util.log_debug("Dashboard link for request",
|
536
865
|
idempotency_key: context.idempotency_key,
|
537
866
|
request_id: context.request_id,
|
538
|
-
url: Util.request_id_dashboard_url(context.request_id,
|
867
|
+
url: Util.request_id_dashboard_url(context.request_id,
|
868
|
+
context.api_key),
|
869
|
+
config: config)
|
539
870
|
end
|
540
|
-
private :log_response
|
541
871
|
|
542
|
-
def log_response_error(context, request_start,
|
872
|
+
private def log_response_error(context, request_start, error)
|
873
|
+
elapsed = request_start ? Util.monotonic_time - request_start : nil
|
543
874
|
Util.log_error("Request error",
|
544
|
-
elapsed:
|
545
|
-
error_message:
|
875
|
+
elapsed: elapsed,
|
876
|
+
error_message: error.message,
|
546
877
|
idempotency_key: context.idempotency_key,
|
547
878
|
method: context.method,
|
548
|
-
path: context.path
|
879
|
+
path: context.path,
|
880
|
+
config: config)
|
549
881
|
end
|
550
|
-
private :log_response_error
|
551
882
|
|
552
883
|
# RequestLogContext stores information about a request that's begin made so
|
553
884
|
# that we can log certain information. It's useful because it means that we
|
@@ -560,7 +891,7 @@ module Stripe
|
|
560
891
|
attr_accessor :idempotency_key
|
561
892
|
attr_accessor :method
|
562
893
|
attr_accessor :path
|
563
|
-
attr_accessor :
|
894
|
+
attr_accessor :query
|
564
895
|
attr_accessor :request_id
|
565
896
|
|
566
897
|
# The idea with this method is that we might want to update some of
|
@@ -569,18 +900,7 @@ module Stripe
|
|
569
900
|
# with for a request. For example, we should trust whatever came back in
|
570
901
|
# a `Stripe-Version` header beyond what configuration information that we
|
571
902
|
# might have had available.
|
572
|
-
def
|
573
|
-
return self if resp.nil?
|
574
|
-
|
575
|
-
# Faraday's API is a little unusual. Normally it'll produce a response
|
576
|
-
# object with a `headers` method, but on error what it puts into
|
577
|
-
# `e.response` is an untyped `Hash`.
|
578
|
-
headers = if resp.is_a?(Faraday::Response)
|
579
|
-
resp.headers
|
580
|
-
else
|
581
|
-
resp[:headers]
|
582
|
-
end
|
583
|
-
|
903
|
+
def dup_from_response_headers(headers)
|
584
904
|
context = dup
|
585
905
|
context.account = headers["Stripe-Account"]
|
586
906
|
context.api_version = headers["Stripe-Version"]
|
@@ -630,7 +950,8 @@ module Stripe
|
|
630
950
|
end
|
631
951
|
|
632
952
|
def user_agent
|
633
|
-
lang_version = "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL}
|
953
|
+
lang_version = "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} " \
|
954
|
+
"(#{RUBY_RELEASE_DATE})"
|
634
955
|
|
635
956
|
{
|
636
957
|
application: Stripe.app_info,
|
@@ -646,7 +967,8 @@ module Stripe
|
|
646
967
|
end
|
647
968
|
end
|
648
969
|
|
649
|
-
# StripeRequestMetrics tracks metadata to be reported to stripe for metrics
|
970
|
+
# StripeRequestMetrics tracks metadata to be reported to stripe for metrics
|
971
|
+
# collection
|
650
972
|
class StripeRequestMetrics
|
651
973
|
# The Stripe request ID of the response.
|
652
974
|
attr_accessor :request_id
|