|
// Plug-in 32: Validate Credit Card
// This is an executable example with additional code supplied
// To obtain just the plug-ins please click on the Download link
$ Card = "4567 1234 5678 9101 ";
$ Exp = "06/11 ";
Echo "Validating: $ card: $ exp
";
$ Result = PIPHP_ValidateCC ($ card, $ exp );
If ($ result! = FALSE) echo "Card Validated ";
Else echo "Card did not validate ";
Function PIPHP_ValidateCC ($ number, $ expiry)
{
// Plug-in 32: Validate Credit Card
//
// This plug-in accepts a credit card number and
// An expiry date and returns TRUE or FALSE,
// Depending on whether the details pass date
// And checksum validation. The arguments required
// Are:
//
// $ Number: Credit Card Number
// $ Expiry: Expiry date in the form:
// 07/12 or 0712 (for July, 2012)
$ Number = preg_replace ('/[^ \ d]/', '', $ number );
$ Expiry = preg_replace ('/[^ \ d]/', '', $ expiry );
$ Left = substr ($ number, 0, 4 );
$ Cclen = strlen ($ number );
$ Chksum = 0;
// Diners Club
If ($ left> = 3000) & ($ left <= 3059) |
($ Left> = 3600) & ($ left <= 3699) |
($ Left> = 3800) & ($ left <= 3889 ))
If ($ cclen! = 14) return FALSE;
// JCB
If ($ left> = 3088) & ($ left <= 3094) |
($ Left> = 3096) & ($ left <= 3102) |
($ Left> = 3112) & ($ left <= 3120) |
($ Left> = 3158) & ($ left <= 3159) |
($ Left> = 3337) & ($ left <= 3349) |
($ Left> = 3528) & ($ left <= 3589 ))
If ($ cclen! = 16) return FALSE;
// American Express
Elseif ($ left> = 3400) & ($ left <= 3499) |
($ Left> = 3700) & ($ left <= 3799 ))
If ($ cclen! = 15) return FALSE;
// Carte Blanche
Elseif ($ left> = 3890) & ($ left <= 3899 ))
If ($ cclen! = 14) return FALSE;
// Visa
Elseif ($ left> = 4000) & ($ left <= 4999 ))
If ($ cclen! = 13 & $ cclen! = 16) return FALSE;
// MasterCard
Elseif ($ left> = 5100) & ($ left <= 5599 ))
If ($ cclen! = 16) return FALSE;
// Financialian BankCard
Elseif ($ left == 5610)
If ($ cclen! = 16) return FALSE;
// Discover
Elseif ($ left == 6011)
If ($ cclen! = 16) return FALSE;
// Unknown
Else return FALSE;
For ($ j = 1-($ cclen % 2); $ j <$ cclen; $ j + = 2)
$ Chksum + = substr ($ number, $ j, 1 );
For ($ j = $ cclen % 2; $ j <$ cclen; $ j + = 2)
{
$ D = substr ($ number, $ j, 1) * 2;
$ Chksum + = $ d <10? $ D: $ d-9;
}
If ($ chksum % 10! = 0) return FALSE;
If (mktime (0, 0, 0, substr ($ expiry, 0, 2), date ("t "),
Substr ($ expiry, 2, 2) <time () return FALSE;
Return TRUE;
}
?>
|