Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-14-2006, 10:01 AM   PM User | #1
NancyJ
Senior Coder

 
NancyJ's Avatar
 
Join Date: Feb 2005
Location: Bradford, UK
Posts: 3,162
Thanks: 19
Thanked 65 Times in 64 Posts
NancyJ will become famous soon enough
Credit Card Class

This is a class I wrote for validating UK credit and debit cards from multiple sources, it defaults to "post" but other input sources can be specified (or you can modify it to accept the values as parameters of the constructor.
It includes validation for Visa, Mastercard, Switch, Solo and Amex but more card type can be added easily, if you dont have a validation pattern for the card type just return from the function (see the Solo example) and the type validation will be skipped.

PHP Code:
<?
class creditCard
{
  var 
$type;
  var 
$number;
  var 
$name;
  var 
$issue;
  var 
$startDate;
  var 
$expDate;
  var 
$secCode;
  var 
$errors = array();
  var 
$valid true;
  var 
$maskedNumber;
  var 
$isDebit false;
  
  function 
creditCard($source="post")
  {
    switch(
$source)
    {
      case 
"post":
          
extract($_POST);
          
$this->type $type;
          
$this->number $number;
          
$this->name $name;
          
$this->issue $issue;
          
$this->startDate $startMonth."/".$startYear;
          
$this->expDate $expMonth."/".$expYear;
          
$this->secCode $secCode;
          
$this->maskedNumber $this->maskNumber();
          break;
      case 
"db":
      
//fill in your own db query here
          
break;
    }
    
        
  }
  
  function 
validate()
  {
    
$this->checkNumber();
    
$this->checkType();
    
$this->checkExp();
    
$this->checkCVV();
    
$this->checkName();
    if(
$this->isDebit)
    {
      
$this->checkDebit();
    }
  }
  
  function 
checkNumber()
  {
    
$number strrev($this->number);
    
$number ereg_replace("[^0-9]"""$number);
    
$arrNumber str_split($number);
    for(
$i 0$i<count($arrNumber); $i++)
    {
      if((
$i%2) == 0)
      {
        
$arrNumber[$i+1] *=2
      }
    }
    
$number implode(""$arrNumber);
    foreach(
str_split($number) as $digit)
    {
      
$sum += $digit;
      
    }
    
$valid = (($sum 10)==0);
    
$this->errors[] =($valid)?"":"The card number you entered is not valid";
    
$this->valid = ($valid) ? $this->valid false;
  }
  
  function 
checkType()
  {
    switch(
strtolower($this->type))
    {
      case 
"mastercard":
          
$strPattern "/^((5[1-5]\d{2})(-?\s?\d{4}){3})$/i";
          break;
      case 
"visa":
        
$strPattern "/^((4\d{3})(-?\s?\d{4}){3})$/i";
        break;
      case 
"switch":
        
$strPattern "/^((67\d{2})(-?\s?\d{4}){3})$/i";
        
$this->isDebit true;
        break;
      case 
"solo":
          
$this->isDebit true;
          return;
          break;
      case 
"amex":
        
$strPattern "/^((3[4,7])\d{2}-?\s?\d{6}-?\s?\d{5})$/i";
        break;
      default:
          
$this->errors[] =    "The card type you selected is not valid";
    }

    
$valid = (@preg_match($strPattern$this->number));
    
$this->errors[] = ($valid)?"":"The card number you entered did not match the card type you selected";
    
$this->valid = ($valid) ? $this->valid false;
  }
  
  function 
checkExp()
  {
    
$arrDate explode("/"$this->expDate);
    if(
count($arrDate)< )
    {
      return 
false;
    }
    
$expDate = @mktime(0,0,0,$arrDate[0],date('t',mktime(0,0,0,2,1,2004)),$arrDate[1]);
    
$valid = ($expDate mktime());
    
$this->errors[] = ($valid) ? "" "Your card has expired";
    
$this->valid = ($valid) ? $this->valid false;
  }
  
  function 
checkStart()
  {
    
$arrDate explode("/"$this->startDate);
    
$startDate mktime(0,0,0,$arrDate[0], 1$arrDate[1]);
    return ((
$startDate mktime())&& ($startDate 0));   
  }
  
  function 
checkDebit() 
  {
    if(((
is_numeric($this->issue))&&($this->issue 0))||$this->checkStart())
    {
      return;
    }
    else
    {
      
$this->valid false;
      
$this->errors[] = "Debit cards require a start date or an issue number.";
    }
  }
  
  function 
checkCVV()
  {
    
$valid = ((is_numeric($this->secCode))&&(strlen($this->secCode)==3));
    
$this->valid = ($valid) ? $this->valid false;
    
$this->errors[] = ($valid)? "":"Your security code should be 3 digits in length and is found on the back of your card";
  }
  
  function 
checkName()
  {
    
$valid = (@preg_match("/^[a-z\.]*\s?([a-z\-\']+\s)+[a-z\-\']+$/i"$this->name));
    
$this->errors[] = "Please enter your name as it appears on your card";
    
$this->valid = ($valid) ? $this->valid false;
  }
  
  function 
maskNumber()
  {
    
$arrNumber str_split($this->number);
    for(
$i 0$i<count($arrNumber); $i++)
    {
      if((
is_numeric($arrNumber[$i]))&&($i< (count($arrNumber)-4)))
      {
        
$maskedNumber .="x";
      }
      else
      {
        
$maskedNumber .= $arrNumber[$i];
      }
    }
    return 
$maskedNumber;
  }
  
}
example usage:
PHP Code:
if(isset($_POST['submit']))
{
  
$CC = new creditCard;
  
$CC->validate();
  if(
$CC->valid)
  {
    echo 
$CC->maskedNumber;
  }
  else
  {
    foreach(
$CC->errors as $error)
    {
      if(!empty(
$error))
      {
        echo 
$error."<br />";
      }
    }
  }

__________________
http://www.hazelryan.co.uk

Last edited by NancyJ; 02-14-2006 at 03:03 PM..
NancyJ is offline   Reply With Quote
Old 02-14-2006, 03:02 PM   PM User | #2
fci
Senior Coder

 
Join Date: Aug 2004
Location: Twin Cities
Posts: 1,345
Thanks: 0
Thanked 0 Times in 0 Posts
fci is an unknown quantity at this point
you have a typo here:
Code:
      case "masterCard":
you are doing strtolower right above it
fci is offline   Reply With Quote
Old 02-14-2006, 03:03 PM   PM User | #3
NancyJ
Senior Coder

 
NancyJ's Avatar
 
Join Date: Feb 2005
Location: Bradford, UK
Posts: 3,162
Thanks: 19
Thanked 65 Times in 64 Posts
NancyJ will become famous soon enough
Quote:
Originally Posted by fci
you have a typo here:
Code:
      case "masterCard":
you are doing strtolower right above it
ta
Edited.
__________________
http://www.hazelryan.co.uk
NancyJ is offline   Reply With Quote
Old 02-14-2006, 08:39 PM   PM User | #4
Velox Letum
Senior Coder

 
Join Date: Apr 2005
Location: Colorado, United States
Posts: 1,208
Thanks: 0
Thanked 0 Times in 0 Posts
Velox Letum is an unknown quantity at this point
That expiration date validator could be a problem across timezones, but otherwise very nice.
__________________
"$question = ( to() ) ? be() : ~be();"
Velox Letum is offline   Reply With Quote
Old 02-15-2006, 10:21 AM   PM User | #5
NancyJ
Senior Coder

 
NancyJ's Avatar
 
Join Date: Feb 2005
Location: Bradford, UK
Posts: 3,162
Thanks: 19
Thanked 65 Times in 64 Posts
NancyJ will become famous soon enough
Quote:
Originally Posted by Velox Letum
That expiration date validator could be a problem across timezones, but otherwise very nice.
If it comes down to hours you're cutting it a bit fine anyway Dont know about anyone else but I usually get my new card at least a week before my old card expires.
__________________
http://www.hazelryan.co.uk
NancyJ is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:25 PM.


Advertisement
Log in to turn off these ads.