stripe 3.3.2 → 3.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rubocop.yml +20 -0
- data/.rubocop_todo.yml +62 -0
- data/.travis.yml +1 -1
- data/Gemfile +19 -12
- data/History.txt +10 -0
- data/README.md +5 -1
- data/Rakefile +8 -5
- data/VERSION +1 -1
- data/bin/stripe-console +2 -2
- data/lib/stripe.rb +72 -74
- data/lib/stripe/account.rb +15 -17
- data/lib/stripe/alipay_account.rb +10 -7
- data/lib/stripe/api_operations/create.rb +1 -1
- data/lib/stripe/api_operations/delete.rb +1 -1
- data/lib/stripe/api_operations/list.rb +2 -2
- data/lib/stripe/api_operations/request.rb +5 -12
- data/lib/stripe/api_operations/save.rb +6 -6
- data/lib/stripe/api_resource.rb +7 -9
- data/lib/stripe/apple_pay_domain.rb +2 -2
- data/lib/stripe/application_fee.rb +5 -5
- data/lib/stripe/application_fee_refund.rb +5 -5
- data/lib/stripe/balance.rb +1 -1
- data/lib/stripe/balance_transaction.rb +2 -2
- data/lib/stripe/bank_account.rb +7 -7
- data/lib/stripe/bitcoin_receiver.rb +4 -2
- data/lib/stripe/bitcoin_transaction.rb +3 -1
- data/lib/stripe/card.rb +5 -5
- data/lib/stripe/charge.rb +18 -18
- data/lib/stripe/country_spec.rb +2 -2
- data/lib/stripe/coupon.rb +1 -1
- data/lib/stripe/customer.rb +23 -23
- data/lib/stripe/dispute.rb +3 -3
- data/lib/stripe/ephemeral_key.rb +4 -4
- data/lib/stripe/errors.rb +4 -4
- data/lib/stripe/event.rb +1 -1
- data/lib/stripe/file_upload.rb +5 -5
- data/lib/stripe/invoice.rb +7 -7
- data/lib/stripe/invoice_item.rb +1 -1
- data/lib/stripe/invoice_line_item.rb +1 -1
- data/lib/stripe/list_object.rb +14 -18
- data/lib/stripe/login_link.rb +3 -3
- data/lib/stripe/oauth.rb +15 -13
- data/lib/stripe/order.rb +5 -5
- data/lib/stripe/order_return.rb +1 -1
- data/lib/stripe/payout.rb +3 -3
- data/lib/stripe/plan.rb +1 -1
- data/lib/stripe/product.rb +1 -1
- data/lib/stripe/recipient.rb +3 -2
- data/lib/stripe/recipient_transfer.rb +1 -2
- data/lib/stripe/refund.rb +1 -1
- data/lib/stripe/reversal.rb +5 -5
- data/lib/stripe/singleton_api_resource.rb +3 -3
- data/lib/stripe/sku.rb +1 -1
- data/lib/stripe/source.rb +13 -10
- data/lib/stripe/stripe_client.rb +149 -169
- data/lib/stripe/stripe_object.rb +77 -76
- data/lib/stripe/subscription.rb +5 -5
- data/lib/stripe/subscription_item.rb +2 -2
- data/lib/stripe/three_d_secure.rb +1 -1
- data/lib/stripe/token.rb +1 -1
- data/lib/stripe/transfer.rb +3 -3
- data/lib/stripe/util.rb +77 -62
- data/lib/stripe/version.rb +1 -1
- data/lib/stripe/webhook.rb +14 -10
- data/stripe.gemspec +14 -14
- data/test/stripe/account_test.rb +69 -81
- data/test/stripe/alipay_account_test.rb +19 -1
- data/test/stripe/api_operations_test.rb +7 -7
- data/test/stripe/api_resource_test.rb +224 -260
- data/test/stripe/apple_pay_domain_test.rb +8 -8
- data/test/stripe/application_fee_refund_test.rb +8 -8
- data/test/stripe/application_fee_test.rb +3 -3
- data/test/stripe/balance_test.rb +2 -2
- data/test/stripe/bank_account_test.rb +9 -11
- data/test/stripe/charge_test.rb +11 -11
- data/test/stripe/country_spec_test.rb +4 -4
- data/test/stripe/coupon_test.rb +10 -10
- data/test/stripe/customer_card_test.rb +11 -15
- data/test/stripe/customer_test.rb +26 -27
- data/test/stripe/dispute_test.rb +8 -8
- data/test/stripe/ephemeral_key_test.rb +14 -14
- data/test/stripe/errors_test.rb +2 -2
- data/test/stripe/file_upload_test.rb +26 -28
- data/test/stripe/invoice_item_test.rb +14 -14
- data/test/stripe/invoice_line_item_test.rb +1 -1
- data/test/stripe/invoice_test.rb +37 -37
- data/test/stripe/list_object_test.rb +60 -76
- data/test/stripe/login_link_test.rb +14 -14
- data/test/stripe/oauth_test.rb +42 -50
- data/test/stripe/order_return_test.rb +5 -5
- data/test/stripe/order_test.rb +12 -12
- data/test/stripe/payout_test.rb +9 -9
- data/test/stripe/plan_test.rb +9 -9
- data/test/stripe/product_test.rb +8 -8
- data/test/stripe/recipient_test.rb +9 -10
- data/test/stripe/refund_test.rb +9 -9
- data/test/stripe/reversal_test.rb +10 -10
- data/test/stripe/sku_test.rb +8 -8
- data/test/stripe/source_test.rb +14 -16
- data/test/stripe/stripe_client_test.rb +235 -266
- data/test/stripe/stripe_object_test.rb +163 -147
- data/test/stripe/stripe_response_test.rb +4 -3
- data/test/stripe/subscription_item_test.rb +11 -11
- data/test/stripe/subscription_test.rb +14 -14
- data/test/stripe/three_d_secure_test.rb +2 -2
- data/test/stripe/transfer_test.rb +8 -8
- data/test/stripe/util_test.rb +59 -57
- data/test/stripe/webhook_test.rb +18 -16
- data/test/stripe_test.rb +4 -4
- data/test/test_data.rb +26 -26
- data/test/test_helper.rb +29 -25
- metadata +6 -10
- data/test/stripe/bitcoin_receiver_test.rb +0 -67
- data/test/stripe/bitcoin_transaction_test.rb +0 -19
- data/test/stripe/recipient_card_test.rb +0 -44
data/lib/stripe/version.rb
CHANGED
data/lib/stripe/webhook.rb
CHANGED
@@ -19,18 +19,18 @@ module Stripe
|
|
19
19
|
end
|
20
20
|
|
21
21
|
module Signature
|
22
|
-
EXPECTED_SCHEME =
|
22
|
+
EXPECTED_SCHEME = "v1".freeze
|
23
23
|
|
24
24
|
def self.compute_signature(payload, secret)
|
25
|
-
OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new(
|
25
|
+
OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha256"), secret, payload)
|
26
26
|
end
|
27
27
|
private_class_method :compute_signature
|
28
28
|
|
29
29
|
# Extracts the timestamp and the signature(s) with the desired scheme
|
30
30
|
# from the header
|
31
31
|
def self.get_timestamp_and_signatures(header, scheme)
|
32
|
-
list_items = header.split(/,\s*/).map { |i| i.split(
|
33
|
-
timestamp = Integer(list_items.select { |i| i[0] ==
|
32
|
+
list_items = header.split(/,\s*/).map { |i| i.split("=", 2) }
|
33
|
+
timestamp = Integer(list_items.select { |i| i[0] == "t" }[0][1])
|
34
34
|
signatures = list_items.select { |i| i[0] == scheme }.map { |i| i[1] }
|
35
35
|
[timestamp, signatures]
|
36
36
|
end
|
@@ -49,30 +49,34 @@ module Stripe
|
|
49
49
|
def self.verify_header(payload, header, secret, tolerance: nil)
|
50
50
|
begin
|
51
51
|
timestamp, signatures = get_timestamp_and_signatures(header, EXPECTED_SCHEME)
|
52
|
-
rescue
|
52
|
+
rescue StandardError
|
53
53
|
raise SignatureVerificationError.new(
|
54
54
|
"Unable to extract timestamp and signatures from header",
|
55
|
-
header, http_body: payload
|
55
|
+
header, http_body: payload
|
56
|
+
)
|
56
57
|
end
|
57
58
|
|
58
59
|
if signatures.empty?
|
59
60
|
raise SignatureVerificationError.new(
|
60
61
|
"No signatures found with expected scheme #{EXPECTED_SCHEME}",
|
61
|
-
header, http_body: payload
|
62
|
+
header, http_body: payload
|
63
|
+
)
|
62
64
|
end
|
63
65
|
|
64
66
|
signed_payload = "#{timestamp}.#{payload}"
|
65
67
|
expected_sig = compute_signature(signed_payload, secret)
|
66
|
-
unless signatures.any? {|s| Util.secure_compare(expected_sig, s)}
|
68
|
+
unless signatures.any? { |s| Util.secure_compare(expected_sig, s) }
|
67
69
|
raise SignatureVerificationError.new(
|
68
70
|
"No signatures found matching the expected signature for payload",
|
69
|
-
header, http_body: payload
|
71
|
+
header, http_body: payload
|
72
|
+
)
|
70
73
|
end
|
71
74
|
|
72
75
|
if tolerance && timestamp < Time.now.to_f - tolerance
|
73
76
|
raise SignatureVerificationError.new(
|
74
77
|
"Timestamp outside the tolerance zone (#{Time.at(timestamp)})",
|
75
|
-
header, http_body: payload
|
78
|
+
header, http_body: payload
|
79
|
+
)
|
76
80
|
end
|
77
81
|
|
78
82
|
true
|
data/stripe.gemspec
CHANGED
@@ -1,22 +1,22 @@
|
|
1
|
-
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "lib"))
|
2
2
|
|
3
|
-
require
|
3
|
+
require "stripe/version"
|
4
4
|
|
5
|
-
|
6
|
-
s.name =
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "stripe"
|
7
7
|
s.version = Stripe::VERSION
|
8
|
-
s.required_ruby_version =
|
9
|
-
s.summary =
|
10
|
-
s.description =
|
11
|
-
s.author =
|
12
|
-
s.email =
|
13
|
-
s.homepage =
|
14
|
-
s.license =
|
8
|
+
s.required_ruby_version = ">= 2.0.0"
|
9
|
+
s.summary = "Ruby bindings for the Stripe API"
|
10
|
+
s.description = "Stripe is the easiest way to accept payments online. See https://stripe.com for details."
|
11
|
+
s.author = "Stripe"
|
12
|
+
s.email = "support@stripe.com"
|
13
|
+
s.homepage = "https://stripe.com/docs/api/ruby"
|
14
|
+
s.license = "MIT"
|
15
15
|
|
16
|
-
s.add_dependency(
|
16
|
+
s.add_dependency("faraday", "~> 0.10")
|
17
17
|
|
18
18
|
s.files = `git ls-files`.split("\n")
|
19
19
|
s.test_files = `git ls-files -- test/*`.split("\n")
|
20
|
-
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
21
|
-
s.require_paths = [
|
20
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
21
|
+
s.require_paths = ["lib"]
|
22
22
|
end
|
data/test/stripe/account_test.rb
CHANGED
@@ -1,62 +1,62 @@
|
|
1
|
-
require File.expand_path(
|
1
|
+
require File.expand_path("../../test_helper", __FILE__)
|
2
2
|
|
3
3
|
module Stripe
|
4
4
|
class AccountTest < Test::Unit::TestCase
|
5
5
|
should "be listable" do
|
6
6
|
accounts = Stripe::Account.list
|
7
7
|
assert_requested :get, "#{Stripe.api_base}/v1/accounts"
|
8
|
-
assert accounts.data.
|
9
|
-
assert accounts.data[0].
|
8
|
+
assert accounts.data.is_a?(Array)
|
9
|
+
assert accounts.data[0].is_a?(Stripe::Account)
|
10
10
|
end
|
11
11
|
|
12
12
|
should "be retrievable using singular endpoint" do
|
13
13
|
account = Stripe::Account.retrieve
|
14
14
|
assert_requested :get, "#{Stripe.api_base}/v1/account"
|
15
|
-
assert account.
|
15
|
+
assert account.is_a?(Stripe::Account)
|
16
16
|
end
|
17
17
|
|
18
18
|
should "be retrievable using plural endpoint" do
|
19
19
|
account = Stripe::Account.retrieve("acct_123")
|
20
20
|
assert_requested :get, "#{Stripe.api_base}/v1/accounts/acct_123"
|
21
|
-
assert account.
|
21
|
+
assert account.is_a?(Stripe::Account)
|
22
22
|
end
|
23
23
|
|
24
24
|
should "be rejectable" do
|
25
|
-
account_data = {:
|
26
|
-
stub_request(:get, "#{Stripe.api_base}/v1/accounts/acct_foo")
|
27
|
-
to_return(body: JSON.generate(account_data))
|
25
|
+
account_data = { id: "acct_foo" }
|
26
|
+
stub_request(:get, "#{Stripe.api_base}/v1/accounts/acct_foo")
|
27
|
+
.to_return(body: JSON.generate(account_data))
|
28
28
|
|
29
|
-
stub_request(:post, "#{Stripe.api_base}/v1/accounts/acct_foo/reject")
|
30
|
-
to_return(body: JSON.generate(account_data))
|
29
|
+
stub_request(:post, "#{Stripe.api_base}/v1/accounts/acct_foo/reject")
|
30
|
+
.to_return(body: JSON.generate(account_data))
|
31
31
|
|
32
|
-
account = Stripe::Account.retrieve(
|
33
|
-
account.reject(:
|
32
|
+
account = Stripe::Account.retrieve("acct_foo")
|
33
|
+
account.reject(reason: "fraud")
|
34
34
|
end
|
35
35
|
|
36
36
|
should "be creatable" do
|
37
|
-
account = Stripe::Account.create(:
|
37
|
+
account = Stripe::Account.create(metadata: {}, type: "standard")
|
38
38
|
assert_requested :post, "#{Stripe.api_base}/v1/accounts"
|
39
|
-
assert account.
|
39
|
+
assert account.is_a?(Stripe::Account)
|
40
40
|
end
|
41
41
|
|
42
42
|
should "be saveable" do
|
43
43
|
account = Stripe::Account.retrieve("acct_123")
|
44
|
-
account.metadata[
|
44
|
+
account.metadata["key"] = "value"
|
45
45
|
account.save
|
46
46
|
assert_requested :post, "#{Stripe.api_base}/v1/accounts/#{account.id}"
|
47
47
|
end
|
48
48
|
|
49
49
|
should "be updateable" do
|
50
|
-
account = Stripe::Account.update("acct_123", metadata: {foo:
|
50
|
+
account = Stripe::Account.update("acct_123", metadata: { foo: "bar" })
|
51
51
|
assert_requested :post, "#{Stripe.api_base}/v1/accounts/acct_123"
|
52
|
-
assert account.
|
52
|
+
assert account.is_a?(Stripe::Account)
|
53
53
|
end
|
54
54
|
|
55
55
|
should "be deletable" do
|
56
56
|
account = Stripe::Account.retrieve("acct_123")
|
57
57
|
account = account.delete
|
58
58
|
assert_requested :delete, "#{Stripe.api_base}/v1/accounts/#{account.id}"
|
59
|
-
assert account.
|
59
|
+
assert account.is_a?(Stripe::Account)
|
60
60
|
end
|
61
61
|
|
62
62
|
context "#bank_account=" do
|
@@ -66,8 +66,8 @@ module Stripe
|
|
66
66
|
begin
|
67
67
|
account = Stripe::Account.retrieve("acct_123")
|
68
68
|
account.bank_account = "tok_123"
|
69
|
-
message = "NOTE: Stripe::Account#bank_account= is "
|
70
|
-
|
69
|
+
message = "NOTE: Stripe::Account#bank_account= is " \
|
70
|
+
"deprecated; use #external_account= instead"
|
71
71
|
assert_match Regexp.new(message), $stderr.string
|
72
72
|
ensure
|
73
73
|
$stderr = old_stderr
|
@@ -81,93 +81,85 @@ module Stripe
|
|
81
81
|
|
82
82
|
# Unfortunately, the OpenAPI spec doesn't yet cover anything under the
|
83
83
|
# Connect endpoints, so for just stub this out with Webmock.
|
84
|
-
stub_request(:post, "#{Stripe.connect_base}/oauth/deauthorize")
|
85
|
-
with(body: {
|
86
|
-
to_return(body: JSON.generate(
|
87
|
-
account.deauthorize(
|
84
|
+
stub_request(:post, "#{Stripe.connect_base}/oauth/deauthorize")
|
85
|
+
.with(body: { "client_id" => "ca_1234", "stripe_user_id" => account.id })
|
86
|
+
.to_return(body: JSON.generate("stripe_user_id" => account.id))
|
87
|
+
account.deauthorize("ca_1234", "sk_test_1234")
|
88
88
|
end
|
89
89
|
end
|
90
90
|
|
91
91
|
context "#legal_entity=" do
|
92
|
-
should
|
92
|
+
should "disallow direct overrides" do
|
93
93
|
account = Stripe::Account.retrieve("acct_123")
|
94
94
|
|
95
95
|
assert_raise NoMethodError do
|
96
|
-
account.legal_entity = {:
|
96
|
+
account.legal_entity = { first_name: "Blah" }
|
97
97
|
end
|
98
98
|
|
99
|
-
account.legal_entity.first_name =
|
99
|
+
account.legal_entity.first_name = "Blah"
|
100
100
|
end
|
101
101
|
end
|
102
102
|
|
103
103
|
context "#serialize_params" do
|
104
104
|
should "serialize an a new additional_owners" do
|
105
105
|
obj = Stripe::Util.convert_to_stripe_object({
|
106
|
-
:
|
107
|
-
:
|
106
|
+
object: "account",
|
107
|
+
legal_entity: Stripe::StripeObject.construct_from({
|
108
108
|
}),
|
109
109
|
}, {})
|
110
110
|
obj.legal_entity.additional_owners = [
|
111
|
-
{ :
|
112
|
-
{ :
|
111
|
+
{ first_name: "Joe" },
|
112
|
+
{ first_name: "Jane" },
|
113
113
|
]
|
114
114
|
|
115
115
|
expected = {
|
116
|
-
:
|
117
|
-
:
|
118
|
-
"0" => { :
|
119
|
-
"1" => { :
|
120
|
-
}
|
121
|
-
}
|
116
|
+
legal_entity: {
|
117
|
+
additional_owners: {
|
118
|
+
"0" => { first_name: "Joe" },
|
119
|
+
"1" => { first_name: "Jane" },
|
120
|
+
},
|
121
|
+
},
|
122
122
|
}
|
123
123
|
assert_equal(expected, obj.serialize_params)
|
124
124
|
end
|
125
125
|
|
126
126
|
should "serialize on an partially changed additional_owners" do
|
127
127
|
obj = Stripe::Util.convert_to_stripe_object({
|
128
|
-
:
|
129
|
-
:
|
130
|
-
:
|
131
|
-
Stripe::StripeObject.construct_from(
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
:first_name => "Jane"
|
136
|
-
}),
|
137
|
-
]
|
138
|
-
}
|
128
|
+
object: "account",
|
129
|
+
legal_entity: {
|
130
|
+
additional_owners: [
|
131
|
+
Stripe::StripeObject.construct_from(first_name: "Joe"),
|
132
|
+
Stripe::StripeObject.construct_from(first_name: "Jane"),
|
133
|
+
],
|
134
|
+
},
|
139
135
|
}, {})
|
140
136
|
obj.legal_entity.additional_owners[1].first_name = "Stripe"
|
141
137
|
|
142
138
|
expected = {
|
143
|
-
:
|
144
|
-
:
|
145
|
-
"1" => { :
|
146
|
-
}
|
147
|
-
}
|
139
|
+
legal_entity: {
|
140
|
+
additional_owners: {
|
141
|
+
"1" => { first_name: "Stripe" },
|
142
|
+
},
|
143
|
+
},
|
148
144
|
}
|
149
145
|
assert_equal(expected, obj.serialize_params)
|
150
146
|
end
|
151
147
|
|
152
148
|
should "serialize on an unchanged additional_owners" do
|
153
149
|
obj = Stripe::Util.convert_to_stripe_object({
|
154
|
-
:
|
155
|
-
:
|
156
|
-
:
|
157
|
-
Stripe::StripeObject.construct_from(
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
:first_name => "Jane"
|
162
|
-
}),
|
163
|
-
]
|
164
|
-
}
|
150
|
+
object: "account",
|
151
|
+
legal_entity: {
|
152
|
+
additional_owners: [
|
153
|
+
Stripe::StripeObject.construct_from(first_name: "Joe"),
|
154
|
+
Stripe::StripeObject.construct_from(first_name: "Jane"),
|
155
|
+
],
|
156
|
+
},
|
165
157
|
}, {})
|
166
158
|
|
167
159
|
expected = {
|
168
|
-
:
|
169
|
-
:
|
170
|
-
}
|
160
|
+
legal_entity: {
|
161
|
+
additional_owners: {},
|
162
|
+
},
|
171
163
|
}
|
172
164
|
assert_equal(expected, obj.serialize_params)
|
173
165
|
end
|
@@ -176,24 +168,20 @@ module Stripe
|
|
176
168
|
# meaning for the server, which interprets it as an array unset.
|
177
169
|
should "serialize on an unset additional_owners" do
|
178
170
|
obj = Stripe::Util.convert_to_stripe_object({
|
179
|
-
:
|
180
|
-
:
|
181
|
-
:
|
182
|
-
Stripe::StripeObject.construct_from(
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
:first_name => "Jane"
|
187
|
-
}),
|
188
|
-
]
|
189
|
-
}
|
171
|
+
object: "account",
|
172
|
+
legal_entity: {
|
173
|
+
additional_owners: [
|
174
|
+
Stripe::StripeObject.construct_from(first_name: "Joe"),
|
175
|
+
Stripe::StripeObject.construct_from(first_name: "Jane"),
|
176
|
+
],
|
177
|
+
},
|
190
178
|
}, {})
|
191
179
|
obj.legal_entity.additional_owners = nil
|
192
180
|
|
193
181
|
expected = {
|
194
|
-
:
|
195
|
-
:
|
196
|
-
}
|
182
|
+
legal_entity: {
|
183
|
+
additional_owners: "",
|
184
|
+
},
|
197
185
|
}
|
198
186
|
assert_equal(expected, obj.serialize_params)
|
199
187
|
end
|
@@ -1,7 +1,25 @@
|
|
1
|
-
require File.expand_path(
|
1
|
+
require File.expand_path("../../test_helper", __FILE__)
|
2
2
|
|
3
3
|
module Stripe
|
4
4
|
class AlipayAccountTest < Test::Unit::TestCase
|
5
|
+
context "#resource_url" do
|
6
|
+
should "return a resource URL" do
|
7
|
+
alipay_account = Stripe::AlipayAccount.construct_from(
|
8
|
+
id: "aliacc_123",
|
9
|
+
customer: "cus_123"
|
10
|
+
)
|
11
|
+
assert_equal "/v1/customers/cus_123/sources/aliacc_123",
|
12
|
+
alipay_account.resource_url
|
13
|
+
end
|
14
|
+
|
15
|
+
should "raise without a customer" do
|
16
|
+
alipay_account = Stripe::AlipayAccount.construct_from(id: "aliacc_123")
|
17
|
+
assert_raises NotImplementedError do
|
18
|
+
alipay_account.resource_url
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
5
23
|
should "raise on #retrieve" do
|
6
24
|
assert_raises NotImplementedError do
|
7
25
|
Stripe::AlipayAccount.retrieve("aliacc_123")
|
@@ -1,5 +1,5 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
|
-
require File.expand_path(
|
2
|
+
require File.expand_path("../../test_helper", __FILE__)
|
3
3
|
|
4
4
|
module Stripe
|
5
5
|
class ApiOperationsTest < Test::Unit::TestCase
|
@@ -13,16 +13,16 @@ module Stripe
|
|
13
13
|
|
14
14
|
context ".update" do
|
15
15
|
should "post the correct parameters to the resource URL" do
|
16
|
-
stub_request(:post, "#{Stripe.api_base}/v1/updateableresources/id")
|
17
|
-
with(body: { foo: "bar" })
|
18
|
-
to_return(body: JSON.generate(
|
19
|
-
resource = UpdateableResource
|
20
|
-
assert_equal(
|
16
|
+
stub_request(:post, "#{Stripe.api_base}/v1/updateableresources/id")
|
17
|
+
.with(body: { foo: "bar" })
|
18
|
+
.to_return(body: JSON.generate(foo: "bar"))
|
19
|
+
resource = UpdateableResource.update("id", foo: "bar")
|
20
|
+
assert_equal("bar", resource.foo)
|
21
21
|
end
|
22
22
|
|
23
23
|
should "error on protected fields" do
|
24
24
|
e = assert_raises do
|
25
|
-
UpdateableResource
|
25
|
+
UpdateableResource.update("id", protected: "bar")
|
26
26
|
end
|
27
27
|
assert_equal "Cannot update protected field: protected", e.message
|
28
28
|
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
|
-
require File.expand_path(
|
2
|
+
require File.expand_path("../../test_helper", __FILE__)
|
3
3
|
|
4
4
|
module Stripe
|
5
5
|
class ApiResourceTest < Test::Unit::TestCase
|
@@ -17,7 +17,7 @@ module Stripe
|
|
17
17
|
should "set a flag if given an object source" do
|
18
18
|
r = NestedTestAPIResource.new("test_resource")
|
19
19
|
r.external_account = {
|
20
|
-
:
|
20
|
+
object: "card",
|
21
21
|
}
|
22
22
|
assert_equal true, r.external_account.save_with_parent
|
23
23
|
end
|
@@ -29,17 +29,15 @@ module Stripe
|
|
29
29
|
end
|
30
30
|
|
31
31
|
should "creating a new APIResource from a hash should not fetch over the network" do
|
32
|
-
Stripe::Customer.construct_from(
|
33
|
-
|
34
|
-
|
35
|
-
:object => "customer"
|
36
|
-
})
|
32
|
+
Stripe::Customer.construct_from(id: "somecustomer",
|
33
|
+
card: { id: "somecard", object: "card" },
|
34
|
+
object: "customer")
|
37
35
|
assert_not_requested :get, %r{#{Stripe.api_base}/.*}
|
38
36
|
end
|
39
37
|
|
40
38
|
should "setting an attribute should not cause a network request" do
|
41
|
-
c = Stripe::Customer.new("cus_123")
|
42
|
-
c.card = {:
|
39
|
+
c = Stripe::Customer.new("cus_123")
|
40
|
+
c.card = { id: "somecard", object: "card" }
|
43
41
|
assert_not_requested :get, %r{#{Stripe.api_base}/.*}
|
44
42
|
assert_not_requested :post, %r{#{Stripe.api_base}/.*}
|
45
43
|
end
|
@@ -62,7 +60,7 @@ module Stripe
|
|
62
60
|
Stripe::Customer.list({}, nil)
|
63
61
|
end
|
64
62
|
assert_raises TypeError do
|
65
|
-
Stripe::Customer.list({},
|
63
|
+
Stripe::Customer.list({}, api_key: nil)
|
66
64
|
end
|
67
65
|
end
|
68
66
|
|
@@ -74,43 +72,43 @@ module Stripe
|
|
74
72
|
end
|
75
73
|
|
76
74
|
should "send expand on fetch properly" do
|
77
|
-
stub_request(:get, "#{Stripe.api_base}/v1/charges/ch_123")
|
78
|
-
with(query: { "expand" => ["customer"] })
|
79
|
-
to_return(body: JSON.generate(charge_fixture))
|
75
|
+
stub_request(:get, "#{Stripe.api_base}/v1/charges/ch_123")
|
76
|
+
.with(query: { "expand" => ["customer"] })
|
77
|
+
.to_return(body: JSON.generate(charge_fixture))
|
80
78
|
|
81
|
-
Stripe::Charge.retrieve(
|
79
|
+
Stripe::Charge.retrieve(id: "ch_123", expand: [:customer])
|
82
80
|
end
|
83
81
|
|
84
82
|
should "preserve expand across refreshes" do
|
85
|
-
stub_request(:get, "#{Stripe.api_base}/v1/charges/ch_123")
|
86
|
-
with(query: { "expand" => ["customer"] })
|
87
|
-
to_return(body: JSON.generate(charge_fixture))
|
83
|
+
stub_request(:get, "#{Stripe.api_base}/v1/charges/ch_123")
|
84
|
+
.with(query: { "expand" => ["customer"] })
|
85
|
+
.to_return(body: JSON.generate(charge_fixture))
|
88
86
|
|
89
|
-
ch = Stripe::Charge.retrieve(
|
87
|
+
ch = Stripe::Charge.retrieve(id: "ch_123", expand: :customer)
|
90
88
|
ch.refresh
|
91
89
|
end
|
92
90
|
|
93
91
|
should "send expand when fetching through ListObject" do
|
94
|
-
stub_request(:get, "#{Stripe.api_base}/v1/customers/cus_123")
|
95
|
-
to_return(body: JSON.generate(customer_fixture))
|
92
|
+
stub_request(:get, "#{Stripe.api_base}/v1/customers/cus_123")
|
93
|
+
.to_return(body: JSON.generate(customer_fixture))
|
96
94
|
|
97
|
-
stub_request(:get, "#{Stripe.api_base}/v1/customers/cus_123/sources/cc_test_card")
|
98
|
-
with(query: { "expand" => ["customer"] })
|
99
|
-
to_return(body: JSON.generate(customer_fixture))
|
95
|
+
stub_request(:get, "#{Stripe.api_base}/v1/customers/cus_123/sources/cc_test_card")
|
96
|
+
.with(query: { "expand" => ["customer"] })
|
97
|
+
.to_return(body: JSON.generate(customer_fixture))
|
100
98
|
|
101
|
-
customer = Stripe::Customer.retrieve(
|
102
|
-
customer.sources.retrieve(
|
99
|
+
customer = Stripe::Customer.retrieve("cus_123")
|
100
|
+
customer.sources.retrieve(id: "cc_test_card", expand: :customer)
|
103
101
|
end
|
104
102
|
|
105
103
|
context "when specifying per-object credentials" do
|
106
104
|
context "with no global API key set" do
|
107
105
|
should "use the per-object credential when creating" do
|
108
|
-
stub_request(:post, "#{Stripe.api_base}/v1/charges")
|
109
|
-
with(headers: {"Authorization" => "Bearer sk_test_local"})
|
110
|
-
to_return(body: JSON.generate(charge_fixture))
|
106
|
+
stub_request(:post, "#{Stripe.api_base}/v1/charges")
|
107
|
+
.with(headers: { "Authorization" => "Bearer sk_test_local" })
|
108
|
+
.to_return(body: JSON.generate(charge_fixture))
|
111
109
|
|
112
|
-
Stripe::Charge.create({:
|
113
|
-
|
110
|
+
Stripe::Charge.create({ source: "tok_visa" },
|
111
|
+
"sk_test_local")
|
114
112
|
end
|
115
113
|
end
|
116
114
|
|
@@ -124,23 +122,23 @@ module Stripe
|
|
124
122
|
end
|
125
123
|
|
126
124
|
should "use the per-object credential when creating" do
|
127
|
-
stub_request(:post, "#{Stripe.api_base}/v1/charges")
|
128
|
-
with(headers: {"Authorization" => "Bearer sk_test_local"})
|
129
|
-
to_return(body: JSON.generate(charge_fixture))
|
125
|
+
stub_request(:post, "#{Stripe.api_base}/v1/charges")
|
126
|
+
.with(headers: { "Authorization" => "Bearer sk_test_local" })
|
127
|
+
.to_return(body: JSON.generate(charge_fixture))
|
130
128
|
|
131
|
-
Stripe::Charge.create({:
|
132
|
-
|
129
|
+
Stripe::Charge.create({ source: "tok_visa" },
|
130
|
+
"sk_test_local")
|
133
131
|
end
|
134
132
|
|
135
133
|
should "use the per-object credential when retrieving and making other calls" do
|
136
|
-
stub_request(:get, "#{Stripe.api_base}/v1/charges/ch_123")
|
137
|
-
with(headers: {"Authorization" => "Bearer sk_test_local"})
|
138
|
-
to_return(body: JSON.generate(charge_fixture))
|
139
|
-
stub_request(:post, "#{Stripe.api_base}/v1/charges/ch_123/refunds")
|
140
|
-
with(headers: {"Authorization" => "Bearer sk_test_local"})
|
141
|
-
to_return(body: "{}")
|
142
|
-
|
143
|
-
ch = Stripe::Charge.retrieve(
|
134
|
+
stub_request(:get, "#{Stripe.api_base}/v1/charges/ch_123")
|
135
|
+
.with(headers: { "Authorization" => "Bearer sk_test_local" })
|
136
|
+
.to_return(body: JSON.generate(charge_fixture))
|
137
|
+
stub_request(:post, "#{Stripe.api_base}/v1/charges/ch_123/refunds")
|
138
|
+
.with(headers: { "Authorization" => "Bearer sk_test_local" })
|
139
|
+
.to_return(body: "{}")
|
140
|
+
|
141
|
+
ch = Stripe::Charge.retrieve("ch_123", "sk_test_local")
|
144
142
|
ch.refunds.create
|
145
143
|
end
|
146
144
|
end
|
@@ -148,52 +146,46 @@ module Stripe
|
|
148
146
|
|
149
147
|
context "with valid credentials" do
|
150
148
|
should "urlencode values in GET params" do
|
151
|
-
stub_request(:get, "#{Stripe.api_base}/v1/charges")
|
152
|
-
with(query: { customer: "test customer" })
|
153
|
-
to_return(body: JSON.generate(
|
154
|
-
|
155
|
-
|
156
|
-
charges = Stripe::Charge.list(:customer => 'test customer').data
|
157
|
-
assert charges.kind_of? Array
|
149
|
+
stub_request(:get, "#{Stripe.api_base}/v1/charges")
|
150
|
+
.with(query: { customer: "test customer" })
|
151
|
+
.to_return(body: JSON.generate(data: [charge_fixture]))
|
152
|
+
charges = Stripe::Charge.list(customer: "test customer").data
|
153
|
+
assert charges.is_a? Array
|
158
154
|
end
|
159
155
|
|
160
156
|
should "construct URL properly with base query parameters" do
|
161
|
-
stub_request(:get, "#{Stripe.api_base}/v1/charges")
|
162
|
-
with(query: { customer: "cus_123" })
|
163
|
-
to_return(body: JSON.generate(
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
data: [charge_fixture],
|
173
|
-
url: "/v1/charges"
|
174
|
-
}))
|
175
|
-
charges.list(:created => 123)
|
157
|
+
stub_request(:get, "#{Stripe.api_base}/v1/charges")
|
158
|
+
.with(query: { customer: "cus_123" })
|
159
|
+
.to_return(body: JSON.generate(data: [charge_fixture],
|
160
|
+
url: "/v1/charges"))
|
161
|
+
charges = Stripe::Invoice.list(customer: "cus_123")
|
162
|
+
|
163
|
+
stub_request(:get, "#{Stripe.api_base}/v1/charges")
|
164
|
+
.with(query: { customer: "cus_123", created: "123" })
|
165
|
+
.to_return(body: JSON.generate(data: [charge_fixture],
|
166
|
+
url: "/v1/charges"))
|
167
|
+
charges.list(created: 123)
|
176
168
|
end
|
177
169
|
|
178
170
|
should "setting a nil value for a param should exclude that param from the request" do
|
179
|
-
stub_request(:get, "#{Stripe.api_base}/v1/charges")
|
180
|
-
with(query: { offset: 5, sad: false })
|
181
|
-
to_return(body: JSON.generate(
|
182
|
-
Stripe::Charge.list(:
|
171
|
+
stub_request(:get, "#{Stripe.api_base}/v1/charges")
|
172
|
+
.with(query: { offset: 5, sad: false })
|
173
|
+
.to_return(body: JSON.generate(count: 1, data: [charge_fixture]))
|
174
|
+
Stripe::Charge.list(count: nil, offset: 5, sad: false)
|
183
175
|
|
184
|
-
stub_request(:post, "#{Stripe.api_base}/v1/charges")
|
185
|
-
with(body: {
|
186
|
-
to_return(body: JSON.generate(
|
187
|
-
Stripe::Charge.create(:
|
176
|
+
stub_request(:post, "#{Stripe.api_base}/v1/charges")
|
177
|
+
.with(body: { "amount" => "50", "currency" => "usd" })
|
178
|
+
.to_return(body: JSON.generate(count: 1, data: [charge_fixture]))
|
179
|
+
Stripe::Charge.create(amount: 50, currency: "usd", card: { number: nil })
|
188
180
|
end
|
189
181
|
|
190
182
|
should "not trigger a warning if a known opt, such as idempotency_key, is in opts" do
|
191
|
-
stub_request(:post, "#{Stripe.api_base}/v1/charges")
|
192
|
-
to_return(body: JSON.generate(charge_fixture))
|
183
|
+
stub_request(:post, "#{Stripe.api_base}/v1/charges")
|
184
|
+
.to_return(body: JSON.generate(charge_fixture))
|
193
185
|
old_stderr = $stderr
|
194
186
|
$stderr = StringIO.new
|
195
187
|
begin
|
196
|
-
Stripe::Charge.create({ :
|
188
|
+
Stripe::Charge.create({ amount: 100, currency: "usd", card: "sc_token" }, idempotency_key: "12345")
|
197
189
|
assert $stderr.string.empty?
|
198
190
|
ensure
|
199
191
|
$stderr = old_stderr
|
@@ -201,21 +193,21 @@ module Stripe
|
|
201
193
|
end
|
202
194
|
|
203
195
|
should "trigger a warning if a known opt, such as idempotency_key, is in params" do
|
204
|
-
stub_request(:post, "#{Stripe.api_base}/v1/charges")
|
205
|
-
to_return(body: JSON.generate(charge_fixture))
|
196
|
+
stub_request(:post, "#{Stripe.api_base}/v1/charges")
|
197
|
+
.to_return(body: JSON.generate(charge_fixture))
|
206
198
|
old_stderr = $stderr
|
207
199
|
$stderr = StringIO.new
|
208
200
|
begin
|
209
|
-
Stripe::Charge.create(
|
210
|
-
assert_match Regexp.new(
|
201
|
+
Stripe::Charge.create(amount: 100, currency: "usd", card: "sc_token", idempotency_key: "12345")
|
202
|
+
assert_match Regexp.new("WARNING:"), $stderr.string
|
211
203
|
ensure
|
212
204
|
$stderr = old_stderr
|
213
205
|
end
|
214
206
|
end
|
215
207
|
|
216
208
|
should "requesting with a unicode ID should result in a request" do
|
217
|
-
stub_request(:get, "#{Stripe.api_base}/v1/customers/%E2%98%83")
|
218
|
-
to_return(body: JSON.generate(make_missing_id_error), status: 404)
|
209
|
+
stub_request(:get, "#{Stripe.api_base}/v1/customers/%E2%98%83")
|
210
|
+
.to_return(body: JSON.generate(make_missing_id_error), status: 404)
|
219
211
|
c = Stripe::Customer.new("☃")
|
220
212
|
assert_raises(Stripe::InvalidRequestError) { c.refresh }
|
221
213
|
end
|
@@ -226,58 +218,58 @@ module Stripe
|
|
226
218
|
end
|
227
219
|
|
228
220
|
should "making a GET request with parameters should have a query string and no body" do
|
229
|
-
stub_request(:get, "#{Stripe.api_base}/v1/charges")
|
230
|
-
with(query: { limit: 1 })
|
231
|
-
to_return(body: JSON.generate(
|
232
|
-
Stripe::Charge.list(
|
221
|
+
stub_request(:get, "#{Stripe.api_base}/v1/charges")
|
222
|
+
.with(query: { limit: 1 })
|
223
|
+
.to_return(body: JSON.generate(data: [charge_fixture]))
|
224
|
+
Stripe::Charge.list(limit: 1)
|
233
225
|
end
|
234
226
|
|
235
227
|
should "making a POST request with parameters should have a body and no query string" do
|
236
|
-
stub_request(:post, "#{Stripe.api_base}/v1/charges")
|
237
|
-
with(body: {
|
238
|
-
to_return(body: JSON.generate(charge_fixture))
|
239
|
-
Stripe::Charge.create(
|
228
|
+
stub_request(:post, "#{Stripe.api_base}/v1/charges")
|
229
|
+
.with(body: { "amount" => "100", "currency" => "usd", "card" => "sc_token" })
|
230
|
+
.to_return(body: JSON.generate(charge_fixture))
|
231
|
+
Stripe::Charge.create(amount: 100, currency: "usd", card: "sc_token")
|
240
232
|
end
|
241
233
|
|
242
234
|
should "loading an object should issue a GET request" do
|
243
|
-
stub_request(:get, "#{Stripe.api_base}/v1/charges/ch_123")
|
244
|
-
to_return(body: JSON.generate(charge_fixture))
|
235
|
+
stub_request(:get, "#{Stripe.api_base}/v1/charges/ch_123")
|
236
|
+
.to_return(body: JSON.generate(charge_fixture))
|
245
237
|
c = Stripe::Charge.new("ch_123")
|
246
238
|
c.refresh
|
247
239
|
end
|
248
240
|
|
249
241
|
should "using array accessors should be the same as the method interface" do
|
250
|
-
stub_request(:get, "#{Stripe.api_base}/v1/charges/ch_123")
|
251
|
-
to_return(body: JSON.generate(charge_fixture))
|
242
|
+
stub_request(:get, "#{Stripe.api_base}/v1/charges/ch_123")
|
243
|
+
.to_return(body: JSON.generate(charge_fixture))
|
252
244
|
c = Stripe::Charge.new("cus_123")
|
253
245
|
c.refresh
|
254
246
|
assert_equal c.created, c[:created]
|
255
|
-
assert_equal c.created, c[
|
256
|
-
c[
|
257
|
-
assert_equal c.created,
|
247
|
+
assert_equal c.created, c["created"]
|
248
|
+
c["created"] = 12_345
|
249
|
+
assert_equal c.created, 12_345
|
258
250
|
end
|
259
251
|
|
260
252
|
should "accessing a property other than id or parent on an unfetched object should fetch it" do
|
261
|
-
stub_request(:get, "#{Stripe.api_base}/v1/charges")
|
262
|
-
with(query: { customer: "cus_123" })
|
263
|
-
to_return(body: JSON.generate(customer_fixture))
|
253
|
+
stub_request(:get, "#{Stripe.api_base}/v1/charges")
|
254
|
+
.with(query: { customer: "cus_123" })
|
255
|
+
.to_return(body: JSON.generate(customer_fixture))
|
264
256
|
c = Stripe::Customer.new("cus_123")
|
265
257
|
c.charges
|
266
258
|
end
|
267
259
|
|
268
260
|
should "updating an object should issue a POST request with only the changed properties" do
|
269
|
-
stub_request(:post, "#{Stripe.api_base}/v1/customers/cus_123")
|
270
|
-
with(body: {
|
271
|
-
to_return(body: JSON.generate(customer_fixture))
|
261
|
+
stub_request(:post, "#{Stripe.api_base}/v1/customers/cus_123")
|
262
|
+
.with(body: { "description" => "another_mn" })
|
263
|
+
.to_return(body: JSON.generate(customer_fixture))
|
272
264
|
c = Stripe::Customer.construct_from(customer_fixture)
|
273
265
|
c.description = "another_mn"
|
274
266
|
c.save
|
275
267
|
end
|
276
268
|
|
277
269
|
should "updating should merge in returned properties" do
|
278
|
-
stub_request(:post, "#{Stripe.api_base}/v1/customers/cus_123")
|
279
|
-
with(body: {
|
280
|
-
to_return(body: JSON.generate(customer_fixture))
|
270
|
+
stub_request(:post, "#{Stripe.api_base}/v1/customers/cus_123")
|
271
|
+
.with(body: { "description" => "another_mn" })
|
272
|
+
.to_return(body: JSON.generate(customer_fixture))
|
281
273
|
c = Stripe::Customer.new("cus_123")
|
282
274
|
c.description = "another_mn"
|
283
275
|
c.save
|
@@ -287,245 +279,221 @@ module Stripe
|
|
287
279
|
should "updating should fail if api_key is overwritten with nil" do
|
288
280
|
c = Stripe::Customer.new
|
289
281
|
assert_raises TypeError do
|
290
|
-
c.save({},
|
282
|
+
c.save({}, api_key: nil)
|
291
283
|
end
|
292
284
|
end
|
293
285
|
|
294
286
|
should "updating should use the supplied api_key" do
|
295
|
-
stub_request(:post, "#{Stripe.api_base}/v1/customers")
|
296
|
-
with(headers: {"Authorization" => "Bearer sk_test_local"})
|
297
|
-
to_return(body: JSON.generate(customer_fixture))
|
287
|
+
stub_request(:post, "#{Stripe.api_base}/v1/customers")
|
288
|
+
.with(headers: { "Authorization" => "Bearer sk_test_local" })
|
289
|
+
.to_return(body: JSON.generate(customer_fixture))
|
298
290
|
c = Stripe::Customer.new
|
299
|
-
c.save({},
|
291
|
+
c.save({}, api_key: "sk_test_local")
|
300
292
|
assert_equal false, c.livemode
|
301
293
|
end
|
302
294
|
|
303
295
|
should "deleting should send no props and result in an object that has no props other deleted" do
|
304
|
-
stub_request(:delete, "#{Stripe.api_base}/v1/customers/cus_123")
|
305
|
-
to_return(body: JSON.generate(
|
296
|
+
stub_request(:delete, "#{Stripe.api_base}/v1/customers/cus_123")
|
297
|
+
.to_return(body: JSON.generate("id" => "cus_123", "deleted" => true))
|
306
298
|
c = Stripe::Customer.construct_from(customer_fixture)
|
307
299
|
c.delete
|
308
300
|
end
|
309
301
|
|
310
302
|
should "loading all of an APIResource should return an array of recursively instantiated objects" do
|
311
|
-
stub_request(:get, "#{Stripe.api_base}/v1/charges")
|
312
|
-
to_return(body: JSON.generate(
|
313
|
-
data: [charge_fixture]
|
314
|
-
}))
|
303
|
+
stub_request(:get, "#{Stripe.api_base}/v1/charges")
|
304
|
+
.to_return(body: JSON.generate(data: [charge_fixture]))
|
315
305
|
charges = Stripe::Charge.list.data
|
316
|
-
assert charges.
|
317
|
-
assert charges[0].
|
318
|
-
assert charges[0].source.
|
306
|
+
assert charges.is_a? Array
|
307
|
+
assert charges[0].is_a? Stripe::Charge
|
308
|
+
assert charges[0].source.is_a?(Stripe::StripeObject)
|
319
309
|
end
|
320
310
|
|
321
311
|
should "passing in a stripe_account header should pass it through on call" do
|
322
|
-
stub_request(:get, "#{Stripe.api_base}/v1/customers/cus_123")
|
323
|
-
with(headers: {"Stripe-Account" => "acct_123"})
|
324
|
-
to_return(body: JSON.generate(customer_fixture))
|
325
|
-
Stripe::Customer.retrieve("cus_123",
|
312
|
+
stub_request(:get, "#{Stripe.api_base}/v1/customers/cus_123")
|
313
|
+
.with(headers: { "Stripe-Account" => "acct_123" })
|
314
|
+
.to_return(body: JSON.generate(customer_fixture))
|
315
|
+
Stripe::Customer.retrieve("cus_123", stripe_account: "acct_123")
|
326
316
|
end
|
327
317
|
|
328
318
|
should "passing in a stripe_account header should pass it through on save" do
|
329
|
-
stub_request(:get, "#{Stripe.api_base}/v1/customers/cus_123")
|
330
|
-
with(headers: {"Stripe-Account" => "acct_123"})
|
331
|
-
to_return(body: JSON.generate(customer_fixture))
|
332
|
-
c = Stripe::Customer.retrieve("cus_123",
|
333
|
-
|
334
|
-
stub_request(:post, "#{Stripe.api_base}/v1/customers/cus_123")
|
335
|
-
with(headers: {"Stripe-Account" => "acct_123"})
|
336
|
-
to_return(body: JSON.generate(customer_fixture))
|
337
|
-
c.description =
|
319
|
+
stub_request(:get, "#{Stripe.api_base}/v1/customers/cus_123")
|
320
|
+
.with(headers: { "Stripe-Account" => "acct_123" })
|
321
|
+
.to_return(body: JSON.generate(customer_fixture))
|
322
|
+
c = Stripe::Customer.retrieve("cus_123", stripe_account: "acct_123")
|
323
|
+
|
324
|
+
stub_request(:post, "#{Stripe.api_base}/v1/customers/cus_123")
|
325
|
+
.with(headers: { "Stripe-Account" => "acct_123" })
|
326
|
+
.to_return(body: JSON.generate(customer_fixture))
|
327
|
+
c.description = "FOO"
|
338
328
|
c.save
|
339
329
|
end
|
340
330
|
|
341
|
-
should
|
342
|
-
acct = Stripe::Account.construct_from(
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
}
|
349
|
-
})
|
331
|
+
should "add key to nested objects" do
|
332
|
+
acct = Stripe::Account.construct_from(id: "myid",
|
333
|
+
legal_entity: {
|
334
|
+
size: "l",
|
335
|
+
score: 4,
|
336
|
+
height: 10,
|
337
|
+
})
|
350
338
|
|
351
|
-
stub_request(:post, "#{Stripe.api_base}/v1/accounts/myid")
|
352
|
-
with(body: { legal_entity: { first_name: "Bob" } })
|
353
|
-
to_return(body: JSON.generate(
|
339
|
+
stub_request(:post, "#{Stripe.api_base}/v1/accounts/myid")
|
340
|
+
.with(body: { legal_entity: { first_name: "Bob" } })
|
341
|
+
.to_return(body: JSON.generate("id" => "myid"))
|
354
342
|
|
355
|
-
acct.legal_entity.first_name =
|
343
|
+
acct.legal_entity.first_name = "Bob"
|
356
344
|
acct.save
|
357
345
|
end
|
358
346
|
|
359
|
-
should
|
360
|
-
acct = Stripe::Account.construct_from(
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
}
|
365
|
-
})
|
347
|
+
should "save nothing if nothing changes" do
|
348
|
+
acct = Stripe::Account.construct_from(id: "acct_id",
|
349
|
+
metadata: {
|
350
|
+
key: "value",
|
351
|
+
})
|
366
352
|
|
367
|
-
stub_request(:post, "#{Stripe.api_base}/v1/accounts/acct_id")
|
368
|
-
with(body: {})
|
369
|
-
to_return(body: JSON.generate(
|
353
|
+
stub_request(:post, "#{Stripe.api_base}/v1/accounts/acct_id")
|
354
|
+
.with(body: {})
|
355
|
+
.to_return(body: JSON.generate("id" => "acct_id"))
|
370
356
|
|
371
357
|
acct.save
|
372
358
|
end
|
373
359
|
|
374
|
-
should
|
375
|
-
ch = Stripe::Charge.construct_from(
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
}
|
381
|
-
})
|
360
|
+
should "not save nested API resources" do
|
361
|
+
ch = Stripe::Charge.construct_from(id: "ch_id",
|
362
|
+
customer: {
|
363
|
+
object: "customer",
|
364
|
+
id: "customer_id",
|
365
|
+
})
|
382
366
|
|
383
|
-
stub_request(:post, "#{Stripe.api_base}/v1/charges/ch_id")
|
384
|
-
with(body: {})
|
385
|
-
to_return(body: JSON.generate(
|
367
|
+
stub_request(:post, "#{Stripe.api_base}/v1/charges/ch_id")
|
368
|
+
.with(body: {})
|
369
|
+
.to_return(body: JSON.generate("id" => "ch_id"))
|
386
370
|
|
387
|
-
ch.customer.description =
|
371
|
+
ch.customer.description = "Bob"
|
388
372
|
ch.save
|
389
373
|
end
|
390
374
|
|
391
|
-
should
|
392
|
-
acct = Stripe::Account.construct_from(
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
}
|
401
|
-
})
|
375
|
+
should "correctly handle replaced nested objects" do
|
376
|
+
acct = Stripe::Account.construct_from(id: "myid",
|
377
|
+
legal_entity: {
|
378
|
+
last_name: "Smith",
|
379
|
+
address: {
|
380
|
+
line1: "test",
|
381
|
+
city: "San Francisco",
|
382
|
+
},
|
383
|
+
})
|
402
384
|
|
403
|
-
stub_request(:post, "#{Stripe.api_base}/v1/accounts/myid")
|
404
|
-
with(body: { legal_entity: { address: { line1: "Test2", city: "" } } })
|
405
|
-
to_return(body: JSON.generate(
|
385
|
+
stub_request(:post, "#{Stripe.api_base}/v1/accounts/myid")
|
386
|
+
.with(body: { legal_entity: { address: { line1: "Test2", city: "" } } })
|
387
|
+
.to_return(body: JSON.generate("id" => "my_id"))
|
406
388
|
|
407
|
-
acct.legal_entity.address = {:
|
389
|
+
acct.legal_entity.address = { line1: "Test2" }
|
408
390
|
acct.save
|
409
391
|
end
|
410
392
|
|
411
|
-
should
|
412
|
-
acct = Stripe::Account.construct_from(
|
413
|
-
|
414
|
-
:legal_entity => {}
|
415
|
-
})
|
393
|
+
should "correctly handle array setting" do
|
394
|
+
acct = Stripe::Account.construct_from(id: "myid",
|
395
|
+
legal_entity: {})
|
416
396
|
|
417
|
-
stub_request(:post, "#{Stripe.api_base}/v1/accounts/myid")
|
418
|
-
with(body: { legal_entity: { additional_owners: [{ first_name: "Bob" }] } })
|
419
|
-
to_return(body: JSON.generate(
|
397
|
+
stub_request(:post, "#{Stripe.api_base}/v1/accounts/myid")
|
398
|
+
.with(body: { legal_entity: { additional_owners: [{ first_name: "Bob" }] } })
|
399
|
+
.to_return(body: JSON.generate("id" => "myid"))
|
420
400
|
|
421
|
-
acct.legal_entity.additional_owners = [{:
|
401
|
+
acct.legal_entity.additional_owners = [{ first_name: "Bob" }]
|
422
402
|
acct.save
|
423
403
|
end
|
424
404
|
|
425
|
-
should
|
426
|
-
acct = Stripe::Account.construct_from(
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
}
|
431
|
-
})
|
405
|
+
should "correctly handle array insertion" do
|
406
|
+
acct = Stripe::Account.construct_from(id: "myid",
|
407
|
+
legal_entity: {
|
408
|
+
additional_owners: [],
|
409
|
+
})
|
432
410
|
|
433
411
|
# Note that this isn't a perfect check because we're using webmock's
|
434
412
|
# data decoding, which isn't aware of the Stripe array encoding that we
|
435
413
|
# use here.
|
436
|
-
stub_request(:post, "#{Stripe.api_base}/v1/accounts/myid")
|
437
|
-
with(body: { legal_entity: { additional_owners: [{ first_name: "Bob" }] } })
|
438
|
-
to_return(body: JSON.generate(
|
414
|
+
stub_request(:post, "#{Stripe.api_base}/v1/accounts/myid")
|
415
|
+
.with(body: { legal_entity: { additional_owners: [{ first_name: "Bob" }] } })
|
416
|
+
.to_return(body: JSON.generate("id" => "myid"))
|
439
417
|
|
440
|
-
acct.legal_entity.additional_owners << {:
|
418
|
+
acct.legal_entity.additional_owners << { first_name: "Bob" }
|
441
419
|
acct.save
|
442
420
|
end
|
443
421
|
|
444
|
-
should
|
445
|
-
acct = Stripe::Account.construct_from(
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
}
|
450
|
-
})
|
422
|
+
should "correctly handle array updates" do
|
423
|
+
acct = Stripe::Account.construct_from(id: "myid",
|
424
|
+
legal_entity: {
|
425
|
+
additional_owners: [{ first_name: "Bob" }, { first_name: "Jane" }],
|
426
|
+
})
|
451
427
|
|
452
428
|
# Note that this isn't a perfect check because we're using webmock's
|
453
429
|
# data decoding, which isn't aware of the Stripe array encoding that we
|
454
430
|
# 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(
|
431
|
+
stub_request(:post, "#{Stripe.api_base}/v1/accounts/myid")
|
432
|
+
.with(body: { legal_entity: { additional_owners: [{ first_name: "Janet" }] } })
|
433
|
+
.to_return(body: JSON.generate("id" => "myid"))
|
458
434
|
|
459
|
-
acct.legal_entity.additional_owners[1].first_name =
|
435
|
+
acct.legal_entity.additional_owners[1].first_name = "Janet"
|
460
436
|
acct.save
|
461
437
|
end
|
462
438
|
|
463
|
-
should
|
464
|
-
acct = Stripe::Account.construct_from(
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
:currencies_supported => ['usd', 'cad']
|
470
|
-
})
|
439
|
+
should "correctly handle array noops" do
|
440
|
+
acct = Stripe::Account.construct_from(id: "myid",
|
441
|
+
legal_entity: {
|
442
|
+
additional_owners: [{ first_name: "Bob" }],
|
443
|
+
},
|
444
|
+
currencies_supported: %w[usd cad])
|
471
445
|
|
472
|
-
stub_request(:post, "#{Stripe.api_base}/v1/accounts/myid")
|
473
|
-
with(body: {})
|
474
|
-
to_return(body: JSON.generate(
|
446
|
+
stub_request(:post, "#{Stripe.api_base}/v1/accounts/myid")
|
447
|
+
.with(body: {})
|
448
|
+
.to_return(body: JSON.generate("id" => "myid"))
|
475
449
|
|
476
450
|
acct.save
|
477
451
|
end
|
478
452
|
|
479
|
-
should
|
480
|
-
acct = Stripe::Account.construct_from(
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
}
|
485
|
-
})
|
453
|
+
should "correctly handle hash noops" do
|
454
|
+
acct = Stripe::Account.construct_from(id: "myid",
|
455
|
+
legal_entity: {
|
456
|
+
address: { line1: "1 Two Three" },
|
457
|
+
})
|
486
458
|
|
487
|
-
stub_request(:post, "#{Stripe.api_base}/v1/accounts/myid")
|
488
|
-
with(body: {})
|
489
|
-
to_return(body: JSON.generate(
|
459
|
+
stub_request(:post, "#{Stripe.api_base}/v1/accounts/myid")
|
460
|
+
.with(body: {})
|
461
|
+
.to_return(body: JSON.generate("id" => "myid"))
|
490
462
|
|
491
463
|
acct.save
|
492
464
|
end
|
493
465
|
|
494
|
-
should
|
495
|
-
account = Stripe::Account.construct_from(
|
496
|
-
|
497
|
-
:display_name => nil,
|
498
|
-
})
|
466
|
+
should "should create a new resource when an object without an id is saved" do
|
467
|
+
account = Stripe::Account.construct_from(id: nil,
|
468
|
+
display_name: nil)
|
499
469
|
|
500
|
-
stub_request(:post, "#{Stripe.api_base}/v1/accounts")
|
501
|
-
with(body: { display_name: "stripe" })
|
502
|
-
to_return(body: JSON.generate(
|
470
|
+
stub_request(:post, "#{Stripe.api_base}/v1/accounts")
|
471
|
+
.with(body: { display_name: "stripe" })
|
472
|
+
.to_return(body: JSON.generate("id" => "acct_123"))
|
503
473
|
|
504
|
-
account.display_name =
|
474
|
+
account.display_name = "stripe"
|
505
475
|
account.save
|
506
476
|
end
|
507
477
|
|
508
|
-
should
|
509
|
-
account = Stripe::Account.construct_from(
|
510
|
-
|
511
|
-
:display_name => nil,
|
512
|
-
})
|
478
|
+
should "set attributes as part of save" do
|
479
|
+
account = Stripe::Account.construct_from(id: nil,
|
480
|
+
display_name: nil)
|
513
481
|
|
514
|
-
stub_request(:post, "#{Stripe.api_base}/v1/accounts")
|
515
|
-
with(body: { display_name: "stripe", metadata: { key: "value" } })
|
516
|
-
to_return(body: JSON.generate(
|
482
|
+
stub_request(:post, "#{Stripe.api_base}/v1/accounts")
|
483
|
+
.with(body: { display_name: "stripe", metadata: { key: "value" } })
|
484
|
+
.to_return(body: JSON.generate("id" => "acct_123"))
|
517
485
|
|
518
|
-
account.save(:
|
486
|
+
account.save(display_name: "stripe", metadata: { key: "value" })
|
519
487
|
end
|
520
488
|
end
|
521
489
|
|
522
490
|
@@fixtures = {}
|
523
491
|
setup do
|
524
492
|
if @@fixtures.empty?
|
525
|
-
|
493
|
+
cache_fixture(:charge) do
|
526
494
|
Charge.retrieve("ch_123")
|
527
495
|
end
|
528
|
-
|
496
|
+
cache_fixture(:customer) do
|
529
497
|
Customer.retrieve("cus_123")
|
530
498
|
end
|
531
499
|
end
|
@@ -541,13 +509,9 @@ module Stripe
|
|
541
509
|
@@fixtures[:customer]
|
542
510
|
end
|
543
511
|
|
544
|
-
def get_fixture(key)
|
545
|
-
@@fixtures.fetch(key)
|
546
|
-
end
|
547
|
-
|
548
512
|
# Expects to retrieve a fixture from stripe-mock (an API call should be
|
549
513
|
# included in the block to yield to) and does very simple memoization.
|
550
|
-
def
|
514
|
+
def cache_fixture(key)
|
551
515
|
return @@fixtures[key] if @@fixtures.key?(key)
|
552
516
|
|
553
517
|
obj = yield
|