stripe 3.2.0 → 5.16.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (230) hide show
  1. checksums.yaml +5 -5
  2. data/.editorconfig +10 -0
  3. data/.gitignore +3 -0
  4. data/.rubocop.yml +80 -0
  5. data/.rubocop_todo.yml +33 -0
  6. data/.travis.yml +9 -7
  7. data/.vscode/extensions.json +7 -0
  8. data/.vscode/settings.json +8 -0
  9. data/CHANGELOG.md +876 -0
  10. data/CODE_OF_CONDUCT.md +77 -0
  11. data/Gemfile +21 -21
  12. data/History.txt +1 -678
  13. data/README.md +195 -53
  14. data/Rakefile +18 -12
  15. data/VERSION +1 -1
  16. data/bin/stripe-console +5 -3
  17. data/lib/stripe/api_operations/create.rb +3 -1
  18. data/lib/stripe/api_operations/delete.rb +26 -2
  19. data/lib/stripe/api_operations/list.rb +3 -12
  20. data/lib/stripe/api_operations/nested_resource.rb +72 -0
  21. data/lib/stripe/api_operations/request.rb +33 -15
  22. data/lib/stripe/api_operations/save.rb +18 -9
  23. data/lib/stripe/api_resource.rb +60 -10
  24. data/lib/stripe/connection_manager.rb +159 -0
  25. data/lib/stripe/error_object.rb +94 -0
  26. data/lib/stripe/errors.rb +39 -19
  27. data/lib/stripe/instrumentation.rb +82 -0
  28. data/lib/stripe/list_object.rb +54 -22
  29. data/lib/stripe/multipart_encoder.rb +131 -0
  30. data/lib/stripe/oauth.rb +22 -14
  31. data/lib/stripe/object_types.rb +96 -0
  32. data/lib/stripe/{account.rb → resources/account.rb} +72 -34
  33. data/lib/stripe/resources/account_link.rb +9 -0
  34. data/lib/stripe/resources/alipay_account.rb +34 -0
  35. data/lib/stripe/{apple_pay_domain.rb → resources/apple_pay_domain.rb} +4 -2
  36. data/lib/stripe/resources/application_fee.rb +13 -0
  37. data/lib/stripe/resources/application_fee_refund.rb +30 -0
  38. data/lib/stripe/{balance.rb → resources/balance.rb} +3 -1
  39. data/lib/stripe/{balance_transaction.rb → resources/balance_transaction.rb} +3 -5
  40. data/lib/stripe/resources/bank_account.rb +42 -0
  41. data/lib/stripe/{bitcoin_receiver.rb → resources/bitcoin_receiver.rb} +6 -5
  42. data/lib/stripe/resources/bitcoin_transaction.rb +15 -0
  43. data/lib/stripe/resources/capability.rb +33 -0
  44. data/lib/stripe/resources/card.rb +37 -0
  45. data/lib/stripe/resources/charge.rb +22 -0
  46. data/lib/stripe/resources/checkout/session.rb +12 -0
  47. data/lib/stripe/{country_spec.rb → resources/country_spec.rb} +3 -5
  48. data/lib/stripe/{coupon.rb → resources/coupon.rb} +4 -2
  49. data/lib/stripe/resources/credit_note.rb +32 -0
  50. data/lib/stripe/resources/credit_note_line_item.rb +7 -0
  51. data/lib/stripe/resources/customer.rb +35 -0
  52. data/lib/stripe/resources/customer_balance_transaction.rb +30 -0
  53. data/lib/stripe/resources/discount.rb +7 -0
  54. data/lib/stripe/resources/dispute.rb +21 -0
  55. data/lib/stripe/resources/ephemeral_key.rb +19 -0
  56. data/lib/stripe/{event.rb → resources/event.rb} +3 -1
  57. data/lib/stripe/resources/exchange_rate.rb +9 -0
  58. data/lib/stripe/resources/file.rb +34 -0
  59. data/lib/stripe/resources/file_link.rb +11 -0
  60. data/lib/stripe/resources/invoice.rb +73 -0
  61. data/lib/stripe/{invoice_item.rb → resources/invoice_item.rb} +4 -2
  62. data/lib/stripe/{invoice_line_item.rb → resources/invoice_line_item.rb} +3 -1
  63. data/lib/stripe/resources/issuing/authorization.rb +33 -0
  64. data/lib/stripe/resources/issuing/card.rb +24 -0
  65. data/lib/stripe/resources/issuing/card_details.rb +9 -0
  66. data/lib/stripe/resources/issuing/cardholder.rb +13 -0
  67. data/lib/stripe/resources/issuing/dispute.rb +13 -0
  68. data/lib/stripe/resources/issuing/transaction.rb +12 -0
  69. data/lib/stripe/resources/login_link.rb +14 -0
  70. data/lib/stripe/resources/mandate.rb +7 -0
  71. data/lib/stripe/resources/order.rb +32 -0
  72. data/lib/stripe/{order_return.rb → resources/order_return.rb} +3 -5
  73. data/lib/stripe/resources/payment_intent.rb +42 -0
  74. data/lib/stripe/resources/payment_method.rb +32 -0
  75. data/lib/stripe/resources/payout.rb +22 -0
  76. data/lib/stripe/resources/person.rb +31 -0
  77. data/lib/stripe/{plan.rb → resources/plan.rb} +3 -1
  78. data/lib/stripe/{product.rb → resources/product.rb} +5 -3
  79. data/lib/stripe/resources/radar/early_fraud_warning.rb +11 -0
  80. data/lib/stripe/resources/radar/value_list.rb +14 -0
  81. data/lib/stripe/resources/radar/value_list_item.rb +13 -0
  82. data/lib/stripe/{recipient.rb → resources/recipient.rb} +5 -6
  83. data/lib/stripe/resources/recipient_transfer.rb +7 -0
  84. data/lib/stripe/{refund.rb → resources/refund.rb} +3 -1
  85. data/lib/stripe/resources/reporting/report_run.rb +12 -0
  86. data/lib/stripe/resources/reporting/report_type.rb +12 -0
  87. data/lib/stripe/resources/reversal.rb +29 -0
  88. data/lib/stripe/resources/review.rb +20 -0
  89. data/lib/stripe/resources/setup_intent.rb +32 -0
  90. data/lib/stripe/resources/sigma/scheduled_query_run.rb +15 -0
  91. data/lib/stripe/{sku.rb → resources/sku.rb} +5 -3
  92. data/lib/stripe/resources/source.rb +46 -0
  93. data/lib/stripe/resources/source_transaction.rb +7 -0
  94. data/lib/stripe/resources/subscription.rb +25 -0
  95. data/lib/stripe/resources/subscription_item.rb +25 -0
  96. data/lib/stripe/resources/subscription_schedule.rb +32 -0
  97. data/lib/stripe/resources/tax_id.rb +26 -0
  98. data/lib/stripe/resources/tax_rate.rb +11 -0
  99. data/lib/stripe/resources/terminal/connection_token.rb +11 -0
  100. data/lib/stripe/resources/terminal/location.rb +14 -0
  101. data/lib/stripe/resources/terminal/reader.rb +14 -0
  102. data/lib/stripe/{three_d_secure.rb → resources/three_d_secure.rb} +3 -1
  103. data/lib/stripe/{token.rb → resources/token.rb} +3 -1
  104. data/lib/stripe/resources/topup.rb +22 -0
  105. data/lib/stripe/resources/transfer.rb +26 -0
  106. data/lib/stripe/resources/usage_record.rb +7 -0
  107. data/lib/stripe/resources/usage_record_summary.rb +7 -0
  108. data/lib/stripe/resources/webhook_endpoint.rb +12 -0
  109. data/lib/stripe/resources.rb +79 -0
  110. data/lib/stripe/singleton_api_resource.rb +10 -4
  111. data/lib/stripe/stripe_client.rb +658 -337
  112. data/lib/stripe/stripe_object.rb +248 -126
  113. data/lib/stripe/stripe_response.rb +55 -21
  114. data/lib/stripe/util.rb +145 -187
  115. data/lib/stripe/version.rb +3 -1
  116. data/lib/stripe/webhook.rb +27 -16
  117. data/lib/stripe.rb +139 -90
  118. data/stripe.gemspec +26 -14
  119. data/test/openapi/README.md +9 -0
  120. data/test/stripe/account_link_test.rb +18 -0
  121. data/test/stripe/account_test.rb +311 -101
  122. data/test/stripe/alipay_account_test.rb +21 -1
  123. data/test/stripe/api_operations_test.rb +57 -8
  124. data/test/stripe/api_resource_test.rb +359 -271
  125. data/test/stripe/apple_pay_domain_test.rb +26 -11
  126. data/test/stripe/application_fee_refund_test.rb +10 -8
  127. data/test/stripe/application_fee_test.rb +49 -3
  128. data/test/stripe/balance_test.rb +4 -2
  129. data/test/stripe/balance_transaction_test.rb +20 -0
  130. data/test/stripe/bank_account_test.rb +11 -11
  131. data/test/stripe/capability_test.rb +45 -0
  132. data/test/stripe/charge_test.rb +25 -18
  133. data/test/stripe/checkout/session_test.rb +41 -0
  134. data/test/stripe/connection_manager_test.rb +163 -0
  135. data/test/stripe/country_spec_test.rb +6 -4
  136. data/test/stripe/coupon_test.rb +29 -10
  137. data/test/stripe/credit_note_test.rb +90 -0
  138. data/test/stripe/customer_balance_transaction_test.rb +37 -0
  139. data/test/stripe/customer_card_test.rb +13 -17
  140. data/test/stripe/customer_test.rb +161 -49
  141. data/test/stripe/dispute_test.rb +19 -8
  142. data/test/stripe/ephemeral_key_test.rb +23 -14
  143. data/test/stripe/errors_test.rb +32 -9
  144. data/test/stripe/exchange_rate_test.rb +20 -0
  145. data/test/stripe/file_link_test.rb +41 -0
  146. data/test/stripe/file_test.rb +87 -0
  147. data/test/stripe/instrumentation_test.rb +74 -0
  148. data/test/stripe/invoice_item_test.rb +31 -18
  149. data/test/stripe/invoice_line_item_test.rb +3 -1
  150. data/test/stripe/invoice_test.rb +158 -39
  151. data/test/stripe/issuing/authorization_test.rb +72 -0
  152. data/test/stripe/issuing/card_test.rb +62 -0
  153. data/test/stripe/issuing/cardholder_test.rb +53 -0
  154. data/test/stripe/issuing/dispute_test.rb +45 -0
  155. data/test/stripe/issuing/transaction_test.rb +48 -0
  156. data/test/stripe/list_object_test.rb +120 -88
  157. data/test/stripe/login_link_test.rb +16 -14
  158. data/test/stripe/mandate_test.rb +14 -0
  159. data/test/stripe/multipart_encoder_test.rb +130 -0
  160. data/test/stripe/oauth_test.rb +69 -50
  161. data/test/stripe/order_return_test.rb +7 -5
  162. data/test/stripe/order_test.rb +39 -14
  163. data/test/stripe/payment_intent_test.rb +107 -0
  164. data/test/stripe/payment_method_test.rb +84 -0
  165. data/test/stripe/payout_test.rb +18 -9
  166. data/test/stripe/person_test.rb +46 -0
  167. data/test/stripe/plan_test.rb +67 -19
  168. data/test/stripe/product_test.rb +28 -14
  169. data/test/stripe/radar/early_fraud_warning_test.rb +22 -0
  170. data/test/stripe/radar/value_list_item_test.rb +48 -0
  171. data/test/stripe/radar/value_list_test.rb +61 -0
  172. data/test/stripe/recipient_test.rb +27 -13
  173. data/test/stripe/refund_test.rb +11 -9
  174. data/test/stripe/reporting/report_run_test.rb +33 -0
  175. data/test/stripe/reporting/report_type_test.rb +22 -0
  176. data/test/stripe/reversal_test.rb +12 -10
  177. data/test/stripe/review_test.rb +27 -0
  178. data/test/stripe/setup_intent_test.rb +84 -0
  179. data/test/stripe/sigma/scheduled_query_run_test.rb +22 -0
  180. data/test/stripe/sku_test.rb +24 -12
  181. data/test/stripe/source_test.rb +70 -19
  182. data/test/stripe/stripe_client_test.rb +867 -326
  183. data/test/stripe/stripe_object_test.rb +284 -182
  184. data/test/stripe/stripe_response_test.rb +73 -24
  185. data/test/stripe/subscription_item_test.rb +47 -15
  186. data/test/stripe/subscription_schedule_test.rb +82 -0
  187. data/test/stripe/subscription_test.rb +41 -19
  188. data/test/stripe/tax_id_test.rb +31 -0
  189. data/test/stripe/tax_rate_test.rb +43 -0
  190. data/test/stripe/terminal/connection_token_test.rb +16 -0
  191. data/test/stripe/terminal/location_test.rb +68 -0
  192. data/test/stripe/terminal/reader_test.rb +62 -0
  193. data/test/stripe/three_d_secure_test.rb +4 -2
  194. data/test/stripe/topup_test.rb +62 -0
  195. data/test/stripe/transfer_test.rb +55 -8
  196. data/test/stripe/usage_record_summary_test.rb +29 -0
  197. data/test/stripe/util_test.rb +173 -84
  198. data/test/stripe/webhook_endpoint_test.rb +59 -0
  199. data/test/stripe/webhook_test.rb +21 -17
  200. data/test/stripe_mock.rb +78 -0
  201. data/test/stripe_test.rb +6 -15
  202. data/test/test_data.rb +28 -26
  203. data/test/test_helper.rb +48 -29
  204. metadata +183 -70
  205. data/lib/stripe/alipay_account.rb +0 -22
  206. data/lib/stripe/application_fee.rb +0 -22
  207. data/lib/stripe/application_fee_refund.rb +0 -20
  208. data/lib/stripe/bank_account.rb +0 -30
  209. data/lib/stripe/bitcoin_transaction.rb +0 -11
  210. data/lib/stripe/card.rb +0 -27
  211. data/lib/stripe/charge.rb +0 -82
  212. data/lib/stripe/customer.rb +0 -79
  213. data/lib/stripe/dispute.rb +0 -17
  214. data/lib/stripe/ephemeral_key.rb +0 -18
  215. data/lib/stripe/file_upload.rb +0 -33
  216. data/lib/stripe/invoice.rb +0 -29
  217. data/lib/stripe/login_link.rb +0 -9
  218. data/lib/stripe/order.rb +0 -29
  219. data/lib/stripe/payout.rb +0 -18
  220. data/lib/stripe/recipient_transfer.rb +0 -6
  221. data/lib/stripe/reversal.rb +0 -20
  222. data/lib/stripe/source.rb +0 -23
  223. data/lib/stripe/subscription.rb +0 -33
  224. data/lib/stripe/subscription_item.rb +0 -14
  225. data/lib/stripe/transfer.rb +0 -18
  226. data/test/api_stub_helpers.rb +0 -0
  227. data/test/stripe/bitcoin_receiver_test.rb +0 -67
  228. data/test/stripe/bitcoin_transaction_test.rb +0 -19
  229. data/test/stripe/file_upload_test.rb +0 -66
  230. data/test/stripe/recipient_card_test.rb +0 -44
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Stripe
2
4
  class ListObject < StripeObject
