Highway to PSR2
[openemr.git] / portal / patient / fwk / libs / verysimple / Payment / CreditCardUtil.php
blob2703147a54abeda4aafa82851acdf380769c6d08
1 <?php
3 /** @package verysimple::Payment */
5 /**
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
13 * @version 1.0
15 class CreditCardUtil
17 /**
18 * Formats the given credit card number with dashes.
20 * @param
21 * string credit card number
22 * @return string
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;
33 } else {
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;
44 /**
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);
54 /**
55 * Returns true if the card meets a valid mod10 (Luhn Algorithm) check
57 * @param
58 * bool
60 static function IsValidMod10($str)
62 if (strspn($str, "0123456789") != strlen($str)) {
63 return false;
66 $map = array (
76 9, // for even indices
87 ); // for odd indices
88 $sum = 0;
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;
98 /**
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
102 * @return string
104 static function GetType($num)
106 $firstOne = substr($num, 0, 1);
108 if (strlen($num) < 4) {
109 return "";
112 if ($firstOne == 4) {
113 return "Visa";
116 $firstTwo = substr($num, 0, 2);
117 if ($firstTwo == 34 || $firstTwo == 37) {
118 return "AmEx";
121 if ($firstTwo >= 51 && $firstTwo <= 55) {
122 return "MasterCard";
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) {
135 return "Discover";
138 $firstFour = substr($num, 0, 4);
139 if ($firstFour == 6011) {
140 return "Discover";
143 if ($firstFour == 2014 || $firstFour == 2149) {
144 return "enRoute";
147 if ($firstFour == 6011) {
148 return "Discover";
151 $firstSix = substr($num, 0, 6);
152 if ($firstSix >= 622126 && $firstSix <= 622925) {
153 return "Discover";
156 if ($firstOne == 3) {
157 return "JCB";
160 if ($firstFour == 2131 || $firstFour == 1800) {
161 return "JCB";
164 return "Other";