1 <?php
2
3 namespace Mypos\IPC;
4
5 /**
6 * IPC Configuration class
7 */
8 class Config
9 {
10 private $privateKey = null;
11 private $APIPublicKey = null;
12 private $encryptPublicKey = null;
13 private $keyIndex = null;
14 private $sid;
15 private $wallet;
16 private $lang = 'en';
17 private $version = '1.4';
18 private $ipc_url = 'https://www.mypos.com/vmp/checkout';
19 private $developerKey;
20 private $source;
21
22 /**
23 * Config constructor.
24 */
25 public function __construct()
26 {
27 $this->source = 'SDK_PHP_' . Defines::SDK_VERSION;
28 }
29
30 /**
31 * Store private RSA key as a filepath
32 *
33 * @param string $path File path
34 *
35 * @return Config
36 * @throws IPC_Exception
37 */
38 public function setPrivateKeyPath($path)
39 {
40 if (!is_file($path) || !is_readable($path)) {
41 throw new IPC_Exception('Private key not found in:'.$path);
42 }
43 $this->privateKey = file_get_contents($path);
44
45 return $this;
46 }
47
48 /**
49 * IPC API public RSA key
50 *
51 * @return string
52 */
53 public function getAPIPublicKey()
54 {
55 return $this->APIPublicKey;
56 }
57
58 /**
59 * IPC API public RSA key
60 *
61 * @param string $publicKey
62 *
63 * @return Config
64 */
65 public function setAPIPublicKey($publicKey)
66 {
67 $this->APIPublicKey = $publicKey;
68
69 return $this;
70 }
71
72 /**
73 * IPC API public RSA key as a filepath
74 *
75 * @param string $path
76 *
77 * @return Config
78 * @throws IPC_Exception
79 */
80 public function setAPIPublicKeyPath($path)
81 {
82 if (!is_file($path) || !is_readable($path)) {
83 throw new IPC_Exception('Public key not found in:'.$path);
84 }
85 $this->APIPublicKey = file_get_contents($path);
86
87 return $this;
88 }
89
90 /**
91 * Public RSA key using for encryption sensitive data
92 *
93 * @return string
94 */
95 public function getEncryptPublicKey()
96 {
97 return $this->encryptPublicKey;
98 }
99
100 /**
101 * Public RSA key using for encryption sensitive data
102 *
103 * @param string $key
104 *
105 * @return Config
106 */
107 public function setEncryptPublicKey($key)
108 {
109 $this->encryptPublicKey = $key;
110
111 return $this;
112 }
113
114 /**
115 * Public RSA key using for encryption sensitive data
116 *
117 * @param string $path File path
118 *
119 * @return Config
120 * @throws IPC_Exception
121 */
122 public function setEncryptPublicKeyPath($path)
123 {
124 if (!is_file($path) || !is_readable($path)) {
125 throw new IPC_Exception('Key not found in:'.$path);
126 }
127 $this->encryptPublicKey = file_get_contents($path);
128
129 return $this;
130 }
131
132 /**
133 * Language code (ISO 639-1)
134 *
135 * @return string
136 */
137 public function getLang()
138 {
139 return $this->lang;
140 }
141
142 /**
143 * Language code (ISO 639-1)
144 *
145 * @param string $lang
146 *
147 * @return Config
148 */
149 public function setLang($lang)
150 {
151 $this->lang = $lang;
152
153 return $this;
154 }
155
156 /**
157 * Store private RSA key
158 *
159 * @return string
160 */
161 public function getDeveloperKey()
162 {
163 return $this->developerKey;
164 }
165
166 /**
167 * Set myPOS developer key.
168 *
169 * @param string $developerKey
170 *
171 * @return Config
172 */
173 public function setDeveloperKey($developerKey)
174 {
175 $this->developerKey = $developerKey;
176
177 return $this;
178 }
179
180 /**
181 * @return string
182 */
183 public function getSource()
184 {
185 return $this->source;
186 }
187
188 /**
189 * Additional parameter to specify the source of request
190 *
191 * @param string $source
192 */
193 public function setSource($source)
194 {
195 $this->source = $source;
196 }
197
198 /**
199 * Validate all set config details
200 *
201 * @return boolean
202 * @throws IPC_Exception
203 */
204 public function validate()
205 {
206 if ($this->getKeyIndex() == null || !is_numeric($this->getKeyIndex())) {
207 throw new IPC_Exception('Invalid Key Index');
208 }
209
210 if ($this->getIpcURL() == null || !Helper::isValidURL($this->getIpcURL())) {
211 throw new IPC_Exception('Invalid IPC URL');
212 }
213
214 if ($this->getSid() == null || !is_numeric($this->getSid())) {
215 throw new IPC_Exception('Invalid SID');
216 }
217
218 if ($this->getWallet() == null || !is_numeric($this->getWallet())) {
219 throw new IPC_Exception('Invalid Wallet number');
220 }
221
222 if ($this->getVersion() == null) {
223 throw new IPC_Exception('Invalid IPC Version');
224 }
225
226 if (!openssl_get_privatekey($this->getPrivateKey())) {
227 throw new IPC_Exception('Invalid Private key');
228 }
229
230 return true;
231 }
232
233 /**
234 * Keyindex used for signing request
235 *
236 * @return string
237 */
238 public function getKeyIndex()
239 {
240 return $this->keyIndex;
241 }
242
243 /**
244 * Keyindex used for signing request
245 *
246 * @param int $keyIndex
247 *
248 * @return Config
249 */
250 public function setKeyIndex($keyIndex)
251 {
252 $this->keyIndex = $keyIndex;
253
254 return $this;
255 }
256
257 /**
258 * IPC API URL
259 *
260 * @return string
261 */
262 public function getIpcURL()
263 {
264 return $this->ipc_url;
265 }
266
267 /**
268 * IPC API URL
269 *
270 * @param string $ipc_url
271 *
272 * @return Config
273 */
274 public function setIpcURL($ipc_url)
275 {
276 $this->ipc_url = $ipc_url;
277
278 return $this;
279 }
280
281 /**
282 * Store ID
283 *
284 * @return int
285 */
286 public function getSid()
287 {
288 return $this->sid;
289 }
290
291 /**
292 * Store ID
293 *
294 * @param int $sid
295 *
296 * @return Config
297 */
298 public function setSid($sid)
299 {
300 $this->sid = $sid;
301
302 return $this;
303 }
304
305 /**
306 * Wallet number
307 *
308 * @return string
309 */
310 public function getWallet()
311 {
312 return $this->wallet;
313 }
314
315 /**
316 * Wallet number
317 *
318 * @param string $wallet
319 *
320 * @return Config
321 */
322 public function setWallet($wallet)
323 {
324 $this->wallet = $wallet;
325
326 return $this;
327 }
328
329 /**
330 * API Version
331 *
332 * @return string
333 */
334 public function getVersion()
335 {
336 return $this->version;
337 }
338
339 /**
340 * API Version
341 *
342 * @param string $version
343 *
344 * @return Config
345 */
346 public function setVersion($version)
347 {
348 $this->version = $version;
349
350 return $this;
351 }
352
353 /**
354 * Store private RSA key
355 *
356 * @return string
357 */
358 public function getPrivateKey()
359 {
360 return $this->privateKey;
361 }
362
363 /**
364 * Store private RSA key
365 *
366 * @param string $privateKey
367 *
368 * @return Config
369 */
370 public function setPrivateKey($privateKey)
371 {
372 $this->privateKey = $privateKey;
373
374 return $this;
375 }
376
377 /**
378 * Decrypt data string and set configuration parameters
379 *
380 * @param string $configurationPackage
381 * @return Config
382 * @throws IPC_Exception
383 */
384 public function loadConfigurationPackage($configurationPackage)
385 {
386 $decoded = base64_decode($configurationPackage);
387
388 if (!$decoded) {
389 throw new IPC_Exception('Invalid autogenerated data');
390 }
391
392 $data = json_decode($decoded, true);
393
394 if (!$data) {
395 throw new IPC_Exception('Invalid autogenerated data');
396 }
397
398 foreach ($data as $key => $value) {
399 switch($key) {
400 case 'sid':
401 $this->setSid($value);
402 break;
403 case 'cn':
404 $this->setWallet($value);
405 break;
406 case 'pk':
407 $this->setPrivateKey($value);
408 break;
409 case 'pc':
410 $this->setAPIPublicKey($value);
411 $this->setEncryptPublicKey($value);
412 break;
413 case 'idx':
414 $this->setKeyIndex($value);
415 break;
416 default:
417 throw new IPC_Exception('Unknown autogenerated authentication data parameter: ' . $key);
418 }
419 }
420
421 return $this;
422 }
423 }
424