Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 3 of 3 total
Thread Credit card validation code
Fri, Feb 28 2014 5:35 AMPermanent Link

Matthew Jones

Anyone have code to validate a credit card number in EWB? Just the basic formula is
needed for browser-level confirmation before I pass it down to the server etc.

Thanks!

/Matthew Jones/
Fri, Feb 28 2014 4:25 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Matthew,

<< Anyone have code to validate a credit card number in EWB? Just the basic
formula is needed for browser-level confirmation before I pass it down to
the server etc. >>

Here's a bunch of convoluted code that I've been using for a while (it came
from somewhere, but I've lost the original source, so apologies to the
original author).  It will need some modification to work with EWB, though
("in" for chars, for example).  I should probably just pop this into a new
WebCommerce unit:

type

  TCardType =
(ctNone,ctEnRoute,ctDinersCarte,ctAmex,ctVisa,ctMasterCard,ctDiscover);

const

  MAX_CARDS = 19;

  CardPrefixes: array[1..MAX_CARDS] of String =
('2014','2149','300','301','302',
                                                 '303','304','305','34','36','37',
                                                 '38','4','51','52','53','54','55','6011');

  CardTypes: array[1..MAX_CARDS] of TCardType = (ctEnRoute,ctEnRoute,
                                                 ctDinersCarte,ctDinersCarte,
                                                 ctDinersCarte,ctDinersCarte,
                                                 ctDinersCarte,ctDinersCarte,
                                                 ctAmex,ctDinersCarte,
                                                 ctAmex,ctDinersCarte,
                                                 ctVisa,ctMasterCard,
                                                 ctMasterCard,ctMasterCard,
                                                 ctMasterCard,ctMasterCard,
                                                 ctDiscover);

implementation

function RemoveChar(const Input: String; DeletedChar: Char): String;
var
  I: Byte;
begin
  Result:=Input;
  for I:=Length(Result) downto 1 do
     begin
     if (Result[I]=DeletedChar) then
        Delete(Result,I,1);
     end;
end;

function ShiftMask(Input: Integer): Integer;
begin
  Result:=(1 shl (Input-12));
end;

function ConfirmChecksum(const CardNumber: String): Boolean;
var
  CheckSum: Integer;
  Flag: Boolean;
  I: Integer;
  PartNumber: String;
  Number: Integer;
begin
  I:=Length(CardNumber);
  CheckSum:=0;
  PartNumber:='';
  Flag:=false;
  while (I >= 1) do
     begin
     PartNumber:=Copy(CardNumber,I,1);
     Number:=StrToInt(PartNumber);
     if Flag then
        begin
        Number:=(Number*2);
        if (Number >= 10) then
           Dec(Number,9)
        end;
     Inc(CheckSum,Number);
     Flag:=(not Flag);
     Dec(I);
     end;
  Result:=((CheckSum mod 10)=0);
end;

function GetMask(CardType: TCardType): Integer;
begin
  Result:=0;
  case CardType of
     ctMasterCard:
        Result:=ShiftMask(16);
     ctVisa:
        Result:=(ShiftMask(13) or ShiftMask(16));
     ctAmex:
        Result:=ShiftMask(15);
     ctDinersCarte:
        Result:=ShiftMask(14);
     ctDiscover:
        Result:=ShiftMask(16);
     end;
end;

function IsValidCreditCardNumber(const CardNumber: String): Boolean;
var
  StrippedNumber: String;
  I: Integer;
  FoundCard: Boolean;
  CardType: TCardType;
  TheMask: Integer;
begin
  Result:=True;
  CardType:=ctNone;
  TheMask:=0;
  StrippedNumber:=RemoveChar(CardNumber,' ');
  StrippedNumber:=RemoveChar(StrippedNumber,'-');
  if (StrippedNumber='') then
     begin
     Result:=False;
     Exit;
     end;
  for I:=1 to Length(StrippedNumber) do
     begin
     if (not (StrippedNumber[I] in ['0'..'9'])) then
        begin
        Result:=False;
        Exit;
        end;
     end;
  FoundCard:=False;
  for I:=1 to MAX_CARDS do
     begin
     if (CardPrefixes[I]=Copy(StrippedNumber,1,Length(CardPrefixes[I])))
then
        begin
        FoundCard:=True;
        CardType:=CardTypes[I];
        TheMask:=GetMask(CardType);
        end;
     end;
  if (not FoundCard) or
     (Length(StrippedNumber) > 28) or
     (Length(StrippedNumber) < 12) or
     ((ShiftMask(Length(StrippedNumber)) and TheMask)=0) then
     Result:=False
  else
     begin
     if (CardType <> ctEnRoute) then
        begin
        if (not ConfirmCheckSum(StrippedNumber)) then
           Result:=False;
        end;
     end;
end;

function GetCreditCardType(const CardNumber: String): TCardType;
var
  StrippedNumber: String;
  I: Integer;
begin
  Result:=ctNone;
  StrippedNumber:=RemoveChar(CardNumber,' ');
  StrippedNumber:=RemoveChar(StrippedNumber,'-');
  if (StrippedNumber='') then
     Exit;
  for I:=1 to Length(StrippedNumber) do
     begin
     if (not (StrippedNumber[I] in ['0'..'9'])) then
        Exit;
     end;
  for I:=1 to MAX_CARDS do
     begin
     if (CardPrefixes[I]=Copy(StrippedNumber,1,Length(CardPrefixes[I])))
then
        Result:=CardTypes[I];
     end;
end;

Tim Young
Elevate Software
www.elevatesoft.com
Mon, Mar 3 2014 4:55 AMPermanent Link

Matthew Jones

Brilliant, thank you.

/Matthew Jones/
Image