Class: CreditCard

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/credit_card.rb

Constant Summary collapse

CARD_TYPES =
[["Visa", "Visa"], ["Mastercard", "Mastercard"], ["American Express", "American Express" ], ["Discover", "Discover"]]
MONTHS =
(1..12).to_a
YEARS =
((Date.today.year)..(Date.today.year + 8)).to_a

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#card_typeObject

Returns the value of attribute card_type.



15
16
17
# File 'app/models/credit_card.rb', line 15

def card_type
  @card_type
end

#card_verificationObject

Returns the value of attribute card_verification.



15
16
17
# File 'app/models/credit_card.rb', line 15

def card_verification
  @card_verification
end

#expiry_monthObject

Returns the value of attribute expiry_month.



15
16
17
# File 'app/models/credit_card.rb', line 15

def expiry_month
  @expiry_month
end

#expiry_yearObject

Returns the value of attribute expiry_year.



15
16
17
# File 'app/models/credit_card.rb', line 15

def expiry_year
  @expiry_year
end

#first_nameObject

Returns the value of attribute first_name.



15
16
17
# File 'app/models/credit_card.rb', line 15

def first_name
  @first_name
end

#last_nameObject

Returns the value of attribute last_name.



15
16
17
# File 'app/models/credit_card.rb', line 15

def last_name
  @last_name
end

Instance Method Details

#active_merchant_cardObject



51
52
53
54
55
56
57
58
59
60
61
# File 'app/models/credit_card.rb', line 51

def active_merchant_card
  @card_attributes ||= ActiveMerchant::Billing::CreditCard.new(
    :type               => card_type,
    :number             => card_number,
    :verification_value => card_verification,
    :month              => expiry_month,
    :year               => expiry_year,
    :first_name         => first_name,
    :last_name          => last_name
  )
end

#card_attributes=(cstring) ⇒ Object



63
64
65
# File 'app/models/credit_card.rb', line 63

def card_attributes=(cstring)
  @card_attributes = cstring
end

#validate_cardObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/credit_card.rb', line 30

def validate_card
  unless active_merchant_card.valid?
      active_merchant_card.errors.full_messages.each do |message|
      case message
      when "First name connot be empty"
        errors[:card_name] = "cannot be empty"
      when "Last name cannot be empty"
        errors[:card_name] = "cannot be empty"
      when "Month is not a valid month"
        errors[:expiry_month] = "Month is not a valid month"
      when "Year expired"
        errors[:expiry_year] = "Year expired"
      when "Number is not a valid credit card number"
        errors[:card_number] = "is not a valid credit card number"
      when "Verification value is required"
        errors[:card_verification] = "value is required"
      end
    end
  end
end