Title: How to work with credit card numbers
Question: This comes from VariousUnit.Pas, that I made years ago (on Delphi 2.0). Three function that allows calculate last digit validity and get card's type. Works completely with 7 types of credit cards, but has no the algorithm for the enRoute cards.
Answer:
Const
ccUnknown : Integer = -1;
ccMasterCard : Integer = 1;
ccVisa : Integer = 2;
ccAmericanExpress : Integer = 3;
ccDinersClub : Integer = 4;
ccCarteBlanche : Integer = 4;
ccDiscover : Integer = 5;
ccenRoute : Integer = 6;
ccJCB : Integer = 7;
Function CheckCreditCard( CardNumber : String ) : Boolean;
Function GetCreditCardID( CardNumber : String ) : Integer;
Function GetCreditCardName( CardType : Integer ) : String;
Function CheckCreditCard( CardNumber : String ) : Boolean;
Var
CC : String;
Bits : Array[ 1..20 ] Of Byte;
IdX, Somma : Integer;
Begin
Result := False;
If ( GetCreditCardID( CardNumber ) = ccenRoute ) Then Exit;
If ( GetCreditCardID( CardNumber ) = ccUnknown ) Then Exit;
Somma := 0;
For IdX := 1 To 20 Do Bits[ IdX ] := 0;
For IdX := 1 To Length( CardNumber ) Do CC := CC + ' ';
For IdX := 1 To Length( CardNumber ) Do CC[ IdX ] := CardNumber[ Length( CardNumber ) - ( IdX - 1 ) ];
For IdX := 1 To Length( CC ) Do Bits[ IdX ] := Ord( CC[ IdX ] ) - 48;
For IdX := 1 To Length( CardNumber ) Do
If Bool( IdX Mod 2 ) Then Begin
Bits[ IdX ] := Bits[ IdX ] * 2;
If ( Bits[ IdX ] 10 ) Then Bits[ IdX ] := Bits[ IdX ] - 9;
End;
For IdX := 1 To Length( CardNumber ) Do
Somma := Somma + Bits[ IdX ];
If ( Somma Mod 10 = 0 ) Then Result := True;
End;
Function GetCreditCardID( CardNumber : String ) : Integer;
Var
L : Integer;
D1, D2, D3, D4 : String;
Begin
Result := ccUnknown;
L := Length( CardNumber );
D1 := Copy( CardNumber, 1, 1 );
D2 := Copy( CardNumber, 1, 2 );
D3 := Copy( CardNumber, 1, 3 );
D4 := Copy( CardNumber, 1, 4 );
If ( D1 = '4' ) And ( L = 16 ) Then Result := ccVisa;
If ( D1 = '4' ) And ( L = 13 ) Then Result := ccVisa;
If ( D2 = '51' ) And ( L = 16 ) Then Result := ccMasterCard;
If ( D2 = '52' ) And ( L = 16 ) Then Result := ccMasterCard;
If ( D2 = '53' ) And ( L = 16 ) Then Result := ccMasterCard;
If ( D2 = '54' ) And ( L = 16 ) Then Result := ccMasterCard;
If ( D2 = '55' ) And ( L = 16 ) Then Result := ccMasterCard;
If ( D2 = '34' ) And ( L = 15 ) Then Result := ccAmericanExpress;
If ( D2 = '37' ) And ( L = 15 ) Then Result := ccAmericanExpress;
If ( D3 = '300' ) And ( L = 14 ) Then Result := ccDinersClub;
If ( D3 = '301' ) And ( L = 14 ) Then Result := ccDinersClub;
If ( D3 = '302' ) And ( L = 14 ) Then Result := ccDinersClub;
If ( D3 = '303' ) And ( L = 14 ) Then Result := ccDinersClub;
If ( D3 = '304' ) And ( L = 14 ) Then Result := ccDinersClub;
If ( D3 = '305' ) And ( L = 14 ) Then Result := ccDinersClub;
If ( D2 = '36' ) And ( L = 14 ) Then Result := ccDinersClub;
If ( D2 = '38' ) And ( L = 14 ) Then Result := ccDinersClub;
If ( D4 = '6011' ) And ( L = 14 ) Then Result := ccDiscover;
If ( D4 = '2014' ) And ( L = 16 ) Then Result := ccenRoute;
If ( D4 = '2149' ) And ( L = 16 ) Then Result := ccenRoute;
If ( D1 = '3' ) And ( L = 16 ) Then Result := ccJCB;
If ( D4 = '2131' ) And ( L = 15 ) Then Result := ccJCB;
If ( D4 = '1800' ) And ( L = 15 ) Then Result := ccJCB;
End;
Function GetCreditCardName( CardType : Integer ) : String;
Begin
Result := '(sconosciuta/non valida)';
If ( CardType = ccMasterCard ) Then Result := 'MasterCard';
If ( CardType = ccVisa ) Then Result := 'Visa';
If ( CardType = ccAmericanExpress ) Then Result := 'American Express';
If ( CardType = ccDinersClub ) Then Result := 'Diner''s Club/Carte Blanche';
If ( CardType = ccDiscover ) Then Result := 'Discover';
If ( CardType = ccenRoute ) Then Result := 'enRoute';
If ( CardType = ccJCB ) Then Result := 'JCB';
End;