3
5
  include Enumerable
@@ -5,7 +7,7 @@ module Stripe
5
7
  include Stripe::APIOperations::Request
6
8
  include Stripe::APIOperations::Create
7
9
 
8
- OBJECT_NAME = 'list'
10
+ OBJECT_NAME = "list"
9
11
 
10
12
  # This accessor allows a `ListObject` to inherit various filters that were
11
13
  # given to a predecessor. This allows for things like consistent limits,
@@ -15,8 +17,8 @@ module Stripe
15
17
  # An empty list object. This is returned from +next+ when we know that
16
18
  # there isn't a next page in order to replicate the behavior of the API
17
19
  # when it attempts to return a page beyond the last.
18
- def self.empty_list(opts={})
19
- ListObject.construct_from({ :data => [] }, opts)
20
+ def self.empty_list(opts = {})
21
+ ListObject.construct_from({ data: [] }, opts)
20
22
  end
21
23
 
22
24
  def initialize(*args)
@@ -24,12 +26,16 @@ module Stripe
24
26
  self.filters = {}
25
27
  end
26
28
 
27
- def [](k)
28
- case k
29
+ def [](key)
30
+ case key
29
31
  when String, Symbol
30
32
  super
31
33
  else
32
- raise ArgumentError.new("You tried to access the #{k.inspect} index, but ListObject types only support String keys. (HINT: List calls return an object with a 'data' (which is the data array). You likely want to call #data[#{k.inspect}])")
34
+ raise ArgumentError,
35
+ "You tried to access the #{key.inspect} index, but ListObject " \
36
+ "types only support String keys. (HINT: List calls return an " \
37
+ "object with a 'data' (which is the data array). You likely " \
38
+ "want to call #data[#{key.inspect}])"
33
39
  end
