Skip to content

Commit

Permalink
Support Braintree::Customer.update.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabe Berke-Williams committed Nov 18, 2011
1 parent 9304ea0 commit c57cde3
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 9 deletions.
14 changes: 12 additions & 2 deletions README.md
Expand Up @@ -9,13 +9,23 @@ Currently in alpha (i.e. it does not support every Braintree call).

## Supported API methods

* `Braintree::Customer.create`
### Customer
* `Braintree::Customer.find`
* `Braintree::Subscription.create`
* `Braintree::Customer.create`
* `Braintree::Customer.update`

### Subscription
* `Braintree::Subscription.find`
* `Braintree::Subscription.create`

### CreditCard
* `Braintree::CreditCard.find`
* `Braintree::CreditCard.sale`

### Transaction
* `Braintree::Transaction.sale`

### TransparentRedirect
* `Braintree::TransparentRedirect.url`
* `Braintree::TransparentRedirect.confirm` (only for creating customers)

Expand Down
32 changes: 26 additions & 6 deletions lib/fake_braintree/customer.rb
Expand Up @@ -2,9 +2,9 @@ module FakeBraintree
class Customer
include Helpers

def initialize(customer_hash, merchant_id)
@customer_hash = customer_hash
@merchant_id = merchant_id
def initialize(customer_hash, options)
@customer_hash = customer_hash.merge("merchant_id" => options[:merchant_id],
"id" => options[:id])
end

def create
Expand All @@ -17,10 +17,18 @@ def create
end
end

def update
if existing_customer_hash
hash = update_existing_customer!
gzipped_response(200, hash.to_xml(:root => 'customer'))
else
failure_response(404)
end
end

def customer_hash
hash = @customer_hash.dup
hash["id"] ||= create_id
hash["merchant-id"] = @merchant_id

if hash["credit_card"] && hash["credit_card"].is_a?(Hash)
if !hash["credit_card"].empty?
Expand Down Expand Up @@ -49,6 +57,14 @@ def split_expiration_date_into_month_and_year!(hash)
end
end

def existing_customer_hash
FakeBraintree.customers[customer_hash["id"]]
end

def update_existing_customer!
existing_customer_hash.merge!(customer_hash)
end

def credit_card_token(hash)
md5("#{hash['merchant_id']}#{hash['id']}")
end
Expand All @@ -57,8 +73,8 @@ def last_four(hash)
hash["credit_card"].delete("number")[-4..-1]
end

def failure_response
gzipped_response(422, FakeBraintree.failure_response(@customer_hash["credit_card"]["number"]).to_xml(:root => 'api_error_response'))
def failure_response(code = 422)
gzipped_response(code, FakeBraintree.failure_response(credit_card_number).to_xml(:root => 'api_error_response'))
end

def credit_card_is_failure?
Expand Down Expand Up @@ -90,5 +106,9 @@ def has_credit_card_number?
@customer_hash["credit_card"].is_a?(Hash) &&
@customer_hash["credit_card"].key?("number")
end

def credit_card_number
has_credit_card_number? && @customer_hash["credit_card"]["number"]
end
end
end
10 changes: 9 additions & 1 deletion lib/fake_braintree/sinatra_app.rb
Expand Up @@ -12,7 +12,8 @@ class SinatraApp < Sinatra::Base
# Braintree::Customer.create
post "/merchants/:merchant_id/customers" do
customer_hash = Hash.from_xml(request.body).delete("customer")
Customer.new(customer_hash, params[:merchant_id]).create
options = {:merchant_id => params[:merchant_id]}
Customer.new(customer_hash, options).create
end

# Braintree::Customer.find
Expand All @@ -25,6 +26,13 @@ class SinatraApp < Sinatra::Base
end
end

# Braintree::Customer.update
put "/merchants/:merchant_id/customers/:id" do
customer_hash = Hash.from_xml(request.body).delete("customer")
options = {:id => params[:id], :merchant_id => params[:merchant_id]}
Customer.new(customer_hash, options).update
end

# Braintree::Subscription.create
post "/merchants/:merchant_id/subscriptions" do
response_hash = Subscription.new(request).response_hash
Expand Down
14 changes: 14 additions & 0 deletions spec/fake_braintree/customer_spec.rb
Expand Up @@ -74,3 +74,17 @@
lambda { Braintree::Customer.find("foo") }.should raise_error(Braintree::NotFoundError)
end
end

describe "Braintree::Customer.update" do
it "successfully updates a customer" do
customer_id = create_customer.customer.id
result = Braintree::Customer.update(customer_id, :first_name => "Jerry")

result.should be_success
Braintree::Customer.find(customer_id).first_name.should == "Jerry"
end

it "raises an error for a nonexistent customer" do
lambda { Braintree::Customer.update("foo", {:first_name => "Bob"}) }.should raise_error(Braintree::NotFoundError)
end
end

0 comments on commit c57cde3

Please sign in to comment.