From de21302f4eb196837784ea719a53ade854c1dc55 Mon Sep 17 00:00:00 2001 From: Olivier Bellone Date: Sat, 14 Oct 2017 17:48:20 +0200 Subject: [PATCH] Encode arrays as hashes when needed --- lib/stripe/invoice.rb | 1 + lib/stripe/order.rb | 6 +++++ lib/stripe/subscription.rb | 10 +++++++ test/stripe/subscription_test.rb | 46 ++++++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+) diff --git a/lib/stripe/invoice.rb b/lib/stripe/invoice.rb index c865ec2b1..bc84d3592 100644 --- a/lib/stripe/invoice.rb +++ b/lib/stripe/invoice.rb @@ -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 diff --git a/lib/stripe/order.rb b/lib/stripe/order.rb index d0583ba88..c0d682314 100644 --- a/lib/stripe/order.rb +++ b/lib/stripe/order.rb @@ -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 diff --git a/lib/stripe/subscription.rb b/lib/stripe/subscription.rb index 07bb0bf5d..cfdfcf1ac 100644 --- a/lib/stripe/subscription.rb +++ b/lib/stripe/subscription.rb @@ -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 diff --git a/test/stripe/subscription_test.rb b/test/stripe/subscription_test.rb index e5ecbf150..ecccf26db 100644 --- a/test/stripe/subscription_test.rb +++ b/test/stripe/subscription_test.rb @@ -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