Skip to content
This repository
Browse code

Massively clean up Customer creation code.

  • Loading branch information...
commit 0259b9d74efc621e0cb7614290d295b90ac88044 1 parent cc7463f
Gabe Berke-Williams authored November 03, 2011
3  lib/fake_braintree.rb
@@ -2,6 +2,9 @@
2 2
 require 'braintree'
3 3
 require 'sham_rack'
4 4
 
  5
+require 'fake_braintree/helpers'
  6
+require 'fake_braintree/customer'
  7
+
5 8
 require 'fake_braintree/sinatra_app'
6 9
 require 'fake_braintree/valid_credit_cards'
7 10
 require 'fake_braintree/version'
49  lib/fake_braintree/customer.rb
... ...
@@ -0,0 +1,49 @@
  1
+module FakeBraintree
  2
+  class Customer
  3
+    include Helpers
  4
+
  5
+    def initialize(request, merchant_id)
  6
+      @customer_hash = Hash.from_xml(request.body).delete("customer")
  7
+      @merchant_id   = merchant_id
  8
+    end
  9
+
  10
+    def invalid?
  11
+      credit_card_is_failure? || invalid_credit_card?
  12
+    end
  13
+
  14
+    def failure_response
  15
+      gzipped_response(422, FakeBraintree.failure_response(@customer_hash["credit_card"]["number"]).to_xml(:root => 'api_error_response'))
  16
+    end
  17
+
  18
+    def customer_hash
  19
+      hash = @customer_hash.dup
  20
+      hash["id"] ||= md5("#{@merchant_id}#{Time.now.to_f}")
  21
+      hash["merchant-id"] = @merchant_id
  22
+      if hash["credit_card"] && hash["credit_card"].is_a?(Hash)
  23
+        hash["credit_card"].delete("__content__")
  24
+        if !hash["credit_card"].empty?
  25
+          hash["credit_card"]["last_4"]           = hash["credit_card"].delete("number")[-4..-1]
  26
+          hash["credit_card"]["token"]            = md5("#{hash['merchant_id']}#{hash['id']}#{Time.now.to_f}")
  27
+          expiration_date = hash["credit_card"].delete("expiration_date")
  28
+          hash["credit_card"]["expiration_month"] = expiration_date.split('/')[0]
  29
+          hash["credit_card"]["expiration_year"]  = expiration_date.split('/')[1]
  30
+
  31
+          credit_card = hash.delete("credit_card")
  32
+          hash["credit_cards"] = [credit_card]
  33
+        end
  34
+      end
  35
+
  36
+      hash
  37
+    end
  38
+
  39
+    private
  40
+
  41
+    def credit_card_is_failure?
  42
+      FakeBraintree.failure?(@customer_hash["credit_card"]["number"])
  43
+    end
  44
+
  45
+    def invalid_credit_card?
  46
+      verify_credit_card?(@customer_hash) && has_invalid_credit_card?(@customer_hash)
  47
+    end
  48
+  end
  49
+end
31  lib/fake_braintree/helpers.rb
... ...
@@ -0,0 +1,31 @@
  1
+require 'digest/md5'
  2
+require 'active_support'
  3
+require 'active_support/core_ext'
  4
+
  5
+module FakeBraintree
  6
+  module Helpers
  7
+    def gzip(content)
  8
+      ActiveSupport::Gzip.compress(content)
  9
+    end
  10
+
  11
+    def gzipped_response(status_code, uncompressed_content)
  12
+      [status_code, { "Content-Encoding" => "gzip" }, gzip(uncompressed_content)]
  13
+    end
  14
+
  15
+    def md5(content)
  16
+      Digest::MD5.hexdigest(content)
  17
+    end
  18
+
  19
+    def verify_credit_card?(customer_hash)
  20
+      return true if FakeBraintree.verify_all_cards
  21
+
  22
+      customer_hash["credit_card"].key?("options") &&
  23
+        customer_hash["credit_card"]["options"].is_a?(Hash) &&
  24
+        customer_hash["credit_card"]["options"]["verify_card"] == true
  25
+    end
  26
+
  27
+    def has_invalid_credit_card?(customer_hash)
  28
+      ! FakeBraintree::VALID_CREDIT_CARDS.include?(customer_hash["credit_card"]["number"])
  29
+    end
  30
+  end
  31
+end
35  lib/fake_braintree/sinatra_app.rb
... ...
@@ -1,7 +1,4 @@
1  
-require 'digest/md5'
2  
-require 'sinatra'
3  
-require 'active_support'
4  
-require 'active_support/core_ext'
  1
+require 'sinatra/base'
5 2
 
6 3
 module FakeBraintree
7 4
   class SinatraApp < Sinatra::Base
@@ -38,31 +35,13 @@ def has_invalid_credit_card?(customer_hash)
38 35
 
39 36
     # Braintree::Customer.create
40 37
     post "/merchants/:merchant_id/customers" do
41  
-      customer = Hash.from_xml(request.body).delete("customer")
42  
-      if FakeBraintree.failure?(customer["credit_card"]["number"])
43  
-        gzipped_response(422, FakeBraintree.failure_response(customer["credit_card"]["number"]).to_xml(:root => 'api_error_response'))
  38
+      customer = Customer.new(request, params[:merchant_id])
  39
+      if customer.invalid?
  40
+        customer.failure_response
44 41
       else
45  
-        if verify_credit_card?(customer) && has_invalid_credit_card?(customer)
46  
-          gzipped_response(422, FakeBraintree.failure_response(customer["credit_card"]["number"]).to_xml(:root => 'api_error_response'))
47  
-        else
48  
-          customer["id"] ||= md5("#{params[:merchant_id]}#{Time.now.to_f}")
49  
-          customer["merchant-id"] = params[:merchant_id]
50  
-          if customer["credit_card"] && customer["credit_card"].is_a?(Hash)
51  
-            customer["credit_card"].delete("__content__")
52  
-            if !customer["credit_card"].empty?
53  
-              customer["credit_card"]["last_4"]           = customer["credit_card"].delete("number")[-4..-1]
54  
-              customer["credit_card"]["token"]            = md5("#{customer['merchant_id']}#{customer['id']}#{Time.now.to_f}")
55  
-              expiration_date = customer["credit_card"].delete("expiration_date")
56  
-              customer["credit_card"]["expiration_month"] = expiration_date.split('/')[0]
57  
-              customer["credit_card"]["expiration_year"]  = expiration_date.split('/')[1]
58  
-
59  
-              credit_card = customer.delete("credit_card")
60  
-              customer["credit_cards"] = [credit_card]
61  
-            end
62  
-          end
63  
-          FakeBraintree.customers[customer["id"]] = customer
64  
-          gzipped_response(201, customer.to_xml(:root => 'customer'))
65  
-        end
  42
+        customer_hash = customer.customer_hash
  43
+        FakeBraintree.customers[customer_hash["id"]] = customer_hash
  44
+        gzipped_response(201, customer_hash.to_xml(:root => 'customer'))
66 45
       end
67 46
     end
68 47
 

0 notes on commit 0259b9d

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