Skip to content
This repository
Browse code

Move customer CC creation logic into method. Generate CC token in a d…

…ifferent way.
  • Loading branch information...
commit 7bd4e2067f2eb9a0d77cc6971e51544624b1e2cf 1 parent 2dde9ed
Gabe Berke-Williams authored January 13, 2012

Showing 1 changed file with 22 additions and 25 deletions. Show diff stats Hide diff stats

  1. 47  lib/fake_braintree/customer.rb
47  lib/fake_braintree/customer.rb
@@ -41,24 +41,7 @@ def delete
41 41
     def customer_hash
42 42
       hash = @customer_hash.dup
43 43
       hash["id"] ||= create_id
44  
-
45  
-      if hash["credit_card"].present? && hash["credit_card"].is_a?(Hash)
46  
-        hash["credit_card"]["last_4"] = last_four(hash)
47  
-        hash["credit_card"]["token"]  = credit_card_token(hash)
48  
-
49  
-        if credit_card_expiration_month
50  
-          hash["credit_card"]["expiration_month"] = credit_card_expiration_month
51  
-        end
52  
-
53  
-        if credit_card_expiration_year
54  
-          hash["credit_card"]["expiration_year"] = credit_card_expiration_year
55  
-        end
56  
-
57  
-        credit_card = hash.delete("credit_card")
58  
-        hash["credit_cards"] = [credit_card]
59  
-      else
60  
-        hash["credit_cards"] = []
61  
-      end
  44
+      hash["credit_cards"] = generate_credit_card_hash_from(hash["credit_card"])
62 45
 
63 46
       hash
64 47
     end
@@ -132,6 +115,25 @@ def credit_card_expiration_date
132 115
       end
133 116
     end
134 117
 
  118
+    def generate_credit_card_hash_from(new_credit_card_hash)
  119
+      if new_credit_card_hash.present? && new_credit_card_hash.is_a?(Hash)
  120
+        new_credit_card_hash["last_4"] = new_credit_card_hash["number"][-4..-1]
  121
+        new_credit_card_hash["token"]  = credit_card_token(new_credit_card_hash)
  122
+
  123
+        if credit_card_expiration_month
  124
+          new_credit_card_hash["expiration_month"] = credit_card_expiration_month
  125
+        end
  126
+
  127
+        if credit_card_expiration_year
  128
+          new_credit_card_hash["expiration_year"] = credit_card_expiration_year
  129
+        end
  130
+
  131
+        [new_credit_card_hash]
  132
+      else
  133
+        []
  134
+      end
  135
+    end
  136
+
135 137
     def delete_customer_with_id(id)
136 138
       FakeBraintree.registry.customers[id] = nil
137 139
     end
@@ -172,13 +174,8 @@ def credit_card_hash
172 174
       @customer_hash["credit_card"] || {}
173 175
     end
174 176
 
175  
-    def credit_card_token(hash)
176  
-      md5("#{hash['merchant_id']}#{hash['id']}")
  177
+    def credit_card_token(credit_card_hash_without_token)
  178
+      md5("#{credit_card_hash_without_token["number"]}#{@customer_hash['merchant_id']}")
177 179
     end
178  
-
179  
-    def last_four(hash)
180  
-      hash["credit_card"].delete("number")[-4..-1]
181  
-    end
182  
-
183 180
   end
184 181
 end

0 notes on commit 7bd4e20

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