stripe 2.0.3 → 5.55.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +1036 -0
- data/CODE_OF_CONDUCT.md +77 -0
- data/Gemfile +24 -23
- data/History.txt +1 -593
- data/Makefile +7 -0
- data/README.md +252 -53
- data/Rakefile +18 -24
- data/VERSION +1 -1
- data/bin/stripe-console +5 -3
- data/lib/stripe/api_operations/create.rb +4 -2
- data/lib/stripe/api_operations/delete.rb +31 -3
- data/lib/stripe/api_operations/list.rb +4 -13
- data/lib/stripe/api_operations/nested_resource.rb +73 -0
- data/lib/stripe/api_operations/request.rb +92 -10
- data/lib/stripe/api_operations/save.rb +25 -13
- data/lib/stripe/api_operations/search.rb +19 -0
- data/lib/stripe/api_resource.rb +55 -11
- data/lib/stripe/api_resource_test_helpers.rb +47 -0
- data/lib/stripe/connection_manager.rb +200 -0
- data/lib/stripe/error_object.rb +93 -0
- data/lib/stripe/errors.rb +98 -13
- data/lib/stripe/instrumentation.rb +84 -0
- data/lib/stripe/list_object.rb +55 -21
- data/lib/stripe/multipart_encoder.rb +131 -0
- data/lib/stripe/oauth.rb +65 -0
- data/lib/stripe/object_types.rb +119 -0
- data/lib/stripe/{account.rb → resources/account.rb} +75 -38
- data/lib/stripe/resources/account_link.rb +10 -0
- data/lib/stripe/resources/alipay_account.rb +34 -0
- data/lib/stripe/{apple_pay_domain.rb → resources/apple_pay_domain.rb} +6 -1
- data/lib/stripe/resources/application_fee.rb +14 -0
- data/lib/stripe/resources/application_fee_refund.rb +31 -0
- data/lib/stripe/resources/balance.rb +8 -0
- data/lib/stripe/resources/balance_transaction.rb +10 -0
- data/lib/stripe/resources/bank_account.rb +43 -0
- data/lib/stripe/resources/billing_portal/configuration.rb +14 -0
- data/lib/stripe/resources/billing_portal/session.rb +12 -0
- data/lib/stripe/{bitcoin_receiver.rb → resources/bitcoin_receiver.rb} +8 -4
- data/lib/stripe/resources/bitcoin_transaction.rb +16 -0
- data/lib/stripe/resources/capability.rb +34 -0
- data/lib/stripe/resources/card.rb +38 -0
- data/lib/stripe/resources/cash_balance.rb +22 -0
- data/lib/stripe/resources/charge.rb +32 -0
- data/lib/stripe/resources/checkout/session.rb +27 -0
- data/lib/stripe/resources/country_spec.rb +10 -0
- data/lib/stripe/{coupon.rb → resources/coupon.rb} +6 -1
- data/lib/stripe/resources/credit_note.rb +33 -0
- data/lib/stripe/resources/credit_note_line_item.rb +8 -0
- data/lib/stripe/resources/customer.rb +74 -0
- data/lib/stripe/resources/customer_balance_transaction.rb +31 -0
- data/lib/stripe/resources/discount.rb +8 -0
- data/lib/stripe/resources/dispute.rb +22 -0
- data/lib/stripe/resources/ephemeral_key.rb +20 -0
- data/lib/stripe/resources/event.rb +10 -0
- data/lib/stripe/resources/exchange_rate.rb +10 -0
- data/lib/stripe/resources/file.rb +36 -0
- data/lib/stripe/resources/file_link.rb +12 -0
- data/lib/stripe/resources/financial_connections/account.rb +31 -0
- data/lib/stripe/resources/financial_connections/account_owner.rb +10 -0
- data/lib/stripe/resources/financial_connections/account_ownership.rb +10 -0
- data/lib/stripe/resources/financial_connections/session.rb +12 -0
- data/lib/stripe/resources/funding_instructions.rb +16 -0
- data/lib/stripe/resources/identity/verification_report.rb +12 -0
- data/lib/stripe/resources/identity/verification_session.rb +35 -0
- data/lib/stripe/resources/invoice.rb +83 -0
- data/lib/stripe/{invoice_item.rb → resources/invoice_item.rb} +6 -1
- data/lib/stripe/resources/invoice_line_item.rb +8 -0
- data/lib/stripe/resources/issuing/authorization.rb +34 -0
- data/lib/stripe/resources/issuing/card.rb +25 -0
- data/lib/stripe/resources/issuing/card_details.rb +10 -0
- data/lib/stripe/resources/issuing/cardholder.rb +14 -0
- data/lib/stripe/resources/issuing/dispute.rb +25 -0
- data/lib/stripe/resources/issuing/transaction.rb +13 -0
- data/lib/stripe/resources/line_item.rb +8 -0
- data/lib/stripe/resources/login_link.rb +15 -0
- data/lib/stripe/resources/mandate.rb +8 -0
- data/lib/stripe/resources/order.rb +33 -0
- data/lib/stripe/resources/order_return.rb +10 -0
- data/lib/stripe/resources/payment_intent.rb +82 -0
- data/lib/stripe/resources/payment_link.rb +23 -0
- data/lib/stripe/resources/payment_method.rb +33 -0
- data/lib/stripe/resources/payout.rb +33 -0
- data/lib/stripe/resources/person.rb +32 -0
- data/lib/stripe/{plan.rb → resources/plan.rb} +5 -0
- data/lib/stripe/resources/price.rb +21 -0
- data/lib/stripe/resources/product.rb +22 -0
- data/lib/stripe/resources/promotion_code.rb +12 -0
- data/lib/stripe/resources/quote.rb +105 -0
- data/lib/stripe/resources/radar/early_fraud_warning.rb +12 -0
- data/lib/stripe/resources/radar/value_list.rb +15 -0
- data/lib/stripe/resources/radar/value_list_item.rb +14 -0
- data/lib/stripe/resources/recipient.rb +14 -0
- data/lib/stripe/resources/recipient_transfer.rb +7 -0
- data/lib/stripe/resources/refund.rb +42 -0
- data/lib/stripe/resources/reporting/report_run.rb +13 -0
- data/lib/stripe/resources/reporting/report_type.rb +13 -0
- data/lib/stripe/resources/reversal.rb +30 -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 +43 -0
- data/lib/stripe/resources/shipping_rate.rb +12 -0
- data/lib/stripe/resources/sigma/scheduled_query_run.rb +16 -0
- data/lib/stripe/{sku.rb → resources/sku.rb} +7 -2
- data/lib/stripe/resources/source.rb +47 -0
- data/lib/stripe/resources/source_transaction.rb +8 -0
- data/lib/stripe/resources/subscription.rb +35 -0
- data/lib/stripe/resources/subscription_item.rb +26 -0
- data/lib/stripe/resources/subscription_schedule.rb +33 -0
- data/lib/stripe/resources/tax_code.rb +10 -0
- data/lib/stripe/resources/tax_id.rb +27 -0
- data/lib/stripe/{product.rb → resources/tax_rate.rb} +7 -3
- data/lib/stripe/resources/terminal/configuration.rb +15 -0
- data/lib/stripe/resources/terminal/connection_token.rb +12 -0
- data/lib/stripe/resources/terminal/location.rb +15 -0
- data/lib/stripe/resources/terminal/reader.rb +75 -0
- data/lib/stripe/resources/test_helpers/test_clock.rb +25 -0
- data/lib/stripe/{three_d_secure.rb → resources/three_d_secure.rb} +5 -0
- data/lib/stripe/resources/token.rb +10 -0
- data/lib/stripe/resources/topup.rb +23 -0
- data/lib/stripe/resources/transfer.rb +27 -0
- data/lib/stripe/resources/usage_record.rb +8 -0
- data/lib/stripe/resources/usage_record_summary.rb +8 -0
- data/lib/stripe/{recipient.rb → resources/webhook_endpoint.rb} +6 -5
- data/lib/stripe/resources.rb +100 -0
- data/lib/stripe/search_result_object.rb +86 -0
- data/lib/stripe/singleton_api_resource.rb +10 -4
- data/lib/stripe/stripe_client.rb +886 -223
- data/lib/stripe/stripe_configuration.rb +194 -0
- data/lib/stripe/stripe_object.rb +301 -132
- data/lib/stripe/stripe_response.rb +89 -27
- data/lib/stripe/util.rb +272 -122
- data/lib/stripe/version.rb +3 -1
- data/lib/stripe/webhook.rb +121 -0
- data/lib/stripe.rb +94 -116
- data/stripe.gemspec +35 -16
- metadata +127 -156
- data/.gitattributes +0 -4
- data/.github/ISSUE_TEMPLATE.md +0 -5
- data/.gitignore +0 -5
- data/.travis.yml +0 -15
- data/lib/stripe/alipay_account.rb +0 -20
- data/lib/stripe/application_fee.rb +0 -20
- data/lib/stripe/application_fee_refund.rb +0 -18
- data/lib/stripe/balance.rb +0 -4
- data/lib/stripe/balance_transaction.rb +0 -9
- data/lib/stripe/bank_account.rb +0 -28
- data/lib/stripe/bitcoin_transaction.rb +0 -9
- data/lib/stripe/card.rb +0 -25
- data/lib/stripe/charge.rb +0 -80
- data/lib/stripe/country_spec.rb +0 -9
- data/lib/stripe/customer.rb +0 -77
- data/lib/stripe/dispute.rb +0 -15
- data/lib/stripe/event.rb +0 -5
- data/lib/stripe/file_upload.rb +0 -31
- data/lib/stripe/invoice.rb +0 -27
- data/lib/stripe/order.rb +0 -27
- data/lib/stripe/order_return.rb +0 -9
- data/lib/stripe/refund.rb +0 -7
- data/lib/stripe/reversal.rb +0 -18
- data/lib/stripe/source.rb +0 -11
- data/lib/stripe/subscription.rb +0 -31
- data/lib/stripe/subscription_item.rb +0 -12
- data/lib/stripe/token.rb +0 -5
- data/lib/stripe/transfer.rb +0 -16
- data/openapi/fixtures.json +0 -1383
- data/openapi/fixtures.yaml +0 -1130
- data/openapi/spec.json +0 -19749
- data/openapi/spec.yaml +0 -15359
- data/test/api_fixtures.rb +0 -29
- data/test/api_stub_helpers.rb +0 -125
- data/test/stripe/account_test.rb +0 -204
- data/test/stripe/alipay_account_test.rb +0 -19
- data/test/stripe/api_operations_test.rb +0 -31
- data/test/stripe/api_resource_test.rb +0 -496
- data/test/stripe/apple_pay_domain_test.rb +0 -33
- data/test/stripe/application_fee_refund_test.rb +0 -38
- data/test/stripe/application_fee_test.rb +0 -14
- data/test/stripe/balance_test.rb +0 -11
- data/test/stripe/bank_account_test.rb +0 -41
- data/test/stripe/bitcoin_receiver_test.rb +0 -70
- data/test/stripe/bitcoin_transaction_test.rb +0 -21
- data/test/stripe/charge_test.rb +0 -59
- data/test/stripe/country_spec_test.rb +0 -20
- data/test/stripe/coupon_test.rb +0 -44
- data/test/stripe/customer_card_test.rb +0 -42
- data/test/stripe/customer_test.rb +0 -116
- data/test/stripe/dispute_test.rb +0 -42
- data/test/stripe/errors_test.rb +0 -18
- data/test/stripe/file_upload_test.rb +0 -66
- data/test/stripe/invoice_item_test.rb +0 -55
- data/test/stripe/invoice_test.rb +0 -66
- data/test/stripe/list_object_test.rb +0 -170
- data/test/stripe/order_return_test.rb +0 -21
- data/test/stripe/order_test.rb +0 -59
- data/test/stripe/plan_test.rb +0 -52
- data/test/stripe/product_test.rb +0 -47
- data/test/stripe/recipient_card_test.rb +0 -40
- data/test/stripe/recipient_test.rb +0 -50
- data/test/stripe/refund_test.rb +0 -39
- data/test/stripe/reversal_test.rb +0 -43
- data/test/stripe/sku_test.rb +0 -50
- data/test/stripe/source_test.rb +0 -43
- data/test/stripe/stripe_client_test.rb +0 -440
- data/test/stripe/stripe_object_test.rb +0 -361
- data/test/stripe/stripe_response_test.rb +0 -46
- data/test/stripe/subscription_item_test.rb +0 -54
- data/test/stripe/subscription_test.rb +0 -60
- data/test/stripe/three_d_secure_test.rb +0 -23
- data/test/stripe/transfer_test.rb +0 -50
- data/test/stripe/util_test.rb +0 -149
- data/test/stripe_test.rb +0 -41
- data/test/test_data.rb +0 -59
- data/test/test_helper.rb +0 -41
@@ -1,11 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Stripe
|
2
4
|
module APIOperations
|
3
5
|
module Request
|
4
6
|
module ClassMethods
|
5
|
-
|
7
|
+
def execute_resource_request(method, url,
|
8
|
+
params = {}, opts = {})
|
9
|
+
execute_resource_request_internal(
|
10
|
+
:execute_request, method, url, params, opts
|
11
|
+
)
|
12
|
+
end
|
13
|
+
|
14
|
+
def execute_resource_request_stream(method, url,
|
15
|
+
params = {}, opts = {},
|
16
|
+
&read_body_chunk_block)
|
17
|
+
execute_resource_request_internal(
|
18
|
+
:execute_request_stream,
|
19
|
+
method,
|
20
|
+
url,
|
21
|
+
params,
|
22
|
+
opts,
|
23
|
+
&read_body_chunk_block
|
24
|
+
)
|
25
|
+
end
|
26
|
+
|
27
|
+
private def execute_resource_request_internal(client_request_method_sym,
|
28
|
+
method, url,
|
29
|
+
params, opts,
|
30
|
+
&read_body_chunk_block)
|
31
|
+
params ||= {}
|
32
|
+
|
33
|
+
error_on_invalid_params(params)
|
34
|
+
warn_on_opts_in_params(params)
|
6
35
|
|
7
|
-
def request(method, url, params={}, opts={})
|
8
36
|
opts = Util.normalize_opts(opts)
|
37
|
+
error_on_non_string_user_opts(opts)
|
38
|
+
|
9
39
|
opts[:client] ||= StripeClient.active_client
|
10
40
|
|
11
41
|
headers = opts.clone
|
@@ -14,34 +44,86 @@ module Stripe
|
|
14
44
|
client = headers.delete(:client)
|
15
45
|
# Assume all remaining opts must be headers
|
16
46
|
|
17
|
-
resp, opts[:api_key] = client.
|
47
|
+
resp, opts[:api_key] = client.send(
|
48
|
+
client_request_method_sym,
|
18
49
|
method, url,
|
19
50
|
api_base: api_base, api_key: api_key,
|
20
|
-
headers: headers, params: params
|
51
|
+
headers: headers, params: params,
|
52
|
+
&read_body_chunk_block
|
21
53
|
)
|
22
54
|
|
23
55
|
# Hash#select returns an array before 1.9
|
24
56
|
opts_to_persist = {}
|
25
57
|
opts.each do |k, v|
|
26
|
-
if
|
27
|
-
opts_to_persist[k] = v
|
28
|
-
end
|
58
|
+
opts_to_persist[k] = v if Util::OPTS_PERSISTABLE.include?(k)
|
29
59
|
end
|
30
60
|
|
31
61
|
[resp, opts_to_persist]
|
32
62
|
end
|
63
|
+
|
64
|
+
# This method used to be called `request`, but it's such a short name
|
65
|
+
# that it eventually conflicted with the name of a field on an API
|
66
|
+
# resource (specifically, `Event#request`), so it was renamed to
|
67
|
+
# something more unique.
|
68
|
+
#
|
69
|
+
# The former name had been around for just about forever though, and
|
70
|
+
# although all internal uses have been renamed, I've left this alias in
|
71
|
+
# place for backwards compatibility. Consider removing it on the next
|
72
|
+
# major.
|
73
|
+
alias request execute_resource_request
|
74
|
+
|
75
|
+
private def error_on_non_string_user_opts(opts)
|
76
|
+
Util::OPTS_USER_SPECIFIED.each do |opt|
|
77
|
+
next unless opts.key?(opt)
|
78
|
+
|
79
|
+
val = opts[opt]
|
80
|
+
next if val.nil?
|
81
|
+
next if val.is_a?(String)
|
82
|
+
|
83
|
+
raise ArgumentError,
|
84
|
+
"request option '#{opt}' should be a string value " \
|
85
|
+
"(was a #{val.class})"
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
private def error_on_invalid_params(params)
|
90
|
+
return if params.nil? || params.is_a?(Hash)
|
91
|
+
|
92
|
+
raise ArgumentError,
|
93
|
+
"request params should be either a Hash or nil " \
|
94
|
+
"(was a #{params.class})"
|
95
|
+
end
|
96
|
+
|
97
|
+
private def warn_on_opts_in_params(params)
|
98
|
+
Util::OPTS_USER_SPECIFIED.each do |opt|
|
99
|
+
if params.key?(opt)
|
100
|
+
warn("WARNING: '#{opt}' should be in opts instead of params.")
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
33
104
|
end
|
34
105
|
|
35
106
|
def self.included(base)
|
36
107
|
base.extend(ClassMethods)
|
37
108
|
end
|
38
109
|
|
39
|
-
protected
|
110
|
+
protected def execute_resource_request(method, url,
|
111
|
+
params = {}, opts = {})
|
112
|
+
opts = @opts.merge(Util.normalize_opts(opts))
|
113
|
+
self.class.execute_resource_request(method, url, params, opts)
|
114
|
+
end
|
40
115
|
|
41
|
-
def
|
116
|
+
protected def execute_resource_request_stream(method, url,
|
117
|
+
params = {}, opts = {},
|
118
|
+
&read_body_chunk_block)
|
42
119
|
opts = @opts.merge(Util.normalize_opts(opts))
|
43
|
-
self.class.
|
120
|
+
self.class.execute_resource_request_stream(
|
121
|
+
method, url, params, opts, &read_body_chunk_block
|
122
|
+
)
|
44
123
|
end
|
124
|
+
|
125
|
+
# See notes on `alias` above.
|
126
|
+
alias request execute_resource_request
|
45
127
|
end
|
46
128
|
end
|
47
129
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Stripe
|
2
4
|
module APIOperations
|
3
5
|
module Save
|
@@ -13,15 +15,17 @@ module Stripe
|
|
13
15
|
# * +opts+ - A Hash of additional options (separate from the params /
|
14
16
|
# object values) to be added to the request. E.g. to allow for an
|
15
17
|
# idempotency_key to be passed in the request headers, or for the
|
16
|
-
# api_key to be overwritten. See
|
17
|
-
|
18
|
-
|
19
|
-
|
18
|
+
# api_key to be overwritten. See
|
19
|
+
# {APIOperations::Request.execute_resource_request}.
|
20
|
+
def update(id, params = {}, opts = {})
|
21
|
+
params.each_key do |k|
|
22
|
+
if protected_fields.include?(k)
|
20
23
|
raise ArgumentError, "Cannot update protected field: #{k}"
|
21
24
|
end
|
22
25
|
end
|
23
26
|
|
24
|
-
resp, opts =
|
27
|
+
resp, opts = execute_resource_request(:post, "#{resource_url}/#{id}",
|
28
|
+
params, opts)
|
25
29
|
Util.convert_to_stripe_object(resp.data, opts)
|
26
30
|
end
|
27
31
|
end
|
@@ -41,8 +45,9 @@ module Stripe
|
|
41
45
|
# * +opts+ - A Hash of additional options (separate from the params /
|
42
46
|
# object values) to be added to the request. E.g. to allow for an
|
43
47
|
# idempotency_key to be passed in the request headers, or for the
|
44
|
-
# api_key to be overwritten. See
|
45
|
-
|
48
|
+
# api_key to be overwritten. See
|
49
|
+
# {APIOperations::Request.execute_resource_request}.
|
50
|
+
def save(params = {}, opts = {})
|
46
51
|
# We started unintentionally (sort of) allowing attributes sent to
|
47
52
|
# +save+ to override values used during the update. So as not to break
|
48
53
|
# the API, this makes that official here.
|
@@ -51,30 +56,37 @@ module Stripe
|
|
51
56
|
# Now remove any parameters that look like object attributes.
|
52
57
|
params = params.reject { |k, _| respond_to?(k) }
|
53
58
|
|
54
|
-
values =
|
59
|
+
values = serialize_params(self).merge(params)
|
55
60
|
|
56
61
|
# note that id gets removed here our call to #url above has already
|
57
62
|
# generated a uri for this object with an identifier baked in
|
58
63
|
values.delete(:id)
|
59
64
|
|
60
|
-
resp, opts =
|
65
|
+
resp, opts = execute_resource_request(:post, save_url, values, opts)
|
61
66
|
initialize_from(resp.data, opts)
|
62
67
|
end
|
63
68
|
|
64
69
|
def self.included(base)
|
70
|
+
# Set `metadata` as additive so that when it's set directly we remember
|
71
|
+
# to clear keys that may have been previously set by sending empty
|
72
|
+
# values for them.
|
73
|
+
#
|
74
|
+
# It's possible that not every object with `Save` has `metadata`, but
|
75
|
+
# it's a close enough heuristic, and having this option set when there
|
76
|
+
# is no `metadata` field is not harmful.
|
77
|
+
base.additive_object_param(:metadata)
|
78
|
+
|
65
79
|
base.extend(ClassMethods)
|
66
80
|
end
|
67
81
|
|
68
|
-
private
|
69
|
-
|
70
|
-
def save_url
|
82
|
+
private def save_url
|
71
83
|
# This switch essentially allows us "upsert"-like functionality. If the
|
72
84
|
# API resource doesn't have an ID set (suggesting that it's new) and
|
73
85
|
# its class responds to .create (which comes from
|
74
86
|
# Stripe::APIOperations::Create), then use the URL to create a new
|
75
87
|
# resource. Otherwise, generate a URL based on the object's identifier
|
76
88
|
# for a normal update.
|
77
|
-
if self[:id]
|
89
|
+
if self[:id].nil? && self.class.respond_to?(:create)
|
78
90
|
self.class.resource_url
|
79
91
|
else
|
80
92
|
resource_url
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Stripe
|
4
|
+
module APIOperations
|
5
|
+
module Search
|
6
|
+
def _search(search_url, filters = {}, opts = {})
|
7
|
+
opts = Util.normalize_opts(opts)
|
8
|
+
|
9
|
+
resp, opts = execute_resource_request(:get, search_url, filters, opts)
|
10
|
+
obj = SearchResultObject.construct_from(resp.data, opts)
|
11
|
+
|
12
|
+
# set filters so that we can fetch the same limit and query
|
13
|
+
# when accessing the next page
|
14
|
+
obj.filters = filters.dup
|
15
|
+
obj
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/stripe/api_resource.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Stripe
|
2
4
|
class APIResource < StripeObject
|
3
5
|
include Stripe::APIOperations::Request
|
@@ -10,14 +12,18 @@ module Stripe
|
|
10
12
|
attr_accessor :save_with_parent
|
11
13
|
|
12
14
|
def self.class_name
|
13
|
-
|
15
|
+
name.split("::")[-1]
|
14
16
|
end
|
15
17
|
|
16
18
|
def self.resource_url
|
17
19
|
if self == APIResource
|
18
|
-
raise NotImplementedError
|
20
|
+
raise NotImplementedError,
|
21
|
+
"APIResource is an abstract class. You should perform actions " \
|
22
|
+
"on its subclasses (Charge, Customer, etc.)"
|
19
23
|
end
|
20
|
-
|
24
|
+
# Namespaces are separated in object names with periods (.) and in URLs
|
25
|
+
# with forward slashes (/), so replace the former with the latter.
|
26
|
+
"/v1/#{self::OBJECT_NAME.downcase.tr('.', '/')}s"
|
21
27
|
end
|
22
28
|
|
23
29
|
# A metaprogramming call that specifies that a field of a resource can be
|
@@ -38,31 +44,69 @@ module Stripe
|
|
38
44
|
# Note that the value may be subresource, but could also be a scalar
|
39
45
|
# (like a tokenized card's ID for example), so we check the type before
|
40
46
|
# setting #save_with_parent here.
|
41
|
-
if value.is_a?(APIResource)
|
42
|
-
value.save_with_parent = true
|
43
|
-
end
|
47
|
+
value.save_with_parent = true if value.is_a?(APIResource)
|
44
48
|
|
45
49
|
value
|
46
50
|
end
|
47
51
|
end
|
48
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
|
+
Util.custom_method self, self, name, http_verb, http_path
|
67
|
+
end
|
68
|
+
|
49
69
|
def resource_url
|
50
|
-
unless id = self[
|
51
|
-
raise InvalidRequestError.new(
|
70
|
+
unless (id = self["id"])
|
71
|
+
raise InvalidRequestError.new(
|
72
|
+
"Could not determine which URL to request: #{self.class} instance " \
|
73
|
+
"has invalid ID: #{id.inspect}",
|
74
|
+
"id"
|
75
|
+
)
|
52
76
|
end
|
53
77
|
"#{self.class.resource_url}/#{CGI.escape(id)}"
|
54
78
|
end
|
55
79
|
|
56
80
|
def refresh
|
57
|
-
resp, opts =
|
81
|
+
resp, opts = execute_resource_request(:get, resource_url,
|
82
|
+
@retrieve_params)
|
58
83
|
initialize_from(resp.data, opts)
|
59
84
|
end
|
60
85
|
|
61
|
-
def self.retrieve(id, opts={})
|
86
|
+
def self.retrieve(id, opts = {})
|
62
87
|
opts = Util.normalize_opts(opts)
|
63
|
-
instance =
|
88
|
+
instance = new(id, opts)
|
64
89
|
instance.refresh
|
65
90
|
instance
|
66
91
|
end
|
92
|
+
|
93
|
+
def request_stripe_object(method:, path:, params:, opts: {})
|
94
|
+
resp, opts = execute_resource_request(method, path, params, opts)
|
95
|
+
|
96
|
+
# If we're getting back this thing, update; otherwise, instantiate.
|
97
|
+
if Util.object_name_matches_class?(resp.data[:object], self.class)
|
98
|
+
initialize_from(resp.data, opts)
|
99
|
+
else
|
100
|
+
Util.convert_to_stripe_object(resp.data, opts)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
protected def request_stream(method:, path:, params:, opts: {},
|
105
|
+
&read_body_chunk_block)
|
106
|
+
resp, = execute_resource_request_stream(
|
107
|
+
method, path, params, opts, &read_body_chunk_block
|
108
|
+
)
|
109
|
+
resp
|
110
|
+
end
|
67
111
|
end
|
68
112
|
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Stripe
|
4
|
+
# The base class for nested TestHelpers classes in resource objects.
|
5
|
+
# The APIResourceTestHelpers handles URL generation and custom method
|
6
|
+
# support for test-helper methods.
|
7
|
+
#
|
8
|
+
# class MyAPIResource < APIResource
|
9
|
+
# class TestHelpers < APIResourceTestHelpers
|
10
|
+
class APIResourceTestHelpers
|
11
|
+
def initialize(resource)
|
12
|
+
@resource = resource
|
13
|
+
end
|
14
|
+
|
15
|
+
# Adds a custom method to a test helper. This is used to add support for
|
16
|
+
# non-CRUDL API requests, e.g. capturing charges. custom_method takes the
|
17
|
+
# following parameters:
|
18
|
+
# - name: the name of the custom method to create (as a symbol)
|
19
|
+
# - http_verb: the HTTP verb for the API request (:get, :post, or :delete)
|
20
|
+
# - http_path: the path to append to the resource's URL. If not provided,
|
21
|
+
# the name is used as the path
|
22
|
+
#
|
23
|
+
# For example, this call:
|
24
|
+
# custom_method :capture, http_verb: post
|
25
|
+
# adds a `capture` class method to the resource class that, when called,
|
26
|
+
# will send a POST request to `/v1/<object_name>/capture`.
|
27
|
+
def self.custom_method(name, http_verb:, http_path: nil)
|
28
|
+
Util.custom_method self::RESOURCE_CLASS, self, name, http_verb, http_path
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.resource_url
|
32
|
+
"/v1/test_helpers/"\
|
33
|
+
"#{self::RESOURCE_CLASS::OBJECT_NAME.downcase.tr('.', '/')}s"
|
34
|
+
end
|
35
|
+
|
36
|
+
def resource_url
|
37
|
+
unless (id = @resource["id"])
|
38
|
+
raise InvalidRequestError.new(
|
39
|
+
"Could not determine which URL to request: #{self.class} instance " \
|
40
|
+
"has invalid ID: #{id.inspect}",
|
41
|
+
"id"
|
42
|
+
)
|
43
|
+
end
|
44
|
+
"#{self.class.resource_url}/#{CGI.escape(id)}"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,200 @@
|
|
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
|
+
attr_reader :config
|
19
|
+
|
20
|
+
def initialize(config = Stripe.config)
|
21
|
+
@config = config
|
22
|
+
@active_connections = {}
|
23
|
+
@last_used = Util.monotonic_time
|
24
|
+
|
25
|
+
# A connection manager may be accessed across threads as one thread makes
|
26
|
+
# requests on it while another is trying to clear it (either because it's
|
27
|
+
# trying to garbage collect the manager or trying to clear it because a
|
28
|
+
# configuration setting has changed). That's probably thread-safe already
|
29
|
+
# because of Ruby's GIL, but just in case the library's running on JRuby
|
30
|
+
# or the like, use a mutex to synchronize access in this connection
|
31
|
+
# manager.
|
32
|
+
@mutex = Mutex.new
|
33
|
+
end
|
34
|
+
|
35
|
+
# Finishes any active connections by closing their TCP connection and
|
36
|
+
# clears them from internal tracking.
|
37
|
+
def clear
|
38
|
+
@mutex.synchronize do
|
39
|
+
@active_connections.each do |_, connection|
|
40
|
+
connection.finish
|
41
|
+
end
|
42
|
+
@active_connections = {}
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# Gets a connection for a given URI. This is for internal use only as it's
|
47
|
+
# subject to change (we've moved between HTTP client schemes in the past
|
48
|
+
# and may do it again).
|
49
|
+
#
|
50
|
+
# `uri` is expected to be a string.
|
51
|
+
def connection_for(uri)
|
52
|
+
@mutex.synchronize do
|
53
|
+
u = URI.parse(uri)
|
54
|
+
connection = @active_connections[[u.host, u.port]]
|
55
|
+
|
56
|
+
if connection.nil?
|
57
|
+
connection = create_connection(u)
|
58
|
+
connection.start
|
59
|
+
|
60
|
+
@active_connections[[u.host, u.port]] = connection
|
61
|
+
end
|
62
|
+
|
63
|
+
connection
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# Executes an HTTP request to the given URI with the given method. Also
|
68
|
+
# allows a request body, headers, and query string to be specified.
|
69
|
+
def execute_request(method, uri, body: nil, headers: nil, query: nil,
|
70
|
+
&block)
|
71
|
+
# Perform some basic argument validation because it's easy to get
|
72
|
+
# confused between strings and hashes for things like body and query
|
73
|
+
# parameters.
|
74
|
+
raise ArgumentError, "method should be a symbol" \
|
75
|
+
unless method.is_a?(Symbol)
|
76
|
+
raise ArgumentError, "uri should be a string" \
|
77
|
+
unless uri.is_a?(String)
|
78
|
+
raise ArgumentError, "body should be a string" \
|
79
|
+
if body && !body.is_a?(String)
|
80
|
+
raise ArgumentError, "headers should be a hash" \
|
81
|
+
if headers && !headers.is_a?(Hash)
|
82
|
+
raise ArgumentError, "query should be a string" \
|
83
|
+
if query && !query.is_a?(String)
|
84
|
+
|
85
|
+
@last_used = Util.monotonic_time
|
86
|
+
|
87
|
+
connection = connection_for(uri)
|
88
|
+
|
89
|
+
u = URI.parse(uri)
|
90
|
+
path = if query
|
91
|
+
u.path + "?" + query
|
92
|
+
else
|
93
|
+
u.path
|
94
|
+
end
|
95
|
+
|
96
|
+
method_name = method.to_s.upcase
|
97
|
+
has_response_body = method_name != "HEAD"
|
98
|
+
request = Net::HTTPGenericRequest.new(
|
99
|
+
method_name,
|
100
|
+
(body ? true : false),
|
101
|
+
has_response_body,
|
102
|
+
path,
|
103
|
+
headers
|
104
|
+
)
|
105
|
+
|
106
|
+
Util.log_debug("ConnectionManager starting request",
|
107
|
+
method_name: method_name,
|
108
|
+
path: path,
|
109
|
+
process_id: Process.pid,
|
110
|
+
thread_object_id: Thread.current.object_id,
|
111
|
+
connection_manager_object_id: object_id,
|
112
|
+
connection_object_id: connection.object_id,
|
113
|
+
log_timestamp: Util.monotonic_time)
|
114
|
+
|
115
|
+
resp = @mutex.synchronize do
|
116
|
+
# The block parameter is special here. If a block is provided, the block
|
117
|
+
# is invoked with the Net::HTTPResponse. However, the body will not have
|
118
|
+
# been read yet in the block, and can be streamed by calling
|
119
|
+
# HTTPResponse#read_body.
|
120
|
+
connection.request(request, body, &block)
|
121
|
+
end
|
122
|
+
|
123
|
+
Util.log_debug("ConnectionManager request complete",
|
124
|
+
method_name: method_name,
|
125
|
+
path: path,
|
126
|
+
process_id: Process.pid,
|
127
|
+
thread_object_id: Thread.current.object_id,
|
128
|
+
connection_manager_object_id: object_id,
|
129
|
+
connection_object_id: connection.object_id,
|
130
|
+
response_object_id: resp.object_id,
|
131
|
+
log_timestamp: Util.monotonic_time)
|
132
|
+
|
133
|
+
resp
|
134
|
+
end
|
135
|
+
|
136
|
+
#
|
137
|
+
# private
|
138
|
+
#
|
139
|
+
|
140
|
+
# `uri` should be a parsed `URI` object.
|
141
|
+
private def create_connection(uri)
|
142
|
+
# These all come back as `nil` if no proxy is configured.
|
143
|
+
proxy_host, proxy_port, proxy_user, proxy_pass = proxy_parts
|
144
|
+
|
145
|
+
connection = Net::HTTP.new(uri.host, uri.port,
|
146
|
+
proxy_host, proxy_port,
|
147
|
+
proxy_user, proxy_pass)
|
148
|
+
|
149
|
+
# Time in seconds within which Net::HTTP will try to reuse an already
|
150
|
+
# open connection when issuing a new operation. Outside this window, Ruby
|
151
|
+
# will transparently close and re-open the connection without trying to
|
152
|
+
# reuse it.
|
153
|
+
#
|
154
|
+
# Ruby's default of 2 seconds is almost certainly too short. Here I've
|
155
|
+
# reused Go's default for `DefaultTransport`.
|
156
|
+
connection.keep_alive_timeout = 30
|
157
|
+
|
158
|
+
connection.open_timeout = config.open_timeout
|
159
|
+
connection.read_timeout = config.read_timeout
|
160
|
+
if connection.respond_to?(:write_timeout=)
|
161
|
+
connection.write_timeout = config.write_timeout
|
162
|
+
end
|
163
|
+
|
164
|
+
connection.use_ssl = uri.scheme == "https"
|
165
|
+
|
166
|
+
if config.verify_ssl_certs
|
167
|
+
connection.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
168
|
+
connection.cert_store = config.ca_store
|
169
|
+
else
|
170
|
+
connection.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
171
|
+
warn_ssl_verify_none
|
172
|
+
end
|
173
|
+
|
174
|
+
connection
|
175
|
+
end
|
176
|
+
|
177
|
+
# `Net::HTTP` somewhat awkwardly requires each component of a proxy URI
|
178
|
+
# (host, port, etc.) rather than the URI itself. This method simply parses
|
179
|
+
# out those pieces to make passing them into a new connection a little less
|
180
|
+
# ugly.
|
181
|
+
private def proxy_parts
|
182
|
+
if config.proxy.nil?
|
183
|
+
[nil, nil, nil, nil]
|
184
|
+
else
|
185
|
+
u = URI.parse(config.proxy)
|
186
|
+
[u.host, u.port, u.user, u.password]
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
private def warn_ssl_verify_none
|
191
|
+
return if @verify_ssl_warned
|
192
|
+
|
193
|
+
@verify_ssl_warned = true
|
194
|
+
warn("WARNING: Running without SSL cert verification. " \
|
195
|
+
"You should never do this in production. " \
|
196
|
+
"Execute `Stripe.verify_ssl_certs = true` to enable " \
|
197
|
+
"verification.")
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
@@ -0,0 +1,93 @@
|
|
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_error`, `card_error`,
|
73
|
+
# `idempotency_error`, or `invalid_request_error`.
|
74
|
+
def type
|
75
|
+
@values[:type]
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
# Represents on OAuth error returned by the OAuth API.
|
80
|
+
#
|
81
|
+
# @see https://stripe.com/docs/connect/oauth-reference#post-token-errors
|
82
|
+
class OAuthErrorObject < StripeObject
|
83
|
+
# A unique error code per error type.
|
84
|
+
def error
|
85
|
+
@values[:error]
|
86
|
+
end
|
87
|
+
|
88
|
+
# A human readable description of the error.
|
89
|
+
def error_description
|
90
|
+
@values[:error_description]
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|