Class: ActiveMerchant::Billing::CreditCard

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

Overview

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

Defined Under Namespace

Classes: ExpiryDate, ExpiryMonth, ExpiryYear

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Validateable

#errors, #initialize, #valid?

Instance Attribute Details

#first_nameObject

required



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

def first_name
  @first_name
end

#last_nameObject

required



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

def last_name
  @last_name
end

#monthObject

required



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

def month
  @month
end

#numberObject

required



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

def number
  @number
end

#typeObject

required



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

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



86
87
88
# File 'lib/active_merchant/billing/credit_card.rb', line 86

def verification_value
  @verification_value
end

#yearObject

required



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

def year
  @year
end

Class Method Details

.card_companiesObject

Regular expressions for the known card companies

Known card types

Card Type                         Prefix                           Length
--------------------------------------------------------------------------
master                            51-55                            16
visa                              4                                13, 16
american_express                  34, 37                           15
diners_club                       300-305, 36, 38                  14
discover                          6011                             16
jcb                               3                                16
jcb                               2131, 1800                       15
switch                            various                          16,18,19
solo                              63, 6767                         16,18,19


153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/active_merchant/billing/credit_card.rb', line 153

def self.card_companies
  { 
    'visa' =>  /^4\d{12}(\d{3})?$/,
    'master' =>  /^5[1-5]\d{14}$/,
    'discover' =>  /^6011\d{12}$/,
    'american_express' =>  /^3[47]\d{13}$/,
    'diners_club' =>  /^3(0[0-5]|[68]\d)\d{11}$/,
    'jcb' =>  /^(3\d{4}|2131|1800)\d{11}$/,
    'switch' =>  [/^49(03(0[2-9]|3[5-9])|11(0[1-2]|7[4-9]|8[1-2])|36[0-9]{2})\d{10}(\d{2,3})?$/, /^564182\d{10}(\d{2,3})?$/, /^6(3(33[0-4][0-9])|759[0-9]{2})\d{10}(\d{2,3})?$/],
    'solo' =>  /^6(3(34[5-9][0-9])|767[0-9]{2})\d{10}(\d{2,3})?$/ 
  }
end

.requires_verification_value?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/active_merchant/billing/credit_card.rb', line 14

def self.requires_verification_value?
  require_verification_value
end

.type?(number) ⇒ Boolean

Returns a string containing the type of card from the list of known information below.

Returns:

  • (Boolean)


167
168
169
170
171
172
173
174
175
# File 'lib/active_merchant/billing/credit_card.rb', line 167

def self.type?(number)
  return 'visa' if Base.gateway_mode == :test and ['1','2','3','success','failure','error'].include?(number.to_s)
  
  card_companies.each do |company, patterns|
    return company if [patterns].flatten.any? { |pattern| number =~ pattern  } 
  end

  return nil
end

.valid_number?(number) ⇒ Boolean

Returns true if it validates. Optionally, you can pass a card type as an argument and make sure it is of the correct type.

References

Returns:

  • (Boolean)


181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/active_merchant/billing/credit_card.rb', line 181

def self.valid_number?(number)
  return true if Base.gateway_mode == :test and ['1','2','3','success','failure','error'].include?(number.to_s)
  
  return false unless number.to_s.length >= 13

  sum = 0
  for i in 0..number.length
    weight = number[-1 * (i + 2), 1].to_i * (2 - (i % 2))
    sum += (weight < 10) ? weight : weight - 9
  end

  (number[-1,1].to_i == (10 - sum % 10) % 10)
end

Instance Method Details

#before_validateObject



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

def before_validate
  self.type.downcase! if type.respond_to?(:downcase)
  self.month = month.to_i
  self.year = year.to_i
  self.number.to_s.gsub!(/[^\d]/, "")
end

#display_numberObject

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



196
197
198
# File 'lib/active_merchant/billing/credit_card.rb', line 196

def display_number
  "XXXX-XXXX-XXXX-#{last_digits}"
end

#expired?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/active_merchant/billing/credit_card.rb', line 116

def expired?
  expiry_date.expired?
end

#expiry_dateObject



212
213
214
# File 'lib/active_merchant/billing/credit_card.rb', line 212

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

#first_name?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/active_merchant/billing/credit_card.rb', line 124

def first_name?
  @first_name != nil
end

#last_digitsObject



200
201
202
# File 'lib/active_merchant/billing/credit_card.rb', line 200

def last_digits
  number.nil? ? "" : number.last(4)
end

#last_name?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/active_merchant/billing/credit_card.rb', line 128

def last_name?
  @last_name != nil
end

#nameObject



132
133
134
# File 'lib/active_merchant/billing/credit_card.rb', line 132

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

#name?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/active_merchant/billing/credit_card.rb', line 120

def name?
  @first_name != nil and @last_name != nil
end

#validateObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/active_merchant/billing/credit_card.rb', line 95

def validate
  @errors.add "year", "expired"                             if expired?
       
  @errors.add "first_name", "cannot be empty"               if @first_name.blank?
  @errors.add "last_name", "cannot be empty"                if @last_name.blank?
  @errors.add "month", "cannot be empty"                    unless month.valid?
  @errors.add "year", "cannot be empty"                     unless year.valid?

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

  @errors.add "number", "is not a valid credit card number" unless CreditCard.valid_number?(number)                     
  @errors.add "type", "is invalid"                         unless CreditCard.card_companies.keys.include?(type)
  @errors.add "type", "is not the correct card type"        unless CreditCard.type?(number) == type
  
  if CreditCard.requires_verification_value?
    @errors.add "verification_value", "is required" unless verification_value?
  end
end

#verification_value?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/active_merchant/billing/credit_card.rb', line 136

def verification_value?
  !@verification_value.blank?
end