Class: ActiveMerchant::Billing::CreditCard

Inherits:
Object
  • Object
show all
Includes:
CreditCardMethods, Validateable
Defined in:
lib/active_merchant/billing/credit_card.rb,
lib/active_merchant/billing/expiry_date.rb

Overview

Description

This credit card object can be used as a stand alone object. It acts just like an ActiveRecord object but doesn’t support the .save method as its not backed by a database.

For testing purposes, use the ‘bogus’ credit card type. This card skips the vast majority of validations. This allows you to focus on your core concerns until you’re ready to be more concerned with the details of particular creditcards or your gateway.

Testing With CreditCard

Often when testing we don’t care about the particulars of a given card type. When using the ‘test’ mode in your Gateway, there are six different valid card numbers: 1, 2, 3, ‘success’, ‘fail’, and ‘error’.

– For details, see CreditCardMethods#valid_number? ++

Example Usage

cc = CreditCard.new(
  :first_name => 'Steve', 
  :last_name  => 'Smith', 
  :month      => '9', 
  :year       => '2010', 
  :type       => 'visa', 
  :number     => '4242424242424242'
)

cc.valid? # => true
cc.display_number # => XXXX-XXXX-XXXX-4242

Defined Under Namespace

Classes: ExpiryDate

Constant Summary

Constants included from CreditCardMethods

ActiveMerchant::Billing::CreditCardMethods::CARD_COMPANIES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Validateable

#errors, #initialize, #valid?

Methods included from CreditCardMethods

included, #valid_expiry_year?, #valid_issue_number?, #valid_month?, #valid_start_year?

Instance Attribute Details

#first_nameObject

Essential attributes for a valid, non-bogus creditcards



47
48
49
# File 'lib/active_merchant/billing/credit_card.rb', line 47

def first_name
  @first_name
end

#issue_numberObject

Required for Switch / Solo cards



50
51
52
# File 'lib/active_merchant/billing/credit_card.rb', line 50

def issue_number
  @issue_number
end

#last_nameObject

Essential attributes for a valid, non-bogus creditcards



47
48
49
# File 'lib/active_merchant/billing/credit_card.rb', line 47

def last_name
  @last_name
end

#monthObject

Essential attributes for a valid, non-bogus creditcards



47
48
49
# File 'lib/active_merchant/billing/credit_card.rb', line 47

def month
  @month
end

#numberObject

Essential attributes for a valid, non-bogus creditcards



47
48
49
# File 'lib/active_merchant/billing/credit_card.rb', line 47

def number
  @number
end

#start_monthObject

Required for Switch / Solo cards



50
51
52
# File 'lib/active_merchant/billing/credit_card.rb', line 50

def start_month
  @start_month
end

#start_yearObject

Required for Switch / Solo cards



50
51
52
# File 'lib/active_merchant/billing/credit_card.rb', line 50

def start_year
  @start_year
end

#typeObject Also known as: brand

Essential attributes for a valid, non-bogus creditcards



47
48
49
# File 'lib/active_merchant/billing/credit_card.rb', line 47

def type
  @type
end

#verification_valueObject

Optional verification_value (CVV, CVV2 etc). Gateways will try their best to run validation on the passed in value if it is supplied



54
55
56
# File 'lib/active_merchant/billing/credit_card.rb', line 54

def verification_value
  @verification_value
end

#yearObject

Essential attributes for a valid, non-bogus creditcards



47
48
49
# File 'lib/active_merchant/billing/credit_card.rb', line 47

def year
  @year
end

Class Method Details

.requires_verification_value?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/active_merchant/billing/credit_card.rb', line 108

def self.requires_verification_value?
  require_verification_value
end

Instance Method Details

#display_numberObject

Show the card number, with all but last 4 numbers replace with “X”. (XXXX-XXXX-XXXX-4338)



88
89
90
# File 'lib/active_merchant/billing/credit_card.rb', line 88

def display_number
  self.class.mask(number)
end

#expired?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/active_merchant/billing/credit_card.rb', line 63

def expired?
  expiry_date.expired?
end

#expiry_dateObject

Provides proxy access to an expiry date object



59
60
61
# File 'lib/active_merchant/billing/credit_card.rb', line 59

def expiry_date
  ExpiryDate.new(@month, @year)
end

#first_name?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/active_merchant/billing/credit_card.rb', line 71

def first_name?
  !@first_name.blank?
end

#last_digitsObject



92
93
94
# File 'lib/active_merchant/billing/credit_card.rb', line 92

def last_digits
  self.class.last_digits(number)
end

#last_name?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/active_merchant/billing/credit_card.rb', line 75

def last_name?
  !@last_name.blank?
end

#nameObject



79
80
81
# File 'lib/active_merchant/billing/credit_card.rb', line 79

def name
  "#{@first_name} #{@last_name}"
end

#name?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/active_merchant/billing/credit_card.rb', line 67

def name?
  first_name? && last_name?
end

#require_verification_valueObject

Attributes



43
# File 'lib/active_merchant/billing/credit_card.rb', line 43

cattr_accessor :require_verification_value

#validateObject



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/active_merchant/billing/credit_card.rb', line 96

def validate
  validate_essential_attributes

  # Bogus card is pretty much for testing purposes. Lets just skip these extra tests if its used
  return if type == 'bogus'

  validate_card_type
  validate_card_number
  validate_verification_value
  validate_switch_or_solo_attributes
end

#verification_value?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/active_merchant/billing/credit_card.rb', line 83

def verification_value?
  !@verification_value.blank?
end