Skip to content

Commit

Permalink
Merge pull request #596 from stripe/ob-fix-554
Browse files Browse the repository at this point in the history
Encode arrays as hashes when needed
  • Loading branch information
brandur-stripe authored Oct 16, 2017
2 parents 6a87b9c + de21302 commit e4af9ad
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/stripe/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Invoice < APIResource
OBJECT_NAME = "invoice".freeze

def self.upcoming(params, opts = {})
params[:subscription_items] = Util.array_to_hash(params[:subscription_items]) if params[:subscription_items]
resp, opts = request(:get, upcoming_url, params, opts)
Util.convert_to_stripe_object(resp.data, opts)
end
Expand Down
6 changes: 6 additions & 0 deletions lib/stripe/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@ def pay(params, opts = {})
end

def return_order(params, opts = {})
params[:items] = Util.array_to_hash(params[:items]) if params[:items]
resp, opts = request(:post, returns_url, params, opts)
Util.convert_to_stripe_object(resp.data, opts)
end

def self.create(params = {}, opts = {})
params[:items] = Util.array_to_hash(params[:items]) if params[:items]
super(params, opts)
end

private

def pay_url
Expand Down
10 changes: 10 additions & 0 deletions lib/stripe/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ def self.create(params = {}, opts = {})
super(params, opts)
end

def serialize_params(options = {})
update_hash = super
if @unsaved_values.include?(:items)
value = Util.array_to_hash(@values[:items])
update_hash[:items] =
serialize_params_value(value, nil, true, options[:force], key: :items)
end
update_hash
end

private

def discount_url
Expand Down
46 changes: 46 additions & 0 deletions test/stripe/subscription_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,51 @@ class SubscriptionTest < Test::Unit::TestCase
assert subscription.is_a?(Stripe::Subscription)
end
end

context "#serialize_params" do
should "serialize when items is set to an Array" do
obj = Stripe::Util.convert_to_stripe_object({
object: "subscription",
items: Stripe::Util.convert_to_stripe_object(
object: "list",
data: []
),
}, {})
obj.items = [
{ id: "si_foo", deleted: true },
{ plan: "plan_bar" },
]

expected = {
items: {
:"0" => { id: "si_foo", deleted: true },
:"1" => { plan: "plan_bar" },
},
}
assert_equal(expected, obj.serialize_params)
end

should "serialize when items is set to a Hash" do
obj = Stripe::Util.convert_to_stripe_object({
object: "subscription",
items: Stripe::Util.convert_to_stripe_object(
object: "list",
data: []
),
}, {})
obj.items = {
"0" => { id: "si_foo", deleted: true },
"1" => { plan: "plan_bar" },
}

expected = {
items: {
:"0" => { id: "si_foo", deleted: true },
:"1" => { plan: "plan_bar" },
},
}
assert_equal(expected, obj.serialize_params)
end
end
end
end

0 comments on commit e4af9ad

Please sign in to comment.