Index: branches/5.2.x/units/helpers/order_helper.php =================================================================== diff -u -N -r14797 -r14813 --- branches/5.2.x/units/helpers/order_helper.php (.../order_helper.php) (revision 14797) +++ branches/5.2.x/units/helpers/order_helper.php (.../order_helper.php) (revision 14813) @@ -165,4 +165,34 @@ return $template; } + + /** + * Detects credit card type by it's number + * + * @param string $number + * @return int + * @access public + */ + public function getCreditCartType($number) + { + // Get rid of any non-digits + $number = preg_replace('/[^\d]/', '', $number); + + $mapping = Array ( + '/^4.{15}$|^4.{12}$/' => 1, // Visa + '/^5[1-5].{14}$/' => 2, // MasterCard + '/^3[47].{13}$/' => 3, // American Express + '/^6011.{12}$/' => 4, // Discover + '/^30[0-5].{11}$|^3[68].{12}$/' => 5, // Diners Club + '/^3.{15}$|^2131|1800.{11}$/' => 6, // JBC + ); + + foreach ($mapping as $number_regex => $card_type) { + if ( preg_match($number_regex, $number) ) { + return $card_type; + } + } + + return false; + } }