stripe 5.31.0 → 5.32.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/VERSION +1 -1
- data/lib/stripe/version.rb +1 -1
- data/stripe.gemspec +9 -2
- metadata +3 -195
- data/.editorconfig +0 -10
- data/.gitattributes +0 -4
- data/.github/ISSUE_TEMPLATE.md +0 -5
- data/.github/workflows/ci.yml +0 -34
- data/.gitignore +0 -8
- data/.rubocop.yml +0 -81
- data/.rubocop_todo.yml +0 -33
- data/.travis.yml +0 -38
- data/.vscode/extensions.json +0 -7
- data/.vscode/settings.json +0 -8
- data/test/openapi/README.md +0 -9
- data/test/stripe/account_link_test.rb +0 -18
- data/test/stripe/account_test.rb +0 -412
- data/test/stripe/alipay_account_test.rb +0 -37
- data/test/stripe/api_operations_test.rb +0 -80
- data/test/stripe/api_resource_test.rb +0 -646
- data/test/stripe/apple_pay_domain_test.rb +0 -46
- data/test/stripe/application_fee_refund_test.rb +0 -37
- data/test/stripe/application_fee_test.rb +0 -58
- data/test/stripe/balance_test.rb +0 -13
- data/test/stripe/balance_transaction_test.rb +0 -20
- data/test/stripe/bank_account_test.rb +0 -36
- data/test/stripe/billing_portal/configuration_test.rb +0 -37
- data/test/stripe/billing_portal/session_test.rb +0 -18
- data/test/stripe/capability_test.rb +0 -45
- data/test/stripe/charge_test.rb +0 -64
- data/test/stripe/checkout/session_test.rb +0 -53
- data/test/stripe/connection_manager_test.rb +0 -210
- data/test/stripe/country_spec_test.rb +0 -20
- data/test/stripe/coupon_test.rb +0 -61
- data/test/stripe/credit_note_test.rb +0 -90
- data/test/stripe/customer_balance_transaction_test.rb +0 -37
- data/test/stripe/customer_card_test.rb +0 -48
- data/test/stripe/customer_test.rb +0 -226
- data/test/stripe/dispute_test.rb +0 -51
- data/test/stripe/ephemeral_key_test.rb +0 -93
- data/test/stripe/errors_test.rb +0 -53
- data/test/stripe/exchange_rate_test.rb +0 -20
- data/test/stripe/file_link_test.rb +0 -41
- data/test/stripe/file_test.rb +0 -87
- data/test/stripe/instrumentation_test.rb +0 -74
- data/test/stripe/invoice_item_test.rb +0 -66
- data/test/stripe/invoice_line_item_test.rb +0 -8
- data/test/stripe/invoice_test.rb +0 -229
- data/test/stripe/issuing/authorization_test.rb +0 -72
- data/test/stripe/issuing/card_test.rb +0 -74
- data/test/stripe/issuing/cardholder_test.rb +0 -53
- data/test/stripe/issuing/dispute_test.rb +0 -54
- data/test/stripe/issuing/transaction_test.rb +0 -48
- data/test/stripe/list_object_test.rb +0 -202
- data/test/stripe/login_link_test.rb +0 -37
- data/test/stripe/mandate_test.rb +0 -14
- data/test/stripe/multipart_encoder_test.rb +0 -130
- data/test/stripe/oauth_test.rb +0 -149
- data/test/stripe/order_return_test.rb +0 -21
- data/test/stripe/order_test.rb +0 -82
- data/test/stripe/payment_intent_test.rb +0 -107
- data/test/stripe/payment_method_test.rb +0 -84
- data/test/stripe/payout_test.rb +0 -72
- data/test/stripe/person_test.rb +0 -46
- data/test/stripe/plan_test.rb +0 -98
- data/test/stripe/price_test.rb +0 -48
- data/test/stripe/product_test.rb +0 -58
- data/test/stripe/promotion_code_test.rb +0 -42
- data/test/stripe/radar/early_fraud_warning_test.rb +0 -22
- data/test/stripe/radar/value_list_item_test.rb +0 -48
- data/test/stripe/radar/value_list_test.rb +0 -61
- data/test/stripe/recipient_test.rb +0 -62
- data/test/stripe/refund_test.rb +0 -39
- data/test/stripe/reporting/report_run_test.rb +0 -33
- data/test/stripe/reporting/report_type_test.rb +0 -22
- data/test/stripe/reversal_test.rb +0 -43
- data/test/stripe/review_test.rb +0 -27
- data/test/stripe/setup_attempt_test.rb +0 -16
- data/test/stripe/setup_intent_test.rb +0 -84
- data/test/stripe/sigma/scheduled_query_run_test.rb +0 -22
- data/test/stripe/sku_test.rb +0 -60
- data/test/stripe/source_test.rb +0 -119
- data/test/stripe/stripe_client_test.rb +0 -1456
- data/test/stripe/stripe_configuration_test.rb +0 -159
- data/test/stripe/stripe_object_test.rb +0 -510
- data/test/stripe/stripe_response_test.rb +0 -95
- data/test/stripe/subscription_item_test.rb +0 -84
- data/test/stripe/subscription_schedule_test.rb +0 -82
- data/test/stripe/subscription_test.rb +0 -80
- data/test/stripe/tax_id_test.rb +0 -31
- data/test/stripe/tax_rate_test.rb +0 -43
- data/test/stripe/terminal/connection_token_test.rb +0 -16
- data/test/stripe/terminal/location_test.rb +0 -68
- data/test/stripe/terminal/reader_test.rb +0 -62
- data/test/stripe/three_d_secure_test.rb +0 -23
- data/test/stripe/topup_test.rb +0 -62
- data/test/stripe/transfer_test.rb +0 -88
- data/test/stripe/usage_record_summary_test.rb +0 -29
- data/test/stripe/util_test.rb +0 -402
- data/test/stripe/webhook_endpoint_test.rb +0 -59
- data/test/stripe/webhook_test.rb +0 -135
- data/test/stripe_mock.rb +0 -78
- data/test/stripe_test.rb +0 -137
- data/test/test_data.rb +0 -61
- data/test/test_helper.rb +0 -74
@@ -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"
|
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,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
|