Module: ActiveMerchant::Billing::CreditCardMethods::ClassMethods

Defined in:
lib/active_merchant/billing/credit_card_methods.rb

Instance Method Summary collapse

Instance Method Details

#brand?(number) ⇒ Boolean

Returns a string containing the brand of card from the list of known information below. Need to check the cards in a particular order, as there is some overlap of the allowable ranges – TODO Refactor this method. We basically need to tighten up the Maestro Regexp.

Right now the Maestro regexp overlaps with the MasterCard regexp (IIRC). If we can tighten things up, we can boil this whole thing down to something like…

def brand?(number)
  return 'visa' if valid_test_mode_card_number?(number)
  card_companies.find([nil]) { |brand, regexp| number =~ regexp }.first.dup
end

Returns:

  • (Boolean)


126
127
128
129
130
131
132
133
134
135
136
# File 'lib/active_merchant/billing/credit_card_methods.rb', line 126

def brand?(number)
  return 'bogus' if valid_test_mode_card_number?(number)

  card_companies.reject { |c,p| c == 'maestro' }.each do |company, pattern|
    return company.dup if number =~ pattern
  end

  return 'maestro' if number =~ card_companies['maestro']

  return nil
end

#card_companiesObject



109
110
111
# File 'lib/active_merchant/billing/credit_card_methods.rb', line 109

def card_companies
  CARD_COMPANIES
end

#electron?(number) ⇒ Boolean

Returns:

  • (Boolean)


138
139
140
141
142
143
144
145
146
147
# File 'lib/active_merchant/billing/credit_card_methods.rb', line 138

def electron?(number)
  return false unless [16, 19].include?(number.length)

  # don't recalculate for each range
  bank_identification_number = first_digits(number).to_i

  ELECTRON_RANGES.any? do |range|
    range.include?(bank_identification_number)
  end
end

#first_digits(number) ⇒ Object



154
155
156
# File 'lib/active_merchant/billing/credit_card_methods.rb', line 154

def first_digits(number)
  number.to_s.slice(0,6)
end

#last_digits(number) ⇒ Object



158
159
160
# File 'lib/active_merchant/billing/credit_card_methods.rb', line 158

def last_digits(number)
  number.to_s.length <= 4 ? number : number.to_s.slice(-4..-1)
end

#mask(number) ⇒ Object



162
163
164
# File 'lib/active_merchant/billing/credit_card_methods.rb', line 162

def mask(number)
  "XXXX-XXXX-XXXX-#{last_digits(number)}"
end

#matching_brand?(number, brand) ⇒ Boolean

Checks to see if the calculated brand matches the specified brand

Returns:

  • (Boolean)


167
168
169
# File 'lib/active_merchant/billing/credit_card_methods.rb', line 167

def matching_brand?(number, brand)
  brand?(number) == brand
end

#matching_type?(number, brand) ⇒ Boolean

Returns:

  • (Boolean)


171
172
173
174
# File 'lib/active_merchant/billing/credit_card_methods.rb', line 171

def matching_type?(number, brand)
  ActiveMerchant.deprecated "CreditCard#matching_type? is deprecated and will be removed from a future release of ActiveMerchant. Please use CreditCard#matching_brand? instead."
  matching_brand?(number, brand)
end

#type?(number) ⇒ Boolean

Returns:

  • (Boolean)


149
150
151
152
# File 'lib/active_merchant/billing/credit_card_methods.rb', line 149

def type?(number)
  ActiveMerchant.deprecated "CreditCard#type? is deprecated and will be removed from a future release of ActiveMerchant. Please use CreditCard#brand? instead."
  brand?(number)
end

#valid_number?(number) ⇒ Boolean

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

References:

Returns:

  • (Boolean)


97
98
99
100
101
102
# File 'lib/active_merchant/billing/credit_card_methods.rb', line 97

def valid_number?(number)
  valid_test_mode_card_number?(number) ||
    valid_card_number_length?(number) &&
    valid_card_number_characters?(number) &&
    valid_checksum?(number)
end