mirror of
https://github.com/stripe/stripe-ruby.git
synced 2025-08-24 00:00:51 -04:00
Correctly encode the subscription items array (#467)
* Correctly encode the subscription items array * Use super * Handle non-arrays as well * Also encode items on create
This commit is contained in:
parent
d2f783df34
commit
c131bcbcac
@ -12,6 +12,16 @@ module Stripe
|
||||
initialize_from({ :discount => nil }, opts, true)
|
||||
end
|
||||
|
||||
def self.update(id, params={}, opts={})
|
||||
params[:items] = Util.array_to_hash(params[:items]) if params[:items]
|
||||
super(id, params, opts)
|
||||
end
|
||||
|
||||
def self.create(params={}, opts={})
|
||||
params[:items] = Util.array_to_hash(params[:items]) if params[:items]
|
||||
super(params, opts)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def discount_url
|
||||
|
@ -121,6 +121,20 @@ module Stripe
|
||||
map { |k,v| "#{url_encode(k)}=#{url_encode(v)}" }.join('&')
|
||||
end
|
||||
|
||||
# Transforms an array into a hash with integer keys. Used for a small
|
||||
# number of API endpoints. If the argument is not an Array, return it
|
||||
# unchanged. Example: [{foo: 'bar'}] => {"0" => {foo: "bar"}}
|
||||
def self.array_to_hash(array)
|
||||
case array
|
||||
when Array
|
||||
hash = {}
|
||||
array.each_with_index { |v,i| hash[i.to_s] = v }
|
||||
hash
|
||||
else
|
||||
array
|
||||
end
|
||||
end
|
||||
|
||||
# Encodes a string in a way that makes it suitable for use in a set of
|
||||
# query parameters in a URI or in a set of form parameters in a request
|
||||
# body.
|
||||
|
@ -95,8 +95,10 @@ module Stripe
|
||||
url == "#{Stripe.api_base}/v1/subscriptions/#{sid}" &&
|
||||
api_key.nil? &&
|
||||
CGI.parse(params) == {
|
||||
'items[][plan]'=>['gold', 'silver'],
|
||||
'items[][quantity]'=>['1', '2'],
|
||||
'items[0][plan]' => ['gold'],
|
||||
'items[0][quantity]' => ['1'],
|
||||
'items[1][plan]' => ['silver'],
|
||||
'items[1][quantity]' => ['2'],
|
||||
}
|
||||
end.returns(make_response(make_subscription(:items => items)))
|
||||
|
||||
@ -108,6 +110,29 @@ module Stripe
|
||||
assert_equal 2, sub.items.data[1].quantity
|
||||
end
|
||||
|
||||
should "subscription items should be updateable with hash" do
|
||||
sid = 's_test_subscription'
|
||||
items = {:data => [{:plan => {:id =>'gold'}, :quantity => 1}, {:plan => {:id =>'silver'}, :quantity => 2}]}
|
||||
|
||||
@mock.expects(:post).once.with do |url, api_key, params|
|
||||
url == "#{Stripe.api_base}/v1/subscriptions/#{sid}" &&
|
||||
api_key.nil? &&
|
||||
CGI.parse(params) == {
|
||||
'items[0][plan]' => ['gold'],
|
||||
'items[0][quantity]' => ['1'],
|
||||
'items[1][plan]' => ['silver'],
|
||||
'items[1][quantity]' => ['2'],
|
||||
}
|
||||
end.returns(make_response(make_subscription(:items => items)))
|
||||
|
||||
sub = Stripe::Subscription.update(sid, :items => {'0' => {:plan => 'gold', :quantity =>1}, '1' => {:plan => 'silver', :quantity =>2}})
|
||||
|
||||
assert_equal 'gold', sub.items.data[0].plan.id
|
||||
assert_equal 1, sub.items.data[0].quantity
|
||||
assert_equal 'silver', sub.items.data[1].plan.id
|
||||
assert_equal 2, sub.items.data[1].quantity
|
||||
end
|
||||
|
||||
should "subscriptions should be saveable" do
|
||||
@mock.expects(:get).once.returns(make_response(make_subscription))
|
||||
sub = Stripe::Subscription.retrieve('s_test_subscription')
|
||||
@ -142,8 +167,10 @@ module Stripe
|
||||
api_key.nil? &&
|
||||
CGI.parse(params) == {
|
||||
'customer' => ['c_test_customer'],
|
||||
'items[][plan]'=>['gold', 'silver'],
|
||||
'items[][quantity]'=>['1', '2'],
|
||||
'items[0][plan]' => ['gold'],
|
||||
'items[0][quantity]' => ['1'],
|
||||
'items[1][plan]' => ['silver'],
|
||||
'items[1][quantity]' => ['2'],
|
||||
}
|
||||
end.returns(make_response(make_subscription(:items => items, :id => 'test_new_subscription')))
|
||||
|
||||
|
@ -141,5 +141,9 @@ module Stripe
|
||||
obj = Util.convert_to_stripe_object([1, 2, 3], {})
|
||||
assert_equal [1, 2, 3], obj
|
||||
end
|
||||
|
||||
should "#array_to_hash should convert an array into a hash with integer keys" do
|
||||
assert_equal({"0" => 1, "1" => 2, "2" => 3}, Util.array_to_hash([1, 2, 3]))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user