lib/fake_braintree/customer.rb in fake_braintree-0.2.1 vs lib/fake_braintree/customer.rb in fake_braintree-0.3

- old
+ new

@@ -2,24 +2,25 @@ class Customer include Helpers def initialize(customer_hash_from_params, options) @customer_hash = { - "id" => options[:id], - "merchant_id" => options[:merchant_id] - }.merge(customer_hash_from_params) - + 'id' => options[:id], + 'merchant_id' => options[:merchant_id] + } + @customer_hash.merge!(customer_hash_from_params) set_customer_id end def create if invalid? response_for_invalid_card else - credit_cards = customer_hash["credit_cards"] + credit_cards = customer_hash['credit_cards'] create_customer_with(customer_hash) credit_cards.each { |card| add_credit_card_to_registry(card) } + set_default_credit_card credit_cards.first response_for_created_customer(customer_hash) end end def update @@ -46,24 +47,24 @@ def invalid? credit_card_is_failure? || invalid_credit_card? end def create_customer_with(hash) - FakeBraintree.registry.customers[hash["id"]] = hash + FakeBraintree.registry.customers[hash['id']] = hash end def add_credit_card_to_registry(new_credit_card_hash) - token = new_credit_card_hash["token"] + token = new_credit_card_hash['token'] FakeBraintree.registry.credit_cards[token] = new_credit_card_hash end def update_existing_customer(updates_hash) customer_from_registry.merge!(updates_hash) end def customer_hash - @customer_hash.merge("credit_cards" => generate_credit_cards_from(@customer_hash["credit_card"])) + @customer_hash.merge('credit_cards' => generate_credit_cards_from(@customer_hash['credit_card'])) end def customer_from_registry FakeBraintree.registry.customers[customer_id] end @@ -81,38 +82,45 @@ end def verify_credit_card?(customer_hash_for_verification) return true if FakeBraintree.verify_all_cards - credit_card_hash_for_verification = customer_hash_for_verification["credit_card"] + credit_card_hash_for_verification = customer_hash_for_verification['credit_card'] if credit_card_hash_for_verification.is_a?(Hash) && - credit_card_hash_for_verification.key?("options") - options = credit_card_hash_for_verification["options"] - options["verify_card"] == true + credit_card_hash_for_verification.key?('options') + options = credit_card_hash_for_verification['options'] + options['verify_card'] == true end end def has_invalid_credit_card?(customer_hash) credit_card_number && ! FakeBraintree::VALID_CREDIT_CARDS.include?(credit_card_number) end def credit_card_number - credit_card_hash["number"] + credit_card_hash['number'] end + def set_default_credit_card(credit_card_hash) + if credit_card_hash + CreditCard.new(credit_card_hash, :customer_id => @customer_hash['id'], :make_default => true).update + end + end + def generate_credit_cards_from(new_credit_card_hash) if new_credit_card_hash.present? && new_credit_card_hash.is_a?(Hash) - new_credit_card_hash["last_4"] = new_credit_card_hash["number"][-4..-1] - new_credit_card_hash["token"] = credit_card_token(new_credit_card_hash) + new_credit_card_hash['bin'] = new_credit_card_hash['number'][0..5] + new_credit_card_hash['last_4'] = new_credit_card_hash['number'][-4..-1] + new_credit_card_hash['token'] = credit_card_token(new_credit_card_hash) if credit_card_expiration_month - new_credit_card_hash["expiration_month"] = credit_card_expiration_month + new_credit_card_hash['expiration_month'] = credit_card_expiration_month end if credit_card_expiration_year - new_credit_card_hash["expiration_year"] = credit_card_expiration_year + new_credit_card_hash['expiration_year'] = credit_card_expiration_year end [new_credit_card_hash] else [] @@ -126,12 +134,12 @@ def credit_card_expiration_year credit_card_expiration_date[1] end def credit_card_expiration_date - if credit_card_hash.key?("expiration_date") - credit_card_hash["expiration_date"].split('/') + if credit_card_hash.key?('expiration_date') + credit_card_hash['expiration_date'].split('/') else [] end end @@ -162,25 +170,25 @@ def failure_response(code) gzipped_response(code, FakeBraintree.failure_response(credit_card_number).to_xml(:root => 'api_error_response')) end def customer_id - customer_hash["id"] + customer_hash['id'] end def has_credit_card? credit_card_hash.present? end def credit_card_hash - @customer_hash["credit_card"] || {} + @customer_hash['credit_card'] || {} end def set_customer_id - @customer_hash["id"] ||= create_id(@customer_hash["merchant_id"]) + @customer_hash['id'] ||= create_id(@customer_hash['merchant_id']) end def credit_card_token(credit_card_hash_without_token) - md5("#{credit_card_hash_without_token["number"]}#{@customer_hash["merchant_id"]}") + md5("#{credit_card_hash_without_token['number']}#{@customer_hash['merchant_id']}") end end end