diff --git a/lib/fake_braintree/customer.rb b/lib/fake_braintree/customer.rb index 7b0ce17..7b2ce27 100644 --- a/lib/fake_braintree/customer.rb +++ b/lib/fake_braintree/customer.rb @@ -25,15 +25,12 @@ 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) - 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']}") - if 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] - end + hash["credit_card"]["last_4"] = last_four(hash) + hash["credit_card"]["token"] = credit_card_token(hash) + split_expiration_date_into_month_and_year!(hash) credit_card = hash.delete("credit_card") hash["credit_cards"] = [credit_card] @@ -45,6 +42,21 @@ def customer_hash private + def split_expiration_date_into_month_and_year!(hash) + if 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] + end + end + + def credit_card_token(hash) + md5("#{hash['merchant_id']}#{hash['id']}") + end + + 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')) end