stripe 5.1.0 → 5.55.0

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