diff --git a/lib/active_merchant/billing/gateways/stripe.rb b/lib/active_merchant/billing/gateways/stripe.rb index 3c1cedecea6..faf7d6b5475 100644 --- a/lib/active_merchant/billing/gateways/stripe.rb +++ b/lib/active_merchant/billing/gateways/stripe.rb @@ -99,20 +99,26 @@ def refund_application_fee(money, identification, options = {}) # Note: creating a new credit card will not change the customer's existing default credit card (use :set_default => true) def store(creditcard, options = {}) post = {} - add_creditcard(post, creditcard, options) - post[:description] = options[:description] - post[:email] = options[:email] + card_params = {} + add_creditcard(card_params, creditcard, options) + post[:description] = options[:description] if options[:description] + post[:email] = options[:email] if options[:email] if options[:customer] MultiResponse.run(:first) do |r| - r.process { commit(:post, "customers/#{CGI.escape(options[:customer])}/cards", post, options) } + # The /cards endpoint does not update other customer parameters. + r.process { commit(:post, "customers/#{CGI.escape(options[:customer])}/cards", card_params, options) } - return r unless options[:set_default] and r.success? and !r.params["id"].blank? + if options[:set_default] and r.success? and !r.params['id'].blank? + post[:default_card] = r.params['id'] + end - r.process { update_customer(options[:customer], :default_card => r.params["id"]) } + if post.count > 0 + r.process { update_customer(options[:customer], post) } + end end else - commit(:post, 'customers', post, options) + commit(:post, 'customers', post.merge(card_params), options) end end diff --git a/test/remote/gateways/remote_stripe_test.rb b/test/remote/gateways/remote_stripe_test.rb index 603386caaef..d42d0e4296b 100644 --- a/test/remote/gateways/remote_stripe_test.rb +++ b/test/remote/gateways/remote_stripe_test.rb @@ -93,6 +93,25 @@ def test_successful_store assert_equal @credit_card.last_digits, first_card["last4"] end + def test_successful_store_with_existing_customer + assert response = @gateway.store(@credit_card, {:currency => @currency, :description => "Active Merchant Test Customer"}) + assert_success response + + assert response = @gateway.store(@new_credit_card, {:customer => response.params['id'], :currency => @currency, :description => "Active Merchant Test Customer", :email => "email@example.com"}) + assert_success response + assert_equal 2, response.responses.size + + card_response = response.responses[0] + assert_equal "card", card_response.params["object"] + assert_equal @new_credit_card.last_digits, card_response.params["last4"] + + customer_response = response.responses[1] + assert_equal "customer", customer_response.params["object"] + assert_equal "Active Merchant Test Customer", customer_response.params["description"] + assert_equal "email@example.com", customer_response.params["email"] + assert_equal 2, customer_response.params["cards"]["count"] + end + def test_successful_update creation = @gateway.store(@credit_card, {:description => "Active Merchant Update Customer"}) assert response = @gateway.update(creation.params['id'], @new_credit_card) diff --git a/test/unit/gateways/stripe_test.rb b/test/unit/gateways/stripe_test.rb index bda7fcbf655..c41b18fffec 100644 --- a/test/unit/gateways/stripe_test.rb +++ b/test/unit/gateways/stripe_test.rb @@ -30,7 +30,7 @@ def test_successful_new_customer_with_card def test_successful_new_card @gateway.expects(:ssl_request).returns(successful_new_card_response) - assert response = @gateway.store(@credit_card, @options.merge(:customer => 'cus_3sgheFxeBgTQ3M')) + assert response = @gateway.store(@credit_card, :customer => 'cus_3sgheFxeBgTQ3M') assert_instance_of MultiResponse, response assert_success response @@ -38,6 +38,20 @@ def test_successful_new_card assert response.test? end + def test_successful_new_card_and_customer_update + @gateway.expects(:ssl_request).twice.returns(successful_new_card_response, successful_new_customer_response) + + assert response = @gateway.store(@credit_card, :customer => 'cus_3sgheFxeBgTQ3M', :email => 'test@test.com') + assert_instance_of MultiResponse, response + assert_success response + + assert_equal 'card_483etw4er9fg4vF3sQdrt3FG', response.authorization + assert_equal 2, response.responses.size + assert_equal 'card_483etw4er9fg4vF3sQdrt3FG', response.responses[0].authorization + assert_equal 'cus_3sgheFxeBgTQ3M', response.responses[1].authorization + assert response.test? + end + def test_successful_new_default_card @gateway.expects(:ssl_request).twice.returns(successful_new_card_response, successful_new_customer_response)