stripe 3.8.2 → 5.45.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (214) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +424 -1
  3. data/CODE_OF_CONDUCT.md +77 -0
  4. data/Gemfile +15 -20
  5. data/README.md +201 -58
  6. data/Rakefile +12 -9
  7. data/VERSION +1 -1
  8. data/bin/stripe-console +3 -1
  9. data/lib/stripe/api_operations/create.rb +3 -1
  10. data/lib/stripe/api_operations/delete.rb +30 -2
  11. data/lib/stripe/api_operations/list.rb +3 -12
  12. data/lib/stripe/api_operations/nested_resource.rb +40 -28
  13. data/lib/stripe/api_operations/request.rb +83 -9
  14. data/lib/stripe/api_operations/save.rb +19 -7
  15. data/lib/stripe/api_resource.rb +65 -4
  16. data/lib/stripe/connection_manager.rb +200 -0
  17. data/lib/stripe/error_object.rb +93 -0
  18. data/lib/stripe/errors.rb +47 -19
  19. data/lib/stripe/instrumentation.rb +84 -0
  20. data/lib/stripe/list_object.rb +43 -7
  21. data/lib/stripe/multipart_encoder.rb +131 -0
  22. data/lib/stripe/oauth.rb +14 -7
  23. data/lib/stripe/object_types.rb +110 -0
  24. data/lib/stripe/{account.rb → resources/account.rb} +60 -28
  25. data/lib/stripe/resources/account_link.rb +10 -0
  26. data/lib/stripe/resources/alipay_account.rb +34 -0
  27. data/lib/stripe/{apple_pay_domain.rb → resources/apple_pay_domain.rb} +4 -1
  28. data/lib/stripe/resources/application_fee.rb +14 -0
  29. data/lib/stripe/resources/application_fee_refund.rb +31 -0
  30. data/lib/stripe/resources/balance.rb +8 -0
  31. data/lib/stripe/resources/balance_transaction.rb +10 -0
  32. data/lib/stripe/resources/bank_account.rb +43 -0
  33. data/lib/stripe/resources/billing_portal/configuration.rb +14 -0
  34. data/lib/stripe/resources/billing_portal/session.rb +12 -0
  35. data/lib/stripe/{bitcoin_receiver.rb → resources/bitcoin_receiver.rb} +6 -6
  36. data/lib/stripe/resources/bitcoin_transaction.rb +16 -0
  37. data/lib/stripe/resources/capability.rb +34 -0
  38. data/lib/stripe/{card.rb → resources/card.rb} +15 -4
  39. data/lib/stripe/resources/charge.rb +23 -0
  40. data/lib/stripe/resources/checkout/session.rb +27 -0
  41. data/lib/stripe/resources/country_spec.rb +10 -0
  42. data/lib/stripe/{coupon.rb → resources/coupon.rb} +5 -2
  43. data/lib/stripe/resources/credit_note.rb +33 -0
  44. data/lib/stripe/resources/credit_note_line_item.rb +8 -0
  45. data/lib/stripe/resources/customer.rb +52 -0
  46. data/lib/stripe/resources/customer_balance_transaction.rb +31 -0
  47. data/lib/stripe/resources/discount.rb +8 -0
  48. data/lib/stripe/resources/dispute.rb +22 -0
  49. data/lib/stripe/resources/ephemeral_key.rb +20 -0
  50. data/lib/stripe/resources/event.rb +10 -0
  51. data/lib/stripe/resources/exchange_rate.rb +10 -0
  52. data/lib/stripe/resources/file.rb +36 -0
  53. data/lib/stripe/resources/file_link.rb +12 -0
  54. data/lib/stripe/resources/identity/verification_report.rb +12 -0
  55. data/lib/stripe/resources/identity/verification_session.rb +35 -0
  56. data/lib/stripe/resources/invoice.rb +74 -0
  57. data/lib/stripe/{invoice_item.rb → resources/invoice_item.rb} +5 -2
  58. data/lib/stripe/resources/invoice_line_item.rb +8 -0
  59. data/lib/stripe/resources/issuing/authorization.rb +34 -0
  60. data/lib/stripe/resources/issuing/card.rb +25 -0
  61. data/lib/stripe/resources/issuing/card_details.rb +10 -0
  62. data/lib/stripe/resources/issuing/cardholder.rb +14 -0
  63. data/lib/stripe/resources/issuing/dispute.rb +25 -0
  64. data/lib/stripe/resources/issuing/transaction.rb +13 -0
  65. data/lib/stripe/resources/line_item.rb +8 -0
  66. data/lib/stripe/resources/login_link.rb +15 -0
  67. data/lib/stripe/resources/mandate.rb +8 -0
  68. data/lib/stripe/resources/order.rb +33 -0
  69. data/lib/stripe/resources/order_return.rb +10 -0
  70. data/lib/stripe/resources/payment_intent.rb +53 -0
  71. data/lib/stripe/resources/payment_link.rb +23 -0
  72. data/lib/stripe/resources/payment_method.rb +33 -0
  73. data/lib/stripe/resources/payout.rb +33 -0
  74. data/lib/stripe/resources/person.rb +32 -0
  75. data/lib/stripe/{plan.rb → resources/plan.rb} +4 -1
  76. data/lib/stripe/resources/price.rb +12 -0
  77. data/lib/stripe/{product.rb → resources/product.rb} +6 -3
  78. data/lib/stripe/resources/promotion_code.rb +12 -0
  79. data/lib/stripe/resources/quote.rb +105 -0
  80. data/lib/stripe/resources/radar/early_fraud_warning.rb +12 -0
  81. data/lib/stripe/resources/radar/value_list.rb +15 -0
  82. data/lib/stripe/resources/radar/value_list_item.rb +14 -0
  83. data/lib/stripe/{recipient.rb → resources/recipient.rb} +5 -6
  84. data/lib/stripe/resources/recipient_transfer.rb +7 -0
  85. data/lib/stripe/{refund.rb → resources/refund.rb} +4 -1
  86. data/lib/stripe/resources/reporting/report_run.rb +13 -0
  87. data/lib/stripe/resources/reporting/report_type.rb +13 -0
  88. data/lib/stripe/resources/reversal.rb +30 -0
  89. data/lib/stripe/resources/review.rb +21 -0
  90. data/lib/stripe/resources/setup_attempt.rb +10 -0
  91. data/lib/stripe/resources/setup_intent.rb +43 -0
  92. data/lib/stripe/resources/shipping_rate.rb +12 -0
  93. data/lib/stripe/resources/sigma/scheduled_query_run.rb +16 -0
  94. data/lib/stripe/{sku.rb → resources/sku.rb} +6 -3
  95. data/lib/stripe/resources/source.rb +47 -0
  96. data/lib/stripe/resources/source_transaction.rb +8 -0
  97. data/lib/stripe/resources/subscription.rb +26 -0
  98. data/lib/stripe/resources/subscription_item.rb +26 -0
  99. data/lib/stripe/resources/subscription_schedule.rb +33 -0
  100. data/lib/stripe/resources/tax_code.rb +10 -0
  101. data/lib/stripe/resources/tax_id.rb +27 -0
  102. data/lib/stripe/resources/tax_rate.rb +12 -0
  103. data/lib/stripe/resources/terminal/connection_token.rb +12 -0
  104. data/lib/stripe/resources/terminal/location.rb +15 -0
  105. data/lib/stripe/resources/terminal/reader.rb +15 -0
  106. data/lib/stripe/resources/test_helpers/test_clock.rb +25 -0
  107. data/lib/stripe/{three_d_secure.rb → resources/three_d_secure.rb} +4 -1
  108. data/lib/stripe/resources/token.rb +10 -0
  109. data/lib/stripe/resources/topup.rb +23 -0
  110. data/lib/stripe/resources/transfer.rb +27 -0
  111. data/lib/stripe/resources/usage_record.rb +8 -0
  112. data/lib/stripe/resources/usage_record_summary.rb +8 -0
  113. data/lib/stripe/resources/webhook_endpoint.rb +13 -0
  114. data/lib/stripe/resources.rb +93 -0
  115. data/lib/stripe/singleton_api_resource.rb +8 -2
  116. data/lib/stripe/stripe_client.rb +712 -233
  117. data/lib/stripe/stripe_configuration.rb +194 -0
  118. data/lib/stripe/stripe_object.rb +180 -67
  119. data/lib/stripe/stripe_response.rb +89 -27
  120. data/lib/stripe/util.rb +87 -168
  121. data/lib/stripe/version.rb +3 -1
  122. data/lib/stripe/webhook.rb +45 -10
  123. data/lib/stripe.rb +47 -160
  124. data/stripe.gemspec +27 -8
  125. metadata +112 -178
  126. data/.gitattributes +0 -4
  127. data/.github/ISSUE_TEMPLATE.md +0 -5
  128. data/.gitignore +0 -6
  129. data/.rubocop.yml +0 -20
  130. data/.rubocop_todo.yml +0 -60
  131. data/.travis.yml +0 -38
  132. data/lib/stripe/alipay_account.rb +0 -25
  133. data/lib/stripe/application_fee.rb +0 -25
  134. data/lib/stripe/application_fee_refund.rb +0 -20
  135. data/lib/stripe/balance.rb +0 -5
  136. data/lib/stripe/balance_transaction.rb +0 -11
  137. data/lib/stripe/bank_account.rb +0 -30
  138. data/lib/stripe/bitcoin_transaction.rb +0 -13
  139. data/lib/stripe/charge.rb +0 -82
  140. data/lib/stripe/country_spec.rb +0 -11
  141. data/lib/stripe/customer.rb +0 -88
  142. data/lib/stripe/dispute.rb +0 -17
  143. data/lib/stripe/ephemeral_key.rb +0 -18
  144. data/lib/stripe/event.rb +0 -7
  145. data/lib/stripe/exchange_rate.rb +0 -11
  146. data/lib/stripe/file_upload.rb +0 -35
  147. data/lib/stripe/invoice.rb +0 -30
  148. data/lib/stripe/invoice_line_item.rb +0 -5
  149. data/lib/stripe/login_link.rb +0 -9
  150. data/lib/stripe/order.rb +0 -35
  151. data/lib/stripe/order_return.rb +0 -11
  152. data/lib/stripe/payout.rb +0 -18
  153. data/lib/stripe/recipient_transfer.rb +0 -5
  154. data/lib/stripe/reversal.rb +0 -20
  155. data/lib/stripe/source.rb +0 -36
  156. data/lib/stripe/source_transaction.rb +0 -5
  157. data/lib/stripe/subscription.rb +0 -43
  158. data/lib/stripe/subscription_item.rb +0 -14
  159. data/lib/stripe/token.rb +0 -7
  160. data/lib/stripe/transfer.rb +0 -21
  161. data/test/api_stub_helpers.rb +0 -0
  162. data/test/stripe/account_external_accounts_operations_test.rb +0 -66
  163. data/test/stripe/account_login_links_operations_test.rb +0 -19
  164. data/test/stripe/account_test.rb +0 -190
  165. data/test/stripe/alipay_account_test.rb +0 -35
  166. data/test/stripe/api_operations_test.rb +0 -76
  167. data/test/stripe/api_resource_test.rb +0 -522
  168. data/test/stripe/apple_pay_domain_test.rb +0 -31
  169. data/test/stripe/application_fee_refund_test.rb +0 -35
  170. data/test/stripe/application_fee_refunds_operations_test.rb +0 -54
  171. data/test/stripe/application_fee_test.rb +0 -12
  172. data/test/stripe/balance_test.rb +0 -11
  173. data/test/stripe/bank_account_test.rb +0 -34
  174. data/test/stripe/charge_test.rb +0 -57
  175. data/test/stripe/country_spec_test.rb +0 -18
  176. data/test/stripe/coupon_test.rb +0 -42
  177. data/test/stripe/customer_card_test.rb +0 -42
  178. data/test/stripe/customer_sources_operations_test.rb +0 -66
  179. data/test/stripe/customer_test.rb +0 -113
  180. data/test/stripe/dispute_test.rb +0 -40
  181. data/test/stripe/ephemeral_key_test.rb +0 -84
  182. data/test/stripe/errors_test.rb +0 -18
  183. data/test/stripe/exchange_rate_test.rb +0 -18
  184. data/test/stripe/file_upload_test.rb +0 -83
  185. data/test/stripe/invoice_item_test.rb +0 -53
  186. data/test/stripe/invoice_line_item_test.rb +0 -6
  187. data/test/stripe/invoice_test.rb +0 -111
  188. data/test/stripe/list_object_test.rb +0 -154
  189. data/test/stripe/login_link_test.rb +0 -35
  190. data/test/stripe/oauth_test.rb +0 -77
  191. data/test/stripe/order_return_test.rb +0 -19
  192. data/test/stripe/order_test.rb +0 -57
  193. data/test/stripe/payout_test.rb +0 -48
  194. data/test/stripe/plan_test.rb +0 -50
  195. data/test/stripe/product_test.rb +0 -45
  196. data/test/stripe/recipient_test.rb +0 -47
  197. data/test/stripe/refund_test.rb +0 -37
  198. data/test/stripe/reversal_test.rb +0 -41
  199. data/test/stripe/sku_test.rb +0 -48
  200. data/test/stripe/source_test.rb +0 -84
  201. data/test/stripe/source_transaction_test.rb +0 -28
  202. data/test/stripe/stripe_client_test.rb +0 -728
  203. data/test/stripe/stripe_object_test.rb +0 -448
  204. data/test/stripe/stripe_response_test.rb +0 -47
  205. data/test/stripe/subscription_item_test.rb +0 -52
  206. data/test/stripe/subscription_test.rb +0 -104
  207. data/test/stripe/three_d_secure_test.rb +0 -21
  208. data/test/stripe/transfer_reversals_operations_test.rb +0 -55
  209. data/test/stripe/transfer_test.rb +0 -41
  210. data/test/stripe/util_test.rb +0 -432
  211. data/test/stripe/webhook_test.rb +0 -94
  212. data/test/stripe_test.rb +0 -59
  213. data/test/test_data.rb +0 -59
  214. data/test/test_helper.rb +0 -60
