Reply
PHP Credit Card Validation
Old 04-30-2007, 12:32 PM PHP Credit Card Validation
Red_X_'s Avatar
Skilled Talker

Posts: 99
Location: USA
This is only custom script for my PHP class. However, I can't seem to get the data to display correctly on the page. Here is the code:

Creditcard.php
PHP Code:
<?php
define
("CARD_TYPE_MC"0);
define("CARD_TYPE_VS"1);
define("CARD_TYPE_AX"2);
define("CARD_TYPE_DC"3);
define("CARD_TYPE_DS"4);
define("CARD_TYPE_JC"5);
 
class 
CCreditCard
{
 
// Class Members
 
   
var $__ccName '';
 
   var 
$__ccType '';
 
   var 
$__ccNum '';
 
   var 
$__ccExpM 0;
 
   var 
$__ccExpY 0;
 
 
// Constructor
 
 
function CCreditCard($name$type$num$expm$expy)
 {
 
   
// Set member variables
 
  
if(!empty($name))
  {
   
$this->__ccName $name
  }
 
  else
  {
   die(
'Must pass name to constructor');
  }
 
 
// Make sure card type is valid
 
  
switch(strtolower($type)) 
  {
 
   case 
'mc':
 
   case 
'mastercard':
 
   case 
'm':
 
   case 
'1':
    
$this->__ccType CARD_TYPE_MC;    
    break;
   case 
'vs':
 
   case 
'visa':
 
   case 
'v':
 
   case 
'2':
    
$this->__ccType CARD_TYPE_VS;     
    break;
   case 
'ax':
 
   case 
'american express':
 
   case 
'a':
 
   case 
'3':
 
    
$this->__ccType CARD_TYPE_AX;
 
    break;
 
   case 
'dc':
 
   case 
'diners club':
 
   case 
'4':
 
    
$this->__ccType CARD_TYPE_DC;
 
    break;
 
   case 
'ds':
 
   case 
'discover':
 
   case 
'5':
 
    
$this->__ccType CARD_TYPE_DS;
 
    break;
 
   case 
'jc':
 
   case 
'jcb':
 
   case 
'6':
 
    
$this->__ccType CARD_TYPE_JC;
 
    break;
    default:
 
     die(
'Invalid type ' $type ' passed to constructor');
   }
 
 
// Don't check the number yet,
 
 // just kill all non numerics
 
   
if(!empty($num))
 
   {
 
    
$cardNumber ereg_replace("[^0-9]"""$num);
 
   
// Make sure the card number isnt empty
 
   
if(!empty($cardNumber))
 
   {
 
    
$this->__ccNum $cardNumber;
 
   }
 
   else
 
   {
 
    die(
'Must pass number to constructor');
 
   }
 
  }
 
  else
 
  {
 
   die(
'Must pass number to constructor');
 
  }
 
  if(!
is_numeric($expm) || $expm || $expm 12)
 
  {
 
   die(
'Invalid expiry month of ' $expm ' passed to constructor');
 
  }
 
  else
 
  {
 
   
$this->__ccExpM $expm;
 
  }
 
 
// Get the current year
 
  
$currentYear date('Y');
 
  
settype($currentYear'integer');
 
  if(!
is_numeric($expy) || $expy $currentYear || $expy $currentYear 10)
 
  {
 
   die(
'Invalid expiry year of ' $expy ' passed to constructor');
 
  }
 
  else
 
  {
 
   
$this->__ccExpY $expy;
 
  }
 
 }
 
 function 
Name()
 
 {
 
  return 
$this->__ccName;
 
 }
 
 function 
Type()
 
 {
 
  switch(
$this->__ccType)
 
  {
 
  case 
CARD_TYPE_MC:
   return 
'mastercard [1]';
   break;
 
  case 
CARD_TYPE_VS:
   return 
'Visa [2]';
   break;
  case 
CARD_TYPE_AX:
   return 
'Amex [3]';
   break;
  case 
CARD_TYPE_DC:
   return 
'Diners Club [1]';
   break;
  case 
CARD_TYPE_DS:
   return 
'Discover [5]';
   break;
  case 
CARD_TYPE_JC:
   return 
'JCB [6]';
   break;
 
  default:
   return 
'Unknown [-1]';
 }
 
 function 
Number()
 
 {
 
  return 
$this->__ccNum;
 
 }
 
 function 
ExpiryMonth()
 
 {
 
  return 
$this->__ccExpM;
 
 }
 
 function 
ExpiryYear()
 
 {
 
  return 
$this->__ccExpY;
 
 }
 
 function 
SafeNumber($char 'x'$numToHide 4)
 
 {
 
  
// Return only part of the number
 
  
if($numToHide 4)
  {
   
$numToHide 4;
  }
 
  if(
$numToHide 10)
  {
   
$numToHide 10;
  }
 
  
$cardNumber $this->__ccNum;
 
  
$cardNumber substr($cardNumber0strlen($cardNumber) - $numToHide);
 
  for(
$i 0$i $numToHide$i++)
  {
   
$cardNumber .= $char;
  }
 
  return 
$cardNumber;
 
 }
 
 function 
IsValid()
 {
 
 
// Not valid by default
 
  
$validFormat false;
 
  
$passCheck false;
 
  
// Is the number in the correct format?
 
  
switch($this->__ccType)
  {
   case 
CARD_TYPE_MC:
    
$validFormat ereg("^5[1-5][0-9]{14}$"$this->__ccNum);
    break;
   case 
CARD_TYPE_VS:
    
$validFormat ereg("^4[0-9]{12}$|^4[0-9]{15}$"$this->__ccNum);
    break;
   case 
CARD_TYPE_AX:
    
$validFormat ereg("^3[4|7][0-9]{13}$"$this->__ccNum);
    break;
 
   case 
CARD_TYPE_DC:
    
$validFormat ereg("^30[0-5][0-9]{11}$|^3[6|8][0-9]{12}$"$this->__ccNum);
    break;
   case 
CARD_TYPE_DS:
    
$validFormat ereg("^6011[0-9]{12}$"$this->__ccNum);    
    break;
 
   case 
CARD_TYPE_JC:
    
$validFormat ereg("^3[0-9]{15}$|^[2131|1800][0-9]{11}$"$this->__ccNum);
    break;
 
   default:
    
$validFormat false;
 
  }
 
 
// Is the number valid against luhn?
 
  
$cardNumber strrev($this->__ccNum);
 
  
$numSum 0;
 
  for(
$i 0$i strlen($cardNumber); $i++)
 
  {
 
   
$currentNum substr($cardNumber$i1);
 
   if(
floor($currentNum 2) != $currentNum 2)
 
   {
 
    
$currentNum *= 2;
 
   }
 
   if(
strlen($currentNum) == 2)
 
   {
 
    
$firstNum substr($currentNum01);
 
    
$secondNum substr($currentNum11);
 
    
$currentNum $firstNum $secondNum;
 
   }
 
   
$numSum += $currentNum;
 
  }
 
  
// If the total has no remained its OK
 
  
$passCheck = ($numSum 10 == true false);
 
  if(
$validFormat && $passCheck)
 
  {
 
   return 
true;
 
  }
 
  else
 
  {
 
   return 
false;
 
  }
 
 }
}
}
 
