Skip to content
This repository
Browse code

Support Braintree::Customer.update.

  • Loading branch information...
commit c57cde3339d24f085b707d4a0ec635c75121f613 1 parent 9304ea0
Gabe Berke-Williams authored November 18, 2011
14  README.md
Source Rendered
@@ -9,13 +9,23 @@ Currently in alpha (i.e. it does not support every Braintree call).
9 9
 
10 10
 ## Supported API methods
11 11
 
12  
-* `Braintree::Customer.create`
  12
+### Customer
13 13
 * `Braintree::Customer.find`
14  
-* `Braintree::Subscription.create`
  14
+* `Braintree::Customer.create`
  15
+* `Braintree::Customer.update`
  16
+
  17
+### Subscription
15 18
 * `Braintree::Subscription.find`
  19
+* `Braintree::Subscription.create`
  20
+
  21
+### CreditCard
16 22
 * `Braintree::CreditCard.find`
17 23
 * `Braintree::CreditCard.sale`
  24
+
  25
+### Transaction
18 26
 * `Braintree::Transaction.sale`
  27
+
  28
+### TransparentRedirect
19 29
 * `Braintree::TransparentRedirect.url`
20 30
 * `Braintree::TransparentRedirect.confirm` (only for creating customers)
21 31
 
32  lib/fake_braintree/customer.rb
@@ -2,9 +2,9 @@ module FakeBraintree
2 2
   class Customer
3 3
     include Helpers
4 4
 
5  
-    def initialize(customer_hash, merchant_id)
6  
-      @customer_hash = customer_hash
7  
-      @merchant_id  = merchant_id
  5
+    def initialize(customer_hash, options)
  6
+      @customer_hash = customer_hash.merge("merchant_id" => options[:merchant_id],
  7
+                                           "id" => options[:id])
8 8
     end
9 9
 
10 10
     def create
@@ -17,10 +17,18 @@ def create
17 17
       end
18 18
     end
19 19
 
  20
+    def update
  21
+      if existing_customer_hash
  22
+        hash = update_existing_customer!
  23
+        gzipped_response(200, hash.to_xml(:root => 'customer'))
  24
+      else
  25
+        failure_response(404)
  26
+      end
  27
+    end
  28
+
20 29
     def customer_hash
21 30
       hash = @customer_hash.dup
22 31
       hash["id"] ||= create_id
23  
-      hash["merchant-id"] = @merchant_id
24 32
 
25 33
       if hash["credit_card"] && hash["credit_card"].is_a?(Hash)
26 34
         if !hash["credit_card"].empty?
@@ -49,6 +57,14 @@ def split_expiration_date_into_month_and_year!(hash)
49 57
       end
50 58
     end
51 59
 
  60
+    def existing_customer_hash
  61
+      FakeBraintree.customers[customer_hash["id"]]
  62
+    end
  63
+
  64
+    def update_existing_customer!
  65
+      existing_customer_hash.merge!(customer_hash)
  66
+    end
  67
+
52 68
     def credit_card_token(hash)
53 69
       md5("#{hash['merchant_id']}#{hash['id']}")
54 70
     end
@@ -57,8 +73,8 @@ def last_four(hash)
57 73
       hash["credit_card"].delete("number")[-4..-1]
58 74
     end
59 75
 
60  
-    def failure_response
61  
-      gzipped_response(422, FakeBraintree.failure_response(@customer_hash["credit_card"]["number"]).to_xml(:root => 'api_error_response'))
  76
+    def failure_response(code = 422)
  77
+      gzipped_response(code, FakeBraintree.failure_response(credit_card_number).to_xml(:root => 'api_error_response'))
62 78
     end
63 79
 
64 80
     def credit_card_is_failure?
@@ -90,5 +106,9 @@ def has_credit_card_number?
90 106
         @customer_hash["credit_card"].is_a?(Hash) &&
91 107
         @customer_hash["credit_card"].key?("number")
92 108
     end
  109
+
  110
+    def credit_card_number
  111
+      has_credit_card_number? && @customer_hash["credit_card"]["number"]
  112
+    end
93 113
   end
94 114
 end
10  lib/fake_braintree/sinatra_app.rb
@@ -12,7 +12,8 @@ class SinatraApp < Sinatra::Base
12 12
     # Braintree::Customer.create
13 13
     post "/merchants/:merchant_id/customers" do
14 14
       customer_hash = Hash.from_xml(request.body).delete("customer")
15  
-      Customer.new(customer_hash, params[:merchant_id]).create
  15
+      options = {:merchant_id => params[:merchant_id]}
  16
+      Customer.new(customer_hash, options).create
16 17
     end
17 18
 
18 19
     # Braintree::Customer.find
@@ -25,6 +26,13 @@ class SinatraApp < Sinatra::Base
25 26
       end
26 27
     end
27 28
 
  29
+    # Braintree::Customer.update
  30
+    put "/merchants/:merchant_id/customers/:id" do
  31
+      customer_hash = Hash.from_xml(request.body).delete("customer")
  32
+      options = {:id => params[:id], :merchant_id => params[:merchant_id]}
  33
+      Customer.new(customer_hash, options).update
  34
+    end
  35
+
28 36
     # Braintree::Subscription.create
29 37
     post "/merchants/:merchant_id/subscriptions" do
30 38
       response_hash = Subscription.new(request).response_hash
14  spec/fake_braintree/customer_spec.rb
@@ -74,3 +74,17 @@
74 74
     lambda { Braintree::Customer.find("foo") }.should raise_error(Braintree::NotFoundError)
75 75
   end
76 76
 end
  77
+
  78
+describe "Braintree::Customer.update" do
  79
+  it "successfully updates a customer" do
  80
+    customer_id = create_customer.customer.id
  81
+    result = Braintree::Customer.update(customer_id, :first_name => "Jerry")
  82
+
  83
+    result.should be_success
  84
+    Braintree::Customer.find(customer_id).first_name.should == "Jerry"
  85
+  end
  86
+
  87
+  it "raises an error for a nonexistent customer" do
  88
+    lambda { Braintree::Customer.update("foo", {:first_name => "Bob"}) }.should raise_error(Braintree::NotFoundError)
  89
+  end
  90
+end

0 notes on commit c57cde3

Please sign in to comment.
Something went wrong with that request. Please try again.