From c57cde3339d24f085b707d4a0ec635c75121f613 Mon Sep 17 00:00:00 2001 From: Gabe Berke-Williams Date: Fri, 18 Nov 2011 11:58:30 -0500 Subject: [PATCH] Support Braintree::Customer.update. --- README.md | 14 ++++++++++-- lib/fake_braintree/customer.rb | 32 ++++++++++++++++++++++------ lib/fake_braintree/sinatra_app.rb | 10 ++++++++- spec/fake_braintree/customer_spec.rb | 14 ++++++++++++ 4 files changed, 61 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 344085e..7057964 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/lib/fake_braintree/customer.rb b/lib/fake_braintree/customer.rb index 2ced325..9871752 100644 --- a/lib/fake_braintree/customer.rb +++ b/lib/fake_braintree/customer.rb @@ -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 @@ -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? @@ -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 @@ -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? @@ -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 diff --git a/lib/fake_braintree/sinatra_app.rb b/lib/fake_braintree/sinatra_app.rb index 829a9bc..6b7f18b 100644 --- a/lib/fake_braintree/sinatra_app.rb +++ b/lib/fake_braintree/sinatra_app.rb @@ -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 @@ -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 diff --git a/spec/fake_braintree/customer_spec.rb b/spec/fake_braintree/customer_spec.rb index e2225d5..bf5c64f 100644 --- a/spec/fake_braintree/customer_spec.rb +++ b/spec/fake_braintree/customer_spec.rb @@ -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