3 /** @package verysimple::Payment */
6 * CreditCardUtil is a standard class used to work in general
7 * with credit card processing, including clean output.
9 * @package verysimple::Payment
10 * @author VerySimple Inc.
11 * @copyright 1997-2008 VerySimple, Inc.
12 * @license http://www.gnu.org/licenses/lgpl.html LGPL
18 * Formats the given credit card number with dashes.
21 * string credit card number
24 static function FormatWithDashes($cc_num)
26 $dashSeparatedNumber = "";
28 if (strlen($cc_num) == 15) {
29 $firstFour = substr($cc_num, 0, 4);
30 $secondSix = substr($cc_num, 4, 6);
31 $thirdFive = substr($cc_num, 10, 5);
32 $dashSeparatedNumber = $firstFour . "-" . $secondSix . "-" . $thirdFive;
34 $firstFour = substr($cc_num, 0, 4);
35 $secondFour = substr($cc_num, 4, 4);
36 $thirdFour = substr($cc_num, 8, 4);
37 $fourthFour = substr($cc_num, 12, 4);
38 $dashSeparatedNumber = $firstFour . "-" . $secondFour . "-" . $thirdFour . "-" . $fourthFour;
41 return $dashSeparatedNumber;
45 * Returns a number with all non-numeric characters removed
47 * @param unknown_type $num
49 static function StripNonNumeric($num)
51 return preg_replace('{\D}', '', $num);
55 * Returns true if the card meets a valid mod10 (Luhn Algorithm) check
60 static function IsValidMod10($str)
62 if (strspn($str, "0123456789") != strlen($str)) {
76 9, // for even indices
89 $last = strlen($str) - 1;
91 for ($i = 0; $i <= $last; $i ++
) {
92 $sum +
= $map [$str [$last - $i] +
($i & 1) * 10];
95 return $sum %
10 == 0;
99 * Returns the Credit Card type based on the card number using the info
100 * at http://en.wikipedia.org/wiki/Credit_card_numbers as a reference
104 static function GetType($num)
106 $firstOne = substr($num, 0, 1);
108 if (strlen($num) < 4) {
112 if ($firstOne == 4) {
116 $firstTwo = substr($num, 0, 2);
117 if ($firstTwo == 34 ||
$firstTwo == 37) {
121 if ($firstTwo >= 51 && $firstTwo <= 55) {
125 if ($firstTwo == 36 ||
$firstTwo == 38 ||
$firstTwo == 54 ||
$firstTwo == 55) {
126 return "Diners Club";
129 $firstThree = substr($num, 0, 3);
130 if ($firstThree >= 300 && $firstThree <= 305) {
131 return "Carte Blanche";
134 if ($firstThree >= 644 && $firstThree <= 649) {
138 $firstFour = substr($num, 0, 4);
139 if ($firstFour == 6011) {
143 if ($firstFour == 2014 ||
$firstFour == 2149) {
147 if ($firstFour == 6011) {
151 $firstSix = substr($num, 0, 6);
152 if ($firstSix >= 622126 && $firstSix <= 622925) {
156 if ($firstOne == 3) {
160 if ($firstFour == 2131 ||
$firstFour == 1800) {