global 
$submit;
 
if(!isset(
$submit))
{
?>
 
<?php
}
else
{
// Check if the card is valid
global $ccName;
global 
$ccNum;
global 
$ccType;
global 
$ccExpM;
global 
$ccExpY;
 
$cc = new CCreditCard($ccName$ccType$ccNum$ccExpM$ccExpY);
}
 
?>
ccvalidation.php
PHP Code:
<?php include('creditcard.php'); ?>
<?php
global $submit;
 
if(!isset(
$submit))
{
?>
 <h2>Validate Credit Card</h2>
 
 <form name="frmCC" action="ccvalidation.php" method="post">
 
  Cardholders name: <input type="text" name="ccName"><br>
 
  Card number: <input type="text" name="ccNum"><br>
 
  Card type: <select name="ccType">
 
   <option value="1">mastercard</option>
 
   <option value="2">Visa</option>
 
   <option value="3">Amex</option>
 
   <option value="4">Diners</option>
 
   <option value="5">Discover</option>
 
   <option value="6">JCB</option>
 
  </select><br>
 
  Expiry Date: <select name="ccExpM"> 
<?php
  
for($i 1$i 13$i++)
 
   { echo 
'<option>' $i '</option>'; }
 
?> 
 </select>
 
 <select name="ccExpY">
<?php
 
  
for($i 2007$i 2010$i++)
 
   { echo 
'<option>' $i '</option>'; }
?> 
 </select><br><br>
 
  <input type="submit" name="submit" value="Validate">
 
 </form>
<?php
} else {
 
 
 
// Check if the card is valid
 
  
$ccName $_POST['$ccName'];
 
  
$ccNum $_POST['$ccNum'];
 
  
$ccType $_POST['$ccType'];
 
  
$ccExpM $_POST['$ccExpM'];
 
  
$ccExpY $_POST['$ccExpY'];
 
  
$cc = new CCreditCard($ccName$ccType$ccNum$ccExpM$ccExpY);
 
 
  echo
"
   <h2>Validation Results</h2>
 
    <b>Name: </b>$cc->Name()<br>
 
    <b>Number: </b>$cc->SafeNumber('x', 6)<br>
 
    <b>Type: </b><$cc->Type()<br>
 
    <b>Expires: </b>$cc->ExpiryMonth() . '/' . $cc->ExpiryYear()<br><br>"
;
 
 
  echo
'<font color="blue" size="2"><b>';
 
 
  if(
$cc->IsValid()){
 
   echo 
"VALID CARD";
 
  } else {
 
   echo 
"INVALID CARD";
 
 }
  echo 
"</b></font>"
}
?>
Any support, and or responces are all appretiated. As i've been racking my brain for quite some time.

