![]() |
|
Snippets |
|
sfValidator extension based on Credit Card Validator code by Harish Chauhan (from phpclasses.org). With this extension you can validate this type of credit cards: VISA, MASTERCARD, DISCOVER, AMEX, DINERS,JCB, Australian Bankcard, EnRoute And Switch Solo.
IMHO isn't a bad idea to include anything like this in standard sfValidator code. While here you have the source code of my adaptation of credit cards validator.
<?php /* Symfony integration as sfValidator of CCVAL Date - Jun 17, 2006 Author - Oriol Rius (oriol@joor.net) Credit CArd Validator Date - Jan 14, 2005 Author - Harish Chauhan ABOUT This PHP script will calidate credit cards by checking there length and pattern and checksum using mod 10. Supported credit cards are VISA, MASTERCARD, DISCOVER, AMEX, DINERS, JCB, Australian Bankcard, EnRoute And Switch Solo. */ class CCVAL extends sfValidator { public function execute (&$value, &$error) { // Recuperamos parámetros validar $num_param = $this->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<strlen($ccnum); $i+=2) { $digit = (int)($ccnum{$i-1}) * 2; if ($digit < 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"=>"/^4\d{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 you can call CCVAL validator from validate yml file:
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
Sorry for my poor english.