data['response'] = "SUCCESS"; ## check expire date (always required) $this->expireDate($expireDate,$expireFormat); if ($scReqd == true) $this->securityCode($securityCode); /************************************************ * BEGIN: Regular Expressions to validate Certain Cards ************************************************/ $mastercard = "^5[1-5][0-9]{14}$"; $visa = "^4[0-9]{12}([0-9]{3})?$"; // $americanExpress = "^3[47][0-9]{13}$"; old in 4.0.3 //$americanExpress = "((^3[47])|(^3[34])|(^3[37]))((\d{11}$)|(\d{13}$))"; $americanExpress = "^[34|37|47]([0-9]{14}|[0-9]{12})$"; $diners = "^3(0[0-5]|[68][0-9])[0-9]{11}$"; $discover = "^6011[0-9]{12}$"; $jcb = "^(3[0-9]{4}|2131|1800)[0-9]{11}$"; $australianBankCard = "^5610([0-9]{12})?$"; $enRoute = "^(2014|2149)([0-9]{11})?$"; $electron = "^(450875|48440[6-9]{1}|4844[1-4]{1}[0-9]{1}|48445[0-5]{1}|4917[3-5]{1}[0-9]{1}|491880|5[1-5]{1})([0-9]{10}|[0-9]{14})?$";// $delta = "^(41373[3-7]{1}|4462[0-9]{2}|45397[8-9]{1}|454313|45443[2-5]{1}|454742|45672[5-9]{1}|45673[0-9]{1}|45674[0-5]{1}|4658[3-7]{1}[0-9]{1}|4659[0-5]{1}[0-9]{1}|4609[6-7]{1}[0-9]{1}|49218[1-2]{1}|498824)([0-9]{10})?$"; $solo = "^(6334[5-9]{1}[0-9]{1}|6767[0-9]{2}|3528[0-9]{2})([0-9]{10})?$"; $maestro = "^(5000[0-9]{2}|5[6-8]{1}|6[0-9]{5})([0-9]{10}|[0-9]{14})?$"; $switch = "^(49030[2-9]{1}|49033[5-9]{1}|49110[1-2]{1}|49117[4-9]{1}|49118[0-2]{1}|4936[0-9]{2}|564182|6333[1-4]{1}[0-9]{1}|6759[0-9]{2})([0-9]{10}|[0-9]{12}|[0-9]{13})?$"; /************************************************ * END: Regular Expressions to validate Certain Cards ************************************************/ if (empty($cardNo)) { $this->error(6); } elseif (ereg($mastercard, $cardNo)) { $this->mod10($cardNo); $this->data['cardType'] = "MASTERCARD"; } elseif (ereg($electron, $cardNo)) { $this->data['cardType'] = "ELECTRON"; } elseif(ereg($visa, $cardNo)) { ## mod 10 fails on 13 length card nos :( Booooooooooooooooooooooooooooo if (strlen($cardNo) !== 13) $this->mod10($cardNo); $this->data['cardType'] = "VISA"; } elseif (ereg($americanExpress, $cardNo)) { $this->mod10($cardNo); $this->data['cardType'] = "AMERICAN EXPRESS"; } elseif (ereg($diners, $cardNo)) { $this->mod10($cardNo); $this->data['cardType'] = "DINERS"; } elseif (ereg($discover, $cardNo)) { $this->mod10($cardNo); $this->data['cardType'] = "DISCOVER"; } elseif (ereg($enRoute, $cardNo)) { $this->data['cardType'] = "ENROUTE"; } elseif (ereg($jcb, $cardNo)) { $this->mod10($cardNo); $this->data['cardType'] = "JCB"; } elseif (ereg($australianBankCard, $cardNo)) { $this->data['cardType'] = "AUSTRALIAN BANK CARD"; } elseif (ereg($delta, $cardNo)) { $this->data['cardType'] = "DELTA"; } elseif (ereg($switch, $cardNo)) { $this->mod10($cardNo); $this->data['cardType'] = "SWITCH"; ## switch/maestro/solo requires an issue date or number if (!$this->issueDate($issueDate,$issueFormat) || !$this->issueNo($issueNo)) $this->error(3); } elseif (ereg($solo, $cardNo)) { $this->mod10($cardNo); $this->data['cardType'] = "SOLO"; ## switch/maestro/solo requires an issue date or number if (!$this->issueDate($issueDate,$issueFormat) || !$this->issueNo($issueNo)) $this->error(3); } elseif (ereg($maestro, $cardNo)) { $this->mod10($cardNo); $this->data['cardType'] = "MAESTRO"; ## switch/maestro/solo requires an issue date or number if (!$this->issueDate($issueDate,$issueFormat) || !$this->issueNo($issueNo)) $this->error(3); } else { $this->error(1); } return $this->data; } // LUHN formula function mod10($cardNo) { $numSum = 0; for($i = 0; $i < strlen($cardNo); $i++) { $currentNum = substr($cardNo, $i, 1); if($i % 2 == 1) { $currentNum *= 2; } if($currentNum > 9) { $firstNum = $currentNum % 10; $secondNum = ($currentNum - $firstNum) / 10; $currentNum = $firstNum + $secondNum; } $numSum += $currentNum; } $passCheck = ($numSum % 10 == 0); if($passCheck == 0){ return true; } else { $this->error(5); return false; } } function expireDate($expireDate,$expireFormat){ if(strlen($expireDate) !== $expireFormat){ $this->error(2); return false; } elseif($expireFormat == 4 && $expireDateerror(2); return false; } elseif($expireFormat == 6 && $expireDateerror(2); return false; } else { return true; } } function issueDate($issueDate,$issueFormat) { if(strlen($issueDate) !== $issueFormat){ $this->error(3); return false; } elseif($issueFormat == 4 && $issueDate>date("ym")) { $this->error(3); return false; } elseif($issueFormat == 6 && $issueDate>date("Ym")) { $this->error(3); return false; } else { return true; } } function issueNo($issueNo) { if($issueNo>0){ return true; } else { return false; } } function securityCode($securityCode) { if(($securityCode>0 && $securityCode<10000) && (strlen($securityCode)==3 || strlen($securityCode)==4)) { return true; } else { $this->error(4); } } function error($errorCode) { global $lang; $this->data['response'] = "FAIL"; switch($errorCode) { case(1): $this->data['error'][1] = "Card not recognised!"; break; case(2): $this->data['error'][2] = "No expiry date was entered or it wasn't valid."; break; case(3): $this->data['error'][3] = "Please enter a valid issue number or date."; break; case(4): $this->data['error'][4] = "Please enter a valid security code."; break; case(5): $this->data['error'][5] = "Credit card number is not valid."; break; case(6): $this->data['error'][5] = "Please enter a card number."; break; } } } ?>