@@ -1,14 +1,54 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Stripe
2
- # StripeResponse encapsulates some vitals of a response that came back from
3
- # the Stripe API.
4
- class StripeResponse
5
- # The data contained by the HTTP body of the response deserialized from
6
- # JSON.
7
- attr_accessor :data
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
- # The raw HTTP body of the response.
10
- attr_accessor :http_body
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
- # Initializes a StripeResponse object from a Hash like the kind returned as
22
- # part of a Faraday exception.
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
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
- # 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)
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.http_headers = http_resp.headers
43
- resp.http_status = http_resp.status
44
- resp.request_id = http_resp.headers["Request-Id"]
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(h)
26
- case h
27
+ def self.objects_to_ids(obj)
28
+ case obj
27
29
  when APIResource
28
- h.id
30
+ obj.id
29
31
  when Hash
30
32
  res = {}
31
- h.each { |k, v| res[k] = objects_to_ids(v) unless v.nil? }
33
+ obj.each { |k, v| res[k] = objects_to_ids(v) unless v.nil? }
32
34
  res
33
35
  when Array
34
- h.map { |v| objects_to_ids(v) }
36
+ obj.map { |v| objects_to_ids(v) }
35
37
  else
36
- h
38
+ obj
37
39
  end
38
40
  end
39
41
 
40
42
  def self.object_classes
41
- @object_classes ||= {
42
- # data structures
43
- ListObject::OBJECT_NAME => ListObject,
44
-
45
- # business objects
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 to generic StripeObject
108
- object_classes.fetch(data[:object], StripeObject).construct_from(data, opts)
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
- if !Stripe.logger.nil? ||
116
- !Stripe.log_level.nil? && Stripe.log_level <= Stripe::LEVEL_ERROR
117
- log_internal(message, data, color: :cyan,
118
- level: Stripe::LEVEL_ERROR, logger: Stripe.logger, out: $stderr)
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
- if !Stripe.logger.nil? ||
124
- !Stripe.log_level.nil? && Stripe.log_level <= Stripe::LEVEL_INFO
125
- log_internal(message, data, color: :cyan,
126
- level: Stripe::LEVEL_INFO, logger: Stripe.logger, out: $stdout)
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
- if !Stripe.logger.nil? ||
132
- !Stripe.log_level.nil? && Stripe.log_level <= Stripe::LEVEL_DEBUG
133
- log_internal(message, data, color: :blue,
134
- level: Stripe::LEVEL_DEBUG, logger: Stripe.logger, out: $stdout)
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.each do |elem|
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
- opts.clone
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
- k = titlecase_parts(k.to_s.tr("_", "-"))
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(a, b)
300
- return false unless a.bytesize == b.bytesize
255
+ def self.secure_compare(str_a, str_b)
256
+ return false unless str_a.bytesize == str_b.bytesize
301
257
 
302
- l = a.unpack "C#{a.bytesize}"
258
+ l = str_a.unpack "C#{str_a.bytesize}"
303
259
 
304
260
  res = 0
305
- b.each_byte { |byte| res |= byte ^ l.shift }
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: 0, light_black: 60,
315
- red: 1, light_red: 61,
316
- green: 2, light_green: 62,
317
- yellow: 3, light_yellow: 63,
318
- blue: 4, light_blue: 64,
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: 6, light_cyan: 66,
321
- white: 7, light_white: 67,
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
- # TODO: Make these named required arguments when we drop support for Ruby
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("%s=%s", colorize(k, color, !out.nil? && out.isatty), wrap_logfmt_value(v))
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=%s %s", wrap_logfmt_value(message), data_str))
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("%s %s %s", colorize(level_name(level)[0, 4].upcase, color, out.isatty), message, data_str)
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=%s level=%s %s", wrap_logfmt_value(message), level_name(level), data_str)
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(%("%s"), val.gsub('"', '\"').delete("\n"))
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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Stripe
2
- VERSION = "3.8.2".freeze
4
+ VERSION = "5.45.0"
3
5
  end
@@ -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, tolerance: DEFAULT_TOLERANCE)
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".freeze
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
- def self.compute_signature(payload, secret)
25
- OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha256"), secret, payload)
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 = get_timestamp_and_signatures(header, EXPECTED_SCHEME)
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
- signed_payload = "#{timestamp}.#{payload}"
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.to_f - tolerance
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