Thanks.

X
__________________
>> Higher a poor college student. PM me :). PHP/MySQL <<
Red_X_ is offline
Reply With Quote
View Public Profile Visit Red_X_'s homepage!
 
When You Register, These Ads Go Away!
Old 04-30-2007, 12:36 PM Re: PHP Credit Card Validation
solomongaby's Avatar
Webmaster Talker

Latest Blog Post:
How Do You Find Music Online ?
Posts: 522
Name: Gabe Solomon
Location: Romania
can you be more clear what exactly doesnt display corectly
__________________
If you like my posts ... TK is appreciated:)
Web Directory | Blog
solomongaby is offline
Reply With Quote
View Public Profile Visit solomongaby's homepage!
 
Old 04-30-2007, 12:44 PM Re: PHP Credit Card Validation
Red_X_'s Avatar
Skilled Talker

Posts: 99
Location: USA
Quote:
Originally Posted by solomongaby View Post
can you be more clear what exactly doesnt display corectly

Well in ccvalidation.php when a user or myself enter information into the page. It just refreshes, and doesn't display the data. I'm trying to figure out why it isn.t'.
__________________
>> Higher a poor college student. PM me :). PHP/MySQL <<
Red_X_ is offline
Reply With Quote
View Public Profile Visit Red_X_'s homepage!
 
Old 04-30-2007, 12:48 PM Re: PHP Credit Card Validation
solomongaby's Avatar
Webmaster Talker

Latest Blog Post:
How Do You Find Music Online ?
Posts: 522
Name: Gabe Solomon
Location: Romania
you are counting on register_globasl ON that probably is OFF for you're hosting

meaning you use $submit instead of $_POST['submit']
__________________
If you like my posts ... TK is appreciated:)
Web Directory | Blog
solomongaby is offline
Reply With Quote
View Public Profile Visit solomongaby's homepage!
 
