Skip to content

Instantly share code, notes, and snippets.

@manveru
Created July 25, 2010 11:51
Show Gist options
  • Save manveru/489510 to your computer and use it in GitHub Desktop.
Save manveru/489510 to your computer and use it in GitHub Desktop.
Check CreditCard IIN to get issuing network
def check(number)
number = number.gsub(/\D+/, '')
first = Hash.new{|h,k| h[k] = number[0, k].to_i }
size = number.size
# American Express 34, 37 Yes 15 Luhn algorithm AmEx
if [13, 16].include?(size) && number =~ /^(34|37)/
:AmEx
# China UnionPay 622126-622925, 624-626, 6282-6288 Yes 16-19 unknown CUP
elsif (16..19) === size && ((622126..622925) === first[6] ||
(624..626) === first[3] ||
(6282..6288) === first[4])
:CUP
# Diners Club Carte Blanche 300-305 Yes 14 Luhn algorithm DC-CB
elsif 14 == size && (300..305) === first[3]
:DC_CB
# Diners Club International 36 Yes 14 Luhn algorithm DC-Int
elsif 14 == size && 36 == first[2]
:DC_Int
# Diners Club United States & Canada 54, 55 Yes 16 Luhn algorithm DC-UC
elsif 16 == size && (54..55) === first[2]
:DC_UC
# Discover Card 6011, 622126-622925, 644-649, 65 Yes 16 Luhn algorithm Disc
elsif 16 == size && (6011 == first[4] ||
(622126..622925) === first[6] ||
(644..649) === first[3] ||
65 == first[1])
:Disc
# InstaPayment 639-637 Yes 16 Luhn algorithm IPI
elsif 16 == size && (637..639) === first[3]
:IPI
# JCB 3528-3589 Yes 16 Luhn algorithm JCB
elsif 16 == size && (3528..3589) === first[4]
:JCB
# Laser 6304, 6706, 6771, 6709 Yes 16-19 unknown Lasr
elsif (16..19) === size && [6304, 6706, 6771, 6709].include?(first[4])
:Lasr
# Maestro 5018, 5020, 5038, 6304, 6759, 6761, 6763 Yes 12-19 Luhn algorithm Maes
elsif (12..19) === size && [5018, 5020, 5038, 6304, 6759, 6761, 6763].include?(first[4])
:Maes
# MasterCard 51-55 Yes 16 Luhn algorithm MC
elsif 16 == size && (51..55) === first[2]
:MC
# Solo 6334, 6767 Yes 16, 18, 19 Luhn algorithm Solo
elsif [16, 18, 19].include?(size) && [6334, 6767].include?(first[4])
:Solo
# Switch 4903, 4905, 4911, 4936, 564182, 633110, 6333, 6759 Yes 16, 18, 19 Luhn algorithm Swch
elsif [16, 18, 19].include?(size) && (
[4903, 4905, 4911, 4936, 6333, 6759].include?(first[4]) ||
[564182, 633110].include?(first[6]))
:Swch
# Visa 4 Yes 16 Luhn algorithm Visa
elsif size == 16 && 4 == first[1]
:Visa
# Visa Electron 4026, 417500, 4508, 4844, 4913, 4917 Yes 16 Luhn algorithm Visa
elsif size == 16 && ([4026, 4508, 4844, 4913, 4917].include?(first[4]) || 417500 == first[6])
:Visa
end
end
['4111 1111 1111 1111',
'5500 0000 0000 0004',
'3400 0000 0000 009',
'3000 0000 0000 04',
'3000 0000 0000 04',
'6011 0000 0000 0004',
'3588 0000 0000 0009',
].each do |number|
p number => check(number)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment