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