stripe 8.7.0 → 9.0.0.pre.beta.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +477 -301
  3. data/OPENAPI_VERSION +1 -1
  4. data/README.md +11 -0
  5. data/VERSION +1 -1
  6. data/lib/stripe/api_operations/request.rb +2 -0
  7. data/lib/stripe/api_version.rb +2 -1
  8. data/lib/stripe/object_types.rb +18 -0
  9. data/lib/stripe/request_signing_authenticator.rb +83 -0
  10. data/lib/stripe/resources/account_session.rb +17 -0
  11. data/lib/stripe/resources/capital/financing_offer.rb +32 -0
  12. data/lib/stripe/resources/capital/financing_summary.rb +12 -0
  13. data/lib/stripe/resources/capital/financing_transaction.rb +13 -0
  14. data/lib/stripe/resources/customer_session.rb +12 -0
  15. data/lib/stripe/resources/financial_connections/account.rb +39 -0
  16. data/lib/stripe/resources/financial_connections/inferred_balance.rb +13 -0
  17. data/lib/stripe/resources/financial_connections/transaction.rb +13 -0
  18. data/lib/stripe/resources/gift_cards/card.rb +25 -0
  19. data/lib/stripe/resources/gift_cards/transaction.rb +56 -0
  20. data/lib/stripe/resources/issuing/card.rb +18 -0
  21. data/lib/stripe/resources/issuing/card_bundle.rb +13 -0
  22. data/lib/stripe/resources/issuing/card_design.rb +58 -0
  23. data/lib/stripe/resources/order.rb +89 -0
  24. data/lib/stripe/resources/payment_method_configuration.rb +13 -0
  25. data/lib/stripe/resources/quote.rb +131 -0
  26. data/lib/stripe/resources/quote_phase.rb +29 -0
  27. data/lib/stripe/resources/quote_preview_invoice.rb +40 -0
  28. data/lib/stripe/resources/quote_preview_schedule.rb +8 -0
  29. data/lib/stripe/resources/subscription_schedule.rb +18 -0
  30. data/lib/stripe/resources/tax/form.rb +39 -0
  31. data/lib/stripe/resources/tax/registration.rb +19 -0
  32. data/lib/stripe/resources/terminal/reader.rb +54 -0
  33. data/lib/stripe/resources.rb +18 -0
  34. data/lib/stripe/stripe_client.rb +60 -26
  35. data/lib/stripe/stripe_configuration.rb +3 -0
  36. data/lib/stripe/util.rb +8 -1
  37. data/lib/stripe/version.rb +1 -1
  38. data/lib/stripe/webhook.rb +2 -1
  39. data/lib/stripe.rb +47 -0
  40. metadata +23 -4
@@ -212,9 +212,10 @@ module Stripe
212
212
  end
213
213
 
214
214
  def execute_request(method, path,
215
- api_base: nil, api_key: nil, headers: {}, params: {})
215
+ api_base: nil, api_key: nil,
216
+ headers: {}, params: {}, api_mode: nil)
216
217
  http_resp, api_key = execute_request_internal(
217
- method, path, api_base, api_key, headers, params
218
+ method, path, api_base, api_key, headers, params, api_mode
218
219
  )
219
220
 
220
221
  begin
@@ -245,6 +246,7 @@ module Stripe
245
246
  def execute_request_stream(method, path,
246
247
  api_base: nil, api_key: nil,
247
248
  headers: {}, params: {},
249
+ api_mode: nil,
248
250
  &read_body_chunk_block)
249
251
  unless block_given?
250
252
  raise ArgumentError,
@@ -252,7 +254,8 @@ module Stripe
252
254
  end
253
255
 
254
256
  http_resp, api_key = execute_request_internal(
255
- method, path, api_base, api_key, headers, params, &read_body_chunk_block
257
+ method, path, api_base, api_key,
258
+ headers, params, api_mode, &read_body_chunk_block
256
259
  )
257
260
 
258
261
  # When the read_body_chunk_block is given, we no longer have access to the
