stripe 3.2.0 → 5.16.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.editorconfig +10 -0
- data/.gitignore +3 -0
- data/.rubocop.yml +80 -0
- data/.rubocop_todo.yml +33 -0
- data/.travis.yml +9 -7
- data/.vscode/extensions.json +7 -0
- data/.vscode/settings.json +8 -0
- data/CHANGELOG.md +876 -0
- data/CODE_OF_CONDUCT.md +77 -0
- data/Gemfile +21 -21
- data/History.txt +1 -678
- data/README.md +195 -53
- data/Rakefile +18 -12
- data/VERSION +1 -1
- data/bin/stripe-console +5 -3
- data/lib/stripe/api_operations/create.rb +3 -1
- data/lib/stripe/api_operations/delete.rb +26 -2
- data/lib/stripe/api_operations/list.rb +3 -12
- data/lib/stripe/api_operations/nested_resource.rb +72 -0
- data/lib/stripe/api_operations/request.rb +33 -15
- data/lib/stripe/api_operations/save.rb +18 -9
- data/lib/stripe/api_resource.rb +60 -10
- data/lib/stripe/connection_manager.rb +159 -0
- data/lib/stripe/error_object.rb +94 -0
- data/lib/stripe/errors.rb +39 -19
- data/lib/stripe/instrumentation.rb +82 -0
- data/lib/stripe/list_object.rb +54 -22
- data/lib/stripe/multipart_encoder.rb +131 -0
- data/lib/stripe/oauth.rb +22 -14
- data/lib/stripe/object_types.rb +96 -0
- data/lib/stripe/{account.rb → resources/account.rb} +72 -34
- data/lib/stripe/resources/account_link.rb +9 -0
- data/lib/stripe/resources/alipay_account.rb +34 -0
- data/lib/stripe/{apple_pay_domain.rb → resources/apple_pay_domain.rb} +4 -2
- data/lib/stripe/resources/application_fee.rb +13 -0
- data/lib/stripe/resources/application_fee_refund.rb +30 -0
- data/lib/stripe/{balance.rb → resources/balance.rb} +3 -1
- data/lib/stripe/{balance_transaction.rb → resources/balance_transaction.rb} +3 -5
- data/lib/stripe/resources/bank_account.rb +42 -0
- data/lib/stripe/{bitcoin_receiver.rb → resources/bitcoin_receiver.rb} +6 -5
- data/lib/stripe/resources/bitcoin_transaction.rb +15 -0
- data/lib/stripe/resources/capability.rb +33 -0
- data/lib/stripe/resources/card.rb +37 -0
- data/lib/stripe/resources/charge.rb +22 -0
- data/lib/stripe/resources/checkout/session.rb +12 -0
- data/lib/stripe/{country_spec.rb → resources/country_spec.rb} +3 -5
- data/lib/stripe/{coupon.rb → resources/coupon.rb} +4 -2
- data/lib/stripe/resources/credit_note.rb +32 -0
- data/lib/stripe/resources/credit_note_line_item.rb +7 -0
- data/lib/stripe/resources/customer.rb +35 -0
- data/lib/stripe/resources/customer_balance_transaction.rb +30 -0
- data/lib/stripe/resources/discount.rb +7 -0
- data/lib/stripe/resources/dispute.rb +21 -0
- data/lib/stripe/resources/ephemeral_key.rb +19 -0
- data/lib/stripe/{event.rb → resources/event.rb} +3 -1
- data/lib/stripe/resources/exchange_rate.rb +9 -0
- data/lib/stripe/resources/file.rb +34 -0
- data/lib/stripe/resources/file_link.rb +11 -0
- data/lib/stripe/resources/invoice.rb +73 -0
- data/lib/stripe/{invoice_item.rb → resources/invoice_item.rb} +4 -2
- data/lib/stripe/{invoice_line_item.rb → resources/invoice_line_item.rb} +3 -1
- data/lib/stripe/resources/issuing/authorization.rb +33 -0
- data/lib/stripe/resources/issuing/card.rb +24 -0
- data/lib/stripe/resources/issuing/card_details.rb +9 -0
- data/lib/stripe/resources/issuing/cardholder.rb +13 -0
- data/lib/stripe/resources/issuing/dispute.rb +13 -0
- data/lib/stripe/resources/issuing/transaction.rb +12 -0
- data/lib/stripe/resources/login_link.rb +14 -0
- data/lib/stripe/resources/mandate.rb +7 -0
- data/lib/stripe/resources/order.rb +32 -0
- data/lib/stripe/{order_return.rb → resources/order_return.rb} +3 -5
- data/lib/stripe/resources/payment_intent.rb +42 -0
- data/lib/stripe/resources/payment_method.rb +32 -0
- data/lib/stripe/resources/payout.rb +22 -0
- data/lib/stripe/resources/person.rb +31 -0
- data/lib/stripe/{plan.rb → resources/plan.rb} +3 -1
- data/lib/stripe/{product.rb → resources/product.rb} +5 -3
- data/lib/stripe/resources/radar/early_fraud_warning.rb +11 -0
- data/lib/stripe/resources/radar/value_list.rb +14 -0
- data/lib/stripe/resources/radar/value_list_item.rb +13 -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} +3 -1
- data/lib/stripe/resources/reporting/report_run.rb +12 -0
- data/lib/stripe/resources/reporting/report_type.rb +12 -0
- data/lib/stripe/resources/reversal.rb +29 -0
- data/lib/stripe/resources/review.rb +20 -0
- data/lib/stripe/resources/setup_intent.rb +32 -0
- data/lib/stripe/resources/sigma/scheduled_query_run.rb +15 -0
- data/lib/stripe/{sku.rb → resources/sku.rb} +5 -3
- data/lib/stripe/resources/source.rb +46 -0
- data/lib/stripe/resources/source_transaction.rb +7 -0
- data/lib/stripe/resources/subscription.rb +25 -0
- data/lib/stripe/resources/subscription_item.rb +25 -0
- data/lib/stripe/resources/subscription_schedule.rb +32 -0
- data/lib/stripe/resources/tax_id.rb +26 -0
- data/lib/stripe/resources/tax_rate.rb +11 -0
- data/lib/stripe/resources/terminal/connection_token.rb +11 -0
- data/lib/stripe/resources/terminal/location.rb +14 -0
- data/lib/stripe/resources/terminal/reader.rb +14 -0
- data/lib/stripe/{three_d_secure.rb → resources/three_d_secure.rb} +3 -1
- data/lib/stripe/{token.rb → resources/token.rb} +3 -1
- data/lib/stripe/resources/topup.rb +22 -0
- data/lib/stripe/resources/transfer.rb +26 -0
- data/lib/stripe/resources/usage_record.rb +7 -0
- data/lib/stripe/resources/usage_record_summary.rb +7 -0
- data/lib/stripe/resources/webhook_endpoint.rb +12 -0
- data/lib/stripe/resources.rb +79 -0
- data/lib/stripe/singleton_api_resource.rb +10 -4
- data/lib/stripe/stripe_client.rb +658 -337
- data/lib/stripe/stripe_object.rb +248 -126
- data/lib/stripe/stripe_response.rb +55 -21
- data/lib/stripe/util.rb +145 -187
- data/lib/stripe/version.rb +3 -1
- data/lib/stripe/webhook.rb +27 -16
- data/lib/stripe.rb +139 -90
- data/stripe.gemspec +26 -14
- data/test/openapi/README.md +9 -0
- data/test/stripe/account_link_test.rb +18 -0
- data/test/stripe/account_test.rb +311 -101
- data/test/stripe/alipay_account_test.rb +21 -1
- data/test/stripe/api_operations_test.rb +57 -8
- data/test/stripe/api_resource_test.rb +359 -271
- data/test/stripe/apple_pay_domain_test.rb +26 -11
- data/test/stripe/application_fee_refund_test.rb +10 -8
- data/test/stripe/application_fee_test.rb +49 -3
- data/test/stripe/balance_test.rb +4 -2
- data/test/stripe/balance_transaction_test.rb +20 -0
- data/test/stripe/bank_account_test.rb +11 -11
- data/test/stripe/capability_test.rb +45 -0
- data/test/stripe/charge_test.rb +25 -18
- data/test/stripe/checkout/session_test.rb +41 -0
- data/test/stripe/connection_manager_test.rb +163 -0
- data/test/stripe/country_spec_test.rb +6 -4
- data/test/stripe/coupon_test.rb +29 -10
- data/test/stripe/credit_note_test.rb +90 -0
- data/test/stripe/customer_balance_transaction_test.rb +37 -0
- data/test/stripe/customer_card_test.rb +13 -17
- data/test/stripe/customer_test.rb +161 -49
- data/test/stripe/dispute_test.rb +19 -8
- data/test/stripe/ephemeral_key_test.rb +23 -14
- data/test/stripe/errors_test.rb +32 -9
- data/test/stripe/exchange_rate_test.rb +20 -0
- data/test/stripe/file_link_test.rb +41 -0
- data/test/stripe/file_test.rb +87 -0
- data/test/stripe/instrumentation_test.rb +74 -0
- data/test/stripe/invoice_item_test.rb +31 -18
- data/test/stripe/invoice_line_item_test.rb +3 -1
- data/test/stripe/invoice_test.rb +158 -39
- data/test/stripe/issuing/authorization_test.rb +72 -0
- data/test/stripe/issuing/card_test.rb +62 -0
- data/test/stripe/issuing/cardholder_test.rb +53 -0
- data/test/stripe/issuing/dispute_test.rb +45 -0
- data/test/stripe/issuing/transaction_test.rb +48 -0
- data/test/stripe/list_object_test.rb +120 -88
- data/test/stripe/login_link_test.rb +16 -14
- data/test/stripe/mandate_test.rb +14 -0
- data/test/stripe/multipart_encoder_test.rb +130 -0
- data/test/stripe/oauth_test.rb +69 -50
- data/test/stripe/order_return_test.rb +7 -5
- data/test/stripe/order_test.rb +39 -14
- data/test/stripe/payment_intent_test.rb +107 -0
- data/test/stripe/payment_method_test.rb +84 -0
- data/test/stripe/payout_test.rb +18 -9
- data/test/stripe/person_test.rb +46 -0
- data/test/stripe/plan_test.rb +67 -19
- data/test/stripe/product_test.rb +28 -14
- data/test/stripe/radar/early_fraud_warning_test.rb +22 -0
- data/test/stripe/radar/value_list_item_test.rb +48 -0
- data/test/stripe/radar/value_list_test.rb +61 -0
- data/test/stripe/recipient_test.rb +27 -13
- data/test/stripe/refund_test.rb +11 -9
- data/test/stripe/reporting/report_run_test.rb +33 -0
- data/test/stripe/reporting/report_type_test.rb +22 -0
- data/test/stripe/reversal_test.rb +12 -10
- data/test/stripe/review_test.rb +27 -0
- data/test/stripe/setup_intent_test.rb +84 -0
- data/test/stripe/sigma/scheduled_query_run_test.rb +22 -0
- data/test/stripe/sku_test.rb +24 -12
- data/test/stripe/source_test.rb +70 -19
- data/test/stripe/stripe_client_test.rb +867 -326
- data/test/stripe/stripe_object_test.rb +284 -182
- data/test/stripe/stripe_response_test.rb +73 -24
- data/test/stripe/subscription_item_test.rb +47 -15
- data/test/stripe/subscription_schedule_test.rb +82 -0
- data/test/stripe/subscription_test.rb +41 -19
- data/test/stripe/tax_id_test.rb +31 -0
- data/test/stripe/tax_rate_test.rb +43 -0
- data/test/stripe/terminal/connection_token_test.rb +16 -0
- data/test/stripe/terminal/location_test.rb +68 -0
- data/test/stripe/terminal/reader_test.rb +62 -0
- data/test/stripe/three_d_secure_test.rb +4 -2
- data/test/stripe/topup_test.rb +62 -0
- data/test/stripe/transfer_test.rb +55 -8
- data/test/stripe/usage_record_summary_test.rb +29 -0
- data/test/stripe/util_test.rb +173 -84
- data/test/stripe/webhook_endpoint_test.rb +59 -0
- data/test/stripe/webhook_test.rb +21 -17
- data/test/stripe_mock.rb +78 -0
- data/test/stripe_test.rb +6 -15
- data/test/test_data.rb +28 -26
- data/test/test_helper.rb +48 -29
- metadata +183 -70
- data/lib/stripe/alipay_account.rb +0 -22
- data/lib/stripe/application_fee.rb +0 -22
- data/lib/stripe/application_fee_refund.rb +0 -20
- data/lib/stripe/bank_account.rb +0 -30
- data/lib/stripe/bitcoin_transaction.rb +0 -11
- data/lib/stripe/card.rb +0 -27
- data/lib/stripe/charge.rb +0 -82
- data/lib/stripe/customer.rb +0 -79
- data/lib/stripe/dispute.rb +0 -17
- data/lib/stripe/ephemeral_key.rb +0 -18
- data/lib/stripe/file_upload.rb +0 -33
- data/lib/stripe/invoice.rb +0 -29
- data/lib/stripe/login_link.rb +0 -9
- data/lib/stripe/order.rb +0 -29
- data/lib/stripe/payout.rb +0 -18
- data/lib/stripe/recipient_transfer.rb +0 -6
- data/lib/stripe/reversal.rb +0 -20
- data/lib/stripe/source.rb +0 -23
- data/lib/stripe/subscription.rb +0 -33
- data/lib/stripe/subscription_item.rb +0 -14
- data/lib/stripe/transfer.rb +0 -18
- data/test/api_stub_helpers.rb +0 -0
- data/test/stripe/bitcoin_receiver_test.rb +0 -67
- data/test/stripe/bitcoin_transaction_test.rb +0 -19
- data/test/stripe/file_upload_test.rb +0 -66
- data/test/stripe/recipient_card_test.rb +0 -44
@@ -1,7 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Stripe
|
2
4
|
# StripeResponse encapsulates some vitals of a response that came back from
|
3
5
|
# the Stripe API.
|
4
6
|
class StripeResponse
|
7
|
+
# Headers provides an access wrapper to an API response's header data. It
|
8
|
+
# mainly exists so that we don't need to expose the entire
|
9
|
+
# `Net::HTTPResponse` object while still getting some of its benefits like
|
10
|
+
# case-insensitive access to header names and flattening of header values.
|
11
|
+
class Headers
|
12
|
+
# Initializes a Headers object from a Net::HTTP::HTTPResponse object.
|
13
|
+
def self.from_net_http(resp)
|
14
|
+
new(resp.to_hash)
|
15
|
+
end
|
16
|
+
|
17
|
+
# `hash` is expected to be a hash mapping header names to arrays of
|
18
|
+
# header values. This is the default format generated by calling
|
19
|
+
# `#to_hash` on a `Net::HTTPResponse` object because headers can be
|
20
|
+
# repeated multiple times. Using `#[]` will collapse values down to just
|
21
|
+
# the first.
|
22
|
+
def initialize(hash)
|
23
|
+
if !hash.is_a?(Hash) ||
|
24
|
+
!hash.keys.all? { |n| n.is_a?(String) } ||
|
25
|
+
!hash.values.all? { |a| a.is_a?(Array) } ||
|
26
|
+
!hash.values.all? { |a| a.all? { |v| v.is_a?(String) } }
|
27
|
+
raise ArgumentError,
|
28
|
+
"expect hash to be a map of string header names to arrays of " \
|
29
|
+
"header values"
|
30
|
+
end
|
31
|
+
|
32
|
+
@hash = {}
|
33
|
+
|
34
|
+
# This shouldn't be strictly necessary because `Net::HTTPResponse` will
|
35
|
+
# produce a hash with all headers downcased, but do it anyway just in
|
36
|
+
# case an object of this class was constructed manually.
|
37
|
+
#
|
38
|
+
# Also has the effect of duplicating the hash, which is desirable for a
|
39
|
+
# little extra object safety.
|
40
|
+
hash.each do |k, v|
|
41
|
+
@hash[k.downcase] = v
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def [](name)
|
46
|
+
values = @hash[name.downcase]
|
47
|
+
if values && values.count > 1
|
48
|
+
warn("Duplicate header values for `#{name}`; returning only first")
|
49
|
+
end
|
50
|
+
values ? values.first : nil
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
5
54
|
# The data contained by the HTTP body of the response deserialized from
|
6
55
|
# JSON.
|
7
56
|
attr_accessor :data
|
@@ -18,30 +67,15 @@ module Stripe
|
|
18
67
|
# The Stripe request ID of the response.
|
19
68
|
attr_accessor :request_id
|
20
69
|
|
21
|
-
# Initializes a StripeResponse object from a
|
22
|
-
#
|
23
|
-
|
24
|
-
# This may throw JSON::ParserError if the response body is not valid JSON.
|
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
|
33
|
-
end
|
34
|
-
|
35
|
-
# Initializes a StripeResponse object from a Faraday HTTP response object.
|
36
|
-
#
|
37
|
-
# This may throw JSON::ParserError if the response body is not valid JSON.
|
38
|
-
def self.from_faraday_response(http_resp)
|
70
|
+
# Initializes a StripeResponse object from a Net::HTTP::HTTPResponse
|
71
|
+
# object.
|
72
|
+
def self.from_net_http(http_resp)
|
39
73
|
resp = StripeResponse.new
|
40
74
|
resp.data = JSON.parse(http_resp.body, symbolize_names: true)
|
41
75
|
resp.http_body = http_resp.body
|
42
|
-
resp.http_headers = http_resp
|
43
|
-
resp.http_status = http_resp.
|
44
|
-
resp.request_id = http_resp
|
76
|
+
resp.http_headers = Headers.from_net_http(http_resp)
|
77
|
+
resp.http_status = http_resp.code.to_i
|
78
|
+
resp.request_id = http_resp["request-id"]
|
45
79
|
resp
|
46
80
|
end
|
47
81
|
end
|
data/lib/stripe/util.rb
CHANGED
@@ -1,68 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "cgi"
|
2
4
|
|
3
5
|
module Stripe
|
4
6
|
module Util
|
5
|
-
|
6
|
-
|
7
|
+
# Options that a user is allowed to specify.
|
8
|
+
OPTS_USER_SPECIFIED = Set[
|
9
|
+
:api_key,
|
10
|
+
:idempotency_key,
|
11
|
+
:stripe_account,
|
12
|
+
:stripe_version
|
13
|
+
].freeze
|
14
|
+
|
15
|
+
# Options that should be copyable from one StripeObject to another
|
16
|
+
# including options that may be internal.
|
17
|
+
OPTS_COPYABLE = (
|
18
|
+
OPTS_USER_SPECIFIED + Set[:api_base]
|
19
|
+
).freeze
|
20
|
+
|
21
|
+
# Options that should be persisted between API requests. This includes
|
22
|
+
# client, which is an object containing an HTTP client to reuse.
|
23
|
+
OPTS_PERSISTABLE = (
|
24
|
+
OPTS_USER_SPECIFIED + Set[:client] - Set[:idempotency_key]
|
25
|
+
).freeze
|
26
|
+
|
27
|
+
def self.objects_to_ids(obj)
|
28
|
+
case obj
|
7
29
|
when APIResource
|
8
|
-
|
30
|
+
obj.id
|
9
31
|
when Hash
|
10
32
|
res = {}
|
11
|
-
|
33
|
+
obj.each { |k, v| res[k] = objects_to_ids(v) unless v.nil? }
|
12
34
|
res
|
13
35
|
when Array
|
14
|
-
|
36
|
+
obj.map { |v| objects_to_ids(v) }
|
15
37
|
else
|
16
|
-
|
38
|
+
obj
|
17
39
|
end
|
18
40
|
end
|
19
41
|
|
20
42
|
def self.object_classes
|
21
|
-
@object_classes ||=
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
Account::OBJECT_NAME => Account,
|
27
|
-
AlipayAccount::OBJECT_NAME => AlipayAccount,
|
28
|
-
ApplePayDomain::OBJECT_NAME => ApplePayDomain,
|
29
|
-
ApplicationFee::OBJECT_NAME => ApplicationFee,
|
30
|
-
ApplicationFeeRefund::OBJECT_NAME => ApplicationFeeRefund,
|
31
|
-
Balance::OBJECT_NAME => Balance,
|
32
|
-
BalanceTransaction::OBJECT_NAME => BalanceTransaction,
|
33
|
-
BankAccount::OBJECT_NAME => BankAccount,
|
34
|
-
BitcoinReceiver::OBJECT_NAME => BitcoinReceiver,
|
35
|
-
BitcoinTransaction::OBJECT_NAME => BitcoinTransaction,
|
36
|
-
Card::OBJECT_NAME => Card,
|
37
|
-
Charge::OBJECT_NAME => Charge,
|
38
|
-
CountrySpec::OBJECT_NAME => CountrySpec,
|
39
|
-
Coupon::OBJECT_NAME => Coupon,
|
40
|
-
Customer::OBJECT_NAME => Customer,
|
41
|
-
Dispute::OBJECT_NAME => Dispute,
|
42
|
-
EphemeralKey::OBJECT_NAME => EphemeralKey,
|
43
|
-
Event::OBJECT_NAME => Event,
|
44
|
-
FileUpload::OBJECT_NAME => FileUpload,
|
45
|
-
Invoice::OBJECT_NAME => Invoice,
|
46
|
-
InvoiceItem::OBJECT_NAME => InvoiceItem,
|
47
|
-
InvoiceLineItem::OBJECT_NAME => InvoiceLineItem,
|
48
|
-
LoginLink::OBJECT_NAME => LoginLink,
|
49
|
-
Order::OBJECT_NAME => Order,
|
50
|
-
OrderReturn::OBJECT_NAME => OrderReturn,
|
51
|
-
Payout::OBJECT_NAME => Payout,
|
52
|
-
Plan::OBJECT_NAME => Plan,
|
53
|
-
Product::OBJECT_NAME => Product,
|
54
|
-
Recipient::OBJECT_NAME => Recipient,
|
55
|
-
RecipientTransfer::OBJECT_NAME => RecipientTransfer,
|
56
|
-
Refund::OBJECT_NAME => Refund,
|
57
|
-
Reversal::OBJECT_NAME => Reversal,
|
58
|
-
SKU::OBJECT_NAME => SKU,
|
59
|
-
Source::OBJECT_NAME => Source,
|
60
|
-
Subscription::OBJECT_NAME => Subscription,
|
61
|
-
SubscriptionItem::OBJECT_NAME => SubscriptionItem,
|
62
|
-
ThreeDSecure::OBJECT_NAME => ThreeDSecure,
|
63
|
-
Token::OBJECT_NAME => Token,
|
64
|
-
Transfer::OBJECT_NAME => Transfer,
|
65
|
-
}
|
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
|
66
48
|
end
|
67
49
|
|
68
50
|
# Converts a hash of fields or an array of hashes into a +StripeObject+ or
|
@@ -78,41 +60,42 @@ module Stripe
|
|
78
60
|
# * +opts+ - Options for +StripeObject+ like an API key that will be reused
|
79
61
|
# on subsequent API calls.
|
80
62
|
def self.convert_to_stripe_object(data, opts = {})
|
63
|
+
opts = normalize_opts(opts)
|
64
|
+
|
81
65
|
case data
|
82
66
|
when Array
|
83
67
|
data.map { |i| convert_to_stripe_object(i, opts) }
|
84
68
|
when Hash
|
85
|
-
# Try converting to a known object class. If none available, fall back
|
86
|
-
|
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)
|
87
73
|
else
|
88
74
|
data
|
89
75
|
end
|
90
76
|
end
|
91
77
|
|
92
|
-
def self.
|
93
|
-
if Stripe.
|
94
|
-
|
95
|
-
|
78
|
+
def self.log_error(message, data = {})
|
79
|
+
if !Stripe.logger.nil? ||
|
80
|
+
!Stripe.log_level.nil? && Stripe.log_level <= Stripe::LEVEL_ERROR
|
81
|
+
log_internal(message, data, color: :cyan, level: Stripe::LEVEL_ERROR,
|
82
|
+
logger: Stripe.logger, out: $stderr)
|
96
83
|
end
|
97
84
|
end
|
98
85
|
|
99
|
-
def self.
|
100
|
-
if Stripe.
|
101
|
-
|
102
|
-
|
86
|
+
def self.log_info(message, data = {})
|
87
|
+
if !Stripe.logger.nil? ||
|
88
|
+
!Stripe.log_level.nil? && Stripe.log_level <= Stripe::LEVEL_INFO
|
89
|
+
log_internal(message, data, color: :cyan, level: Stripe::LEVEL_INFO,
|
90
|
+
logger: Stripe.logger, out: $stdout)
|
103
91
|
end
|
104
92
|
end
|
105
93
|
|
106
|
-
def self.
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
File.open(file) { |f| }
|
112
|
-
rescue
|
113
|
-
false
|
114
|
-
else
|
115
|
-
true
|
94
|
+
def self.log_debug(message, data = {})
|
95
|
+
if !Stripe.logger.nil? ||
|
96
|
+
!Stripe.log_level.nil? && Stripe.log_level <= Stripe::LEVEL_DEBUG
|
97
|
+
log_internal(message, data, color: :blue, level: Stripe::LEVEL_DEBUG,
|
98
|
+
logger: Stripe.logger, out: $stdout)
|
116
99
|
end
|
117
100
|
end
|
118
101
|
|
@@ -121,7 +104,11 @@ module Stripe
|
|
121
104
|
when Hash
|
122
105
|
new_hash = {}
|
123
106
|
object.each do |key, value|
|
124
|
-
key = (
|
107
|
+
key = (begin
|
108
|
+
key.to_sym
|
109
|
+
rescue StandardError
|
110
|
+
key
|
111
|
+
end) || key
|
125
112
|
new_hash[key] = symbolize_names(value)
|
126
113
|
end
|
127
114
|
new_hash
|
@@ -137,22 +124,8 @@ module Stripe
|
|
137
124
|
# involves escaping special characters from parameter keys and values (e.g.
|
138
125
|
# `&`).
|
139
126
|
def self.encode_parameters(params)
|
140
|
-
Util.flatten_params(params)
|
141
|
-
|
142
|
-
end
|
143
|
-
|
144
|
-
# Transforms an array into a hash with integer keys. Used for a small
|
145
|
-
# number of API endpoints. If the argument is not an Array, return it
|
146
|
-
# unchanged. Example: [{foo: 'bar'}] => {"0" => {foo: "bar"}}
|
147
|
-
def self.array_to_hash(array)
|
148
|
-
case array
|
149
|
-
when Array
|
150
|
-
hash = {}
|
151
|
-
array.each_with_index { |v,i| hash[i.to_s] = v }
|
152
|
-
hash
|
153
|
-
else
|
154
|
-
array
|
155
|
-
end
|
127
|
+
Util.flatten_params(params)
|
128
|
+
.map { |k, v| "#{url_encode(k)}=#{url_encode(v)}" }.join("&")
|
156
129
|
end
|
157
130
|
|
158
131
|
# Encodes a string in a way that makes it suitable for use in a set of
|
@@ -163,20 +136,19 @@ module Stripe
|
|
163
136
|
# Don't use strict form encoding by changing the square bracket control
|
164
137
|
# characters back to their literals. This is fine by the server, and
|
165
138
|
# makes these parameter strings easier to read.
|
166
|
-
gsub(
|
139
|
+
gsub("%5B", "[").gsub("%5D", "]")
|
167
140
|
end
|
168
141
|
|
169
|
-
def self.flatten_params(params, parent_key=nil)
|
142
|
+
def self.flatten_params(params, parent_key = nil)
|
170
143
|
result = []
|
171
144
|
|
172
145
|
# do not sort the final output because arrays (and arrays of hashes
|
173
146
|
# especially) can be order sensitive, but do sort incoming parameters
|
174
147
|
params.each do |key, value|
|
175
|
-
calculated_key = parent_key ? "#{parent_key}[#{key}]" :
|
148
|
+
calculated_key = parent_key ? "#{parent_key}[#{key}]" : key.to_s
|
176
149
|
if value.is_a?(Hash)
|
177
150
|
result += flatten_params(value, calculated_key)
|
178
151
|
elsif value.is_a?(Array)
|
179
|
-
check_array_of_maps_start_keys!(value)
|
180
152
|
result += flatten_params_array(value, calculated_key)
|
181
153
|
else
|
182
154
|
result << [calculated_key, value]
|
@@ -188,20 +160,32 @@ module Stripe
|
|
188
160
|
|
189
161
|
def self.flatten_params_array(value, calculated_key)
|
190
162
|
result = []
|
191
|
-
value.
|
163
|
+
value.each_with_index do |elem, i|
|
192
164
|
if elem.is_a?(Hash)
|
193
|
-
result += flatten_params(elem, "#{calculated_key}[]")
|
165
|
+
result += flatten_params(elem, "#{calculated_key}[#{i}]")
|
194
166
|
elsif elem.is_a?(Array)
|
195
167
|
result += flatten_params_array(elem, calculated_key)
|
196
168
|
else
|
197
|
-
result << ["#{calculated_key}[]", elem]
|
169
|
+
result << ["#{calculated_key}[#{i}]", elem]
|
198
170
|
end
|
199
171
|
end
|
200
172
|
result
|
201
173
|
end
|
202
174
|
|
175
|
+
# `Time.now` can be unstable in cases like an administrator manually
|
176
|
+
# updating its value or a reconcilation via NTP. For this reason, prefer
|
177
|
+
# the use of the system's monotonic clock especially where comparing times
|
178
|
+
# to calculate an elapsed duration.
|
179
|
+
#
|
180
|
+
# Shortcut for getting monotonic time, mostly for purposes of line length
|
181
|
+
# and test stubbing. Returns time in seconds since the event used for
|
182
|
+
# monotonic reference purposes by the platform (e.g. system boot time).
|
183
|
+
def self.monotonic_time
|
184
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
185
|
+
end
|
186
|
+
|
203
187
|
def self.normalize_id(id)
|
204
|
-
if id.
|
188
|
+
if id.is_a?(Hash) # overloaded id
|
205
189
|
params_hash = id.dup
|
206
190
|
id = params_hash.delete(:id)
|
207
191
|
else
|
@@ -215,22 +199,24 @@ module Stripe
|
|
215
199
|
def self.normalize_opts(opts)
|
216
200
|
case opts
|
217
201
|
when String
|
218
|
-
{:
|
202
|
+
{ api_key: opts }
|
219
203
|
when Hash
|
220
|
-
check_api_key!(opts.fetch(:api_key)) if opts.
|
204
|
+
check_api_key!(opts.fetch(:api_key)) if opts.key?(:api_key)
|
221
205
|
opts.clone
|
222
206
|
else
|
223
|
-
raise TypeError
|
207
|
+
raise TypeError, "normalize_opts expects a string or a hash"
|
224
208
|
end
|
225
209
|
end
|
226
210
|
|
227
211
|
def self.check_string_argument!(key)
|
228
|
-
raise TypeError
|
212
|
+
raise TypeError, "argument must be a string" unless key.is_a?(String)
|
213
|
+
|
229
214
|
key
|
230
215
|
end
|
231
216
|
|
232
217
|
def self.check_api_key!(key)
|
233
|
-
raise TypeError
|
218
|
+
raise TypeError, "api_key must be a string" unless key.is_a?(String)
|
219
|
+
|
234
220
|
key
|
235
221
|
end
|
236
222
|
|
@@ -240,15 +226,11 @@ module Stripe
|
|
240
226
|
# certain key values when the user could have set them with a variety of
|
241
227
|
# diffent naming schemes.
|
242
228
|
def self.normalize_headers(headers)
|
243
|
-
headers.
|
244
|
-
if k.is_a?(Symbol)
|
245
|
-
|
246
|
-
elsif k.is_a?(String)
|
247
|
-
k = titlecase_parts(k)
|
248
|
-
end
|
229
|
+
headers.each_with_object({}) do |(k, v), new_headers|
|
230
|
+
k = k.to_s.tr("_", "-") if k.is_a?(Symbol)
|
231
|
+
k = k.split("-").reject(&:empty?).map(&:capitalize).join("-")
|
249
232
|
|
250
233
|
new_headers[k] = v
|
251
|
-
new_headers
|
252
234
|
end
|
253
235
|
end
|
254
236
|
|
@@ -262,69 +244,33 @@ module Stripe
|
|
262
244
|
|
263
245
|
# Constant time string comparison to prevent timing attacks
|
264
246
|
# Code borrowed from ActiveSupport
|
265
|
-
def self.secure_compare(
|
266
|
-
return false unless
|
247
|
+
def self.secure_compare(str_a, str_b)
|
248
|
+
return false unless str_a.bytesize == str_b.bytesize
|
267
249
|
|
268
|
-
l =
|
250
|
+
l = str_a.unpack "C#{str_a.bytesize}"
|
269
251
|
|
270
252
|
res = 0
|
271
|
-
|
272
|
-
res
|
253
|
+
str_b.each_byte { |byte| res |= byte ^ l.shift }
|
254
|
+
res.zero?
|
273
255
|
end
|
274
256
|
|
275
|
-
|
257
|
+
#
|
258
|
+
# private
|
259
|
+
#
|
276
260
|
|
277
261
|
COLOR_CODES = {
|
278
|
-
:
|
279
|
-
:
|
280
|
-
:
|
281
|
-
:
|
282
|
-
:
|
283
|
-
:
|
284
|
-
:
|
285
|
-
:
|
286
|
-
:
|
287
|
-
}
|
262
|
+
black: 0, light_black: 60,
|
263
|
+
red: 1, light_red: 61,
|
264
|
+
green: 2, light_green: 62,
|
265
|
+
yellow: 3, light_yellow: 63,
|
266
|
+
blue: 4, light_blue: 64,
|
267
|
+
magenta: 5, light_magenta: 65,
|
268
|
+
cyan: 6, light_cyan: 66,
|
269
|
+
white: 7, light_white: 67,
|
270
|
+
default: 9,
|
271
|
+
}.freeze
|
288
272
|
private_constant :COLOR_CODES
|
289
273
|
|
290
|
-
# We use a pretty janky version of form encoding (Rack's) that supports
|
291
|
-
# more complex data structures like maps and arrays through the use of
|
292
|
-
# specialized syntax. To encode an array of maps like:
|
293
|
-
#
|
294
|
-
# [{a: 1, b: 2}, {a: 3, b: 4}]
|
295
|
-
#
|
296
|
-
# We have to produce something that looks like this:
|
297
|
-
#
|
298
|
-
# arr[][a]=1&arr[][b]=2&arr[][a]=3&arr[][b]=4
|
299
|
-
#
|
300
|
-
# The only way for the server to recognize that this is a two item array is
|
301
|
-
# that it notices the repetition of element "a", so it's key that these
|
302
|
-
# repeated elements are encoded first.
|
303
|
-
#
|
304
|
-
# This method is invoked for any arrays being encoded and checks that if
|
305
|
-
# the array contains all non-empty maps, that each of those maps must start
|
306
|
-
# with the same key so that their boundaries can be properly encoded.
|
307
|
-
def self.check_array_of_maps_start_keys!(arr)
|
308
|
-
expected_key = nil
|
309
|
-
arr.each do |item|
|
310
|
-
return if !item.is_a?(Hash)
|
311
|
-
return if item.count == 0
|
312
|
-
|
313
|
-
first_key = item.first[0]
|
314
|
-
|
315
|
-
if expected_key
|
316
|
-
if expected_key != first_key
|
317
|
-
raise ArgumentError,
|
318
|
-
"All maps nested in an array should start with the same key " +
|
319
|
-
"(expected starting key '#{expected_key}', got '#{first_key}')"
|
320
|
-
end
|
321
|
-
else
|
322
|
-
expected_key = first_key
|
323
|
-
end
|
324
|
-
end
|
325
|
-
end
|
326
|
-
private_class_method :check_array_of_maps_start_keys!
|
327
|
-
|
328
274
|
# Uses an ANSI escape code to colorize text if it's going to be sent to a
|
329
275
|
# TTY.
|
330
276
|
def self.colorize(val, color, isatty)
|
@@ -338,35 +284,47 @@ module Stripe
|
|
338
284
|
end
|
339
285
|
private_class_method :colorize
|
340
286
|
|
341
|
-
#
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
287
|
+
# Turns an integer log level into a printable name.
|
288
|
+
def self.level_name(level)
|
289
|
+
case level
|
290
|
+
when LEVEL_DEBUG then "debug"
|
291
|
+
when LEVEL_ERROR then "error"
|
292
|
+
when LEVEL_INFO then "info"
|
293
|
+
else level
|
294
|
+
end
|
295
|
+
end
|
296
|
+
private_class_method :level_name
|
297
|
+
|
298
|
+
def self.log_internal(message, data = {}, color:, level:, logger:, out:)
|
299
|
+
data_str = data.reject { |_k, v| v.nil? }
|
300
|
+
.map do |(k, v)|
|
301
|
+
format("%<key>s=%<value>s",
|
302
|
+
key: colorize(k, color, logger.nil? && !out.nil? && out.isatty),
|
303
|
+
value: wrap_logfmt_value(v))
|
304
|
+
end.join(" ")
|
305
|
+
|
306
|
+
if !logger.nil?
|
307
|
+
# the library's log levels are mapped to the same values as the
|
308
|
+
# standard library's logger
|
309
|
+
logger.log(level,
|
310
|
+
format("message=%<message>s %<data_str>s",
|
311
|
+
message: wrap_logfmt_value(message),
|
312
|
+
data_str: data_str))
|
313
|
+
elsif out.isatty
|
314
|
+
out.puts format("%<level>s %<message>s %<data_str>s",
|
315
|
+
level: colorize(level_name(level)[0, 4].upcase,
|
316
|
+
color, out.isatty),
|
317
|
+
message: message,
|
318
|
+
data_str: data_str)
|
355
319
|
else
|
356
|
-
out.puts "message
|
357
|
-
|
320
|
+
out.puts format("message=%<message>s level=%<level>s %<data_str>s",
|
321
|
+
message: wrap_logfmt_value(message),
|
322
|
+
level: level_name(level),
|
323
|
+
data_str: data_str)
|
358
324
|
end
|
359
325
|
end
|
360
326
|
private_class_method :log_internal
|
361
327
|
|
362
|
-
def self.titlecase_parts(s)
|
363
|
-
s.split("-").
|
364
|
-
select { |p| p != "" }.
|
365
|
-
map { |p| p[0].upcase + p[1..-1].downcase }.
|
366
|
-
join("-")
|
367
|
-
end
|
368
|
-
private_class_method :titlecase_parts
|
369
|
-
|
370
328
|
# Wraps a value in double quotes if it looks sufficiently complex so that
|
371
329
|
# it can be read by logfmt parsers.
|
372
330
|
def self.wrap_logfmt_value(val)
|
@@ -380,7 +338,7 @@ module Stripe
|
|
380
338
|
if %r{[^\w\-/]} =~ val
|
381
339
|
# If the string contains any special characters, escape any double
|
382
340
|
# quotes it has, remove newlines, and wrap the whole thing in quotes.
|
383
|
-
%
|
341
|
+
format(%("%<value>s"), value: val.gsub('"', '\"').delete("\n"))
|
384
342
|
else
|
385
343
|
# Otherwise use the basic value if it looks like a standard set of
|
386
344
|
# 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,28 +8,32 @@ 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,
|
10
|
-
|
11
|
-
event = Event.construct_from(data)
|
12
|
-
|
11
|
+
def self.construct_event(payload, sig_header, secret,
|
12
|
+
tolerance: DEFAULT_TOLERANCE)
|
13
13
|
Signature.verify_header(payload, sig_header, secret, tolerance: tolerance)
|
14
14
|
|
15
|
-
|
15
|
+
# It's a good idea to parse the payload only after verifying it. We use
|
16
|
+
# `symbolize_names` so it would otherwise be technically possible to
|
17
|
+
# flood a target's memory if they were on an older version of Ruby that
|
18
|
+
# doesn't GC symbols. It also decreases the likelihood that we receive a
|
19
|
+
# bad payload that fails to parse and throws an exception.
|
20
|
+
data = JSON.parse(payload, symbolize_names: true)
|
21
|
+
Event.construct_from(data)
|
16
22
|
end
|
17
23
|
|
18
24
|
module Signature
|
19
|
-
EXPECTED_SCHEME =
|
25
|
+
EXPECTED_SCHEME = "v1"
|
20
26
|
|
21
27
|
def self.compute_signature(payload, secret)
|
22
|
-
OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new(
|
28
|
+
OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha256"), secret, payload)
|
23
29
|
end
|
24
30
|
private_class_method :compute_signature
|
25
31
|
|
26
32
|
# Extracts the timestamp and the signature(s) with the desired scheme
|
27
33
|
# from the header
|
28
34
|
def self.get_timestamp_and_signatures(header, scheme)
|
29
|
-
list_items = header.split(/,\s*/).map { |i| i.split(
|
30
|
-
timestamp = Integer(list_items.select { |i| i[0] ==
|
35
|
+
list_items = header.split(/,\s*/).map { |i| i.split("=", 2) }
|
36
|
+
timestamp = Integer(list_items.select { |i| i[0] == "t" }[0][1])
|
31
37
|
signatures = list_items.select { |i| i[0] == scheme }.map { |i| i[1] }
|
32
38
|
[timestamp, signatures]
|
33
39
|
end
|
@@ -45,31 +51,36 @@ module Stripe
|
|
45
51
|
# Returns true otherwise
|
46
52
|
def self.verify_header(payload, header, secret, tolerance: nil)
|
47
53
|
begin
|
48
|
-
timestamp, signatures =
|
49
|
-
|
54
|
+
timestamp, signatures =
|
55
|
+
get_timestamp_and_signatures(header, EXPECTED_SCHEME)
|
56
|
+
rescue StandardError
|
50
57
|
raise SignatureVerificationError.new(
|
51
58
|
"Unable to extract timestamp and signatures from header",
|
52
|
-
header, http_body: payload
|
59
|
+
header, http_body: payload
|
60
|
+
)
|
53
61
|
end
|
54
62
|
|
55
63
|
if signatures.empty?
|
56
64
|
raise SignatureVerificationError.new(
|
57
65
|
"No signatures found with expected scheme #{EXPECTED_SCHEME}",
|
58
|
-
header, http_body: payload
|
66
|
+
header, http_body: payload
|
67
|
+
)
|
59
68
|
end
|
60
69
|
|
61
70
|
signed_payload = "#{timestamp}.#{payload}"
|
62
71
|
expected_sig = compute_signature(signed_payload, secret)
|
63
|
-
unless signatures.any? {|s| Util.secure_compare(expected_sig, s)}
|
72
|
+
unless signatures.any? { |s| Util.secure_compare(expected_sig, s) }
|
64
73
|
raise SignatureVerificationError.new(
|
65
74
|
"No signatures found matching the expected signature for payload",
|
66
|
-
header, http_body: payload
|
75
|
+
header, http_body: payload
|
76
|
+
)
|
67
77
|
end
|
68
78
|
|
69
79
|
if tolerance && timestamp < Time.now.to_f - tolerance
|
70
80
|
raise SignatureVerificationError.new(
|
71
81
|
"Timestamp outside the tolerance zone (#{Time.at(timestamp)})",
|
72
|
-
header, http_body: payload
|
82
|
+
header, http_body: payload
|
83
|
+
)
|
73
84
|
end
|
74
85
|
|
75
86
|
true
|