def match_cc_number(card, card_type)
orig_card = card
card_type = card_type.to_s
index = nil
digit = nil
multiplier = 2
sum = 0
return nil if card.length == 0
return nil unless card_type =~ /^[admv]/i
return nil if ((card_type =~ /^v/i && card[0,1] != "4") ||
(card_type =~ /^m/i && card[0,2] !~ /^51|55$/) ||
(card_type =~ /^d/i && card[0,4] !~ "6011") ||
(card_type =~ /^a/i && card[0,2] !~ /^34|37$/))
card.gsub!(" ", "")
return nil if card !~ /^\d+$/
digit = card[0,1]
index = (card.length-1)
return nil if ((digit == "3" && index != 14) ||
(digit == "4" && index != 12 && index != 15) ||
(digit == "5" && index != 15) ||
(digit == "6" && index != 13 && index != 15))
(index-1).downto(0) do |i|
digit = card[i, 1].to_i
product = multiplier * digit
sum += (product > 9) ? (product-9) : product
multiplier = 3 - multiplier
end
sum %= 10
sum = 10 - sum unless sum == 0
if sum.to_s == card[-1,1]
match = /^([\d\s]*)$/.match(orig_card)
return match ? match[1] : nil
end
end