2004. április 7., szerda

Yet Another Credit Card Validator


Problem/Question/Abstract:

A routine to validate and identify Visa/MasterCard/Amex credit cards. Might come handy if you want to perform arbitrary card checking. Original code by Daniel J. Karnes (1990)

Answer:

{-------------------------------------------------
  Credit card validator

  Returns:

   0  : Card is invalid or unknown
   1  : Card is a valid AmEx
   2  : Card is a valid Visa
   3  : Card is a valid MasterCard

-------------------------------------------------}

function Vc(c: string): integer;
var
  card: string[21];
  Vcard: array[0..21] of byte absolute card;
  Xcard: integer;
  Cstr: string[21];
  y, x: integer;
begin
  Cstr := '';
  fillchar(Vcard, 22, #0);
  card := c;
  for x := 1 to 20 do
    if (Vcard[x] in [48..57]) then
      Cstr := Cstr + chr(Vcard[x]);
  card := '';
  card := Cstr;
  Xcard := 0;
  if not odd(length(card)) then
    for x := (length(card) - 1) downto 1 do
    begin
      if odd(x) then
        y := ((Vcard[x] - 48) * 2)
      else
        y := (Vcard[x] - 48);
      if (y >= 10) then
        y := ((y - 10) + 1);
      Xcard := (Xcard + y)
    end
  else
    for x := (length(card) - 1) downto 1 do
    begin
      if odd(x) then
        y := (Vcard[x] - 48)
      else
        y := ((Vcard[x] - 48) * 2);
      if (y >= 10) then
        y := ((y - 10) + 1);
      Xcard := (Xcard + y)
    end;
  x := (10 - (Xcard mod 10));
  if (x = 10) then
    x := 0;
  if (x = (Vcard[length(card)] - 48)) then
    Vc := ord(Cstr[1]) - ord('2')
  else
    Vc := 0
end;

Nincsenek megjegyzések:

Megjegyzés küldése