stripe 4.24.0 → 5.36.0

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