diff --git a/lib/active_merchant/billing/gateways/stripe.rb b/lib/active_merchant/billing/gateways/stripe.rb index cf2af37b42c..3c1cedecea6 100644 --- a/lib/active_merchant/billing/gateways/stripe.rb +++ b/lib/active_merchant/billing/gateways/stripe.rb @@ -40,7 +40,7 @@ def authorize(money, creditcard, options = {}) post = create_post_for_auth_or_purchase(money, creditcard, options) post[:capture] = "false" - commit(:post, 'charges', post, generate_options(options)) + commit(:post, 'charges', post, options) end # To create a charge on a card or a token, call @@ -53,31 +53,30 @@ def authorize(money, creditcard, options = {}) def purchase(money, creditcard, options = {}) post = create_post_for_auth_or_purchase(money, creditcard, options) - commit(:post, 'charges', post, generate_options(options)) + commit(:post, 'charges', post, options) end def capture(money, authorization, options = {}) post = {:amount => amount(money)} add_application_fee(post, options) - commit(:post, "charges/#{CGI.escape(authorization)}/capture", post) + commit(:post, "charges/#{CGI.escape(authorization)}/capture", post, options) end def void(identification, options = {}) - commit(:post, "charges/#{CGI.escape(identification)}/refund", {}) + commit(:post, "charges/#{CGI.escape(identification)}/refund", {}, options) end def refund(money, identification, options = {}) post = {:amount => amount(money)} - commit_options = generate_options(options) MultiResponse.run(:first) do |r| - r.process { commit(:post, "charges/#{CGI.escape(identification)}/refund", post, commit_options) } + r.process { commit(:post, "charges/#{CGI.escape(identification)}/refund", post, options) } return r unless options[:refund_fee_amount] - r.process { fetch_application_fees(identification, commit_options) } - r.process { refund_application_fee(options[:refund_fee_amount], application_fee_from_response(r), commit_options) } + r.process { fetch_application_fees(identification, options) } + r.process { refund_application_fee(options[:refund_fee_amount], application_fee_from_response(r), options) } end end @@ -104,17 +103,16 @@ def store(creditcard, options = {}) post[:description] = options[:description] post[:email] = options[:email] - commit_options = generate_options(options) if options[:customer] MultiResponse.run(:first) do |r| - r.process { commit(:post, "customers/#{CGI.escape(options[:customer])}/cards", post, commit_options) } + r.process { commit(:post, "customers/#{CGI.escape(options[:customer])}/cards", post, options) } return r unless options[:set_default] and r.success? and !r.params["id"].blank? r.process { update_customer(options[:customer], :default_card => r.params["id"]) } end else - commit(:post, 'customers', post, commit_options) + commit(:post, 'customers', post, options) end end @@ -124,14 +122,14 @@ def update(customer_id, creditcard, options = {}) end def update_customer(customer_id, options = {}) - commit(:post, "customers/#{CGI.escape(customer_id)}", options, generate_options(options)) + commit(:post, "customers/#{CGI.escape(customer_id)}", options, options) end def unstore(customer_id, card_id = nil, options = {}) if card_id.nil? - commit(:delete, "customers/#{CGI.escape(customer_id)}", nil, generate_options(options)) + commit(:delete, "customers/#{CGI.escape(customer_id)}", nil, options) else - commit(:delete, "customers/#{CGI.escape(customer_id)}/cards/#{CGI.escape(card_id)}", nil, generate_options(options)) + commit(:delete, "customers/#{CGI.escape(customer_id)}/cards/#{CGI.escape(card_id)}", nil, options) end end @@ -159,6 +157,10 @@ def add_application_fee(post, options) post[:application_fee] = options[:application_fee] if options[:application_fee] end + def add_expand_parameters(post, options) + post[:expand] = Array.wrap(options[:expand]) + end + def add_customer_data(post, options) metadata_options = [:description, :ip, :user_agent, :referrer] post.update(options.slice(*metadata_options)) @@ -233,21 +235,14 @@ def post_data(params) h["#{key}[#{k}]"] = v unless v.blank? end post_data(h) + elsif value.is_a?(Array) + value.map { |v| "#{key}[]=#{CGI.escape(v.to_s)}" }.join("&") else "#{key}=#{CGI.escape(value.to_s)}" end end.compact.join("&") end - def generate_options(raw_options) - options = generate_meta(raw_options) - options.merge!(raw_options.slice(:version, :key)) - end - - def generate_meta(options) - {:meta => {:ip => options[:ip]}} - end - def headers(options = {}) @@ua ||= JSON.dump({ :bindings_version => ActiveMerchant::VERSION, @@ -263,13 +258,15 @@ def headers(options = {}) "Authorization" => "Basic " + Base64.encode64(key.to_s + ":").strip, "User-Agent" => "Stripe/v1 ActiveMerchantBindings/#{ActiveMerchant::VERSION}", "X-Stripe-Client-User-Agent" => @@ua, - "X-Stripe-Client-User-Metadata" => options[:meta].to_json + "X-Stripe-Client-User-Metadata" => {:ip => options[:ip]}.to_json } headers.merge!("Stripe-Version" => options[:version]) if options[:version] headers end def commit(method, url, parameters=nil, options = {}) + add_expand_parameters(parameters, options) if parameters + raw_response = response = nil success = false begin diff --git a/lib/active_merchant/billing/gateways/webpay.rb b/lib/active_merchant/billing/gateways/webpay.rb index de8d6e484dc..c4543e63682 100644 --- a/lib/active_merchant/billing/gateways/webpay.rb +++ b/lib/active_merchant/billing/gateways/webpay.rb @@ -21,15 +21,14 @@ def capture(money, authorization, options = {}) def refund(money, identification, options = {}) post = {:amount => localized_amount(money)} - commit_options = generate_meta(options) MultiResponse.run do |r| - r.process { commit(:post, "charges/#{CGI.escape(identification)}/refund", post, commit_options) } + r.process { commit(:post, "charges/#{CGI.escape(identification)}/refund", post, options) } return r unless options[:refund_fee_amount] - r.process { fetch_application_fees(identification, commit_options) } - r.process { refund_application_fee(options[:refund_fee_amount], application_fee_from_response(r), commit_options) } + r.process { fetch_application_fees(identification, options) } + r.process { refund_application_fee(options[:refund_fee_amount], application_fee_from_response(r), options) } end end @@ -56,17 +55,16 @@ def store(creditcard, options = {}) post[:description] = options[:description] post[:email] = options[:email] - commit_options = generate_options(options) if options[:customer] MultiResponse.run(:first) do |r| - r.process { commit(:post, "customers/#{CGI.escape(options[:customer])}/", post, commit_options) } + r.process { commit(:post, "customers/#{CGI.escape(options[:customer])}/", post, options) } return r unless options[:set_default] and r.success? and !r.params["id"].blank? r.process { update_customer(options[:customer], :default_card => r.params["id"]) } end else - commit(:post, 'customers', post, commit_options) + commit(:post, 'customers', post, options) end end @@ -93,7 +91,7 @@ def headers(options = {}) "Authorization" => "Basic " + Base64.encode64(@api_key.to_s + ":").strip, "User-Agent" => "Webpay/v1 ActiveMerchantBindings/#{ActiveMerchant::VERSION}", "X-Webpay-Client-User-Agent" => @@ua, - "X-Webpay-Client-User-Metadata" => options[:meta].to_json + "X-Webpay-Client-User-Metadata" => {:ip => options[:ip]}.to_json } end end diff --git a/test/remote/gateways/remote_stripe_test.rb b/test/remote/gateways/remote_stripe_test.rb index f82fb5dbf5a..603386caaef 100644 --- a/test/remote/gateways/remote_stripe_test.rb +++ b/test/remote/gateways/remote_stripe_test.rb @@ -186,4 +186,11 @@ def test_creditcard_purchase_with_customer assert_equal "charge", response.params["object"] assert response.params["paid"] end + + def test_expanding_objects + assert response = @gateway.purchase(@amount, @credit_card, @options.merge(:expand => 'balance_transaction')) + assert_success response + assert response.params['balance_transaction'].is_a?(Hash) + assert_equal 'balance_transaction', response.params['balance_transaction']['object'] + end end diff --git a/test/unit/gateways/stripe_test.rb b/test/unit/gateways/stripe_test.rb index 9ef3407819c..bda7fcbf655 100644 --- a/test/unit/gateways/stripe_test.rb +++ b/test/unit/gateways/stripe_test.rb @@ -331,6 +331,26 @@ def generate_options_should_allow_key assert_equal({:key => '12345'}, generate_options({:key => '12345'})) end + def test_passing_expand_parameters + @gateway.expects(:ssl_request).with do |method, url, post, headers| + assert post.include?("expand[]=balance_transaction") + end.returns(successful_authorization_response) + + @options.merge!(:expand => :balance_transaction) + + @gateway.authorize(@amount, @credit_card, @options) + end + + def test_passing_expand_parameters_as_array + @gateway.expects(:ssl_request).with do |method, url, post, headers| + assert post.include?("expand[]=balance_transaction&expand[]=customer") + end.returns(successful_authorization_response) + + @options.merge!(:expand => [:balance_transaction, :customer]) + + @gateway.authorize(@amount, @credit_card, @options) + end + private # Create new customer and set default credit card