forked from corbanworks/fng-credit-card-validator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fngccvalidator.class.php
108 lines (89 loc) · 2.56 KB
/
fngccvalidator.class.php
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
<?php
/*
FNG Credit Card Validator v1.1
Copyright © 2009 Fake Name Generator <http://www.fakenamegenerator.com/>
FNG Credit Card Validator v1.1 by the Fake Name Generator is licensed to you
under a Creative Commons Attribution-Share Alike 3.0 United States License.
For full license details, please visit:
http://www.fakenamegenerator.com/license.php
*/
class fngccvalidator{
/**
* Validate credit card number and return card type.
* Optionally you can validate if it is a specific type.
*
* @param string $ccnumber
* @param string $cardtype
* @param string $allowTest
* @return mixed
*/
public function CreditCard($ccnumber, $cardtype = '', $allowTest = false){
// Check for test cc number
if($allowTest == false && $ccnumber == '4111111111111111'){
return false;
}
$ccnumber = preg_replace('/[^0-9]/','',$ccnumber); // Strip non-numeric characters
$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($cardtype)){
$match=false;
foreach($creditcard as $cardtype=>$pattern){
if(preg_match($pattern,$ccnumber)==1){
$match=true;
break;
}
}
if(!$match){
return false;
}
}elseif(@preg_match($creditcard[strtolower(trim($cardtype))],$ccnumber)==0){
return false;
}
$return['valid'] = $this->LuhnCheck($ccnumber);
$return['ccnum'] = $ccnumber;
$return['type'] = $cardtype;
return $return;
}
/**
* Do a modulus 10 (Luhn algorithm) check
*
* @param string $ccnum
* @return boolean
*/
public function LuhnCheck($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;
}
}
}
/* Example usage: */
/*
// Validate a credit card
$fngccvalidator = new fngccvalidator();
print_r($fngccvalidator->CreditCard('5330 4171 3521 4522'));
*/
?>