@@ -432,7 +435,7 @@ module Stripe
432
435
 
433
436
  private def execute_request_internal(method, path,
434
437
  api_base, api_key, headers, params,
435
- &read_body_chunk_block)
438
+ api_mode, &read_body_chunk_block)
436
439
  raise ArgumentError, "method should be a symbol" \
437
440
  unless method.is_a?(Symbol)
438
441
  raise ArgumentError, "path should be a string" \
@@ -440,9 +443,10 @@ module Stripe
440
443
 
441
444
  api_base ||= config.api_base
442
445
  api_key ||= config.api_key
446
+ authenticator ||= config.authenticator
443
447
  params = Util.objects_to_ids(params)
444
448
 
445
- check_api_key!(api_key)
449
+ check_keys!(api_key, authenticator)
446
450
 
447
451
  body_params = nil
448
452
  query_params = nil
@@ -455,8 +459,9 @@ module Stripe
455
459
 
456
460
  query_params, path = merge_query_params(query_params, path)
457
461
 
458
- headers = request_headers(api_key, method)
462
+ headers = request_headers(api_key, method, api_mode)
459
463
  .update(Util.normalize_headers(headers))
464
+
460
465
  url = api_url(path, api_base)
461
466
 
462
467
  # Merge given query parameters with any already encoded in the path.
@@ -467,13 +472,16 @@ module Stripe
467
472
  # a log-friendly variant of the encoded form. File objects are displayed
468
473
  # as such instead of as their file contents.
469
474
  body, body_log =
470
- body_params ? encode_body(body_params, headers) : [nil, nil]
475
+ body_params ? encode_body(body_params, headers, api_mode) : [nil, nil]
476
+
477
+ authenticator.authenticate(method, headers, body) unless api_key
471
478
 
472
479
  # stores information on the request we're about to make so that we don't
473
480
  # have to pass as many parameters around for logging.
474
481
  context = RequestLogContext.new
475
482
  context.account = headers["Stripe-Account"]
476
483
  context.api_key = api_key
484
+ context.authenticator = authenticator
477
485
  context.api_version = headers["Stripe-Version"]
478
486
  context.body = body_log
479
487
  context.idempotency_key = headers["Idempotency-Key"]
@@ -512,8 +520,16 @@ module Stripe
512
520
  (api_base || config.api_base) + url
513
521
  end
514
522
 
515
- private def check_api_key!(api_key)
516
- unless api_key
523
+ private def check_keys!(api_key, authenticator)
524
+ if api_key && authenticator
525
+ raise AuthenticationError, "Can't specify both API key " \
526
+ "and authenticator. Either set your API key" \
527
+ 'using "Stripe.api_key = <API-KEY>", or set your authenticator ' \
528
+ 'using "Stripe.authenticator = <AUTHENTICATOR>"' \
529
+ end
530
+
531
+ unless api_key || authenticator
532
+ # Default to missing API key error message for general users.
517
533
  raise AuthenticationError, "No API key provided. " \
518
534
  'Set your API key using "Stripe.api_key = <API-KEY>". ' \
519
535
  "You can generate API keys from the Stripe web interface. " \
@@ -532,7 +548,7 @@ module Stripe
532
548
  # Encodes a set of body parameters using multipart if `Content-Type` is set
533
549
  # for that, or standard form-encoding otherwise. Returns the encoded body
534
550
  # and a version of the encoded body that's safe to be logged.
535
- private def encode_body(body_params, headers)
551
+ private def encode_body(body_params, headers, api_mode)
536
552
  body = nil
537
553
  flattened_params = Util.flatten_params(body_params)
538
554
 
@@ -548,15 +564,22 @@ module Stripe
548
564
  flattened_params =
549
565
  flattened_params.map { |k, v| [k, v.is_a?(String) ? v : v.to_s] }.to_h
550
566
 
567
+ elsif api_mode == :preview
568
+ body = JSON.generate(body_params)
569
+ headers["Content-Type"] = "application/json"
551
570
  else