Old 04-30-2007, 12:52 PM Re: PHP Credit Card Validation
Red_X_'s Avatar
Skilled Talker

Posts: 99
Location: USA
Quote:
Originally Posted by solomongaby View Post
you are counting on register_globasl ON that probably is OFF for you're hosting

meaning you use $submit instead of $_POST['submit']
I talked to my server register_globals is on.

edit: ran a php_info(); to double check.

register_globals: Off -_-..... So could i get a example of what your talking about. I thought I had a $submit.
__________________
>> Higher a poor college student. PM me :). PHP/MySQL <<

Last edited by Red_X_ : 04-30-2007 at 12:55 PM.
Red_X_ is offline
Reply With Quote
View Public Profile Visit Red_X_'s homepage!
 
Old 04-30-2007, 01:00 PM Re: PHP Credit Card Validation
solomongaby's Avatar
Webmaster Talker

Latest Blog Post:
How Do You Find Music Online ?
Posts: 522
Name: Gabe Solomon
Location: Romania
instead of this lines
global $submit;

if(!isset(
$submit))
{

put this one
if(!isset($_POST['submit']))
{
__________________
If you like my posts ... TK is appreciated:)
Web Directory | Blog
solomongaby is offline
Reply With Quote
View Public Profile Visit solomongaby's homepage!
 
Old 04-30-2007, 05:23 PM Re: PHP Credit Card Validation
Red_X_'s Avatar
Skilled Talker

Posts: 99
Location: USA
Quote:
Originally Posted by solomongaby View Post
instead of this lines
global $submit;

if(!isset(
$submit))
{

put this one
if(!isset($_POST['submit']))
{

Didn't work, only shows the page error. Without the form.
__________________
>> Higher a poor college student. PM me :). PHP/MySQL <<
Red_X_ is offline
Reply With Quote
View Public Profile Visit Red_X_'s homepage!
 
Old 05-01-2007, 04:54 AM Re: PHP Credit Card Validation
kaisellgren's Avatar
Extreme Talker

Posts: 214
Name: Kai Sellgren
Location: Finland
Hey this is cool, I have always just use to check cards like this:

PHP Code:
<?php
function kpreg_is_valid_visa_cc_code($code "")
 {
  
$code preg_replace("/[^\d]+/","",$code);
  if (
preg_match("/^4[0-9]{12}(?:[0-9]{3})?$/",$code))
   return 
true;
  return 
false;
 }
?>
__________________
Adept Web Community
kaisellgren is offline
Reply With Quote
View Public Profile Visit kaisellgren's homepage!
 
Old 05-01-2007, 05:35 AM Re: PHP Credit Card Validation
solomongaby's Avatar
Webmaster Talker

Latest Blog Post:
How Do You Find Music Online ?
Posts: 522
Name: Gabe Solomon
Location: Romania
What page error ?
__________________
If you like my posts ... TK is appreciated:)
Web Directory | Blog
solomongaby is offline
Reply With Quote
View Public Profile Visit solomongaby's homepage!
 
Old 05-01-2007, 07:58 PM Re: PHP Credit Card Validation
Red_X_'s Avatar
Skilled Talker

Posts: 99
Location: USA
Quote:
Originally Posted by solomongaby View Post
What page error ?
Consistant Credit Card error. Regardless if information is filled out.
__________________
>> Higher a poor college student. PM me :). PHP/MySQL <<
Red_X_ is offline
Reply With Quote
View Public Profile Visit Red_X_'s homepage!
 
Old 05-01-2007, 10:55 PM Re: PHP Credit Card Validation
kids's Avatar
Ultra Talker

Posts: 271
It's good! But I think you need check validation before transfer user to ccvalidation.php by javascript. It will be better. Users must to turn back filling page but if you use form validation by java it will check validation on page.
kids is offline
Reply With Quote
View Public Profile Visit kids's homepage!
 
Old 05-02-2007, 03:29 AM Re: PHP Credit Card Validation
Red_X_'s Avatar
Skilled Talker

Posts: 99
Location: USA