Credit card validator for symfony
Translation of the original in Catalan. Read it in: CatalàCastellano

Despite the many form field validators that the
famework
includes, it doesn't include a rather important one. The validator for credit card numbers. As you surely know, a credit card number is self-verifying. That is, it follows a mathematical algorithm across its groups of 4 digits. Well then, since I had no desire at all to implement what has already been implemented many times, I found that on
a certain
Harish Chauhan had posted a class programmed in PHP4 that implemented a validator
for quite a few types of credit card (VISA, MASTERCARD, DISCOVER, AMEX, DINERS, JCB, Australian Bankcard, EnRoute and Switch Solo). So I decided to integrate that class with symfony's form field validator system.
The first thing I did was declare the original methods as private and then extend the
sfValidator
class, which is the one used by symfony's validators. Creating a couple of standard public methods for the validators, they basically serve to instantiate the real validator's method and to capture the parameters received from the corresponding
yml
file.
Well then, if you want to look at the class code and an example of use, besides being able to consult it in the extended part of this article, you can look at the
, specifically at
.
Validator code:
getParameterHolder()->get('num');
$num = $this->getContext()->getRequest()->getParameter($num_param);
$tipo_param = $this->getParameterHolder()->get('tipo');
$tipo = $this->getContext()->getRequest()->getParameter($tipo_param);
// Lanzamos la validación
$validada=$this->_isVAlidCreditCard($num,$tipo,false);
// Informamos de como ha ido la validación
sfContext::getInstance()->getLogger()->info("CCVAL.class.php: Tipo: ".$tipo." Num: ".$num." Validada: ".$validada);
if ($validada==false)
{
$error = $this->getParameterHolder()->get('error');
return false;
}
return true;
}
public function initialize ($context, $parameters = null)
{
// initialize parent
parent::initialize($context);
$this->getParameterHolder()->add($parameters);
return true;
}
/**
* Testing checksum
*
* @param integer $ccnum
* @return boolean
*/
private function _checkSum($ccnum)
{
$checksum = 0;
for ($i=(2-(strlen($ccnum) % 2)); $i<=strlen($ccnum); $i+=2)
{
$checksum += (int)($ccnum{$i-1});
}
// Analyze odd digits in even length strings or even digits in odd length strings.
for ($i=(strlen($ccnum)% 2) + 1; $i< 10)
{ $checksum += $digit; }
else
{ $checksum += ($digit-9); }
}
if (($checksum % 10) == 0)
return true;
else
return false;
}
/**
* Launch validation
*
* @param integer $ccnum
* @param string $type
* @param boolean $returnobj
* @return boolean
*/
private function _isVAlidCreditCard($ccnum,$type="",$returnobj=false)
{
$creditcard=array( "visa"=>"/^4d{3}-?d{4}-?d{4}-?d{4}$/",
"mastercard"=>"/^5[1-5]d{2}-?d{4}-?d{4}-?d{4}$/",
"discover"=>"/^6011-?d{4}-?d{4}-?d{4}$/",
"amex"=>"/^3[4,7]d{13}$/",
"diners"=>"/^3[0,6,8]d{12}$/",
"bankcard"=>"/^5610-?d{4}-?d{4}-?d{4}$/",
"jcb"=>"/^[3088|3096|3112|3158|3337|3528]d{12}$/",
"enroute"=>"/^[2014|2149]d{11}$/",
"switch"=>"/^[4903|4911|4936|5641|6333|6759|6334|6767]d{12}$/");
if(empty($type))
{
$match=false;
foreach($creditcard as $type=>$pattern)
if(preg_match($pattern,$ccnum)==1)
{
$match=true;
break;
}
if(!$match)
return false;
else
{
if($returnobj)
{
$return=new stdclass;
$return->valid=$this->_checkSum($ccnum);
$return->ccnum=$ccnum;
$return->type=$type;
return $return;
}
else
return $this->_checkSum($ccnum);
}
}
else
{
if(@preg_match($creditcard[strtolower(trim($type))],$ccnum)==0)
return false;
else
{
if($returnobj)
{
$return=new stdclass;
$return->valid=$this->_checkSum($ccnum);
$return->ccnum=$ccnum;
$return->type=$type;
return $return;
}
else
return $this->_checkSum($ccnum);
}
}
}
}
?>
An example of how the validator is called from the
yml
configuration files in the
validate
directory:
methods:
post: [ntarjeta]
names:
ntarjeta:
required: Yes
required_msg: Credit Card number is required
validators: validarCC
validarCC:
class: CCVAL
param:
num: ntarjeta
tipo: tipoCC
error: Your credit card number is invalid