1 <?php
2
3 namespace Mypos\IPC;
4
5 6 7
8 class Response
9 {
10
11 12 13
14 private $cnf;
15 private $raw_data, $format, $data, $signature;
16
17 18 19 20 21 22
23 public function __construct(Config $cnf, $raw_data, $format)
24 {
25 $this->cnf = $cnf;
26 $this->_setData($raw_data, $format);
27 }
28
29 30 31 32 33 34
35 public static function getInstance(Config $cnf, $raw_data, $format)
36 {
37 return new self($cnf, $raw_data, $format);
38 }
39
40 41 42 43
44 public function isSignatureCorrect()
45 {
46 try {
47 $this->_verifySignature();
48 } catch (Exception $ex) {
49 return false;
50 }
51 return true;
52 }
53
54 55 56 57
58 function getSignature()
59 {
60 return $this->signature;
61 }
62
63 64 65 66
67 function getStatus()
68 {
69 return Helper::getArrayVal($this->getData(CASE_LOWER), 'status');
70 }
71
72 73 74 75
76 function getStatusMsg()
77 {
78 return Helper::getArrayVal($this->getData(CASE_LOWER), 'statusmsg');
79 }
80
81 82 83 84 85 86
87 function getData($case = null)
88 {
89 if ($case !== null) {
90 if (!in_array($case, array(CASE_LOWER, CASE_UPPER))) {
91 throw new IPC_Exception('Invalid Key Case!');
92 }
93 return array_change_key_case($this->data, $case);
94 }
95
96 return $this->data;
97 }
98
99 100 101 102
103 function getDataRaw()
104 {
105 return $this->raw_data;
106 }
107
108
109
110 private function _setData($raw_data, $format)
111 {
112 if (empty($raw_data)) {
113 throw new IPC_Exception('Invalid Reponce data');
114 }
115
116 $this->format = $format;
117 $this->raw_data = $raw_data;
118
119 switch ($this->format) {
120 case Defines::COMMUNICATION_FORMAT_JSON:
121 $this->data = json_decode($this->raw_data, 1);
122 break;
123 case Defines::COMMUNICATION_FORMAT_XML:
124 $this->data = (array)new \SimpleXMLElement($this->raw_data);
125 if (isset($this->data['@attributes'])) {
126 unset($this->data['@attributes']);
127 }
128 break;
129 case Defines::COMMUNICATION_FORMAT_POST:
130 $this->data = $this->raw_data;
131 break;
132 default:
133 throw new IPC_Exception('Invalid response format!');
134 break;
135 }
136
137 if (empty($this->data)) {
138 throw new IPC_Exception('No IPC Response!');
139 }
140
141 $this->_exctractSignature() && $this->_verifySignature();
142 return $this;
143 }
144
145 private function _exctractSignature()
146 {
147 if (is_array($this->data) && !empty($this->data)) {
148 foreach ($this->data as $k => $v) {
149 if (strtolower($k) == 'signature') {
150 $this->signature = $v;
151 unset($this->data[$k]);
152 }
153 }
154 }
155 return true;
156 }
157
158 private function _verifySignature()
159 {
160 if (empty($this->signature)) {
161 throw new IPC_Exception('Missing request signature!');
162 }
163
164 if (!$this->cnf) {
165 throw new IPC_Exception('Missing config object!');
166 }
167
168
169 $pubKeyId = openssl_get_publickey($this->cnf->getAPIPublicKey());
170 if (!openssl_verify($this->_getSignData(), base64_decode($this->signature), $pubKeyId, Defines::SIGNATURE_ALGO)) {
171 throw new IPC_Exception('Signature check failed!');
172 }
173 }
174
175 private function _getSignData()
176 {
177 return base64_encode(implode('-', Helper::getValuesFromMultiDemensionalArray($this->data)));
178 }
179
180 }
181