'TuabaDev', 'Password' => '2!2E2A736@' ); /** * Collect vehicle particulars from VIN number. * @param string $vin * @param integer $regYear * @param integer $mileage * @param integer $condition * @return array */ static public function searchByVin($vin, $regYear, $mileage = null, $condition = null) { $params = array( 'Credential' => array(), 'VIN' => $vin, 'RegYear' => $regYear ); if (!is_null($mileage) && !is_null($condition) && is_numeric($mileage) && is_numeric($condition) && 0 < $condition && 6 > $condition) { $params['Mileage'] = $mileage; $params['Condition'] = $condition; } return self::_speaketh('SearchByVIN', $params); } /** * Collect vehicle particulars from Registration number. * @param string $regNo * @param integer $regYear * @param integer $mileage * @param integer $condition * @return array */ static public function searchByRegNo($regNo, $regYear, $mileage = null, $condition = null) { $params = array( 'Credential' => array(), 'RegNo' => $regNo, 'RegYear' => $regYear ); if (!is_null($mileage) && !is_null($condition) && is_numeric($mileage) && is_numeric($condition) && 0 < $condition && 6 > $condition) { $params['Mileage'] = $mileage; $params['Condition'] = $condition; } return self::_speaketh('SearchByRegNo', $params); } /** * Collect vehicle particulars from MM code. * @param string $mmCode * @param integer $regYear * @param integer $mileage * @param integer $condition * @return array */ static public function searchByMmCode($mmCode, $regYear, $mileage = null, $condition = null) { if ('other' == $mmCode) { return array(); } $mmCode = str_pad($mmCode, 8, "0", STR_PAD_LEFT); $params = array( 'Credential' => array(), 'MMCode' => $mmCode, 'RegYear' => $regYear ); if (!is_null($mileage) && !is_null($condition) && is_numeric($mileage) && is_numeric($condition) && 0 < $condition && 6 > $condition) { $params['Mileage'] = $mileage; $params['Condition'] = $condition; } return self::_speaketh('SearchByMMCode', $params); } /** * Speak to the TransUnion server. * @param string $channel * @param array $message * @return array|boolean */ static private function _speaketh($method, array $params) { $config = \Utility\Registry::getConfigParam('Transunion'); $production = ('development' != getenv('APPLICATION_ENV')) ? true : false; $production = true; $url = $production ? self::$liveUrl : self::$devUrl; $credentials = $production ? array( 'Username' => $config['Username'], 'Password' => $config['Password'] ) : self::$devCredentials; $params['Credential'] = $credentials; $cmd = \Zend\Json\Json::encode($params); try { if (false && $production && function_exists('curl_init')) { #-> Have curl, use it. $ch = curl_init($url . '/' . $method); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $cmd); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($cmd)) ); $result = curl_exec($ch); curl_close($ch); return self::evalResult($result); } else { #-> No curl, use the slower option. $result = file_get_contents($url . '/' . $method, null, stream_context_create(array( 'http' => array( 'method' => 'POST', 'header' => 'Content-Type: application/json' . "\r\n" . 'Content-Length: ' . strlen($cmd) . "\r\n", 'content' => $cmd, ), ))); return self::evalResult($result, $params); } } catch (\Exception $e) { \Utility\Debug::errorLog(__CLASS__, "$e"); return false; } } /** * Pack result into array and provide a text status. * @param unknown $result * @return array|NULL */ static protected function evalResult($result, $params) { $authData = \Utility\Registry::getAuthData(); $em = \Utility\Registry::getEntityManager(); $logEntry = new \Utility\Entity\ApiLog(); $logEntry->fromCompany = isset($authData['company']) && isset($authData['company']['id']) ? $em->getReference( '\Company\Entity\Company', $authData['company']['id'] ) : null; $logEntry->fromProfile = isset($authData['id']) ? $em->getReference( '\User\Entity\Profile', $authData['id'] ) : null; $logEntry->service = 'TransUnion'; $logEntry->request = $params; try { $result = \Zend\Json\Json::decode($result, \Zend\Json\Json::TYPE_ARRAY); $statusMap = array( 2 => 'Success', 3 => 'No Results From TransUnion', 4 => 'No Values From TransUnion', 5 => 'Year is out of range' ); isset($result['Status']) && isset($statusMap[$result['Status']]) && $result['Status'] = $statusMap[$result['Status']]; $logEntry->response = $result; $logEntry->status = isset($result['Status']) ? $result['Status'] : 'Error'; $em->persist($logEntry); $em->flush(); return $result; } catch (\Exception $e) { $logEntry->status = 'Error'; $em->persist($logEntry); $em->flush(); return null; } } }