Código Delphi
// Função: Validar Cartão de Credito ²
// Verifica se o Cartao é Válido ou Não.
Código
// Declare a constante abaixo na seção CONST global do Form.
CardPrefixes: array[ 1..19 ] of string = ( '2014', '2149', '300', '301', '302', '303', '304', '305', '34', '36', '37', '38', '4', '51', '52', '53', '54', '55', '6011' );
CardTypes: array[ 1..19 ] of String = ( 'enRoute', 'enRoute', 'Diner Club/Carte Blanche', 'Diner Club/Carte Blanche', 'Diner Club/Carte Blanche', 'Diner Club/Carte Blanche', 'Diner Club/Carte Blanche', 'Diner Club/Carte Blanche', 'American Express', 'Diner Club/Carte Blanche', 'American Express', 'Diner Club/Carte Blanche', 'Visa', 'MasterCard','MasterCard','MasterCard','MasterCard','MasterCard','Discover' );
/// Fim da constante. \\\
function RemoveChar(const Input: String; DeletedChar: Char): String;
var
Index: Word;
begin
Result := Input;
for Index := Length( Result ) downto 1 do
if Result[ Index ] = DeletedChar then Delete( Result, Index, 1 );
end;
function ShiftMask( Input: Integer ): Integer;
begin
result := ( 1 shl ( Input - 12 ) );
end;
function ConfirmChecksum( CardNumber: String ): Boolean;
var
CheckSum: Integer;
Flag: Boolean;
Counter: Integer;
PartNumber: String;
Number: Integer;
begin
Counter := Length( CardNumber );
CheckSum := 0;
PartNumber := ';
Number := 0;
Flag := false;
while ( Counter >= 1 ) do
begin
PartNumber := Copy( CardNumber, Counter, 1 );
Number := StrToInt( PartNumber );
if ( Flag ) then
begin
Number := Number * 2;
if ( Number >= 10 ) then Number := Number - 9;
end;
CheckSum := CheckSum + Number;
Flag := not( Flag );
Counter := Counter - 1;
end;
result := ( ( CheckSum mod 10 ) = 0 );
end;
function GetMask( CardName: String ): Integer;
begin
result := 0;
if ( CardName = 'MasterCard' ) then result := ShiftMask( 16 );
if ( CardName = 'Visa' ) then result := ( ShiftMask( 13 ) or ShiftMask( 16 ) );
if ( CardName = 'American Express' ) then result := ShiftMask( 15 );
if ( CardName = 'Diner Club/Carte Blanche' ) then result := ShiftMask( 14 );
if ( CardName = 'Discover' ) then result := ShiftMask( 16 );
end;
function IsValidCreditCardNumber( CardNumber: String; var MessageText: String ): Boolean;
var
StrippedNumber: String;
Index: Integer;
TheMask: Integer;
FoundIt: Boolean;
CardName: String;
PerformChecksum: Boolean;
begin
StrippedNumber := RemoveChar( CardNumber, ' ' );
StrippedNumber := RemoveChar( StrippedNumber, '-' );
if ( StrippedNumber = ' ) then
begin
result := true;
exit; end;
MessageText := ';
result := true;
FoundIt := false;
for Index := 1 to Length( StrippedNumber ) do
begin
case StrippedNumber[ Index ] of
'0'..'9': FoundIt := FoundIt;
else
MessageText := 'Caracter inválido!';
result := false;
exit; end; end;
for Index := 1 to 19 do
begin
if ( Pos( CardPrefixes[ Index ], StrippedNumber ) = 1 ) then
begin
FoundIt := true;
CardName := CardTypes[ Index ];
TheMask := GetMask( CardName );
end; end;
if ( not FoundIt ) then
begin
CardName := 'Unknown Card Type';
TheMask := 0;
MessageText := 'Unknown Card Type ';
result := false;
exit; end;
if ( ( Length( StrippedNumber ) > 28 ) and result ) then
begin
MessageText := 'Number is too long ';
result := false;
exit; end;
if ( ( Length( StrippedNumber ) < 12 ) or ( ( shiftmask( length( strippednumber ) ) and themask ) = 0 ) ) then
begin
messagetext := 'number length incorrect';
result := false;
exit; end;
if ( cardname = 'enroute' ) then
performchecksum := false
else
performchecksum := true;
if ( performchecksum and ( not confirmchecksum( strippednumber ) ) ) then
begin
messagetext := 'bad checksum';
result := false;
exit; end;
if ( result ) then
messagetext := 'number ok: card type: ' + cardname;
if ( strippednumber = ' ) then
result := true; end;
CardPrefixes: array[ 1..19 ] of string = ( '2014', '2149', '300', '301', '302', '303', '304', '305', '34', '36', '37', '38', '4', '51', '52', '53', '54', '55', '6011' );
CardTypes: array[ 1..19 ] of String = ( 'enRoute', 'enRoute', 'Diner Club/Carte Blanche', 'Diner Club/Carte Blanche', 'Diner Club/Carte Blanche', 'Diner Club/Carte Blanche', 'Diner Club/Carte Blanche', 'Diner Club/Carte Blanche', 'American Express', 'Diner Club/Carte Blanche', 'American Express', 'Diner Club/Carte Blanche', 'Visa', 'MasterCard','MasterCard','MasterCard','MasterCard','MasterCard','Discover' );
/// Fim da constante. \\\
function RemoveChar(const Input: String; DeletedChar: Char): String;
var
Index: Word;
begin
Result := Input;
for Index := Length( Result ) downto 1 do
if Result[ Index ] = DeletedChar then Delete( Result, Index, 1 );
end;
function ShiftMask( Input: Integer ): Integer;
begin
result := ( 1 shl ( Input - 12 ) );
end;
function ConfirmChecksum( CardNumber: String ): Boolean;
var
CheckSum: Integer;
Flag: Boolean;
Counter: Integer;
PartNumber: String;
Number: Integer;
begin
Counter := Length( CardNumber );
CheckSum := 0;
PartNumber := ';
Number := 0;
Flag := false;
while ( Counter >= 1 ) do
begin
PartNumber := Copy( CardNumber, Counter, 1 );
Number := StrToInt( PartNumber );
if ( Flag ) then
begin
Number := Number * 2;
if ( Number >= 10 ) then Number := Number - 9;
end;
CheckSum := CheckSum + Number;
Flag := not( Flag );
Counter := Counter - 1;
end;
result := ( ( CheckSum mod 10 ) = 0 );
end;
function GetMask( CardName: String ): Integer;
begin
result := 0;
if ( CardName = 'MasterCard' ) then result := ShiftMask( 16 );
if ( CardName = 'Visa' ) then result := ( ShiftMask( 13 ) or ShiftMask( 16 ) );
if ( CardName = 'American Express' ) then result := ShiftMask( 15 );
if ( CardName = 'Diner Club/Carte Blanche' ) then result := ShiftMask( 14 );
if ( CardName = 'Discover' ) then result := ShiftMask( 16 );
end;
function IsValidCreditCardNumber( CardNumber: String; var MessageText: String ): Boolean;
var
StrippedNumber: String;
Index: Integer;
TheMask: Integer;
FoundIt: Boolean;
CardName: String;
PerformChecksum: Boolean;
begin
StrippedNumber := RemoveChar( CardNumber, ' ' );
StrippedNumber := RemoveChar( StrippedNumber, '-' );
if ( StrippedNumber = ' ) then
begin
result := true;
exit; end;
MessageText := ';
result := true;
FoundIt := false;
for Index := 1 to Length( StrippedNumber ) do
begin
case StrippedNumber[ Index ] of
'0'..'9': FoundIt := FoundIt;
else
MessageText := 'Caracter inválido!';
result := false;
exit; end; end;
for Index := 1 to 19 do
begin
if ( Pos( CardPrefixes[ Index ], StrippedNumber ) = 1 ) then
begin
FoundIt := true;
CardName := CardTypes[ Index ];
TheMask := GetMask( CardName );
end; end;
if ( not FoundIt ) then
begin
CardName := 'Unknown Card Type';
TheMask := 0;
MessageText := 'Unknown Card Type ';
result := false;
exit; end;
if ( ( Length( StrippedNumber ) > 28 ) and result ) then
begin
MessageText := 'Number is too long ';
result := false;
exit; end;
if ( ( Length( StrippedNumber ) < 12 ) or ( ( shiftmask( length( strippednumber ) ) and themask ) = 0 ) ) then
begin
messagetext := 'number length incorrect';
result := false;
exit; end;
if ( cardname = 'enroute' ) then
performchecksum := false
else
performchecksum := true;
if ( performchecksum and ( not confirmchecksum( strippednumber ) ) ) then
begin
messagetext := 'bad checksum';
result := false;
exit; end;
if ( result ) then
messagetext := 'number ok: card type: ' + cardname;
if ( strippednumber = ' ) then
result := true; end;