Skip to content
This repository has been archived by the owner on May 27, 2021. It is now read-only.

Commit

Permalink
Merge pull request activemerchant#971 from michellebu/master
Browse files Browse the repository at this point in the history
Stripe /cards endpoint does not update a customer.
  • Loading branch information
odorcicd committed Jan 7, 2014
2 parents d6a248c + 035cc65 commit 1b7ca22
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
20 changes: 13 additions & 7 deletions lib/active_merchant/billing/gateways/stripe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 19 additions & 0 deletions test/remote/gateways/remote_stripe_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 protected]"})
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 protected]", 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)
Expand Down
16 changes: 15 additions & 1 deletion test/unit/gateways/stripe_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,28 @@ 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

assert_equal 'card_483etw4er9fg4vF3sQdrt3FG', response.authorization
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 => '[email protected]')
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)

Expand Down

0 comments on commit 1b7ca22

Please sign in to comment.