552
571
  body = Util.encode_parameters(body_params)
553
572
  end
554
573
 
555
- # We don't use `Util.encode_parameters` partly as an optimization (to not
556
- # redo work we've already done), and partly because the encoded forms of
557
- # certain characters introduce a lot of visual noise and it's nice to
558
- # have a clearer format for logs.
559
- body_log = flattened_params.map { |k, v| "#{k}=#{v}" }.join("&")
574
+ if api_mode == :preview
575
+ body_log = body
576
+ else
577
+ # We don't use `Util.encode_parameters` partly as an optimization (to
578
+ # not redo work we've already done), and partly because the encoded
579
+ # forms of certain characters introduce a lot of visual noise and it's
580
+ # nice to have a clearer format for logs.
581
+ body_log = flattened_params.map { |k, v| "#{k}=#{v}" }.join("&")
582
+ end
560
583
 
561
584
  [body, body_log]
562
585
  end
@@ -745,10 +768,11 @@ module Stripe
745
768
  end
746
769
 
747
770
  private def specific_api_error(resp, error_data, context)
771
+ message = error_data[:message]
748
772
  Util.log_error("Stripe API error",
749
773
  status: resp.http_status,
750
774
  error_code: error_data[:code],
751
- error_message: error_data[:message],
775
+ error_message: message,
752
776
  error_param: error_data[:param],
753
777
  error_type: error_data[:type],
754
778
  idempotency_key: context.idempotency_key,
@@ -769,26 +793,26 @@ module Stripe
769
793
  when 400, 404
770
794
  case error_data[:type]
771
795
  when "idempotency_error"
772
- IdempotencyError.new(error_data[:message], **opts)
796
+ IdempotencyError.new(message, **opts)
773
797
  else
774
798
  InvalidRequestError.new(
775
- error_data[:message], error_data[:param],
799
+ message, error_data[:param],
776
800
  **opts
777
801
  )
778
802
  end
779
803
  when 401
780
- AuthenticationError.new(error_data[:message], **opts)
804
+ AuthenticationError.new(message, **opts)
781
805
  when 402
782
806
  CardError.new(
783
- error_data[:message], error_data[:param],
807
+ message, error_data[:param],
784
808
  **opts
785
809
  )
786
810
  when 403
787
- PermissionError.new(error_data[:message], **opts)
811
+ PermissionError.new(message, **opts)
788
812
  when 429
789
- RateLimitError.new(error_data[:message], **opts)
813
+ RateLimitError.new(message, **opts)
790
814
  else
791
- APIError.new(error_data[:message], **opts)
815
+ APIError.new(message, **opts)
792
816
  end
793
817
  end
794
818
 
@@ -856,7 +880,7 @@ module Stripe
856
880
  message + "\n\n(Network error: #{error.message})"
857
881
  end
858
882
 
859
- private def request_headers(api_key, method)
883
+ private def request_headers(api_key, method, api_mode)
860
884
  user_agent = "Stripe/v1 RubyBindings/#{Stripe::VERSION}"
861
885
  unless Stripe.app_info.nil?
862
886
  user_agent += " " + format_app_info(Stripe.app_info)
@@ -865,9 +889,13 @@ module Stripe
865
889
  headers = {
866
890
  "User-Agent" => user_agent,
867
891
  "Authorization" => "Bearer #{api_key}",
868
- "Content-Type" => "application/x-www-form-urlencoded",
869
892
  }
870
893
 
894
+ if api_mode != :preview
895
+ # TODO: (major) don't set Content-Type if method is not post
896
+ headers["Content-Type"] = "application/x-www-form-urlencoded"
897
+ end
898
+
871
899
  if config.enable_telemetry? && !@last_request_metrics.nil?
872
900
  headers["X-Stripe-Client-Telemetry"] = JSON.generate(
873
901
  last_request_metrics: @last_request_metrics.payload
@@ -880,7 +908,12 @@ module Stripe
880
908
  headers["Idempotency-Key"] ||= SecureRandom.uuid
881
909
  end
882
910
 
883
- headers["Stripe-Version"] = config.api_version if config.api_version
911
+ if api_mode == :preview
912
+ headers["Stripe-Version"] = ApiVersion::PREVIEW
913
+ elsif config.api_version
914
+ headers["Stripe-Version"] = config.api_version
915
+ end
916
+
884
917
  headers["Stripe-Account"] = config.stripe_account if config.stripe_account
885
918
 
886
919
  user_agent = @system_profiler.user_agent
@@ -966,6 +999,7 @@ module Stripe
966
999
  attr_accessor :body
967
1000
  attr_accessor :account
968
1001
  attr_accessor :api_key
1002
+ attr_accessor :authenticator
969
1003
  attr_accessor :api_version
970
1004
  attr_accessor :idempotency_key
971
1005
  attr_accessor :method
@@ -27,6 +27,7 @@ module Stripe
27
27
  class StripeConfiguration
28
28
  attr_accessor :api_key
29
29
  attr_accessor :api_version
30
+ attr_accessor :authenticator
30
31
  attr_accessor :client_id
31
32
  attr_accessor :enable_telemetry
32
33
  attr_accessor :logger
@@ -63,6 +64,8 @@ module Stripe
63
64
  end
64
65
 
65
66
  def initialize
67
+ @api_version = ApiVersion::CURRENT
68
+
66
69
  @ca_bundle_path = Stripe::DEFAULT_CA_BUNDLE_PATH
67
70
  @enable_telemetry = true
68
71
  @verify_ssl_certs = true
data/lib/stripe/util.rb CHANGED
@@ -7,6 +7,7 @@ module Stripe
7
7
  # Options that a user is allowed to specify.
8
8
  OPTS_USER_SPECIFIED = Set[
9
9
  :api_key,
10
+ :authenticator,
10
11
  :idempotency_key,
11
12
  :stripe_account,
12
13
  :stripe_version
@@ -281,7 +282,13 @@ module Stripe
281
282
  when String
282
283
  { api_key: opts }
283
284
  when Hash
284
- check_api_key!(opts.fetch(:api_key)) if opts.key?(:api_key)
285
+ # If the user is using request signing for authentication,
286
+ # no need to check the api_key per request.
287
+ if !(opts.key?(:client) &&
288
+ opts.fetch(:client).config.authenticator) &&
289
+ opts.key?(:api_key)
290
+ check_api_key!(opts.fetch(:api_key))
291
+ end
285
292
  # Explicitly use dup here instead of clone to avoid preserving freeze
286
293
  # state on input params.
287
294
  opts.dup
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Stripe
4
- VERSION = "8.7.0"
4
+ VERSION = "9.0.0-beta.1"
5
5
  end
@@ -108,8 +108,9 @@ module Stripe
108
108
  end
109
109
 
110
110
  if tolerance && timestamp < Time.now - tolerance
111
+ formatted_timestamp = Time.at(timestamp).strftime("%F %T")
111
112
  raise SignatureVerificationError.new(
112
- "Timestamp outside the tolerance zone (#{Time.at(timestamp)})",
113
+ "Timestamp outside the tolerance zone (#{formatted_timestamp})",
113
114
  header, http_body: payload
114
115
  )
115
116
  end
data/lib/stripe.rb CHANGED
@@ -13,8 +13,10 @@ require "set"
13
13
  require "socket"
14
14
  require "uri"
15
15
  require "forwardable"
16
+ require "base64"
16
17
 
17
18
  # Version
19
+ require "stripe/api_version"
18
20
  require "stripe/version"
19
21
 
20
22
  # API operations
@@ -43,6 +45,7 @@ require "stripe/api_resource_test_helpers"
43
45
  require "stripe/singleton_api_resource"
44
46
  require "stripe/webhook"
45
47
  require "stripe/stripe_configuration"
48
+ require "stripe/request_signing_authenticator"
46
49
 
47
50
  # Named API resources
48
51
  require "stripe/resources"
@@ -69,6 +72,7 @@ module Stripe
69
72
 
70
73
  # User configurable options
71
74
  def_delegators :@config, :api_key, :api_key=
75
+ def_delegators :@config, :authenticator, :authenticator=
72
76
  def_delegators :@config, :api_version, :api_version=
73
77
  def_delegators :@config, :stripe_account, :stripe_account=
74
78
  def_delegators :@config, :api_base, :api_base=
@@ -115,6 +119,49 @@ module Stripe
115
119
  version: version,
116
120
  }
117
121
  end
122
+
123
+ class Preview
124
+ def self._get_default_opts(opts)
125
+ { api_mode: :preview }.merge(opts)
126
+ end
127
+
128
+ def self.get(url, opts = {})
129
+ Stripe.raw_request(:get, url, {}, _get_default_opts(opts))
130
+ end
131
+
132
+ def self.post(url, params = {}, opts = {})
133
+ Stripe.raw_request(:post, url, params, _get_default_opts(opts))
134
+ end
135
+
136
+ def self.delete(url, opts = {})
137
+ Stripe.raw_request(:delete, url, {}, _get_default_opts(opts))
138
+ end
139
+ end
140
+
141
+ class RawRequest
142
+ include Stripe::APIOperations::Request
143
+
144
+ def initialize
145
+ @opts = {}
146
+ end
147
+
148
+ def execute(method, url, params = {}, opts = {})
149
+ resp, = execute_resource_request(method, url, params, opts)
150
+
151
+ resp
152
+ end
153
+ end
154
+
155
+ # Sends a request to Stripe REST API
156
+ def self.raw_request(method, url, params = {}, opts = {})
157
+ req = RawRequest.new
158
+ req.execute(method, url, params, opts)
159
+ end
160
+
161
+ def self.deserialize(data)
162
+ data = JSON.parse(data) if data.is_a?(String)
163
+ Util.convert_to_stripe_object(data, {})
164
+ end
118
165
  end
119
166
 
120
167
  Stripe.log_level = ENV["STRIPE_LOG"] unless ENV["STRIPE_LOG"].nil?
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stripe
3
3
  version: !ruby/object:Gem::Version
4
- version: 8.7.0
4
+ version: 9.0.0.pre.beta.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stripe
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-08-11 00:00:00.000000000 Z
11
+ date: 2023-08-24 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Stripe is the easiest way to accept payments online. See https://stripe.com
14
14
  for details.
@@ -50,9 +50,11 @@ files:
50
50
  - lib/stripe/multipart_encoder.rb
51
51
  - lib/stripe/oauth.rb
52
52
  - lib/stripe/object_types.rb
53
+ - lib/stripe/request_signing_authenticator.rb
53
54
  - lib/stripe/resources.rb
54
55
  - lib/stripe/resources/account.rb
55
56
  - lib/stripe/resources/account_link.rb
57
+ - lib/stripe/resources/account_session.rb
56
58
  - lib/stripe/resources/alipay_account.rb
57
59
  - lib/stripe/resources/apple_pay_domain.rb
58
60
  - lib/stripe/resources/application_fee.rb
@@ -64,6 +66,9 @@ files:
64
66
  - lib/stripe/resources/billing_portal/configuration.rb
65
67
  - lib/stripe/resources/billing_portal/session.rb
66
68
  - lib/stripe/resources/capability.rb
69
+ - lib/stripe/resources/capital/financing_offer.rb
70
+ - lib/stripe/resources/capital/financing_summary.rb
71
+ - lib/stripe/resources/capital/financing_transaction.rb
67
72
  - lib/stripe/resources/card.rb
68
73
  - lib/stripe/resources/cash_balance.rb
69
74
  - lib/stripe/resources/charge.rb
@@ -75,6 +80,7 @@ files:
75
80
  - lib/stripe/resources/customer.rb
76
81
  - lib/stripe/resources/customer_balance_transaction.rb
77
82
  - lib/stripe/resources/customer_cash_balance_transaction.rb
83
+ - lib/stripe/resources/customer_session.rb
78
84
  - lib/stripe/resources/discount.rb
79
85
  - lib/stripe/resources/dispute.rb
80
86
  - lib/stripe/resources/ephemeral_key.rb
@@ -85,8 +91,12 @@ files:
85
91
  - lib/stripe/resources/financial_connections/account.rb
86
92
  - lib/stripe/resources/financial_connections/account_owner.rb
87
93
  - lib/stripe/resources/financial_connections/account_ownership.rb
94
+ - lib/stripe/resources/financial_connections/inferred_balance.rb
88
95
  - lib/stripe/resources/financial_connections/session.rb
96
+ - lib/stripe/resources/financial_connections/transaction.rb
89
97
  - lib/stripe/resources/funding_instructions.rb
98
+ - lib/stripe/resources/gift_cards/card.rb
99
+ - lib/stripe/resources/gift_cards/transaction.rb
90
100
  - lib/stripe/resources/identity/verification_report.rb
91
101
  - lib/stripe/resources/identity/verification_session.rb
92
102
  - lib/stripe/resources/invoice.rb
@@ -94,15 +104,19 @@ files:
94
104
  - lib/stripe/resources/invoice_line_item.rb
95
105
  - lib/stripe/resources/issuing/authorization.rb
96
106
  - lib/stripe/resources/issuing/card.rb
107
+ - lib/stripe/resources/issuing/card_bundle.rb
108
+ - lib/stripe/resources/issuing/card_design.rb
97
109
  - lib/stripe/resources/issuing/cardholder.rb
98
110
  - lib/stripe/resources/issuing/dispute.rb
99
111
  - lib/stripe/resources/issuing/transaction.rb
100
112
  - lib/stripe/resources/line_item.rb
101
113
  - lib/stripe/resources/login_link.rb
102
114
  - lib/stripe/resources/mandate.rb
115
+ - lib/stripe/resources/order.rb
103
116
  - lib/stripe/resources/payment_intent.rb
104
117
  - lib/stripe/resources/payment_link.rb
105
118
  - lib/stripe/resources/payment_method.rb
119
+ - lib/stripe/resources/payment_method_configuration.rb
106
120
  - lib/stripe/resources/payout.rb
107
121
  - lib/stripe/resources/person.rb
108
122
  - lib/stripe/resources/plan.rb
@@ -110,6 +124,9 @@ files:
110
124
  - lib/stripe/resources/product.rb
111
125
  - lib/stripe/resources/promotion_code.rb
112
126
  - lib/stripe/resources/quote.rb
127
+ - lib/stripe/resources/quote_phase.rb
128
+ - lib/stripe/resources/quote_preview_invoice.rb
129
+ - lib/stripe/resources/quote_preview_schedule.rb
113
130
  - lib/stripe/resources/radar/early_fraud_warning.rb
114
131
  - lib/stripe/resources/radar/value_list.rb
115
132
  - lib/stripe/resources/radar/value_list_item.rb
@@ -130,6 +147,8 @@ files:
130
147
  - lib/stripe/resources/subscription_schedule.rb
131
148
  - lib/stripe/resources/tax/calculation.rb
132
149
  - lib/stripe/resources/tax/calculation_line_item.rb
150
+ - lib/stripe/resources/tax/form.rb
151
+ - lib/stripe/resources/tax/registration.rb
133
152
  - lib/stripe/resources/tax/settings.rb
134
153
  - lib/stripe/resources/tax/transaction.rb
135
154
  - lib/stripe/resources/tax/transaction_line_item.rb
@@ -189,9 +208,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
189
208
  version: 2.3.0
190
209
  required_rubygems_version: !ruby/object:Gem::Requirement
191
210
  requirements:
192
- - - ">="
211
+ - - ">"
193
212
  - !ruby/object:Gem::Version
194
- version: '0'
213
+ version: 1.3.1
195
214
  requirements: []
196
215
  rubygems_version: 3.3.26
197
216
  signing_key: