stripe 5.28.0 → 8.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (243) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +286 -0
  3. data/Gemfile +5 -3
  4. data/Makefile +13 -0
  5. data/OPENAPI_VERSION +1 -0
  6. data/README.md +43 -5
  7. data/VERSION +1 -1
  8. data/lib/data/ca-certificates.crt +1241 -1937
  9. data/lib/stripe/api_operations/create.rb +6 -2
  10. data/lib/stripe/api_operations/delete.rb +12 -7
  11. data/lib/stripe/api_operations/list.rb +6 -9
  12. data/lib/stripe/api_operations/nested_resource.rb +62 -34
  13. data/lib/stripe/api_operations/request.rb +45 -2
  14. data/lib/stripe/api_operations/save.rb +13 -3
  15. data/lib/stripe/api_operations/search.rb +16 -0
  16. data/lib/stripe/api_resource.rb +11 -18
  17. data/lib/stripe/api_resource_test_helpers.rb +49 -0
  18. data/lib/stripe/api_version.rb +8 -0
  19. data/lib/stripe/connection_manager.rb +49 -11
  20. data/lib/stripe/error_object.rb +2 -3
  21. data/lib/stripe/instrumentation.rb +46 -6
  22. data/lib/stripe/oauth.rb +4 -3
  23. data/lib/stripe/object_types.rb +36 -10
  24. data/lib/stripe/resources/account.rb +39 -17
  25. data/lib/stripe/resources/account_link.rb +4 -0
  26. data/lib/stripe/resources/application_fee_refund.rb +7 -1
  27. data/lib/stripe/resources/apps/secret.rb +40 -0
  28. data/lib/stripe/resources/balance.rb +11 -0
  29. data/lib/stripe/resources/balance_transaction.rb +4 -0
  30. data/lib/stripe/resources/bank_account.rb +7 -0
  31. data/lib/stripe/resources/billing_portal/configuration.rb +15 -0
  32. data/lib/stripe/resources/billing_portal/session.rb +14 -0
  33. data/lib/stripe/resources/capability.rb +5 -1
  34. data/lib/stripe/resources/card.rb +6 -3
  35. data/lib/stripe/resources/cash_balance.rb +23 -0
  36. data/lib/stripe/resources/charge.rb +23 -3
  37. data/lib/stripe/resources/checkout/session.rb +49 -2
  38. data/lib/stripe/resources/country_spec.rb +6 -0
  39. data/lib/stripe/resources/coupon.rb +3 -0
  40. data/lib/stripe/resources/credit_note.rb +27 -9
  41. data/lib/stripe/resources/credit_note_line_item.rb +2 -0
  42. data/lib/stripe/resources/customer.rb +132 -4
  43. data/lib/stripe/resources/customer_balance_transaction.rb +9 -2
  44. data/lib/stripe/resources/customer_cash_balance_transaction.rb +14 -0
  45. data/lib/stripe/resources/discount.rb +5 -0
  46. data/lib/stripe/resources/dispute.rb +17 -3
  47. data/lib/stripe/resources/event.rb +29 -0
  48. data/lib/stripe/resources/exchange_rate.rb +11 -0
  49. data/lib/stripe/resources/file.rb +9 -1
  50. data/lib/stripe/resources/file_link.rb +3 -0
  51. data/lib/stripe/resources/financial_connections/account.rb +67 -0
  52. data/lib/stripe/resources/financial_connections/account_owner.rb +11 -0
  53. data/lib/stripe/resources/financial_connections/account_ownership.rb +11 -0
  54. data/lib/stripe/resources/financial_connections/session.rb +13 -0
  55. data/lib/stripe/resources/funding_instructions.rb +21 -0
  56. data/lib/stripe/resources/identity/verification_report.rb +23 -0
  57. data/lib/stripe/resources/identity/verification_session.rb +61 -0
  58. data/lib/stripe/resources/invoice.rb +105 -17
  59. data/lib/stripe/resources/invoice_item.rb +11 -0
  60. data/lib/stripe/resources/invoice_line_item.rb +1 -0
  61. data/lib/stripe/resources/issuing/authorization.rb +25 -5
  62. data/lib/stripe/resources/issuing/card.rb +78 -8
  63. data/lib/stripe/resources/issuing/cardholder.rb +3 -0
  64. data/lib/stripe/resources/issuing/dispute.rb +13 -3
  65. data/lib/stripe/resources/issuing/transaction.rb +5 -0
  66. data/lib/stripe/resources/line_item.rb +2 -0
  67. data/lib/stripe/resources/login_link.rb +2 -0
  68. data/lib/stripe/resources/mandate.rb +1 -0
  69. data/lib/stripe/resources/payment_intent.rb +103 -6
  70. data/lib/stripe/resources/payment_link.rb +35 -0
  71. data/lib/stripe/resources/payment_method.rb +25 -5
  72. data/lib/stripe/resources/payout.rb +28 -5
  73. data/lib/stripe/resources/person.rb +7 -0
  74. data/lib/stripe/resources/plan.rb +8 -0
  75. data/lib/stripe/resources/price.rb +15 -0
  76. data/lib/stripe/resources/product.rb +17 -0
  77. data/lib/stripe/resources/promotion_code.rb +2 -0
  78. data/lib/stripe/resources/quote.rb +128 -0
  79. data/lib/stripe/resources/radar/early_fraud_warning.rb +4 -0
  80. data/lib/stripe/resources/radar/value_list.rb +3 -0
  81. data/lib/stripe/resources/radar/value_list_item.rb +3 -0
  82. data/lib/stripe/resources/refund.rb +49 -0
  83. data/lib/stripe/resources/reporting/report_run.rb +8 -0
  84. data/lib/stripe/resources/reporting/report_type.rb +8 -1
  85. data/lib/stripe/resources/reversal.rb +16 -2
  86. data/lib/stripe/resources/review.rb +14 -3
  87. data/lib/stripe/resources/setup_attempt.rb +4 -0
  88. data/lib/stripe/resources/setup_intent.rb +60 -5
  89. data/lib/stripe/resources/shipping_rate.rb +14 -0
  90. data/lib/stripe/resources/sigma/scheduled_query_run.rb +4 -0
  91. data/lib/stripe/resources/source.rb +26 -6
  92. data/lib/stripe/resources/source_transaction.rb +5 -0
  93. data/lib/stripe/resources/subscription.rb +57 -3
  94. data/lib/stripe/resources/subscription_item.rb +2 -7
  95. data/lib/stripe/resources/subscription_schedule.rb +23 -5
  96. data/lib/stripe/resources/tax/calculation.rb +33 -0
  97. data/lib/stripe/resources/tax/calculation_line_item.rb +10 -0
  98. data/lib/stripe/resources/tax/settings.rb +15 -0
  99. data/lib/stripe/resources/tax/transaction.rb +49 -0
  100. data/lib/stripe/resources/tax/transaction_line_item.rb +10 -0
  101. data/lib/stripe/resources/tax_code.rb +11 -0
  102. data/lib/stripe/resources/tax_id.rb +5 -0
  103. data/lib/stripe/resources/tax_rate.rb +3 -0
  104. data/lib/stripe/resources/terminal/configuration.rb +16 -0
  105. data/lib/stripe/resources/terminal/connection_token.rb +3 -0
  106. data/lib/stripe/resources/terminal/location.rb +3 -0
  107. data/lib/stripe/resources/terminal/reader.rb +119 -0
  108. data/lib/stripe/resources/test_helpers/test_clock.rb +35 -0
  109. data/lib/stripe/resources/token.rb +20 -0
  110. data/lib/stripe/resources/topup.rb +15 -3
  111. data/lib/stripe/resources/transfer.rb +10 -11
  112. data/lib/stripe/resources/treasury/credit_reversal.rb +14 -0
  113. data/lib/stripe/resources/treasury/debit_reversal.rb +14 -0
  114. data/lib/stripe/resources/treasury/financial_account.rb +52 -0
  115. data/lib/stripe/resources/treasury/financial_account_features.rb +12 -0
  116. data/lib/stripe/resources/treasury/inbound_transfer.rb +94 -0
  117. data/lib/stripe/resources/treasury/outbound_payment.rb +96 -0
  118. data/lib/stripe/resources/treasury/outbound_transfer.rb +100 -0
  119. data/lib/stripe/resources/treasury/received_credit.rb +30 -0
  120. data/lib/stripe/resources/treasury/received_debit.rb +30 -0
  121. data/lib/stripe/resources/treasury/transaction.rb +13 -0
  122. data/lib/stripe/resources/treasury/transaction_entry.rb +17 -0
  123. data/lib/stripe/resources/usage_record.rb +5 -0
  124. data/lib/stripe/resources/usage_record_summary.rb +1 -0
  125. data/lib/stripe/resources/webhook_endpoint.rb +7 -0
  126. data/lib/stripe/resources.rb +34 -10
  127. data/lib/stripe/search_result_object.rb +86 -0
  128. data/lib/stripe/stripe_client.rb +267 -121
  129. data/lib/stripe/stripe_configuration.rb +29 -10
  130. data/lib/stripe/stripe_object.rb +25 -2
  131. data/lib/stripe/stripe_response.rb +80 -52
  132. data/lib/stripe/util.rb +100 -9
  133. data/lib/stripe/version.rb +1 -1
  134. data/lib/stripe.rb +27 -23
  135. data/stripe.gemspec +12 -5
  136. metadata +46 -204
  137. data/.editorconfig +0 -10
  138. data/.gitattributes +0 -4
  139. data/.github/ISSUE_TEMPLATE.md +0 -5
  140. data/.gitignore +0 -8
  141. data/.rubocop.yml +0 -80
  142. data/.rubocop_todo.yml +0 -33
  143. data/.travis.yml +0 -38
  144. data/.vscode/extensions.json +0 -7
  145. data/.vscode/settings.json +0 -8
  146. data/lib/stripe/resources/bitcoin_receiver.rb +0 -24
  147. data/lib/stripe/resources/bitcoin_transaction.rb +0 -15
  148. data/lib/stripe/resources/issuing/card_details.rb +0 -9
  149. data/lib/stripe/resources/order.rb +0 -33
  150. data/lib/stripe/resources/order_return.rb +0 -10
  151. data/lib/stripe/resources/recipient.rb +0 -14
  152. data/lib/stripe/resources/sku.rb +0 -13
  153. data/lib/stripe/resources/three_d_secure.rb +0 -14
  154. data/test/openapi/README.md +0 -9
  155. data/test/stripe/account_link_test.rb +0 -18
  156. data/test/stripe/account_test.rb +0 -412
  157. data/test/stripe/alipay_account_test.rb +0 -37
  158. data/test/stripe/api_operations_test.rb +0 -80
  159. data/test/stripe/api_resource_test.rb +0 -646
  160. data/test/stripe/apple_pay_domain_test.rb +0 -46
  161. data/test/stripe/application_fee_refund_test.rb +0 -37
  162. data/test/stripe/application_fee_test.rb +0 -58
  163. data/test/stripe/balance_test.rb +0 -13
  164. data/test/stripe/balance_transaction_test.rb +0 -20
  165. data/test/stripe/bank_account_test.rb +0 -36
  166. data/test/stripe/billing_portal/session_test.rb +0 -18
  167. data/test/stripe/capability_test.rb +0 -45
  168. data/test/stripe/charge_test.rb +0 -64
  169. data/test/stripe/checkout/session_test.rb +0 -53
  170. data/test/stripe/connection_manager_test.rb +0 -167
  171. data/test/stripe/country_spec_test.rb +0 -20
  172. data/test/stripe/coupon_test.rb +0 -61
  173. data/test/stripe/credit_note_test.rb +0 -90
  174. data/test/stripe/customer_balance_transaction_test.rb +0 -37
  175. data/test/stripe/customer_card_test.rb +0 -48
  176. data/test/stripe/customer_test.rb +0 -226
  177. data/test/stripe/dispute_test.rb +0 -51
  178. data/test/stripe/ephemeral_key_test.rb +0 -93
  179. data/test/stripe/errors_test.rb +0 -53
  180. data/test/stripe/exchange_rate_test.rb +0 -20
  181. data/test/stripe/file_link_test.rb +0 -41
  182. data/test/stripe/file_test.rb +0 -87
  183. data/test/stripe/instrumentation_test.rb +0 -74
  184. data/test/stripe/invoice_item_test.rb +0 -66
  185. data/test/stripe/invoice_line_item_test.rb +0 -8
  186. data/test/stripe/invoice_test.rb +0 -229
  187. data/test/stripe/issuing/authorization_test.rb +0 -72
  188. data/test/stripe/issuing/card_test.rb +0 -74
  189. data/test/stripe/issuing/cardholder_test.rb +0 -53
  190. data/test/stripe/issuing/dispute_test.rb +0 -54
  191. data/test/stripe/issuing/transaction_test.rb +0 -48
  192. data/test/stripe/list_object_test.rb +0 -202
  193. data/test/stripe/login_link_test.rb +0 -37
  194. data/test/stripe/mandate_test.rb +0 -14
  195. data/test/stripe/multipart_encoder_test.rb +0 -130
  196. data/test/stripe/oauth_test.rb +0 -104
  197. data/test/stripe/order_return_test.rb +0 -21
  198. data/test/stripe/order_test.rb +0 -82
  199. data/test/stripe/payment_intent_test.rb +0 -107
  200. data/test/stripe/payment_method_test.rb +0 -84
  201. data/test/stripe/payout_test.rb +0 -72
  202. data/test/stripe/person_test.rb +0 -46
  203. data/test/stripe/plan_test.rb +0 -98
  204. data/test/stripe/price_test.rb +0 -48
  205. data/test/stripe/product_test.rb +0 -58
  206. data/test/stripe/promotion_code_test.rb +0 -42
  207. data/test/stripe/radar/early_fraud_warning_test.rb +0 -22
  208. data/test/stripe/radar/value_list_item_test.rb +0 -48
  209. data/test/stripe/radar/value_list_test.rb +0 -61
  210. data/test/stripe/recipient_test.rb +0 -62
  211. data/test/stripe/refund_test.rb +0 -39
  212. data/test/stripe/reporting/report_run_test.rb +0 -33
  213. data/test/stripe/reporting/report_type_test.rb +0 -22
  214. data/test/stripe/reversal_test.rb +0 -43
  215. data/test/stripe/review_test.rb +0 -27
  216. data/test/stripe/setup_attempt_test.rb +0 -16
  217. data/test/stripe/setup_intent_test.rb +0 -84
  218. data/test/stripe/sigma/scheduled_query_run_test.rb +0 -22
  219. data/test/stripe/sku_test.rb +0 -60
  220. data/test/stripe/source_test.rb +0 -119
  221. data/test/stripe/stripe_client_test.rb +0 -1291
  222. data/test/stripe/stripe_configuration_test.rb +0 -131
  223. data/test/stripe/stripe_object_test.rb +0 -500
  224. data/test/stripe/stripe_response_test.rb +0 -95
  225. data/test/stripe/subscription_item_test.rb +0 -84
  226. data/test/stripe/subscription_schedule_test.rb +0 -82
  227. data/test/stripe/subscription_test.rb +0 -80
  228. data/test/stripe/tax_id_test.rb +0 -31
  229. data/test/stripe/tax_rate_test.rb +0 -43
  230. data/test/stripe/terminal/connection_token_test.rb +0 -16
  231. data/test/stripe/terminal/location_test.rb +0 -68
  232. data/test/stripe/terminal/reader_test.rb +0 -62
  233. data/test/stripe/three_d_secure_test.rb +0 -23
  234. data/test/stripe/topup_test.rb +0 -62
  235. data/test/stripe/transfer_test.rb +0 -88
  236. data/test/stripe/usage_record_summary_test.rb +0 -29
  237. data/test/stripe/util_test.rb +0 -402
  238. data/test/stripe/webhook_endpoint_test.rb +0 -59
  239. data/test/stripe/webhook_test.rb +0 -135
  240. data/test/stripe_mock.rb +0 -78
  241. data/test/stripe_test.rb +0 -132
  242. data/test/test_data.rb +0 -61
  243. data/test/test_helper.rb +0 -77
@@ -1,646 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require ::File.expand_path("../test_helper", __dir__)
4
-
5
- module Stripe
6
- class ApiResourceTest < Test::Unit::TestCase
7
- class CustomMethodAPIResource < APIResource
8
- OBJECT_NAME = "custom_method"
9
- custom_method :my_method, http_verb: :post
10
- end
11
-
12
- class NestedTestAPIResource < APIResource
13
- save_nested_resource :external_account
14
- end
15
-
16
- context ".custom_method" do
17
- should "call to an RPC-style method" do
18
- stub_request(:post, "#{Stripe.api_base}/v1/custom_methods/ch_123/my_method")
19
- .to_return(body: JSON.generate({}))
20
- CustomMethodAPIResource.my_method("ch_123")
21
- end
22
-
23
- should "raise an error if a non-ID is passed" do
24
- e = assert_raises ArgumentError do
25
- CustomMethodAPIResource.my_method(id: "ch_123")
26
- end
27
- assert_equal "id should be a string representing the ID of an API resource",
28
- e.message
29
- end
30
- end
31
-
32
- context ".save_nested_resource" do
33
- should "can have a scalar set" do
34
- r = NestedTestAPIResource.new("test_resource")
35
- r.external_account = "tok_123"
36
- assert_equal "tok_123", r.external_account
37
- end
38
-
39
- should "set a flag if given an object source" do
40
- r = NestedTestAPIResource.new("test_resource")
41
- r.external_account = {
42
- object: "card",
43
- }
44
- assert_equal true, r.external_account.save_with_parent
45
- end
46
- end
47
-
48
- should "creating a new APIResource should not fetch over the network" do
49
- Stripe::Customer.new("someid")
50
- assert_not_requested :get, %r{#{Stripe.api_base}/.*}
51
- end
52
-
53
- should "creating a new APIResource from a hash should not fetch over the network" do
54
- Stripe::Customer.construct_from(id: "somecustomer",
55
- card: { id: "somecard", object: "card" },
56
- object: "customer")
57
- assert_not_requested :get, %r{#{Stripe.api_base}/.*}
58
- end
59
-
60
- should "setting an attribute should not cause a network request" do
61
- c = Stripe::Customer.new("cus_123")
62
- c.card = { id: "somecard", object: "card" }
63
- assert_not_requested :get, %r{#{Stripe.api_base}/.*}
64
- assert_not_requested :post, %r{#{Stripe.api_base}/.*}
65
- end
66
-
67
- should "accessing id should not issue a fetch" do
68
- c = Stripe::Customer.new("cus_123")
69
- c.id
70
- assert_not_requested :get, %r{#{Stripe.api_base}/.*}
71
- end
72
-
73
- should "not specifying api credentials should raise an exception" do
74
- Stripe.api_key = nil
75
- assert_raises Stripe::AuthenticationError do
76
- Stripe::Customer.new("cus_123").refresh
77
- end
78
- end
79
-
80
- should "using a nil api key should raise an exception" do
81
- assert_raises TypeError do
82
- Stripe::Customer.list({}, nil)
83
- end
84
- assert_raises TypeError do
85
- Stripe::Customer.list({}, api_key: nil)
86
- end
87
- end
88
-
89
- should "specifying api credentials containing whitespace should raise an exception" do
90
- Stripe.api_key = "key "
91
- assert_raises Stripe::AuthenticationError do
92
- Stripe::Customer.new("cus_123").refresh
93
- end
94
- end
95
-
96
- should "send expand on fetch properly" do
97
- stub_request(:get, "#{Stripe.api_base}/v1/charges/ch_123")
98
- .with(query: { "expand" => ["customer"] })
99
- .to_return(body: JSON.generate(charge_fixture))
100
-
101
- Stripe::Charge.retrieve(id: "ch_123", expand: [:customer])
102
- end
103
-
104
- should "preserve expand across refreshes" do
105
- stub_request(:get, "#{Stripe.api_base}/v1/charges/ch_123")
106
- .with(query: { "expand" => ["customer"] })
107
- .to_return(body: JSON.generate(charge_fixture))
108
-
109
- ch = Stripe::Charge.retrieve(id: "ch_123", expand: [:customer])
110
- ch.refresh
111
- end
112
-
113
- should "send expand when fetching through ListObject" do
114
- stub_request(:get, "#{Stripe.api_base}/v1/charges/ch_123")
115
- .to_return(body: JSON.generate(charge_fixture))
116
-
117
- stub_request(:get, "#{Stripe.api_base}/v1/charges/ch_123/refunds/re_123")
118
- .with(query: { "expand" => ["balance_transaction"] })
119
- .to_return(body: JSON.generate(charge_fixture))
120
-
121
- charge = Stripe::Charge.retrieve("ch_123")
122
- charge.refunds.retrieve(id: "re_123", expand: [:balance_transaction])
123
- end
124
-
125
- context "when specifying per-object credentials" do
126
- context "with no global API key set" do
127
- should "use the per-object credential when creating" do
128
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
129
- .with(headers: { "Authorization" => "Bearer sk_test_local" })
130
- .to_return(body: JSON.generate(charge_fixture))
131
-
132
- Stripe::Charge.create({ source: "tok_visa" },
133
- "sk_test_local")
134
- end
135
- end
136
-
137
- context "with a global API key set" do
138
- setup do
139
- Stripe.api_key = "global"
140
- end
141
-
142
- teardown do
143
- Stripe.api_key = nil
144
- end
145
-
146
- should "use the per-object credential when creating" do
147
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
148
- .with(headers: { "Authorization" => "Bearer sk_test_local" })
149
- .to_return(body: JSON.generate(charge_fixture))
150
-
151
- Stripe::Charge.create({ source: "tok_visa" },
152
- "sk_test_local")
153
- end
154
-
155
- should "use the per-object credential when retrieving and making other calls" do
156
- stub_request(:get, "#{Stripe.api_base}/v1/charges/ch_123")
157
- .with(headers: { "Authorization" => "Bearer sk_test_local" })
158
- .to_return(body: JSON.generate(charge_fixture))
159
- stub_request(:post, "#{Stripe.api_base}/v1/charges/ch_123/refunds")
160
- .with(headers: { "Authorization" => "Bearer sk_test_local" })
161
- .to_return(body: "{}")
162
-
163
- ch = Stripe::Charge.retrieve("ch_123", "sk_test_local")
164
- ch.refunds.create
165
- end
166
- end
167
- end
168
-
169
- context "with valid credentials" do
170
- should "urlencode values in GET params" do
171
- stub_request(:get, "#{Stripe.api_base}/v1/charges")
172
- .with(query: { customer: "test customer" })
173
- .to_return(body: JSON.generate(data: [charge_fixture]))
174
- charges = Stripe::Charge.list(customer: "test customer").data
175
- assert charges.is_a? Array
176
- end
177
-
178
- should "construct URL properly with base query parameters" do
179
- stub_request(:get, "#{Stripe.api_base}/v1/charges")
180
- .with(query: { customer: "cus_123" })
181
- .to_return(body: JSON.generate(data: [charge_fixture],
182
- url: "/v1/charges"))
183
- charges = Stripe::Charge.list(customer: "cus_123")
184
-
185
- stub_request(:get, "#{Stripe.api_base}/v1/charges")
186
- .with(query: { customer: "cus_123", created: "123" })
187
- .to_return(body: JSON.generate(data: [charge_fixture],
188
- url: "/v1/charges"))
189
- charges.list(created: 123)
190
- end
191
-
192
- should "setting a nil value for a param should exclude that param from the request" do
193
- stub_request(:get, "#{Stripe.api_base}/v1/charges")
194
- .with(query: { offset: 5, sad: false })
195
- .to_return(body: JSON.generate(count: 1, data: [charge_fixture]))
196
- Stripe::Charge.list(count: nil, offset: 5, sad: false)
197
-
198
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
199
- .with(body: { "amount" => "50", "currency" => "usd" })
200
- .to_return(body: JSON.generate(count: 1, data: [charge_fixture]))
201
- Stripe::Charge.create(amount: 50, currency: "usd", card: { number: nil })
202
- end
203
-
204
- should "not trigger a warning if a known opt, such as idempotency_key, is in opts" do
205
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
206
- .to_return(body: JSON.generate(charge_fixture))
207
- old_stderr = $stderr
208
- $stderr = StringIO.new
209
- begin
210
- Stripe::Charge.create({ amount: 100, currency: "usd", card: "sc_token" }, idempotency_key: "12345")
211
- assert $stderr.string.empty?
212
- ensure
213
- $stderr = old_stderr
214
- end
215
- end
216
-
217
- should "trigger a warning if a known opt, such as idempotency_key, is in params" do
218
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
219
- .to_return(body: JSON.generate(charge_fixture))
220
- old_stderr = $stderr
221
- $stderr = StringIO.new
222
- begin
223
- Stripe::Charge.create(amount: 100, currency: "usd", card: "sc_token", idempotency_key: "12345")
224
- assert_match Regexp.new("WARNING:"), $stderr.string
225
- ensure
226
- $stderr = old_stderr
227
- end
228
- end
229
-
230
- should "error if the params is not a Hash" do
231
- stub_request(:post, "#{Stripe.api_base}/v1/charges/ch_123/capture")
232
- .to_return(body: JSON.generate(charge_fixture))
233
-
234
- e = assert_raises(ArgumentError) { Stripe::Charge.capture("ch_123", "sk_test_secret") }
235
-
236
- assert_equal "request params should be either a Hash or nil (was a String)", e.message
237
- end
238
-
239
- should "allow making a request with params set to nil" do
240
- stub_request(:post, "#{Stripe.api_base}/v1/charges/ch_123/capture")
241
- .to_return(body: JSON.generate(charge_fixture))
242
-
243
- Stripe::Charge.capture("ch_123", nil, "sk_test_secret")
244
- end
245
-
246
- should "error if a user-specified opt is given a non-nil non-string value" do
247
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
248
- .to_return(body: JSON.generate(charge_fixture))
249
-
250
- # Works fine if not included or a string.
251
- Stripe::Charge.create({ amount: 100, currency: "usd" }, {})
252
- Stripe::Charge.create({ amount: 100, currency: "usd" }, idempotency_key: "12345")
253
-
254
- # Errors on a non-string.
255
- e = assert_raises(ArgumentError) do
256
- Stripe::Charge.create({ amount: 100, currency: "usd" }, idempotency_key: :foo)
257
- end
258
- assert_equal "request option 'idempotency_key' should be a string value " \
259
- "(was a Symbol)",
260
- e.message
261
- end
262
-
263
- should "requesting with a unicode ID should result in a request" do
264
- stub_request(:get, "#{Stripe.api_base}/v1/customers/%E2%98%83")
265
- .to_return(body: JSON.generate(make_missing_id_error), status: 404)
266
- c = Stripe::Customer.new("☃")
267
- assert_raises(Stripe::InvalidRequestError) { c.refresh }
268
- end
269
-
270
- should "requesting with no ID should result in an InvalidRequestError with no request" do
271
- c = Stripe::Customer.new
272
- assert_raises(Stripe::InvalidRequestError) { c.refresh }
273
- end
274
-
275
- should "making a GET request with parameters should have a query string and no body" do
276
- stub_request(:get, "#{Stripe.api_base}/v1/charges")
277
- .with(query: { limit: 1 })
278
- .to_return(body: JSON.generate(data: [charge_fixture]))
279
- Stripe::Charge.list(limit: 1)
280
- end
281
-
282
- should "making a POST request with parameters should have a body and no query string" do
283
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
284
- .with(body: { "amount" => "100", "currency" => "usd", "card" => "sc_token" })
285
- .to_return(body: JSON.generate(charge_fixture))
286
- Stripe::Charge.create(amount: 100, currency: "usd", card: "sc_token")
287
- end
288
-
289
- should "loading an object should issue a GET request" do
290
- stub_request(:get, "#{Stripe.api_base}/v1/charges/ch_123")
291
- .to_return(body: JSON.generate(charge_fixture))
292
- c = Stripe::Charge.new("ch_123")
293
- c.refresh
294
- end
295
-
296
- should "using array accessors should be the same as the method interface" do
297
- stub_request(:get, "#{Stripe.api_base}/v1/charges/ch_123")
298
- .to_return(body: JSON.generate(charge_fixture))
299
- c = Stripe::Charge.new("cus_123")
300
- c.refresh
301
- assert_equal c.created, c[:created]
302
- assert_equal c.created, c["created"]
303
- c["created"] = 12_345
304
- assert_equal c.created, 12_345
305
- end
306
-
307
- should "updating an object should issue a POST request with only the changed properties" do
308
- stub_request(:post, "#{Stripe.api_base}/v1/customers/cus_123")
309
- .with(body: { "description" => "another_mn" })
310
- .to_return(body: JSON.generate(customer_fixture))
311
- c = Stripe::Customer.construct_from(customer_fixture)
312
- c.description = "another_mn"
313
- c.save
314
- end
315
-
316
- should "updating should merge in returned properties" do
317
- stub_request(:post, "#{Stripe.api_base}/v1/customers/cus_123")
318
- .with(body: { "description" => "another_mn" })
319
- .to_return(body: JSON.generate(customer_fixture))
320
- c = Stripe::Customer.new("cus_123")
321
- c.description = "another_mn"
322
- c.save
323
- assert_equal false, c.livemode
324
- end
325
-
326
- should "updating should fail if api_key is overwritten with nil" do
327
- c = Stripe::Customer.new
328
- assert_raises TypeError do
329
- c.save({}, api_key: nil)
330
- end
331
- end
332
-
333
- should "updating should use the supplied api_key" do
334
- stub_request(:post, "#{Stripe.api_base}/v1/customers")
335
- .with(headers: { "Authorization" => "Bearer sk_test_local" })
336
- .to_return(body: JSON.generate(customer_fixture))
337
- c = Stripe::Customer.new
338
- c.save({}, api_key: "sk_test_local")
339
- assert_equal false, c.livemode
340
- end
341
-
342
- should "deleting should send no props and result in an object that has no props other deleted" do
343
- stub_request(:delete, "#{Stripe.api_base}/v1/customers/cus_123")
344
- .to_return(body: JSON.generate("id" => "cus_123", "deleted" => true))
345
- c = Stripe::Customer.construct_from(customer_fixture)
346
- c.delete
347
- end
348
-
349
- should "loading all of an APIResource should return an array of recursively instantiated objects" do
350
- stub_request(:get, "#{Stripe.api_base}/v1/charges")
351
- .to_return(body: JSON.generate(data: [charge_fixture]))
352
- charges = Stripe::Charge.list.data
353
- assert charges.is_a? Array
354
- assert charges[0].is_a? Stripe::Charge
355
- assert charges[0].payment_method_details.is_a?(Stripe::StripeObject)
356
- end
357
-
358
- should "passing in a stripe_account header should pass it through on call" do
359
- stub_request(:get, "#{Stripe.api_base}/v1/customers/cus_123")
360
- .with(headers: { "Stripe-Account" => "acct_123" })
361
- .to_return(body: JSON.generate(customer_fixture))
362
- Stripe::Customer.retrieve("cus_123", stripe_account: "acct_123")
363
- end
364
-
365
- should "passing in a stripe_account header should pass it through on save" do
366
- stub_request(:get, "#{Stripe.api_base}/v1/customers/cus_123")
367
- .with(headers: { "Stripe-Account" => "acct_123" })
368
- .to_return(body: JSON.generate(customer_fixture))
369
- c = Stripe::Customer.retrieve("cus_123", stripe_account: "acct_123")
370
-
371
- stub_request(:post, "#{Stripe.api_base}/v1/customers/cus_123")
372
- .with(headers: { "Stripe-Account" => "acct_123" })
373
- .to_return(body: JSON.generate(customer_fixture))
374
- c.description = "FOO"
375
- c.save
376
- end
377
-
378
- should "add key to nested objects" do
379
- acct = Stripe::Account.construct_from(id: "myid",
380
- legal_entity: {
381
- size: "l",
382
- score: 4,
383
- height: 10,
384
- })
385
-
386
- stub_request(:post, "#{Stripe.api_base}/v1/accounts/myid")
387
- .with(body: { legal_entity: { first_name: "Bob" } })
388
- .to_return(body: JSON.generate("id" => "myid"))
389
-
390
- acct.legal_entity.first_name = "Bob"
391
- acct.save
392
- end
393
-
394
- should "save nothing if nothing changes" do
395
- acct = Stripe::Account.construct_from(id: "acct_id",
396
- metadata: {
397
- key: "value",
398
- })
399
-
400
- stub_request(:post, "#{Stripe.api_base}/v1/accounts/acct_id")
401
- .with(body: {})
402
- .to_return(body: JSON.generate("id" => "acct_id"))
403
-
404
- acct.save
405
- end
406
-
407
- should "not save nested API resources" do
408
- ch = Stripe::Charge.construct_from(id: "ch_id",
409
- customer: {
410
- object: "customer",
411
- id: "customer_id",
412
- })
413
-
414
- stub_request(:post, "#{Stripe.api_base}/v1/charges/ch_id")
415
- .with(body: {})
416
- .to_return(body: JSON.generate("id" => "ch_id"))
417
-
418
- ch.customer.description = "Bob"
419
- ch.save
420
- end
421
-
422
- should "correctly handle replaced nested objects" do
423
- acct = Stripe::Account.construct_from(
424
- id: "acct_123",
425
- company: {
426
- name: "company_name",
427
- address: {
428
- line1: "test",
429
- city: "San Francisco",
430
- },
431
- }
432
- )
433
-
434
- stub_request(:post, "#{Stripe.api_base}/v1/accounts/acct_123")
435
- .with(body: { company: { address: { line1: "Test2", city: "" } } })
436
- .to_return(body: JSON.generate("id" => "my_id"))
437
-
438
- acct.company.address = { line1: "Test2" }
439
- acct.save
440
- end
441
-
442
- should "correctly handle array setting" do
443
- acct = Stripe::Account.construct_from(id: "myid",
444
- legal_entity: {})
445
-
446
- stub_request(:post, "#{Stripe.api_base}/v1/accounts/myid")
447
- .with(body: { legal_entity: { additional_owners: [{ first_name: "Bob" }] } })
448
- .to_return(body: JSON.generate("id" => "myid"))
449
-
450
- acct.legal_entity.additional_owners = [{ first_name: "Bob" }]
451
- acct.save
452
- end
453
-
454
- should "correctly handle array insertion" do
455
- acct = Stripe::Account.construct_from(id: "myid",
456
- legal_entity: {
457
- additional_owners: [],
458
- })
459
-
460
- # Note that this isn't a perfect check because we're using webmock's
461
- # data decoding, which isn't aware of the Stripe array encoding that we
462
- # use here.
463
- stub_request(:post, "#{Stripe.api_base}/v1/accounts/myid")
464
- .with(body: { legal_entity: { additional_owners: [{ first_name: "Bob" }] } })
465
- .to_return(body: JSON.generate("id" => "myid"))
466
-
467
- acct.legal_entity.additional_owners << { first_name: "Bob" }
468
- acct.save
469
- end
470
-
471
- should "correctly handle array updates" do
472
- acct = Stripe::Account.construct_from(id: "myid",
473
- legal_entity: {
474
- additional_owners: [{ first_name: "Bob" }, { first_name: "Jane" }],
475
- })
476
-
477
- # Note that this isn't a perfect check because we're using webmock's
478
- # data decoding, which isn't aware of the Stripe array encoding that we
479
- # use here.
480
- stub_request(:post, "#{Stripe.api_base}/v1/accounts/myid")
481
- .with(body: { legal_entity: { additional_owners: [{ first_name: "Janet" }] } })
482
- .to_return(body: JSON.generate("id" => "myid"))
483
-
484
- acct.legal_entity.additional_owners[1].first_name = "Janet"
485
- acct.save
486
- end
487
-
488
- should "correctly handle array noops" do
489
- acct = Stripe::Account.construct_from(id: "myid",
490
- legal_entity: {
491
- additional_owners: [{ first_name: "Bob" }],
492
- },
493
- currencies_supported: %w[usd cad])
494
-
495
- stub_request(:post, "#{Stripe.api_base}/v1/accounts/myid")
496
- .with(body: {})
497
- .to_return(body: JSON.generate("id" => "myid"))
498
-
499
- acct.save
500
- end
501
-
502
- should "correctly handle hash noops" do
503
- acct = Stripe::Account.construct_from(id: "myid",
504
- legal_entity: {
505
- address: { line1: "1 Two Three" },
506
- })
507
-
508
- stub_request(:post, "#{Stripe.api_base}/v1/accounts/myid")
509
- .with(body: {})
510
- .to_return(body: JSON.generate("id" => "myid"))
511
-
512
- acct.save
513
- end
514
-
515
- should "should create a new resource when an object without an id is saved" do
516
- account = Stripe::Account.construct_from(id: nil,
517
- display_name: nil)
518
-
519
- stub_request(:post, "#{Stripe.api_base}/v1/accounts")
520
- .with(body: { display_name: "stripe" })
521
- .to_return(body: JSON.generate("id" => "acct_123"))
522
-
523
- account.display_name = "stripe"
524
- account.save
525
- end
526
-
527
- should "set attributes as part of save" do
528
- account = Stripe::Account.construct_from(id: nil,
529
- display_name: nil)
530
-
531
- stub_request(:post, "#{Stripe.api_base}/v1/accounts")
532
- .with(body: { display_name: "stripe", metadata: { key: "value" } })
533
- .to_return(body: JSON.generate("id" => "acct_123"))
534
-
535
- account.save(display_name: "stripe", metadata: { key: "value" })
536
- end
537
- end
538
-
539
- context "#request_stripe_object" do
540
- class HelloTestAPIResource < APIResource
541
- OBJECT_NAME = "hello"
542
- def say_hello(params = {}, opts = {})
543
- request_stripe_object(
544
- method: :post,
545
- path: resource_url + "/say",
546
- params: params,
547
- opts: opts
548
- )
549
- end
550
- end
551
-
552
- setup do
553
- Util.instance_variable_set(
554
- :@object_classes,
555
- Stripe::ObjectTypes.object_names_to_classes.merge(
556
- "hello" => HelloTestAPIResource
557
- )
558
- )
559
- end
560
- teardown do
561
- Util.class.instance_variable_set(:@object_classes, Stripe::ObjectTypes.object_names_to_classes)
562
- end
563
-
564
- should "make requests appropriately" do
565
- stub_request(:post, "#{Stripe.api_base}/v1/hellos/hi_123/say")
566
- .with(body: { foo: "bar" }, headers: { "Stripe-Account" => "acct_hi" })
567
- .to_return(body: JSON.generate("object" => "hello"))
568
-
569
- hello = HelloTestAPIResource.new(id: "hi_123")
570
- hello.say_hello({ foo: "bar" }, stripe_account: "acct_hi")
571
- end
572
-
573
- should "update attributes in-place when it returns the same thing" do
574
- stub_request(:post, "#{Stripe.api_base}/v1/hellos/hi_123/say")
575
- .to_return(body: JSON.generate("object" => "hello", "additional" => "attribute"))
576
-
577
- hello = HelloTestAPIResource.new(id: "hi_123")
578
- hello.unsaved = "a value"
579
- new_hello = hello.say_hello
580
-
581
- # Doesn't matter if you use the return variable or the instance.
582
- assert_equal(hello, new_hello)
583
-
584
- # It updates new attributes in-place.
585
- assert_equal("attribute", hello.additional)
586
-
587
- # It removes unsaved attributes, but at least lets you know about them.
588
- e = assert_raises(NoMethodError) { hello.unsaved }
589
- assert_match("The 'unsaved' attribute was set in the past", e.message)
590
- end
591
-
592
- should "instantiate a new object of the appropriate class when it is different than the host class" do
593
- stub_request(:post, "#{Stripe.api_base}/v1/hellos/hi_123/say")
594
- .to_return(body: JSON.generate("object" => "goodbye", "additional" => "attribute"))
595
-
596
- hello = HelloTestAPIResource.new(id: "hi_123")
597
- hello.unsaved = "a value"
598
- new_goodbye = hello.say_hello
599
-
600
- # The returned value and the instance are different objects.
601
- refute_equal(new_goodbye, hello)
602
-
603
- # The returned value has stuff from the server.
604
- assert_equal("attribute", new_goodbye.additional)
605
- assert_equal("goodbye", new_goodbye.object)
606
-
607
- # You instance doesn't have stuff from the server.
608
- e = assert_raises(NoMethodError) { hello.additional }
609
- refute_match(/was set in the past/, e.message)
610
-
611
- # The instance preserves unset attributes on the original instance (not sure this is good behavior?)
612
- assert_equal("a value", hello.unsaved)
613
- end
614
- end
615
-
616
- @@fixtures = {} # rubocop:disable Style/ClassVars
617
- setup do
618
- if @@fixtures.empty?
619
- cache_fixture(:charge) do
620
- Charge.retrieve("ch_123")
621
- end
622
- cache_fixture(:customer) do
623
- Customer.retrieve("cus_123")
624
- end
625
- end
626
- end
627
-
628
- private def charge_fixture
629
- @@fixtures[:charge]
630
- end
631
-
632
- private def customer_fixture
633
- @@fixtures[:customer]
634
- end
635
-
636
- # Expects to retrieve a fixture from stripe-mock (an API call should be
637
- # included in the block to yield to) and does very simple memoization.
638
- private def cache_fixture(key)
639
- return @@fixtures[key] if @@fixtures.key?(key)
640
-
641
- obj = yield
642
- @@fixtures[key] = obj.instance_variable_get(:@values).freeze
643
- @@fixtures[key]
644
- end
645
- end
646
- end
@@ -1,46 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require ::File.expand_path("../test_helper", __dir__)
4
-
5
- module Stripe
6
- class ApplePayDomainTest < Test::Unit::TestCase
7
- should "be listable" do
8
- domains = Stripe::ApplePayDomain.list
9
- assert_requested :get, "#{Stripe.api_base}/v1/apple_pay/domains"
10
- assert domains.data.is_a?(Array)
11
- assert domains.data[0].is_a?(Stripe::ApplePayDomain)
12
- end
13
-
14
- should "be retrievable" do
15
- domain = Stripe::ApplePayDomain.retrieve("apwc_123")
16
- assert_requested :get, "#{Stripe.api_base}/v1/apple_pay/domains/apwc_123"
17
- assert domain.is_a?(Stripe::ApplePayDomain)
18
- end
19
-
20
- should "be creatable" do
21
- domain = Stripe::ApplePayDomain.create(domain_name: "example.com")
22
- assert_requested :post, "#{Stripe.api_base}/v1/apple_pay/domains"
23
- assert domain.is_a?(Stripe::ApplePayDomain)
24
- end
25
-
26
- should "be deletable" do
27
- end
28
-
29
- context "#delete" do
30
- should "be deletable" do
31
- domain = Stripe::ApplePayDomain.retrieve("apwc_123")
32
- domain = domain.delete
33
- assert_requested :delete, "#{Stripe.api_base}/v1/apple_pay/domains/#{domain.id}"
34
- assert domain.is_a?(Stripe::ApplePayDomain)
35
- end
36
- end
37
-
38
- context ".delete" do
39
- should "be deletable" do
40
- domain = Stripe::ApplePayDomain.delete("apwc_123")
41
- assert_requested :delete, "#{Stripe.api_base}/v1/apple_pay/domains/apwc_123"
42
- assert domain.is_a?(Stripe::ApplePayDomain)
43
- end
44
- end
45
- end
46
- end