Permalink
Browse files

Massively clean up Customer creation code.

  • Loading branch information...
1 parent cc7463f commit 0259b9d74efc621e0cb7614290d295b90ac88044 @gabebw gabebw committed Nov 3, 2011
Showing with 90 additions and 28 deletions.
  1. +3 −0 lib/fake_braintree.rb
  2. +49 −0 lib/fake_braintree/customer.rb
  3. +31 −0 lib/fake_braintree/helpers.rb
  4. +7 −28 lib/fake_braintree/sinatra_app.rb
@@ -2,6 +2,9 @@
require 'braintree'
require 'sham_rack'
+require 'fake_braintree/helpers'
+require 'fake_braintree/customer'
+
require 'fake_braintree/sinatra_app'
require 'fake_braintree/valid_credit_cards'
require 'fake_braintree/version'
@@ -0,0 +1,49 @@
+module FakeBraintree
+ class Customer
+ include Helpers
+
+ def initialize(request, merchant_id)
+ @customer_hash = Hash.from_xml(request.body).delete("customer")
+ @merchant_id = merchant_id
+ end
+
+ def invalid?
+ credit_card_is_failure? || invalid_credit_card?
+ end
+
+ def failure_response
+ gzipped_response(422, FakeBraintree.failure_response(@customer_hash["credit_card"]["number"]).to_xml(:root => 'api_error_response'))
+ end
+
+ def customer_hash
+ hash = @customer_hash.dup
+ hash["id"] ||= md5("#{@merchant_id}#{Time.now.to_f}")
+ hash["merchant-id"] = @merchant_id
+ if hash["credit_card"] && hash["credit_card"].is_a?(Hash)
+ hash["credit_card"].delete("__content__")
+ if !hash["credit_card"].empty?
+ hash["credit_card"]["last_4"] = hash["credit_card"].delete("number")[-4..-1]
+ hash["credit_card"]["token"] = md5("#{hash['merchant_id']}#{hash['id']}#{Time.now.to_f}")
+ expiration_date = hash["credit_card"].delete("expiration_date")
+ hash["credit_card"]["expiration_month"] = expiration_date.split('/')[0]
+ hash["credit_card"]["expiration_year"] = expiration_date.split('/')[1]
+
+ credit_card = hash.delete("credit_card")
+ hash["credit_cards"] = [credit_card]
+ end
+ end
+
+ hash
+ end
+
+ private
+
+ def credit_card_is_failure?
+ FakeBraintree.failure?(@customer_hash["credit_card"]["number"])
+ end
+
+ def invalid_credit_card?
+ verify_credit_card?(@customer_hash) && has_invalid_credit_card?(@customer_hash)
+ end
+ end
+end
@@ -0,0 +1,31 @@
+require 'digest/md5'
+require 'active_support'
+require 'active_support/core_ext'
+
+module FakeBraintree
+ module Helpers
+ def gzip(content)
+ ActiveSupport::Gzip.compress(content)
+ end
+
+ def gzipped_response(status_code, uncompressed_content)
+ [status_code, { "Content-Encoding" => "gzip" }, gzip(uncompressed_content)]
+ end
+
+ def md5(content)
+ Digest::MD5.hexdigest(content)
+ end
+
+ def verify_credit_card?(customer_hash)
+ return true if FakeBraintree.verify_all_cards
+
+ customer_hash["credit_card"].key?("options") &&
+ customer_hash["credit_card"]["options"].is_a?(Hash) &&
+ customer_hash["credit_card"]["options"]["verify_card"] == true
+ end
+
+ def has_invalid_credit_card?(customer_hash)
+ ! FakeBraintree::VALID_CREDIT_CARDS.include?(customer_hash["credit_card"]["number"])
+ end
+ end
+end
@@ -1,7 +1,4 @@
-require 'digest/md5'
-require 'sinatra'
-require 'active_support'
-require 'active_support/core_ext'
+require 'sinatra/base'
module FakeBraintree
class SinatraApp < Sinatra::Base
@@ -38,31 +35,13 @@ def has_invalid_credit_card?(customer_hash)
# Braintree::Customer.create
post "/merchants/:merchant_id/customers" do
- customer = Hash.from_xml(request.body).delete("customer")
- if FakeBraintree.failure?(customer["credit_card"]["number"])
- gzipped_response(422, FakeBraintree.failure_response(customer["credit_card"]["number"]).to_xml(:root => 'api_error_response'))
+ customer = Customer.new(request, params[:merchant_id])
+ if customer.invalid?
+ customer.failure_response
else
- if verify_credit_card?(customer) && has_invalid_credit_card?(customer)
- gzipped_response(422, FakeBraintree.failure_response(customer["credit_card"]["number"]).to_xml(:root => 'api_error_response'))
- else
- customer["id"] ||= md5("#{params[:merchant_id]}#{Time.now.to_f}")
- customer["merchant-id"] = params[:merchant_id]
- if customer["credit_card"] && customer["credit_card"].is_a?(Hash)
- customer["credit_card"].delete("__content__")
- if !customer["credit_card"].empty?
- customer["credit_card"]["last_4"] = customer["credit_card"].delete("number")[-4..-1]
- customer["credit_card"]["token"] = md5("#{customer['merchant_id']}#{customer['id']}#{Time.now.to_f}")
- expiration_date = customer["credit_card"].delete("expiration_date")
- customer["credit_card"]["expiration_month"] = expiration_date.split('/')[0]
- customer["credit_card"]["expiration_year"] = expiration_date.split('/')[1]
-
- credit_card = customer.delete("credit_card")
- customer["credit_cards"] = [credit_card]
- end
- end
- FakeBraintree.customers[customer["id"]] = customer
- gzipped_response(201, customer.to_xml(:root => 'customer'))
- end
+ customer_hash = customer.customer_hash
+ FakeBraintree.customers[customer_hash["id"]] = customer_hash
+ gzipped_response(201, customer_hash.to_xml(:root => 'customer'))
end
end

0 comments on commit 0259b9d

Please sign in to comment.