jngo (owner)

Revisions

gist: 119088 Download_button fork
public
Description:
Implementing single name attribute for ActiveMerchant's CreditCard class.
Public Clone URL: git://gist.github.com/119088.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class CreditCard < ActiveMerchant::Billing::CreditCard
  def name_on_card
    "#{first_name} #{last_name}".strip
  end
 
  def name_on_card=(name)
    if name and match = name.strip.match(/^(.+)\s([^\s]+)$/)
      self.first_name, self.last_name = match[1], match[2]
    else
      self.first_name, self.last_name = name, ''
    end
  end
 
  def number=(number)
    super
    self.type = ActiveMerchant::Billing::CreditCard.type?(number)
  end
 
  def validate
    validate_essential_attributes
 
    return if type == 'bogus'
 
    validate_card_number
    validate_card_type
    validate_verification_value
    validate_switch_or_solo_attributes
  end
 
  private
 
  def validate_essential_attributes
    super
    errors_on_first_name = errors.delete(:first_name)
    errors_on_last_name = errors.delete(:last_name)
 
    if name_on_card.blank?
      errors.add(:name_on_card, "cannot be empty")
    elsif errors_on_first_name or errors_on_last_name
      errors.add(:name_on_card, "is invalid; please enter a first and last name")
    end
  end
 
  def validate_card_type
    unless errors.on(:number) or CreditCard.card_companies.keys.include?(type)
      errors.add(:type, "is not accepted")
    end
  end
end