2 namespace Utility\Comms;
8 * Facilitates chatting to TransUnion JSON api.
18 static private $liveUrl = 'https://tuaba.transunion.co.za/100/TransUnionAuto.JSon.svc';
21 * Development/staging URL
24 static private $devUrl = 'https://devtuaba.transunion.co.za/100/TransUnionAuto.JSon.svc';
27 * Development/staging credentials.
30 static private $devCredentials = array(
31 'Username' => 'TuabaDev',
32 'Password' => '2!2E2A736@'
38 * Collect vehicle particulars from VIN number.
40 * @param integer $regYear
41 * @param integer $mileage
42 * @param integer $condition
45 static public function searchByVin($vin, $regYear, $mileage = null, $condition = null)
48 'Credential' => array(),
52 if (!is_null($mileage) && !is_null($condition)
53 && is_numeric($mileage) && is_numeric($condition)
54 && 0 < $condition && 6 > $condition)
56 $params['Mileage'] = $mileage;
57 $params['Condition'] = $condition;
59 return self::_speaketh('SearchByVIN', $params);
63 * Collect vehicle particulars from Registration number.
64 * @param string $regNo
65 * @param integer $regYear
66 * @param integer $mileage
67 * @param integer $condition
70 static public function searchByRegNo($regNo, $regYear, $mileage = null, $condition = null)
73 'Credential' => array(),
77 if (!is_null($mileage) && !is_null($condition)
78 && is_numeric($mileage) && is_numeric($condition)
79 && 0 < $condition && 6 > $condition)
81 $params['Mileage'] = $mileage;
82 $params['Condition'] = $condition;
84 return self::_speaketh('SearchByRegNo', $params);
88 * Collect vehicle particulars from MM code.
89 * @param string $mmCode
90 * @param integer $regYear
91 * @param integer $mileage
92 * @param integer $condition
95 static public function searchByMmCode($mmCode, $regYear, $mileage = null, $condition = null)
97 if ('other' == $mmCode)
101 $mmCode = str_pad($mmCode, 8, "0", STR_PAD_LEFT);
103 'Credential' => array(),
105 'RegYear' => $regYear
107 if (!is_null($mileage) && !is_null($condition)
108 && is_numeric($mileage) && is_numeric($condition)
109 && 0 < $condition && 6 > $condition)
111 $params['Mileage'] = $mileage;
112 $params['Condition'] = $condition;
114 return self::_speaketh('SearchByMMCode', $params);
118 * Speak to the TransUnion server.
119 * @param string $channel
120 * @param array $message
121 * @return array|boolean
123 static private function _speaketh($method, array $params)
125 $config = \Utility\Registry::getConfigParam('Transunion');
126 $production = ('development' != getenv('APPLICATION_ENV'))
133 $credentials = $production
135 'Username' => $config['Username'],
136 'Password' => $config['Password']
138 : self::$devCredentials;
140 $params['Credential'] = $credentials;
141 $cmd = \Zend\Json\Json::encode($params);
144 if (false && $production && function_exists('curl_init'))
146 #-> Have curl, use it.
147 $ch = curl_init($url . '/' . $method);
148 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
149 curl_setopt($ch, CURLOPT_POSTFIELDS, $cmd);
150 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
151 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
152 'Content-Type: application/json',
153 'Content-Length: ' . strlen($cmd))
155 $result = curl_exec($ch);
157 return self::evalResult($result);
161 #-> No curl, use the slower option.
162 $result = file_get_contents($url . '/' . $method, null, stream_context_create(array(
165 'header' => 'Content-Type: application/json' . "\r\n"
166 . 'Content-Length: ' . strlen($cmd) . "\r\n",
170 return self::evalResult($result, $params);
173 catch (\Exception $e)
175 \Utility\Debug::errorLog(__CLASS__, "$e");
181 * Pack result into array and provide a text status.
182 * @param unknown $result
185 static protected function evalResult($result, $params)
187 $authData = \Utility\Registry::getAuthData();
188 $em = \Utility\Registry::getEntityManager();
189 $logEntry = new \Utility\Entity\ApiLog();
190 $logEntry->fromCompany = isset($authData['company']) && isset($authData['company']['id'])
192 '\Company\Entity\Company',
193 $authData['company']['id']
196 $logEntry->fromProfile = isset($authData['id'])
198 '\User\Entity\Profile',
202 $logEntry->service = 'TransUnion';
203 $logEntry->request = $params;
206 $result = \Zend\Json\Json::decode($result, \Zend\Json\Json::TYPE_ARRAY);
209 3 => 'No Results From TransUnion',
210 4 => 'No Values From TransUnion',
211 5 => 'Year is out of range'
213 isset($result['Status'])
214 && isset($statusMap[$result['Status']])
215 && $result['Status'] = $statusMap[$result['Status']];
216 $logEntry->response = $result;
217 $logEntry->status = isset($result['Status'])
220 $em->persist($logEntry);
224 catch (\Exception $e)
226 $logEntry->status = 'Error';
227 $em->persist($logEntry);