34
40
  end
35
41
 
@@ -39,12 +45,22 @@ module Stripe
39
45
  # Note that this method makes no effort to fetch a new page when it gets to
40
46
  # the end of the current page's resources. See also +auto_paging_each+.
41
47
  def each(&blk)
42
- self.data.each(&blk)
48
+ data.each(&blk)
43
49
  end
44
50
 
45
51
  # Iterates through each resource in all pages, making additional fetches to
46
52
  # the API as necessary.
47
53
  #
54
+ # The default iteration direction is forwards according to Stripe's API
55
+ # "natural" ordering direction -- newer objects first, and moving towards
56
+ # older objects.
57
+ #
58
+ # However, if the initial list object was fetched using an `ending_before`
59
+ # cursor (and only `ending_before`, `starting_after` cannot also be
60
+ # included), the method assumes that the user is trying to iterate
61
+ # backwards compared to natural ordering and returns results that way --
62
+ # older objects first, and moving towards newer objects.
63
+ #
48
64
  # Note that this method will make as many API calls as necessary to fetch
49
65
  # all resources. For more granular control, please see +each+ and
50
66
  # +next_page+.
@@ -53,20 +69,31 @@ module Stripe
53
69
 
54
70
  page = self
55
71
  loop do
56
- page.each(&blk)
57
- page = page.next_page
72
+ # Backward iterating activates if we have an `ending_before` constraint
73
+ # and _just_ an `ending_before` constraint. If `starting_after` was
74
+ # also used, we iterate forwards normally.
75
+ if filters.include?(:ending_before) &&
76
+ !filters.include?(:starting_after)
77
+ page.reverse_each(&blk)
78
+ page = page.previous_page
79
+ else
80
+ page.each(&blk)
81
+ page = page.next_page
82
+ end
83
+
58
84
  break if page.empty?
59
85
  end
60
86
  end
61
87
 
62
88
  # Returns true if the page object contains no elements.
63
89
  def empty?
64
- self.data.empty?
90
+ data.empty?
65
91
  end
66
92
 
67
- def retrieve(id, opts={})
93
+ def retrieve(id, opts = {})
68
94
  id, retrieve_params = Util.normalize_id(id)
69
- resp, opts = request(:get,"#{resource_url}/#{CGI.escape(id)}", retrieve_params, opts)
95
+ resp, opts = request(:get, "#{resource_url}/#{CGI.escape(id)}",
96
+ retrieve_params, opts)
70
97
  Util.convert_to_stripe_object(resp.data, opts)
71
98
  end
72
99
 
@@ -74,13 +101,12 @@ module Stripe
74
101
  #
75
102
  # This method will try to respect the limit of the current page. If none
76
103
  # was given, the default limit will be fetched again.
77
- def next_page(params={}, opts={})
78
- return self.class.empty_list(opts) if !has_more
104
+ def next_page(params = {}, opts = {})
105
+ return self.class.empty_list(opts) unless has_more
106
+
79
107
  last_id = data.last.id
80
108
 
81
- params = filters.merge({
82
- :starting_after => last_id,
83
- }).merge(params)
109
+ params = filters.merge(starting_after: last_id).merge(params)
84
110
 
85
111
  list(params, opts)
86
112
  end
@@ -89,19 +115,25 @@ module Stripe
89
115
  #
90
116
  # This method will try to respect the limit of the current page. If none
91
117
  # was given, the default limit will be fetched again.
92
- def previous_page(params={}, opts={})
118
+ def previous_page(params = {}, opts = {})
119
+ return self.class.empty_list(opts) unless has_more
120
+
93
121
  first_id = data.first.id
94
122
 
95
- params = filters.merge({
96
- :ending_before => first_id,
97
- }).merge(params)
123
+ params = filters.merge(ending_before: first_id).merge(params)
98
124
 
99
125
  list(params, opts)
100
126
  end
101
127
 
102
128
  def resource_url
103
- self.url ||
129
+ url ||
104
130
  raise(ArgumentError, "List object does not contain a 'url' field.")
105
131
  end
132
+
133
+ # Iterates through each resource in the page represented by the current
134
+ # `ListObject` in reverse.
135
+ def reverse_each(&blk)
136
+ data.reverse_each(&blk)
137
+ end
106
138
  end
107
139
  end
@@ -0,0 +1,131 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "securerandom"
4
+ require "tempfile"
5
+
6
+ module Stripe
7
+ # Encodes parameters into a `multipart/form-data` payload as described by RFC
8
+ # 2388:
9
+ #
10
+ # https://tools.ietf.org/html/rfc2388
11
+ #
12
+ # This is most useful for transferring file-like objects.
13
+ #
14
+ # Parameters should be added with `#encode`. When ready, use `#body` to get
15
+ # the encoded result and `#content_type` to get the value that should be
16
+ # placed in the `Content-Type` header of a subsequent request (which includes
17
+ # a boundary value).
18
+ class MultipartEncoder
19
+ MULTIPART_FORM_DATA = "multipart/form-data"
20
+
21
+ # A shortcut for encoding a single set of parameters and finalizing a
22
+ # result.
23
+ #
24
+ # Returns an encoded body and the value that should be set in the content
25
+ # type header of a subsequent request.
26
+ def self.encode(params)
27
+ encoder = MultipartEncoder.new
28
+ encoder.encode(params)
29
+ encoder.close
30
+ [encoder.body, encoder.content_type]
31
+ end
32
+
33
+ # Gets the object's randomly generated boundary string.
34
+ attr_reader :boundary
35
+
36
+ # Initializes a new multipart encoder.
37
+ def initialize
38
+ # Kind of weird, but required by Rubocop because the unary plus operator
39
+ # is considered faster than `Stripe.new`.
40
+ @body = +""
41
+
42
+ # Chose the same number of random bytes that Go uses in its standard
43
+ # library implementation. Easily enough entropy to ensure that it won't
44
+ # be present in a file we're sending.
45
+ @boundary = SecureRandom.hex(30)
46
+
47
+ @closed = false
48
+ @first_field = true
49
+ end
50
+
51
+ # Gets the encoded body. `#close` must be called first.
52
+ def body
53
+ raise "object must be closed before getting body" unless @closed
54
+
55
+ @body
56
+ end
57
+
58
+ # Finalizes the object by writing the final boundary.
59
+ def close
60
+ raise "object already closed" if @closed
61
+
62
+ @body << "\r\n"
63
+ @body << "--#{@boundary}--"
64
+
65
+ @closed = true
66
+
67
+ nil
68
+ end
69
+
70
+ # Gets the value including boundary that should be put into a multipart
71
+ # request's `Content-Type`.
72
+ def content_type
73
+ "#{MULTIPART_FORM_DATA}; boundary=#{@boundary}"
74
+ end
75
+
76
+ # Encodes a set of parameters to the body.
77
+ #
78
+ # Note that parameters are expected to be a hash, but a "flat" hash such
79
+ # that complex substructures like hashes and arrays have already been
80
+ # appropriately Stripe-encoded. Pass a complex structure through
81
+ # `Util.flatten_params` first before handing it off to this method.
82
+ def encode(params)
83
+ raise "no more parameters can be written to closed object" if @closed
84
+
85
+ params.each do |name, val|
86
+ if val.is_a?(::File) || val.is_a?(::Tempfile)
87
+ write_field(name, val.read, filename: ::File.basename(val.path))
88
+ elsif val.respond_to?(:read)
89
+ write_field(name, val.read, filename: "blob")
90
+ else
91
+ write_field(name, val, filename: nil)
92
+ end
93
+ end
94
+
95
+ nil
96
+ end
97
+
98
+ #
99
+ # private
100
+ #
101
+
102
+ # Escapes double quotes so that the given value can be used in a
103
+ # double-quoted string and replaces any linebreak characters with spaces.
104
+ private def escape(str)
105
+ str.gsub('"', "%22").tr("\n", " ").tr("\r", " ")
106
+ end
107
+
108
+ private def write_field(name, data, filename:)
109
+ if !@first_field
110
+ @body << "\r\n"
111
+ else
112
+ @first_field = false
113
+ end
114
+
115
+ @body << "--#{@boundary}\r\n"
116
+
117
+ if filename
118
+ @body << %(Content-Disposition: form-data) +
119
+ %(; name="#{escape(name.to_s)}") +
120
+ %(; filename="#{escape(filename)}"\r\n)
121
+ @body << %(Content-Type: application/octet-stream\r\n)
122
+ else
123
+ @body << %(Content-Disposition: form-data) +
124
+ %(; name="#{escape(name.to_s)}"\r\n)
125
+ end
126
+
127
+ @body << "\r\n"
128
+ @body << data.to_s
129
+ end
130
+ end
131
+ end
data/lib/stripe/oauth.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Stripe
2
4
  module OAuth
3
5
  module OAuthOperations
@@ -12,43 +14,49 @@ module Stripe
12
14
  end
13
15
  end
14
16
 
15
- def self.get_client_id(params={})
17
+ def self.get_client_id(params = {})
16
18
  client_id = params[:client_id] || Stripe.client_id
17
19
  unless client_id
18
- raise AuthenticationError.new('No client_id provided. ' \
20
+ raise AuthenticationError, "No client_id provided. " \
19
21
  'Set your client_id using "Stripe.client_id = <CLIENT-ID>". ' \
20
- 'You can find your client_ids in your Stripe dashboard at ' \
21
- 'https://dashboard.stripe.com/account/applications/settings, ' \
22
- 'after registering your account as a platform. See ' \
23
- 'https://stripe.com/docs/connect/standalone-accounts for details, ' \
24
- 'or email support@stripe.com if you have any questions.')
22
+ "You can find your client_ids in your Stripe dashboard at " \
23
+ "https://dashboard.stripe.com/account/applications/settings, " \
24
+ "after registering your account as a platform. See " \
25
+ "https://stripe.com/docs/connect/standalone-accounts for details, " \
26
+ "or email support@stripe.com if you have any questions."
25
27
  end
26
28
  client_id
27
29
  end
28
30
 
29
- def self.authorize_url(params={}, opts={})
31
+ def self.authorize_url(params = {}, opts = {})
30
32
  base = opts[:connect_base] || Stripe.connect_base
31
33
 
34
+ path = "/oauth/authorize"
35
+ path = "/express" + path if opts[:express]
36
+
32
37
  params[:client_id] = get_client_id(params)
33
- params[:response_type] ||= 'code'
38
+ params[:response_type] ||= "code"
34
39
  query = Util.encode_parameters(params)
35
40
 
36
- "#{base}/oauth/authorize?#{query}"
41
+ "#{base}#{path}?#{query}"
37
42
  end
38
43
 
39
- def self.token(params={}, opts={})
44
+ def self.token(params = {}, opts = {})
40
45
  opts = Util.normalize_opts(opts)
46
+ opts[:api_key] = params[:client_secret] if params[:client_secret]
41
47
  resp, opts = OAuthOperations.request(
42
- :post, '/oauth/token', params, opts)
48
+ :post, "/oauth/token", params, opts
49
+ )
43
50
  # This is just going to return a generic StripeObject, but that's okay
44
51
  Util.convert_to_stripe_object(resp.data, opts)
45
52
  end
46
53
 
47
- def self.deauthorize(params={}, opts={})
54
+ def self.deauthorize(params = {}, opts = {})
48
55
  opts = Util.normalize_opts(opts)
49
56
  params[:client_id] = get_client_id(params)
50
57
  resp, opts = OAuthOperations.request(
51
- :post, '/oauth/deauthorize', params, opts)
58
+ :post, "/oauth/deauthorize", params, opts
59
+ )
52
60
  # This is just going to return a generic StripeObject, but that's okay
53
61
  Util.convert_to_stripe_object(resp.data, opts)
54
62
  end
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ # rubocop:disable Metrics/MethodLength
4
+
5
+ module Stripe
6
+ module ObjectTypes
7
+ def self.object_names_to_classes
8
+ {
9
+ # data structures
10
+ ListObject::OBJECT_NAME => ListObject,
11
+
12
+ # business objects
13
+ Account::OBJECT_NAME => Account,
14
+ AccountLink::OBJECT_NAME => AccountLink,
15
+ AlipayAccount::OBJECT_NAME => AlipayAccount,
16
+ ApplePayDomain::OBJECT_NAME => ApplePayDomain,
17
+ ApplicationFee::OBJECT_NAME => ApplicationFee,
18
+ ApplicationFeeRefund::OBJECT_NAME => ApplicationFeeRefund,
19
+ Balance::OBJECT_NAME => Balance,
20
+ BalanceTransaction::OBJECT_NAME => BalanceTransaction,
21
+ BankAccount::OBJECT_NAME => BankAccount,
22
+ BitcoinReceiver::OBJECT_NAME => BitcoinReceiver,
23
+ BitcoinTransaction::OBJECT_NAME => BitcoinTransaction,
24
+ Capability::OBJECT_NAME => Capability,
25
+ Card::OBJECT_NAME => Card,
26
+ Charge::OBJECT_NAME => Charge,
27
+ Checkout::Session::OBJECT_NAME => Checkout::Session,
28
+ CountrySpec::OBJECT_NAME => CountrySpec,
29
+ Coupon::OBJECT_NAME => Coupon,
30
+ CreditNote::OBJECT_NAME => CreditNote,
31
+ CreditNoteLineItem::OBJECT_NAME => CreditNoteLineItem,
32
+ Customer::OBJECT_NAME => Customer,
33
+ CustomerBalanceTransaction::OBJECT_NAME => CustomerBalanceTransaction,
34
+ Discount::OBJECT_NAME => Discount,
35
+ Dispute::OBJECT_NAME => Dispute,
36
+ EphemeralKey::OBJECT_NAME => EphemeralKey,
37
+ Event::OBJECT_NAME => Event,
38
+ ExchangeRate::OBJECT_NAME => ExchangeRate,
39
+ File::OBJECT_NAME => File,
40
+ File::OBJECT_NAME_ALT => File,
41
+ FileLink::OBJECT_NAME => FileLink,
42
+ Invoice::OBJECT_NAME => Invoice,
43
+ InvoiceItem::OBJECT_NAME => InvoiceItem,
44
+ InvoiceLineItem::OBJECT_NAME => InvoiceLineItem,
45
+ Issuing::Authorization::OBJECT_NAME => Issuing::Authorization,
46
+ Issuing::Card::OBJECT_NAME => Issuing::Card,
47
+ Issuing::CardDetails::OBJECT_NAME => Issuing::CardDetails,
48
+ Issuing::Cardholder::OBJECT_NAME => Issuing::Cardholder,
49
+ Issuing::Dispute::OBJECT_NAME => Issuing::Dispute,
50
+ Issuing::Transaction::OBJECT_NAME => Issuing::Transaction,
51
+ LoginLink::OBJECT_NAME => LoginLink,
52
+ Mandate::OBJECT_NAME => Mandate,
53
+ Order::OBJECT_NAME => Order,
54
+ OrderReturn::OBJECT_NAME => OrderReturn,
55
+ PaymentIntent::OBJECT_NAME => PaymentIntent,
56
+ PaymentMethod::OBJECT_NAME => PaymentMethod,
57
+ Payout::OBJECT_NAME => Payout,
58
+ Person::OBJECT_NAME => Person,
59
+ Plan::OBJECT_NAME => Plan,
60
+ Product::OBJECT_NAME => Product,
61
+ Radar::EarlyFraudWarning::OBJECT_NAME => Radar::EarlyFraudWarning,
62
+ Radar::ValueList::OBJECT_NAME => Radar::ValueList,
63
+ Radar::ValueListItem::OBJECT_NAME => Radar::ValueListItem,
64
+ Recipient::OBJECT_NAME => Recipient,
65
+ RecipientTransfer::OBJECT_NAME => RecipientTransfer,
66
+ Refund::OBJECT_NAME => Refund,
67
+ Reporting::ReportRun::OBJECT_NAME => Reporting::ReportRun,
68
+ Reporting::ReportType::OBJECT_NAME => Reporting::ReportType,
69
+ Reversal::OBJECT_NAME => Reversal,
70
+ Review::OBJECT_NAME => Review,
71
+ SKU::OBJECT_NAME => SKU,
72
+ SetupIntent::OBJECT_NAME => SetupIntent,
73
+ Sigma::ScheduledQueryRun::OBJECT_NAME => Sigma::ScheduledQueryRun,
74
+ Source::OBJECT_NAME => Source,
75
+ SourceTransaction::OBJECT_NAME => SourceTransaction,
76
+ Subscription::OBJECT_NAME => Subscription,
77
+ SubscriptionItem::OBJECT_NAME => SubscriptionItem,
78
+ SubscriptionSchedule::OBJECT_NAME => SubscriptionSchedule,
79
+ TaxId::OBJECT_NAME => TaxId,
80
+ TaxRate::OBJECT_NAME => TaxRate,
81
+ Terminal::ConnectionToken::OBJECT_NAME => Terminal::ConnectionToken,
82
+ Terminal::Location::OBJECT_NAME => Terminal::Location,
83
+ Terminal::Reader::OBJECT_NAME => Terminal::Reader,
84
+ ThreeDSecure::OBJECT_NAME => ThreeDSecure,
85
+ Token::OBJECT_NAME => Token,
86
+ Topup::OBJECT_NAME => Topup,
87
+ Transfer::OBJECT_NAME => Transfer,
88
+ UsageRecord::OBJECT_NAME => UsageRecord,
89
+ UsageRecordSummary::OBJECT_NAME => UsageRecordSummary,
90
+ WebhookEndpoint::OBJECT_NAME => WebhookEndpoint,
91
+ }
92
+ end
93
+ end
94
+ end
95
+
96
+ # rubocop:enable Metrics/MethodLength
@@ -1,21 +1,42 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Stripe
2
4
  class Account < APIResource
