Git Repository Public Repository

namibia

URLs

Copy to Clipboard
 
df0489e1eeeeab5a9bd44e1d84fce49924fe1bac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
namespace Utility\Comms;



use Utility;
/**
 * Facilitates chatting to TransUnion JSON api.
 * @author andre.fourie
 */
class TransUnion
{

	/**
	 * Production URL
	 * @var string
	 */
	static private $liveUrl = 'https://tuaba.transunion.co.za/100/TransUnionAuto.JSon.svc';

	/**
	 * Development/staging URL
	 * @var string
	 */
	static private $devUrl  = 'https://devtuaba.transunion.co.za/100/TransUnionAuto.JSon.svc';

	/**
	 * Development/staging credentials.
	 * @var array
	 */
	static private $devCredentials = array(
						'Username' => '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;
		}
	}


}

Commits for namibiamodule/Utility/src/Utility/Comms/TransUnion.php

Diff revisions: vs.
Revision Author Commited Message
df0489 ... Mark Fri 14 Oct, 2016 10:01:00 +0000

initial commit