Skip to content

Instantly share code, notes, and snippets.

@jonathonbyrdziak
Created April 6, 2011 18:01
Show Gist options
  • Save jonathonbyrdziak/906162 to your computer and use it in GitHub Desktop.
Save jonathonbyrdziak/906162 to your computer and use it in GitHub Desktop.
Identify a Credit Card by its number
/**
* function is responsible for returning card data, based off of the
* credit card number alone.
*
* @param unknown_type $cc_no
* @return unknown
*/
function identifyCard ($cc_no)
{ // Get card type based on prefix and length of card number
// I am sure there are smarter ways of implementing this, however due to lack of experience with regexp...
//in no paticular order
//--------------------------------------------
if (ereg ('^5[1-5].{14}$', $cc_no)) {return array('Type' => 'Mastercard','Index' => 11, 'CanAccept' => TRUE);}
//--------------------------------------------
if (ereg ('^6334[5-9].{11}$', $cc_no)) {return array('Type' => 'Solo / Maestro','Index' => 16, 'CanAccept' => FALSE);}
if (ereg ('^6767[0-9].{11}$', $cc_no)) {return array('Type' => 'Solo / Maestro','Index' => 16, 'CanAccept' => FALSE);}
//--------------------------------------------
if (ereg ('^564182[0-9].{9}$', $cc_no)) {return array('Type' => 'Switch / Maestro','Index' => 19, 'CanAccept' => FALSE);}
if (ereg ('^6333[0-4].{11}$', $cc_no)) {return array('Type' => 'Switch / Maestro','Index' => 19, 'CanAccept' => FALSE);}
if (ereg ('^6759[0-9].{11}$', $cc_no)) {return array('Type' => 'Switch / Maestro','Index' => 19, 'CanAccept' => FALSE);}
//--------------------------------------------
if (ereg ('^49030[2-9].{10}$', $cc_no)) {return array('Type' => 'Switch','Index' => 18, 'CanAccept' => FALSE);}
if (ereg ('^49033[5-9].{10}$', $cc_no)) {return array('Type' => 'Switch','Index' => 18, 'CanAccept' => FALSE);}
if (ereg ('^49110[1-2].{10}$', $cc_no)) {return array('Type' => 'Switch','Index' => 18, 'CanAccept' => FALSE);}
if (ereg ('^49117[4-9].{10}$', $cc_no)) {return array('Type' => 'Switch','Index' => 18, 'CanAccept' => FALSE);}
if (ereg ('^49118[0-2].{10}$', $cc_no)) {return array('Type' => 'Switch','Index' => 18, 'CanAccept' => FALSE);}
if (ereg ('^4936[0-9].{11}$', $cc_no)) {return array('Type' => 'Switch','Index' => 18, 'CanAccept' => FALSE);}
//--------------------------------------------
if (ereg ('^6011.{12}$', $cc_no)) {return array('Type' => 'Discover Card','Index' => 23, 'CanAccept' => FALSE);}
//--------------------------------------------
//failing earlier 6xxx xxxx xxxx xxxx checks then its a Maestro card
if (ereg ('^6[0-9].{14}$', $cc_no)) {return array('Type' => 'Maestro','Index' => 20, 'CanAccept' => FALSE);}
if (ereg ('^5[0,6-8].{14}$', $cc_no)) {return array('Type' => 'Maestro','Index' => 20, 'CanAccept' => FALSE);}
//--------------------------------------------
if (ereg ('^450875[0-9].{9}$', $cc_no)) {return array('Type' => 'UK Electron','Index' => 21, 'CanAccept' => FALSE);}
if (ereg ('^48440[6-8].{10}$', $cc_no)) {return array('Type' => 'UK Electron','Index' => 21, 'CanAccept' => FALSE);}
if (ereg ('^48441[1-9].{10}$', $cc_no)) {return array('Type' => 'UK Electron','Index' => 21, 'CanAccept' => FALSE);}
if (ereg ('^4844[2-4].{11}$', $cc_no)) {return array('Type' => 'UK Electron','Index' => 21, 'CanAccept' => FALSE);}
if (ereg ('^48445[0-5].{10}$', $cc_no)) {return array('Type' => 'UK Electron','Index' => 21, 'CanAccept' => FALSE);}
if (ereg ('^4917[3-5].{11}$', $cc_no)) {return array('Type' => 'UK Electron','Index' => 21, 'CanAccept' => FALSE);}
if (ereg ('^491880[0-9].{9}$', $cc_no)) {return array('Type' => 'UK Electron','Index' => 21, 'CanAccept' => FALSE);}
//--------------------------------------------
if (ereg ('^41373[3-7].{10}$', $cc_no)) {return array('Type' => 'Visa Delta','Index' => 13, 'CanAccept' => TRUE);}
if (ereg ('^4462[0-9].{11}$', $cc_no)) {return array('Type' => 'Visa Delta','Index' => 13, 'CanAccept' => TRUE);}
if (ereg ('^45397[8-9].{10}$', $cc_no)) {return array('Type' => 'Visa Delta','Index' => 13, 'CanAccept' => TRUE);}
if (ereg ('^454313[0-9].{9}$', $cc_no)) {return array('Type' => 'Visa Delta','Index' => 13, 'CanAccept' => TRUE);}
if (ereg ('^45443[2-5].{10}$', $cc_no)) {return array('Type' => 'Visa Delta','Index' => 13, 'CanAccept' => TRUE);}
if (ereg ('^454742[0-9].{9}$', $cc_no)) {return array('Type' => 'Visa Delta','Index' => 13, 'CanAccept' => TRUE);}
if (ereg ('^45672[5-9].{10}$', $cc_no)) {return array('Type' => 'Visa Delta','Index' => 13, 'CanAccept' => TRUE);}
if (ereg ('^45673[0-9].{10}$', $cc_no)) {return array('Type' => 'Visa Delta','Index' => 13, 'CanAccept' => TRUE);}
if (ereg ('^45674[0-5].{10}$', $cc_no)) {return array('Type' => 'Visa Delta','Index' => 13, 'CanAccept' => TRUE);}
if (ereg ('^4658[3-7].{11}$', $cc_no)) {return array('Type' => 'Visa Delta','Index' => 13, 'CanAccept' => TRUE);}
if (ereg ('^4659[0-5].{11}$', $cc_no)) {return array('Type' => 'Visa Delta','Index' => 13, 'CanAccept' => TRUE);}
if (ereg ('^484409[0-9].{9}$', $cc_no)) {return array('Type' => 'Visa Delta','Index' => 13, 'CanAccept' => TRUE);}
if (ereg ('^48441[0-9].{10}$', $cc_no)) {return array('Type' => 'Visa Delta','Index' => 13, 'CanAccept' => TRUE);}
if (ereg ('^4909[6-7].{11}$', $cc_no)) {return array('Type' => 'Visa Delta','Index' => 13, 'CanAccept' => TRUE);}
if (ereg ('^49218[1-2].{10}$', $cc_no)) {return array('Type' => 'Visa Delta','Index' => 13, 'CanAccept' => TRUE);}
if (ereg ('^498824[0-9].{9}$', $cc_no)) {return array('Type' => 'Visa Delta','Index' => 13, 'CanAccept' => TRUE);}
//--------------------------------------------
if (ereg ('^40550[1-4].{10}$', $cc_no)) {return array('Type' => 'Visa Purchasing','Index' => 12, 'CanAccept' => TRUE);}
if (ereg ('^40555[0-4].{10}$', $cc_no)) {return array('Type' => 'Visa Purchasing','Index' => 12, 'CanAccept' => TRUE);}
if (ereg ('^415928[0-4].{9}$', $cc_no)) {return array('Type' => 'Visa Purchasing','Index' => 12, 'CanAccept' => TRUE);}
if (ereg ('^42460[4-5].{10}$', $cc_no)) {return array('Type' => 'Visa Purchasing','Index' => 12, 'CanAccept' => TRUE);}
if (ereg ('^427533[0-9].{9}$', $cc_no)) {return array('Type' => 'Visa Purchasing','Index' => 12, 'CanAccept' => TRUE);}
if (ereg ('^4288[0-9].{11}$', $cc_no)) {return array('Type' => 'Visa Purchasing','Index' => 12, 'CanAccept' => TRUE);}
if (ereg ('^443085[0-9].{9}$', $cc_no)) {return array('Type' => 'Visa Purchasing','Index' => 12, 'CanAccept' => TRUE);}
if (ereg ('^448[4-6].{12}$', $cc_no)) {return array('Type' => 'Visa Purchasing','Index' => 12, 'CanAccept' => TRUE);}
if (ereg ('^471[5-6].{12}$', $cc_no)) {return array('Type' => 'Visa Purchasing','Index' => 12, 'CanAccept' => TRUE);}
if (ereg ('^4804[0-9].{11}$', $cc_no)) {return array('Type' => 'Visa Purchasing','Index' => 12, 'CanAccept' => TRUE);}
//--------------------------------------------
if (ereg ('^49030[0-1].{10}$', $cc_no)) {return array('Type' => 'Visa ATM','Index' => 14, 'CanAccept' => FALSE);}
if (ereg ('^4903[1-2].{11}$', $cc_no)) {return array('Type' => 'Visa ATM','Index' => 14, 'CanAccept' => FALSE);}
if (ereg ('^49033[0-4].{10}$', $cc_no)) {return array('Type' => 'Visa ATM','Index' => 14, 'CanAccept' => FALSE);}
if (ereg ('^4903[4-9].{11}$', $cc_no)) {return array('Type' => 'Visa ATM','Index' => 14, 'CanAccept' => FALSE);}
if (ereg ('^49040[0-9].{10}$', $cc_no)) {return array('Type' => 'Visa ATM','Index' => 14, 'CanAccept' => FALSE);}
if (ereg ('^490419[0-9].{9}$', $cc_no)) {return array('Type' => 'Visa ATM','Index' => 14, 'CanAccept' => FALSE);}
if (ereg ('^490451[0-9].{9}$', $cc_no)) {return array('Type' => 'Visa ATM','Index' => 14, 'CanAccept' => FALSE);}
if (ereg ('^490459[0-9].{9}$', $cc_no)) {return array('Type' => 'Visa ATM','Index' => 14, 'CanAccept' => FALSE);}
if (ereg ('^490467[0-9].{9}$', $cc_no)) {return array('Type' => 'Visa ATM','Index' => 14, 'CanAccept' => FALSE);}
if (ereg ('^49047[5-8].{10}$', $cc_no)) {return array('Type' => 'Visa ATM','Index' => 14, 'CanAccept' => FALSE);}
if (ereg ('^4905[0-9].{11}$', $cc_no)) {return array('Type' => 'Visa ATM','Index' => 14, 'CanAccept' => FALSE);}
if (ereg ('^491001[0-9].{9}$', $cc_no)) {return array('Type' => 'Visa ATM','Index' => 14, 'CanAccept' => FALSE);}
if (ereg ('^49110[3-9].{10}$', $cc_no)) {return array('Type' => 'Visa ATM','Index' => 14, 'CanAccept' => FALSE);}
if (ereg ('^4911[1-6].{11}$', $cc_no)) {return array('Type' => 'Visa ATM','Index' => 14, 'CanAccept' => FALSE);}
if (ereg ('^49117[0-3].{10}$', $cc_no)) {return array('Type' => 'Visa ATM','Index' => 14, 'CanAccept' => FALSE);}
if (ereg ('^49118[3-9].{10}$', $cc_no)) {return array('Type' => 'Visa ATM','Index' => 14, 'CanAccept' => FALSE);}
if (ereg ('^49119[0-9].{10}$', $cc_no)) {return array('Type' => 'Visa ATM','Index' => 14, 'CanAccept' => FALSE);}
if (ereg ('^4928[0-9].{11}$', $cc_no)) {return array('Type' => 'Visa ATM','Index' => 14, 'CanAccept' => FALSE);}
if (ereg ('^4987[0-9].{11}$', $cc_no)) {return array('Type' => 'Visa ATM','Index' => 14, 'CanAccept' => FALSE);}
//--------------------------------------------
//failing earlier 4xxx xxxx xxxx xxxx checks then it must be a Visa
if (ereg ('^4(.{12}|.{15})$', $cc_no)) {return array('Type' => 'Visa','Index' => 12, 'CanAccept' => TRUE);}
//--------------------------------------------
if (ereg ('^3[4-7].{13}$', $cc_no)) {return array('Type' => 'American Express','Index' => 18, 'CanAccept' => FALSE);}
//--------------------------------------------
if (ereg ('^3(0[0-5].{11}|[6].{12}|[8].{12})$', $cc_no)) {return array('Type' => 'Diners Club/Carte Blanche','Index' => 19, 'CanAccept' => FALSE);}
//--------------------------------------------
if (ereg ('^(3.{15}|(2131|1800).{11})$', $cc_no)){return array('Type' => 'JCB','Index' => 21, 'CanAccept' => FALSE);}
if (ereg ('^(3528[0-9].{11})$', $cc_no)) {return array('Type' => 'JCB','Index' => 21, 'CanAccept' => FALSE);}
if (ereg ('^(35[3-8].{13})$', $cc_no)) {return array('Type' => 'JCB','Index' => 21, 'CanAccept' => FALSE);}
//--------------------------------------------
if (ereg ('^2(014|149).{11})$', $cc_no)) {return array('Type' => 'enRoute','Index' => 22, 'CanAccept' => FALSE);}
//the following are from http://www.merriampark.com/anatomycc.htm
//put in for 'fullness' - the following indicate the broad type of card
//if you are in a business that can reasonably be expected to accept fuel cards then you could accept cards starting with a 7
//please note I do not know how many digits there could be on the card, some specs suggest that cards can have
//upto 19 digits including the check digit.
//
//cards starting with a ? are issued by the following industry sectors
// 0 ISO/OTC 68 and related industries - if you know what that means good luck
// 1 Airlines
// 2 Airlines and other industries
// 3 Travel and Entertainment comment : AMEX / Cart Blanche etc
// 4 Banking and Financial comment : read VISA
// 5 Banking and Financial comment : read MasterCard
// 6 Merchandising and Banking comment : read store cards, bank cash cards / EFT cards
// 7 Petroleum comment : read fuel cards
// 8 Telecomm and related
// 9 National assignements comment : who knows, who cares?
//--------------------------------------------
return array('Type' => 'unknown or invalid','Index' => 0, 'CanAccept' => FALSE);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment