Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Support Braintree::Customer.update.

  • Loading branch information...
commit c57cde3339d24f085b707d4a0ec635c75121f613 1 parent 9304ea0
Gabe Berke-Williams gabebw authored
14 README.md
View
@@ -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)
32 lib/fake_braintree/customer.rb
View
@@ -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
10 lib/fake_braintree/sinatra_app.rb
View
@@ -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
14 spec/fake_braintree/customer_spec.rb
View
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.