3
5
  extend Gem::Deprecate
4
6
  extend Stripe::APIOperations::Create
5
- extend Stripe::APIOperations::List
6
7
  include Stripe::APIOperations::Delete
8
+ extend Stripe::APIOperations::List
7
9
  include Stripe::APIOperations::Save
10
+ extend Stripe::APIOperations::NestedResource
11
+
12
+ OBJECT_NAME = "account"
8
13
 
9
- OBJECT_NAME = 'account'
14
+ custom_method :reject, http_verb: :post
15
+
16
+ nested_resource_class_methods :capability,
17
+ operations: %i[retrieve update list],
18
+ resource_plural: "capabilities"
19
+ nested_resource_class_methods :person,
20
+ operations: %i[create retrieve update delete list]
21
+
22
+ def reject(params = {}, opts = {})
23
+ request_stripe_object(
24
+ method: :post,
25
+ path: resource_url + "/reject",
26
+ params: params,
27
+ opts: opts
28
+ )
29
+ end
10
30
 
11
31
  save_nested_resource :external_account
12
32
 
13
- # This method is deprecated. Please use `#external_account=` instead.
14
- save_nested_resource :bank_account
15
- deprecate :bank_account=, "#external_account=", 2017, 8
33
+ nested_resource_class_methods :external_account,
34
+ operations: %i[create retrieve update delete list]
35
+
36
+ nested_resource_class_methods :login_link, operations: %i[create]
16
37
 
17
38
  def resource_url
18
- if self['id']
39
+ if self["id"]
19
40
  super
20
41
  else
21
42
  "/v1/account"
@@ -23,26 +44,34 @@ module Stripe
23
44
  end
24
45
 
25
46
  # @override To make id optional
26
- def self.retrieve(id=ARGUMENT_NOT_PROVIDED, opts={})
27
- id = id.equal?(ARGUMENT_NOT_PROVIDED) ? nil : Util.check_string_argument!(id)
47
+ def self.retrieve(id = ARGUMENT_NOT_PROVIDED, opts = {})
48
+ id = if id.equal?(ARGUMENT_NOT_PROVIDED)
49
+ nil
50
+ else
51
+ Util.check_string_argument!(id)
52
+ end
28
53
 
29
54
  # Account used to be a singleton, where this method's signature was
30
55
  # `(opts={})`. For the sake of not breaking folks who pass in an OAuth
31
56
  # key in opts, let's lurkily string match for it.
32
- if opts == {} && id.is_a?(String) && id.start_with?('sk_')
33
- # `super` properly assumes a String opts is the apiKey and normalizes as expected.
57
+ if opts == {} && id.is_a?(String) && id.start_with?("sk_")
58
+ # `super` properly assumes a String opts is the apiKey and normalizes
59
+ # as expected.
34
60
  opts = id
35
61
  id = nil
36
62
  end
37
63
  super(id, opts)
38
64
  end
39
65
 
40
- def reject(params={}, opts={})
41
- opts = Util.normalize_opts(opts)
42
- resp, opts = request(:post, resource_url + '/reject', params, opts)
43
- initialize_from(resp.data, opts)
66
+ def persons(params = {}, opts = {})
67
+ resp, opts = request(:get, resource_url + "/persons", params, opts)
68
+ Util.convert_to_stripe_object(resp.data, opts)
44
69
  end
45
70
 
71
+ # We are not adding a helper for capabilities here as the Account object
72
+ # already has a capabilities property which is a hash and not the sub-list
73
+ # of capabilities.
74
+
46
75
  # Somewhat unfortunately, we attempt to do a special encoding trick when
47
76
  # serializing `additional_owners` under an account: when updating a value,
48
77
  # we actually send the update parameters up as an integer-indexed hash
@@ -67,17 +96,22 @@ module Stripe
67
96
  # We're trying to get this overturned on the server side, but for now,
68
97
  # patch in a special allowance.
69
98
  def serialize_params(options = {})
70
- serialize_params_account(self, super)
99
+ serialize_params_account(self, super, options)
71
100
  end
72
101
 
73
- def serialize_params_account(obj, update_hash)
74
- if entity = @values[:legal_entity]
75
- if owners = entity[:additional_owners]
102
+ def serialize_params_account(_obj, update_hash, options = {})
103
+ if (entity = @values[:legal_entity])
104
+ if (owners = entity[:additional_owners])
76
105
  entity_update = update_hash[:legal_entity] ||= {}
77
106
  entity_update[:additional_owners] =
78
107
  serialize_additional_owners(entity, owners)
79
108
  end
80
109
  end
110
+ if (individual = @values[:individual])
111
+ if individual.is_a?(Person) && !update_hash.key?(:individual)
112
+ update_hash[:individual] = individual.serialize_params(options)
113
+ end
114
+ end
81
115
  update_hash
82
116
  end
83
117
 
@@ -86,34 +120,37 @@ module Stripe
86
120
  end
87
121
 
88
122
  def legal_entity
89
- self['legal_entity']
123
+ self["legal_entity"]
90
124
  end
91
125
 
92
- def legal_entity=(_)
93
- raise NoMethodError.new('Overridding legal_entity can cause serious issues. Instead, set the individual fields of legal_entity like blah.legal_entity.first_name = \'Blah\'')
126
+ def legal_entity=(_legal_entity)
127
+ raise NoMethodError,
128
+ "Overriding legal_entity can cause serious issues. Instead, set " \
129
+ "the individual fields of legal_entity like " \
130
+ "`account.legal_entity.first_name = 'Blah'`"
94
131
  end
95
132
 
96
- def deauthorize(client_id=nil, opts={})
133
+ def deauthorize(client_id = nil, opts = {})
97
134
  params = {
98
135
  client_id: client_id,
99
- stripe_user_id: self.id,
136
+ stripe_user_id: id,
100
137
  }
101
138
  OAuth.deauthorize(params, opts)
102
139
  end
103
140
 
104
141
  ARGUMENT_NOT_PROVIDED = Object.new
105
142
 
106
- private
107
-
108
- def serialize_additional_owners(legal_entity, additional_owners)
109
- original_value = legal_entity.instance_variable_get(:@original_values)[:additional_owners]
143
+ private def serialize_additional_owners(legal_entity, additional_owners)
144
+ original_value =
145
+ legal_entity
146
+ .instance_variable_get(:@original_values)[:additional_owners]
110
147
  if original_value && original_value.length > additional_owners.length
111
148
  # url params provide no mechanism for deleting an item in an array,
112
149
  # just overwriting the whole array or adding new items. So let's not
113
150
  # allow deleting without a full overwrite until we have a solution.
114
- raise ArgumentError.new(
115
- "You cannot delete an item from an array, you must instead set a new array"
116
- )
151
+ raise ArgumentError,
152
+ "You cannot delete an item from an array, you must instead " \
153
+ "set a new array"
117
154
  end
118
155
 
119
156
  update_hash = {}
@@ -125,10 +162,11 @@ module Stripe
125
162
  # StripeObject.
126
163
  update = v.is_a?(StripeObject) ? v.serialize_params : v
127
164
 
128
- if update != {} && (!original_value ||
129
- update != legal_entity.serialize_params_value(original_value[i], nil, false, true))
130
- update_hash[i.to_s] = update
131
- end
165
+ next unless update != {} && (!original_value ||
166
+ update != legal_entity.serialize_params_value(original_value[i], nil,
167
+ false, true))
168
+
169
+ update_hash[i.to_s] = update
132
170
  end
133
171
  update_hash
134
172
  end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Stripe
4
+ class AccountLink < APIResource
5
+ extend Stripe::APIOperations::Create
6
+
7
+ OBJECT_NAME = "account_link"
8
+ end
9
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Stripe
4
+ class AlipayAccount < APIResource
5
+ include Stripe::APIOperations::Save
6
+ include Stripe::APIOperations::Delete
7
+
8
+ OBJECT_NAME = "alipay_account"
9
+
10
+ def resource_url
11
+ if !respond_to?(:customer) || customer.nil?
12
+ raise NotImplementedError,
13
+ "Alipay accounts cannot be accessed without a customer ID."
14
+ end
15
+
16
+ "#{Customer.resource_url}/#{CGI.escape(customer)}/sources" \
17
+ "/#{CGI.escape(id)}"
18
+ end
19
+
20
+ def self.update(_id, _params = nil, _opts = nil)
21
+ raise NotImplementedError,
22
+ "Alipay accounts cannot be updated without a customer ID. " \
23
+ "Update an Alipay account using `Customer.update_source(" \
24
+ "'customer_id', 'alipay_account_id', update_params)`"
25
+ end
26
+
27
+ def self.retrieve(_id, _opts = nil)
28
+ raise NotImplementedError,
29
+ "Alipay accounts cannot be retrieved without a customer ID. " \
30
+ "Retrieve an Alipay account using `Customer.retrieve_source(" \
31
+ "'customer_id', 'alipay_account_id')`"
32
+ end
33
+ end
34
+ end