anonymous / gist:181049
Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View gist:181049
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
<?php
/*
Credit Card Validator
Date - Sep 04, 2009
Author - Brent Shaffer
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.
 
Borrows from sfValidatorCCVAL by Oriol Rius (oriol@joor.net)
*/
class sfValidatorCreditCardNumber extends sfValidatorBase
{
protected $_ccregex = 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}$/"
);
public function configure($options = array(), $messages = array())
{
$this->addOption('card_type', null);
$this->addMessage('invalid', 'The credit card number you entered is not valid');
}
public function doClean($value)
{
if (!$this->_isValidCreditCard($value, $this->options['card_type']))
{
throw new sfValidatorError($this, 'invalid', array('value' => $value));
}
return $value;
}
/**
* 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);
}
}
 
return (($checksum % 10) == 0);
 
}
 
/**
* Launch validation
*
* @param integer $ccnum
* @param string $type
* @param boolean $returnobj
* @return boolean
*/
private function _isValidCreditCard($ccnum, $type = null)
{
if(!$type)
{
$match = false;
foreach($this->_ccregex as $type => $pattern)
{
if(preg_match($pattern,$ccnum) == 1)
{
$match = true;
break;
}
}
return $match ? $this->_checkSum($ccnum) : false;
}
 
if(@preg_match($this->_ccregex[strtolower(trim($type))],$ccnum) == 0)
{
return false;
}
return $this->_checkSum($ccnum);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.