stripe 3.8.2 → 5.45.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 +424 -1
- data/CODE_OF_CONDUCT.md +77 -0
- data/Gemfile +15 -20
- data/README.md +201 -58
- data/Rakefile +12 -9
- data/VERSION +1 -1
- data/bin/stripe-console +3 -1
- data/lib/stripe/api_operations/create.rb +3 -1
- data/lib/stripe/api_operations/delete.rb +30 -2
- data/lib/stripe/api_operations/list.rb +3 -12
- data/lib/stripe/api_operations/nested_resource.rb +40 -28
- data/lib/stripe/api_operations/request.rb +83 -9
- data/lib/stripe/api_operations/save.rb +19 -7
- data/lib/stripe/api_resource.rb +65 -4
- data/lib/stripe/connection_manager.rb +200 -0
- data/lib/stripe/error_object.rb +93 -0
- data/lib/stripe/errors.rb +47 -19
- data/lib/stripe/instrumentation.rb +84 -0
- data/lib/stripe/list_object.rb +43 -7
- data/lib/stripe/multipart_encoder.rb +131 -0
- data/lib/stripe/oauth.rb +14 -7
- data/lib/stripe/object_types.rb +110 -0
- data/lib/stripe/{account.rb → resources/account.rb} +60 -28
- 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} +4 -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} +6 -6
- data/lib/stripe/resources/bitcoin_transaction.rb +16 -0
- data/lib/stripe/resources/capability.rb +34 -0
- data/lib/stripe/{card.rb → resources/card.rb} +15 -4
- data/lib/stripe/resources/charge.rb +23 -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} +5 -2
- 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 +52 -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/identity/verification_report.rb +12 -0
- data/lib/stripe/resources/identity/verification_session.rb +35 -0
- data/lib/stripe/resources/invoice.rb +74 -0
- data/lib/stripe/{invoice_item.rb → resources/invoice_item.rb} +5 -2
- 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 +53 -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} +4 -1
- data/lib/stripe/resources/price.rb +12 -0
- data/lib/stripe/{product.rb → resources/product.rb} +6 -3
- 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/{recipient.rb → resources/recipient.rb} +5 -6
- data/lib/stripe/resources/recipient_transfer.rb +7 -0
- data/lib/stripe/{refund.rb → resources/refund.rb} +4 -1
- 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} +6 -3
- data/lib/stripe/resources/source.rb +47 -0
- data/lib/stripe/resources/source_transaction.rb +8 -0
- data/lib/stripe/resources/subscription.rb +26 -0
- data/lib/stripe/resources/subscription_item.rb +26 -0
- data/lib/stripe/resources/subscription_schedule.rb +33 -0
- data/lib/stripe/resources/tax_code.rb +10 -0
- data/lib/stripe/resources/tax_id.rb +27 -0
- data/lib/stripe/resources/tax_rate.rb +12 -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 +15 -0
- data/lib/stripe/resources/test_helpers/test_clock.rb +25 -0
- data/lib/stripe/{three_d_secure.rb → resources/three_d_secure.rb} +4 -1
- 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/resources/webhook_endpoint.rb +13 -0
- data/lib/stripe/resources.rb +93 -0
- data/lib/stripe/singleton_api_resource.rb +8 -2
- data/lib/stripe/stripe_client.rb +712 -233
- data/lib/stripe/stripe_configuration.rb +194 -0
- data/lib/stripe/stripe_object.rb +180 -67
- data/lib/stripe/stripe_response.rb +89 -27
- data/lib/stripe/util.rb +87 -168
- data/lib/stripe/version.rb +3 -1
- data/lib/stripe/webhook.rb +45 -10
- data/lib/stripe.rb +47 -160
- data/stripe.gemspec +27 -8
- metadata +112 -178
- data/.gitattributes +0 -4
- data/.github/ISSUE_TEMPLATE.md +0 -5
- data/.gitignore +0 -6
- data/.rubocop.yml +0 -20
- data/.rubocop_todo.yml +0 -60
- data/.travis.yml +0 -38
- data/lib/stripe/alipay_account.rb +0 -25
- data/lib/stripe/application_fee.rb +0 -25
- data/lib/stripe/application_fee_refund.rb +0 -20
- data/lib/stripe/balance.rb +0 -5
- data/lib/stripe/balance_transaction.rb +0 -11
- data/lib/stripe/bank_account.rb +0 -30
- data/lib/stripe/bitcoin_transaction.rb +0 -13
- data/lib/stripe/charge.rb +0 -82
- data/lib/stripe/country_spec.rb +0 -11
- data/lib/stripe/customer.rb +0 -88
- data/lib/stripe/dispute.rb +0 -17
- data/lib/stripe/ephemeral_key.rb +0 -18
- data/lib/stripe/event.rb +0 -7
- data/lib/stripe/exchange_rate.rb +0 -11
- data/lib/stripe/file_upload.rb +0 -35
- data/lib/stripe/invoice.rb +0 -30
- data/lib/stripe/invoice_line_item.rb +0 -5
- data/lib/stripe/login_link.rb +0 -9
- data/lib/stripe/order.rb +0 -35
- data/lib/stripe/order_return.rb +0 -11
- data/lib/stripe/payout.rb +0 -18
- data/lib/stripe/recipient_transfer.rb +0 -5
- data/lib/stripe/reversal.rb +0 -20
- data/lib/stripe/source.rb +0 -36
- data/lib/stripe/source_transaction.rb +0 -5
- data/lib/stripe/subscription.rb +0 -43
- data/lib/stripe/subscription_item.rb +0 -14
- data/lib/stripe/token.rb +0 -7
- data/lib/stripe/transfer.rb +0 -21
- data/test/api_stub_helpers.rb +0 -0
- data/test/stripe/account_external_accounts_operations_test.rb +0 -66
- data/test/stripe/account_login_links_operations_test.rb +0 -19
- data/test/stripe/account_test.rb +0 -190
- data/test/stripe/alipay_account_test.rb +0 -35
- data/test/stripe/api_operations_test.rb +0 -76
- data/test/stripe/api_resource_test.rb +0 -522
- data/test/stripe/apple_pay_domain_test.rb +0 -31
- data/test/stripe/application_fee_refund_test.rb +0 -35
- data/test/stripe/application_fee_refunds_operations_test.rb +0 -54
- data/test/stripe/application_fee_test.rb +0 -12
- data/test/stripe/balance_test.rb +0 -11
- data/test/stripe/bank_account_test.rb +0 -34
- data/test/stripe/charge_test.rb +0 -57
- data/test/stripe/country_spec_test.rb +0 -18
- data/test/stripe/coupon_test.rb +0 -42
- data/test/stripe/customer_card_test.rb +0 -42
- data/test/stripe/customer_sources_operations_test.rb +0 -66
- data/test/stripe/customer_test.rb +0 -113
- data/test/stripe/dispute_test.rb +0 -40
- data/test/stripe/ephemeral_key_test.rb +0 -84
- data/test/stripe/errors_test.rb +0 -18
- data/test/stripe/exchange_rate_test.rb +0 -18
- data/test/stripe/file_upload_test.rb +0 -83
- data/test/stripe/invoice_item_test.rb +0 -53
- data/test/stripe/invoice_line_item_test.rb +0 -6
- data/test/stripe/invoice_test.rb +0 -111
- data/test/stripe/list_object_test.rb +0 -154
- data/test/stripe/login_link_test.rb +0 -35
- data/test/stripe/oauth_test.rb +0 -77
- data/test/stripe/order_return_test.rb +0 -19
- data/test/stripe/order_test.rb +0 -57
- data/test/stripe/payout_test.rb +0 -48
- data/test/stripe/plan_test.rb +0 -50
- data/test/stripe/product_test.rb +0 -45
- data/test/stripe/recipient_test.rb +0 -47
- data/test/stripe/refund_test.rb +0 -37
- data/test/stripe/reversal_test.rb +0 -41
- data/test/stripe/sku_test.rb +0 -48
- data/test/stripe/source_test.rb +0 -84
- data/test/stripe/source_transaction_test.rb +0 -28
- data/test/stripe/stripe_client_test.rb +0 -728
- data/test/stripe/stripe_object_test.rb +0 -448
- data/test/stripe/stripe_response_test.rb +0 -47
- data/test/stripe/subscription_item_test.rb +0 -52
- data/test/stripe/subscription_test.rb +0 -104
- data/test/stripe/three_d_secure_test.rb +0 -21
- data/test/stripe/transfer_reversals_operations_test.rb +0 -55
- data/test/stripe/transfer_test.rb +0 -41
- data/test/stripe/util_test.rb +0 -432
- data/test/stripe/webhook_test.rb +0 -94
- data/test/stripe_test.rb +0 -59
- data/test/test_data.rb +0 -59
- data/test/test_helper.rb +0 -60
@@ -1,14 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Stripe
|
2
|
-
#
|
3
|
-
# the
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
# Headers provides an access wrapper to an API response's header data. It
|
5
|
+
# mainly exists so that we don't need to expose the entire
|
6
|
+
# `Net::HTTPResponse` object while still getting some of its benefits like
|
7
|
+
# case-insensitive access to header names and flattening of header values.
|
8
|
+
class StripeResponseHeaders
|
9
|
+
# Initializes a Headers object from a Net::HTTP::HTTPResponse object.
|
10
|
+
def self.from_net_http(resp)
|
11
|
+
new(resp.to_hash)
|
12
|
+
end
|
8
13
|
|
9
|
-
#
|
10
|
-
|
14
|
+
# `hash` is expected to be a hash mapping header names to arrays of
|
15
|
+
# header values. This is the default format generated by calling
|
16
|
+
# `#to_hash` on a `Net::HTTPResponse` object because headers can be
|
17
|
+
# repeated multiple times. Using `#[]` will collapse values down to just
|
18
|
+
# the first.
|
19
|
+
def initialize(hash)
|
20
|
+
if !hash.is_a?(Hash) ||
|
21
|
+
!hash.keys.all? { |n| n.is_a?(String) } ||
|
22
|
+
!hash.values.all? { |a| a.is_a?(Array) } ||
|
23
|
+
!hash.values.all? { |a| a.all? { |v| v.is_a?(String) } }
|
24
|
+
raise ArgumentError,
|
25
|
+
"expect hash to be a map of string header names to arrays of " \
|
26
|
+
"header values"
|
27
|
+
end
|
28
|
+
|
29
|
+
@hash = {}
|
11
30
|
|
31
|
+
# This shouldn't be strictly necessary because `Net::HTTPResponse` will
|
32
|
+
# produce a hash with all headers downcased, but do it anyway just in
|
33
|
+
# case an object of this class was constructed manually.
|
34
|
+
#
|
35
|
+
# Also has the effect of duplicating the hash, which is desirable for a
|
36
|
+
# little extra object safety.
|
37
|
+
hash.each do |k, v|
|
38
|
+
@hash[k.downcase] = v
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def [](name)
|
43
|
+
values = @hash[name.downcase]
|
44
|
+
if values && values.count > 1
|
45
|
+
warn("Duplicate header values for `#{name}`; returning only first")
|
46
|
+
end
|
47
|
+
values ? values.first : nil
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
module StripeResponseBase
|
12
52
|
# A Hash of the HTTP headers of the response.
|
13
53
|
attr_accessor :http_headers
|
14
54
|
|
@@ -18,30 +58,52 @@ module Stripe
|
|
18
58
|
# The Stripe request ID of the response.
|
19
59
|
attr_accessor :request_id
|
20
60
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
def self.from_faraday_hash(http_resp)
|
26
|
-
resp = StripeResponse.new
|
27
|
-
resp.data = JSON.parse(http_resp[:body], symbolize_names: true)
|
28
|
-
resp.http_body = http_resp[:body]
|
29
|
-
resp.http_headers = http_resp[:headers]
|
30
|
-
resp.http_status = http_resp[:status]
|
31
|
-
resp.request_id = http_resp[:headers]["Request-Id"]
|
32
|
-
resp
|
61
|
+
def self.populate_for_net_http(resp, http_resp)
|
62
|
+
resp.http_headers = StripeResponseHeaders.from_net_http(http_resp)
|
63
|
+
resp.http_status = http_resp.code.to_i
|
64
|
+
resp.request_id = http_resp["request-id"]
|
33
65
|
end
|
66
|
+
end
|
34
67
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
68
|
+
# StripeResponse encapsulates some vitals of a response that came back from
|
69
|
+
# the Stripe API.
|
70
|
+
class StripeResponse
|
71
|
+
include StripeResponseBase
|
72
|
+
# The data contained by the HTTP body of the response deserialized from
|
73
|
+
# JSON.
|
74
|
+
attr_accessor :data
|
75
|
+
|
76
|
+
# The raw HTTP body of the response.
|
77
|
+
attr_accessor :http_body
|
78
|
+
|
79
|
+
# Initializes a StripeResponse object from a Net::HTTP::HTTPResponse
|
80
|
+
# object.
|
81
|
+
def self.from_net_http(http_resp)
|
39
82
|
resp = StripeResponse.new
|
40
83
|
resp.data = JSON.parse(http_resp.body, symbolize_names: true)
|
41
84
|
resp.http_body = http_resp.body
|
42
|
-
resp
|
43
|
-
resp
|
44
|
-
|
85
|
+
StripeResponseBase.populate_for_net_http(resp, http_resp)
|
86
|
+
resp
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
# We have to alias StripeResponseHeaders to StripeResponse::Headers, as this
|
91
|
+
# class used to be embedded within StripeResponse and we want to be backwards
|
92
|
+
# compatible.
|
93
|
+
StripeResponse::Headers = StripeResponseHeaders
|
94
|
+
|
95
|
+
# StripeHeadersOnlyResponse includes only header-related vitals of the
|
96
|
+
# response. This is used for streaming requests where the response was read
|
97
|
+
# directly in a block and we explicitly don't want to store the body of the
|
98
|
+
# response in memory.
|
99
|
+
class StripeHeadersOnlyResponse
|
100
|
+
include StripeResponseBase
|
101
|
+
|
102
|
+
# Initializes a StripeHeadersOnlyResponse object from a
|
103
|
+
# Net::HTTP::HTTPResponse object.
|
104
|
+
def self.from_net_http(http_resp)
|
105
|
+
resp = StripeHeadersOnlyResponse.new
|
106
|
+
StripeResponseBase.populate_for_net_http(resp, http_resp)
|
45
107
|
resp
|
46
108
|
end
|
47
109
|
end
|
data/lib/stripe/util.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "cgi"
|
2
4
|
|
3
5
|
module Stripe
|
@@ -22,69 +24,27 @@ module Stripe
|
|
22
24
|
OPTS_USER_SPECIFIED + Set[:client] - Set[:idempotency_key]
|
23
25
|
).freeze
|
24
26
|
|
25
|
-
def self.objects_to_ids(
|
26
|
-
case
|
27
|
+
def self.objects_to_ids(obj)
|
28
|
+
case obj
|
27
29
|
when APIResource
|
28
|
-
|
30
|
+
obj.id
|
29
31
|
when Hash
|
30
32
|
res = {}
|
31
|
-
|
33
|
+
obj.each { |k, v| res[k] = objects_to_ids(v) unless v.nil? }
|
32
34
|
res
|
33
35
|
when Array
|
34
|
-
|
36
|
+
obj.map { |v| objects_to_ids(v) }
|
35
37
|
else
|
36
|
-
|
38
|
+
obj
|
37
39
|
end
|
38
40
|
end
|
39
41
|
|
40
42
|
def self.object_classes
|
41
|
-
@object_classes ||=
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
Account::OBJECT_NAME => Account,
|
47
|
-
AlipayAccount::OBJECT_NAME => AlipayAccount,
|
48
|
-
ApplePayDomain::OBJECT_NAME => ApplePayDomain,
|
49
|
-
ApplicationFee::OBJECT_NAME => ApplicationFee,
|
50
|
-
ApplicationFeeRefund::OBJECT_NAME => ApplicationFeeRefund,
|
51
|
-
Balance::OBJECT_NAME => Balance,
|
52
|
-
BalanceTransaction::OBJECT_NAME => BalanceTransaction,
|
53
|
-
BankAccount::OBJECT_NAME => BankAccount,
|
54
|
-
BitcoinReceiver::OBJECT_NAME => BitcoinReceiver,
|
55
|
-
BitcoinTransaction::OBJECT_NAME => BitcoinTransaction,
|
56
|
-
Card::OBJECT_NAME => Card,
|
57
|
-
Charge::OBJECT_NAME => Charge,
|
58
|
-
CountrySpec::OBJECT_NAME => CountrySpec,
|
59
|
-
Coupon::OBJECT_NAME => Coupon,
|
60
|
-
Customer::OBJECT_NAME => Customer,
|
61
|
-
Dispute::OBJECT_NAME => Dispute,
|
62
|
-
EphemeralKey::OBJECT_NAME => EphemeralKey,
|
63
|
-
Event::OBJECT_NAME => Event,
|
64
|
-
ExchangeRate::OBJECT_NAME => ExchangeRate,
|
65
|
-
FileUpload::OBJECT_NAME => FileUpload,
|
66
|
-
Invoice::OBJECT_NAME => Invoice,
|
67
|
-
InvoiceItem::OBJECT_NAME => InvoiceItem,
|
68
|
-
InvoiceLineItem::OBJECT_NAME => InvoiceLineItem,
|
69
|
-
LoginLink::OBJECT_NAME => LoginLink,
|
70
|
-
Order::OBJECT_NAME => Order,
|
71
|
-
OrderReturn::OBJECT_NAME => OrderReturn,
|
72
|
-
Payout::OBJECT_NAME => Payout,
|
73
|
-
Plan::OBJECT_NAME => Plan,
|
74
|
-
Product::OBJECT_NAME => Product,
|
75
|
-
Recipient::OBJECT_NAME => Recipient,
|
76
|
-
RecipientTransfer::OBJECT_NAME => RecipientTransfer,
|
77
|
-
Refund::OBJECT_NAME => Refund,
|
78
|
-
Reversal::OBJECT_NAME => Reversal,
|
79
|
-
SKU::OBJECT_NAME => SKU,
|
80
|
-
Source::OBJECT_NAME => Source,
|
81
|
-
SourceTransaction::OBJECT_NAME => SourceTransaction,
|
82
|
-
Subscription::OBJECT_NAME => Subscription,
|
83
|
-
SubscriptionItem::OBJECT_NAME => SubscriptionItem,
|
84
|
-
ThreeDSecure::OBJECT_NAME => ThreeDSecure,
|
85
|
-
Token::OBJECT_NAME => Token,
|
86
|
-
Transfer::OBJECT_NAME => Transfer,
|
87
|
-
}
|
43
|
+
@object_classes ||= Stripe::ObjectTypes.object_names_to_classes
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.object_name_matches_class?(object_name, klass)
|
47
|
+
Util.object_classes[object_name] == klass
|
88
48
|
end
|
89
49
|
|
90
50
|
# Converts a hash of fields or an array of hashes into a +StripeObject+ or
|
@@ -100,53 +60,51 @@ module Stripe
|
|
100
60
|
# * +opts+ - Options for +StripeObject+ like an API key that will be reused
|
101
61
|
# on subsequent API calls.
|
102
62
|
def self.convert_to_stripe_object(data, opts = {})
|
63
|
+
opts = normalize_opts(opts)
|
64
|
+
|
103
65
|
case data
|
104
66
|
when Array
|
105
67
|
data.map { |i| convert_to_stripe_object(i, opts) }
|
106
68
|
when Hash
|
107
|
-
# Try converting to a known object class. If none available, fall back
|
108
|
-
|
69
|
+
# Try converting to a known object class. If none available, fall back
|
70
|
+
# to generic StripeObject
|
71
|
+
object_classes.fetch(data[:object], StripeObject)
|
72
|
+
.construct_from(data, opts)
|
109
73
|
else
|
110
74
|
data
|
111
75
|
end
|
112
76
|
end
|
113
77
|
|
114
78
|
def self.log_error(message, data = {})
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
79
|
+
config = data.delete(:config) || Stripe.config
|
80
|
+
logger = config.logger || Stripe.logger
|
81
|
+
if !logger.nil? ||
|
82
|
+
!config.log_level.nil? && config.log_level <= Stripe::LEVEL_ERROR
|
83
|
+
log_internal(message, data, color: :cyan, level: Stripe::LEVEL_ERROR,
|
84
|
+
logger: Stripe.logger, out: $stderr)
|
119
85
|
end
|
120
86
|
end
|
121
87
|
|
122
88
|
def self.log_info(message, data = {})
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
89
|
+
config = data.delete(:config) || Stripe.config
|
90
|
+
logger = config.logger || Stripe.logger
|
91
|
+
if !logger.nil? ||
|
92
|
+
!config.log_level.nil? && config.log_level <= Stripe::LEVEL_INFO
|
93
|
+
log_internal(message, data, color: :cyan, level: Stripe::LEVEL_INFO,
|
94
|
+
logger: Stripe.logger, out: $stdout)
|
127
95
|
end
|
128
96
|
end
|
129
97
|
|
130
98
|
def self.log_debug(message, data = {})
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
99
|
+
config = data.delete(:config) || Stripe.config
|
100
|
+
logger = config.logger || Stripe.logger
|
101
|
+
if !logger.nil? ||
|
102
|
+
!config.log_level.nil? && config.log_level <= Stripe::LEVEL_DEBUG
|
103
|
+
log_internal(message, data, color: :blue, level: Stripe::LEVEL_DEBUG,
|
104
|
+
logger: Stripe.logger, out: $stdout)
|
135
105
|
end
|
136
106
|
end
|
137
107
|
|
138
|
-
def self.file_readable(file)
|
139
|
-
# This is nominally equivalent to File.readable?, but that can
|
140
|
-
# report incorrect results on some more oddball filesystems
|
141
|
-
# (such as AFS)
|
142
|
-
|
143
|
-
File.open(file) { |f| }
|
144
|
-
rescue StandardError
|
145
|
-
false
|
146
|
-
else
|
147
|
-
true
|
148
|
-
end
|
149
|
-
|
150
108
|
def self.symbolize_names(object)
|
151
109
|
case object
|
152
110
|
when Hash
|
@@ -176,20 +134,6 @@ module Stripe
|
|
176
134
|
.map { |k, v| "#{url_encode(k)}=#{url_encode(v)}" }.join("&")
|
177
135
|
end
|
178
136
|
|
179
|
-
# Transforms an array into a hash with integer keys. Used for a small
|
180
|
-
# number of API endpoints. If the argument is not an Array, return it
|
181
|
-
# unchanged. Example: [{foo: 'bar'}] => {"0" => {foo: "bar"}}
|
182
|
-
def self.array_to_hash(array)
|
183
|
-
case array
|
184
|
-
when Array
|
185
|
-
hash = {}
|
186
|
-
array.each_with_index { |v, i| hash[i.to_s] = v }
|
187
|
-
hash
|
188
|
-
else
|
189
|
-
array
|
190
|
-
end
|
191
|
-
end
|
192
|
-
|
193
137
|
# Encodes a string in a way that makes it suitable for use in a set of
|
194
138
|
# query parameters in a URI or in a set of form parameters in a request
|
195
139
|
# body.
|
@@ -211,7 +155,6 @@ module Stripe
|
|
211
155
|
if value.is_a?(Hash)
|
212
156
|
result += flatten_params(value, calculated_key)
|
213
157
|
elsif value.is_a?(Array)
|
214
|
-
check_array_of_maps_start_keys!(value)
|
215
158
|
result += flatten_params_array(value, calculated_key)
|
216
159
|
else
|
217
160
|
result << [calculated_key, value]
|
@@ -223,18 +166,30 @@ module Stripe
|
|
223
166
|
|
224
167
|
def self.flatten_params_array(value, calculated_key)
|
225
168
|
result = []
|
226
|
-
value.
|
169
|
+
value.each_with_index do |elem, i|
|
227
170
|
if elem.is_a?(Hash)
|
228
|
-
result += flatten_params(elem, "#{calculated_key}[]")
|
171
|
+
result += flatten_params(elem, "#{calculated_key}[#{i}]")
|
229
172
|
elsif elem.is_a?(Array)
|
230
173
|
result += flatten_params_array(elem, calculated_key)
|
231
174
|
else
|
232
|
-
result << ["#{calculated_key}[]", elem]
|
175
|
+
result << ["#{calculated_key}[#{i}]", elem]
|
233
176
|
end
|
234
177
|
end
|
235
178
|
result
|
236
179
|
end
|
237
180
|
|
181
|
+
# `Time.now` can be unstable in cases like an administrator manually
|
182
|
+
# updating its value or a reconcilation via NTP. For this reason, prefer
|
183
|
+
# the use of the system's monotonic clock especially where comparing times
|
184
|
+
# to calculate an elapsed duration.
|
185
|
+
#
|
186
|
+
# Shortcut for getting monotonic time, mostly for purposes of line length
|
187
|
+
# and test stubbing. Returns time in seconds since the event used for
|
188
|
+
# monotonic reference purposes by the platform (e.g. system boot time).
|
189
|
+
def self.monotonic_time
|
190
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
191
|
+
end
|
192
|
+
|
238
193
|
def self.normalize_id(id)
|
239
194
|
if id.is_a?(Hash) # overloaded id
|
240
195
|
params_hash = id.dup
|
@@ -253,7 +208,9 @@ module Stripe
|
|
253
208
|
{ api_key: opts }
|
254
209
|
when Hash
|
255
210
|
check_api_key!(opts.fetch(:api_key)) if opts.key?(:api_key)
|
256
|
-
|
211
|
+
# Explicitly use dup here instead of clone to avoid preserving freeze
|
212
|
+
# state on input params.
|
213
|
+
opts.dup
|
257
214
|
else
|
258
215
|
raise TypeError, "normalize_opts expects a string or a hash"
|
259
216
|
end
|
@@ -261,11 +218,13 @@ module Stripe
|
|
261
218
|
|
262
219
|
def self.check_string_argument!(key)
|
263
220
|
raise TypeError, "argument must be a string" unless key.is_a?(String)
|
221
|
+
|
264
222
|
key
|
265
223
|
end
|
266
224
|
|
267
225
|
def self.check_api_key!(key)
|
268
226
|
raise TypeError, "api_key must be a string" unless key.is_a?(String)
|
227
|
+
|
269
228
|
key
|
270
229
|
end
|
271
230
|
|
@@ -276,11 +235,8 @@ module Stripe
|
|
276
235
|
# diffent naming schemes.
|
277
236
|
def self.normalize_headers(headers)
|
278
237
|
headers.each_with_object({}) do |(k, v), new_headers|
|
279
|
-
if k.is_a?(Symbol)
|
280
|
-
|
281
|
-
elsif k.is_a?(String)
|
282
|
-
k = titlecase_parts(k)
|
283
|
-
end
|
238
|
+
k = k.to_s.tr("_", "-") if k.is_a?(Symbol)
|
239
|
+
k = k.split("-").reject(&:empty?).map(&:capitalize).join("-")
|
284
240
|
|
285
241
|
new_headers[k] = v
|
286
242
|
end
|
@@ -296,13 +252,13 @@ module Stripe
|
|
296
252
|
|
297
253
|
# Constant time string comparison to prevent timing attacks
|
298
254
|
# Code borrowed from ActiveSupport
|
299
|
-
def self.secure_compare(
|
300
|
-
return false unless
|
255
|
+
def self.secure_compare(str_a, str_b)
|
256
|
+
return false unless str_a.bytesize == str_b.bytesize
|
301
257
|
|
302
|
-
l =
|
258
|
+
l = str_a.unpack "C#{str_a.bytesize}"
|
303
259
|
|
304
260
|
res = 0
|
305
|
-
|
261
|
+
str_b.each_byte { |byte| res |= byte ^ l.shift }
|
306
262
|
res.zero?
|
307
263
|
end
|
308
264
|
|
@@ -311,56 +267,18 @@ module Stripe
|
|
311
267
|
#
|
312
268
|
|
313
269
|
COLOR_CODES = {
|
314
|
-
black:
|
315
|
-
red:
|
316
|
-
green:
|
317
|
-
yellow:
|
318
|
-
blue:
|
270
|
+
black: 0, light_black: 60,
|
271
|
+
red: 1, light_red: 61,
|
272
|
+
green: 2, light_green: 62,
|
273
|
+
yellow: 3, light_yellow: 63,
|
274
|
+
blue: 4, light_blue: 64,
|
319
275
|
magenta: 5, light_magenta: 65,
|
320
|
-
cyan:
|
321
|
-
white:
|
276
|
+
cyan: 6, light_cyan: 66,
|
277
|
+
white: 7, light_white: 67,
|
322
278
|
default: 9,
|
323
279
|
}.freeze
|
324
280
|
private_constant :COLOR_CODES
|
325
281
|
|
326
|
-
# We use a pretty janky version of form encoding (Rack's) that supports
|
327
|
-
# more complex data structures like maps and arrays through the use of
|
328
|
-
# specialized syntax. To encode an array of maps like:
|
329
|
-
#
|
330
|
-
# [{a: 1, b: 2}, {a: 3, b: 4}]
|
331
|
-
#
|
332
|
-
# We have to produce something that looks like this:
|
333
|
-
#
|
334
|
-
# arr[][a]=1&arr[][b]=2&arr[][a]=3&arr[][b]=4
|
335
|
-
#
|
336
|
-
# The only way for the server to recognize that this is a two item array is
|
337
|
-
# that it notices the repetition of element "a", so it's key that these
|
338
|
-
# repeated elements are encoded first.
|
339
|
-
#
|
340
|
-
# This method is invoked for any arrays being encoded and checks that if
|
341
|
-
# the array contains all non-empty maps, that each of those maps must start
|
342
|
-
# with the same key so that their boundaries can be properly encoded.
|
343
|
-
def self.check_array_of_maps_start_keys!(arr)
|
344
|
-
expected_key = nil
|
345
|
-
arr.each do |item|
|
346
|
-
break unless item.is_a?(Hash)
|
347
|
-
break if item.count.zero?
|
348
|
-
|
349
|
-
first_key = item.first[0]
|
350
|
-
|
351
|
-
if expected_key
|
352
|
-
if expected_key != first_key
|
353
|
-
raise ArgumentError,
|
354
|
-
"All maps nested in an array should start with the same key " \
|
355
|
-
"(expected starting key '#{expected_key}', got '#{first_key}')"
|
356
|
-
end
|
357
|
-
else
|
358
|
-
expected_key = first_key
|
359
|
-
end
|
360
|
-
end
|
361
|
-
end
|
362
|
-
private_class_method :check_array_of_maps_start_keys!
|
363
|
-
|
364
282
|
# Uses an ANSI escape code to colorize text if it's going to be sent to a
|
365
283
|
# TTY.
|
366
284
|
def self.colorize(val, color, isatty)
|
@@ -385,35 +303,36 @@ module Stripe
|
|
385
303
|
end
|
386
304
|
private_class_method :level_name
|
387
305
|
|
388
|
-
|
389
|
-
# 2.0.
|
390
|
-
def self.log_internal(message, data = {}, color: nil, level: nil, logger: nil, out: nil)
|
306
|
+
def self.log_internal(message, data = {}, color:, level:, logger:, out:)
|
391
307
|
data_str = data.reject { |_k, v| v.nil? }
|
392
308
|
.map do |(k, v)|
|
393
|
-
format("
|
309
|
+
format("%<key>s=%<value>s",
|
310
|
+
key: colorize(k, color, logger.nil? && !out.nil? && out.isatty),
|
311
|
+
value: wrap_logfmt_value(v))
|
394
312
|
end.join(" ")
|
395
313
|
|
396
314
|
if !logger.nil?
|
397
315
|
# the library's log levels are mapped to the same values as the
|
398
316
|
# standard library's logger
|
399
317
|
logger.log(level,
|
400
|
-
format("message
|
318
|
+
format("message=%<message>s %<data_str>s",
|
319
|
+
message: wrap_logfmt_value(message),
|
320
|
+
data_str: data_str))
|
401
321
|
elsif out.isatty
|
402
|
-
out.puts format("
|
322
|
+
out.puts format("%<level>s %<message>s %<data_str>s",
|
323
|
+
level: colorize(level_name(level)[0, 4].upcase,
|
324
|
+
color, out.isatty),
|
325
|
+
message: message,
|
326
|
+
data_str: data_str)
|
403
327
|
else
|
404
|
-
out.puts format("message
|
328
|
+
out.puts format("message=%<message>s level=%<level>s %<data_str>s",
|
329
|
+
message: wrap_logfmt_value(message),
|
330
|
+
level: level_name(level),
|
331
|
+
data_str: data_str)
|
405
332
|
end
|
406
333
|
end
|
407
334
|
private_class_method :log_internal
|
408
335
|
|
409
|
-
def self.titlecase_parts(s)
|
410
|
-
s.split("-")
|
411
|
-
.reject { |p| p == "" }
|
412
|
-
.map { |p| p[0].upcase + p[1..-1].downcase }
|
413
|
-
.join("-")
|
414
|
-
end
|
415
|
-
private_class_method :titlecase_parts
|
416
|
-
|
417
336
|
# Wraps a value in double quotes if it looks sufficiently complex so that
|
418
337
|
# it can be read by logfmt parsers.
|
419
338
|
def self.wrap_logfmt_value(val)
|
@@ -427,7 +346,7 @@ module Stripe
|
|
427
346
|
if %r{[^\w\-/]} =~ val
|
428
347
|
# If the string contains any special characters, escape any double
|
429
348
|
# quotes it has, remove newlines, and wrap the whole thing in quotes.
|
430
|
-
format(%("
|
349
|
+
format(%("%<value>s"), value: val.gsub('"', '\"').delete("\n"))
|
431
350
|
else
|
432
351
|
# Otherwise use the basic value if it looks like a standard set of
|
433
352
|
# characters (and allow a few special characters like hyphens, and
|
data/lib/stripe/version.rb
CHANGED
data/lib/stripe/webhook.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Stripe
|
2
4
|
module Webhook
|
3
5
|
DEFAULT_TOLERANCE = 300
|
@@ -6,7 +8,8 @@ module Stripe
|
|
6
8
|
#
|
7
9
|
# This may raise JSON::ParserError if the payload is not valid JSON, or
|
8
10
|
# SignatureVerificationError if the signature verification fails.
|
9
|
-
def self.construct_event(payload, sig_header, secret,
|
11
|
+
def self.construct_event(payload, sig_header, secret,
|
12
|
+
tolerance: DEFAULT_TOLERANCE)
|
10
13
|
Signature.verify_header(payload, sig_header, secret, tolerance: tolerance)
|
11
14
|
|
12
15
|
# It's a good idea to parse the payload only after verifying it. We use
|
@@ -19,12 +22,39 @@ module Stripe
|
|
19
22
|
end
|
20
23
|
|
21
24
|
module Signature
|
22
|
-
EXPECTED_SCHEME = "v1"
|
25
|
+
EXPECTED_SCHEME = "v1"
|
26
|
+
|
27
|
+
# Computes a webhook signature given a time (probably the current time),
|
28
|
+
# a payload, and a signing secret.
|
29
|
+
def self.compute_signature(timestamp, payload, secret)
|
30
|
+
raise ArgumentError, "timestamp should be an instance of Time" \
|
31
|
+
unless timestamp.is_a?(Time)
|
32
|
+
raise ArgumentError, "payload should be a string" \
|
33
|
+
unless payload.is_a?(String)
|
34
|
+
raise ArgumentError, "secret should be a string" \
|
35
|
+
unless secret.is_a?(String)
|
23
36
|
|
24
|
-
|
25
|
-
OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha256"), secret,
|
37
|
+
timestamped_payload = "#{timestamp.to_i}.#{payload}"
|
38
|
+
OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha256"), secret,
|
39
|
+
timestamped_payload)
|
40
|
+
end
|
41
|
+
|
42
|
+
# Generates a value that would be added to a `Stripe-Signature` for a
|
43
|
+
# given webhook payload.
|
44
|
+
#
|
45
|
+
# Note that this isn't needed to verify webhooks in any way, and is
|
46
|
+
# mainly here for use in test cases (those that are both within this
|
47
|
+
# project and without).
|
48
|
+
def self.generate_header(timestamp, signature, scheme: EXPECTED_SCHEME)
|
49
|
+
raise ArgumentError, "timestamp should be an instance of Time" \
|
50
|
+
unless timestamp.is_a?(Time)
|
51
|
+
raise ArgumentError, "signature should be a string" \
|
52
|
+
unless signature.is_a?(String)
|
53
|
+
raise ArgumentError, "scheme should be a string" \
|
54
|
+
unless scheme.is_a?(String)
|
55
|
+
|
56
|
+
"t=#{timestamp.to_i},#{scheme}=#{signature}"
|
26
57
|
end
|
27
|
-
private_class_method :compute_signature
|
28
58
|
|
29
59
|
# Extracts the timestamp and the signature(s) with the desired scheme
|
30
60
|
# from the header
|
@@ -32,7 +62,7 @@ module Stripe
|
|
32
62
|
list_items = header.split(/,\s*/).map { |i| i.split("=", 2) }
|
33
63
|
timestamp = Integer(list_items.select { |i| i[0] == "t" }[0][1])
|
34
64
|
signatures = list_items.select { |i| i[0] == scheme }.map { |i| i[1] }
|
35
|
-
[timestamp, signatures]
|
65
|
+
[Time.at(timestamp), signatures]
|
36
66
|
end
|
37
67
|
private_class_method :get_timestamp_and_signatures
|
38
68
|
|
@@ -48,7 +78,13 @@ module Stripe
|
|
48
78
|
# Returns true otherwise
|
49
79
|
def self.verify_header(payload, header, secret, tolerance: nil)
|
50
80
|
begin
|
51
|
-
timestamp, signatures =
|
81
|
+
timestamp, signatures =
|
82
|
+
get_timestamp_and_signatures(header, EXPECTED_SCHEME)
|
83
|
+
|
84
|
+
# TODO: Try to knock over this blanket rescue as it can unintentionally
|
85
|
+
# swallow many valid errors. Instead, try to validate an incoming
|
86
|
+
# header one piece at a time, and error with a known exception class if
|
87
|
+
# any part is found to be invalid. Rescue that class here.
|
52
88
|
rescue StandardError
|
53
89
|
raise SignatureVerificationError.new(
|
54
90
|
"Unable to extract timestamp and signatures from header",
|
@@ -63,8 +99,7 @@ module Stripe
|
|
63
99
|
)
|
64
100
|
end
|
65
101
|
|
66
|
-
|
67
|
-
expected_sig = compute_signature(signed_payload, secret)
|
102
|
+
expected_sig = compute_signature(timestamp, payload, secret)
|
68
103
|
unless signatures.any? { |s| Util.secure_compare(expected_sig, s) }
|
69
104
|
raise SignatureVerificationError.new(
|
70
105
|
"No signatures found matching the expected signature for payload",
|
@@ -72,7 +107,7 @@ module Stripe
|
|
72
107
|
)
|
73
108
|
end
|
74
109
|
|
75
|
-
if tolerance && timestamp < Time.now
|
110
|
+
if tolerance && timestamp < Time.now - tolerance
|
76
111
|
raise SignatureVerificationError.new(
|
77
112
|
"Timestamp outside the tolerance zone (#{Time.at(timestamp)})",
|
78
113
|
header, http_body: payload
|