1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* General class for all request types |
5
|
|
|
* |
6
|
|
|
* @package php_EasyPay |
7
|
|
|
* @version 1.1 |
8
|
|
|
* @author Dmitry Shovchko <[email protected]> |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace EasyPay\Provider31\Request; |
13
|
|
|
|
14
|
|
|
use EasyPay\Log as Log; |
15
|
|
|
use EasyPay\Exception; |
16
|
|
|
use EasyPay\Sign as Sign; |
17
|
|
|
use EasyPay\Provider31\Request\RAW as RAW; |
18
|
|
|
|
19
|
|
|
class General |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var EasyPay\Provider31\Request\RAW raw request |
|
|
|
|
23
|
|
|
*/ |
24
|
|
|
protected $raw_request; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string 'DateTime' node |
28
|
|
|
*/ |
29
|
|
|
protected $DateTime; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string 'Sign' node |
33
|
|
|
*/ |
34
|
|
|
protected $Sign; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string 'Operation' type |
38
|
|
|
*/ |
39
|
|
|
protected $Operation; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string 'ServiceId' node |
43
|
|
|
*/ |
44
|
|
|
protected $ServiceId; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var array list of possible operations |
48
|
|
|
*/ |
49
|
|
|
protected $operations = array('Check','Payment','Confirm','Cancel'); |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* General constructor |
53
|
|
|
* |
54
|
|
|
* @param \EasyPay\Provider31\Request\RAW $raw Raw request data |
55
|
|
|
*/ |
56
|
|
|
public function __construct($raw) |
57
|
|
|
{ |
58
|
|
|
$this->raw_request = $raw; |
|
|
|
|
59
|
|
|
|
60
|
|
|
$this->parse_request_data(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get DateTime |
65
|
|
|
* |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
public function DateTime() |
69
|
|
|
{ |
70
|
|
|
return $this->DateTime; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get Sign |
75
|
|
|
* |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
|
|
public function Sign() |
79
|
|
|
{ |
80
|
|
|
return $this->Sign; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get Operation type |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public function Operation() |
89
|
|
|
{ |
90
|
|
|
return $this->Operation; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get ServiceId |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
public function ServiceId() |
99
|
|
|
{ |
100
|
|
|
return $this->ServiceId; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Parse xml-request, which was previously "extracted" from the body of the http request |
105
|
|
|
* |
106
|
|
|
* @throws Exception\Structure |
107
|
|
|
*/ |
108
|
|
|
protected function parse_request_data() |
109
|
|
|
{ |
110
|
|
|
// process <Request> group |
111
|
|
|
$r = $this->raw_request->get_nodes_from_request('Request'); |
112
|
|
|
|
113
|
|
|
if (count($r) < 1) |
114
|
|
|
{ |
115
|
|
|
throw new Exception\Structure('The xml-query does not contain any element Request!', -52); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
foreach ($r[0]->childNodes as $child) |
119
|
|
|
{ |
120
|
|
|
$this->check_and_parse_request_node($child, 'DateTime'); |
121
|
|
|
$this->check_and_parse_request_node($child, 'Sign'); |
122
|
|
|
|
123
|
|
|
$this->check_and_parse_operation($child); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if ( ! isset($this->Operation)) |
127
|
|
|
{ |
128
|
|
|
throw new Exception\Structure('There is no Operation type element in the xml request!', -55); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
// process <Operation> group |
132
|
|
|
$r = $this->raw_request->get_nodes_from_request($this->Operation); |
133
|
|
|
|
134
|
|
|
foreach ($r[0]->childNodes as $child) |
135
|
|
|
{ |
136
|
|
|
$this->check_and_parse_request_node($child, 'ServiceId'); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Check if present node of request and then parse it |
142
|
|
|
* |
143
|
|
|
* @param \DOMNode $n |
144
|
|
|
* @param string $name |
145
|
|
|
*/ |
146
|
|
|
protected function check_and_parse_request_node($n, $name) |
147
|
|
|
{ |
148
|
|
|
if ($n->nodeName == $name) |
149
|
|
|
{ |
150
|
|
|
$this->parse_request_node($n, $name); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Check if given node is Operation |
156
|
|
|
* |
157
|
|
|
* @throws Exception\Structure |
158
|
|
|
*/ |
159
|
|
|
protected function check_and_parse_operation($n) |
160
|
|
|
{ |
161
|
|
|
if (in_array($n->nodeName, $this->operations)) |
162
|
|
|
{ |
163
|
|
|
if ( ! isset($this->Operation)) |
164
|
|
|
{ |
165
|
|
|
$this->Operation = $n->nodeName; |
166
|
|
|
} |
167
|
|
|
else |
168
|
|
|
{ |
169
|
|
|
throw new Exception\Structure('There is more than one Operation type element in the xml-query!', -53); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Parse node of request |
176
|
|
|
* |
177
|
|
|
* @param \DOMNode $n |
178
|
|
|
* @param string $name |
179
|
|
|
* |
180
|
|
|
* @throws Exception\Structure |
181
|
|
|
*/ |
182
|
|
|
protected function parse_request_node($n, $name) |
183
|
|
|
{ |
184
|
|
|
if ( ! isset($this->$name)) |
185
|
|
|
{ |
186
|
|
|
$this->$name = $n->nodeValue; |
187
|
|
|
} |
188
|
|
|
else |
189
|
|
|
{ |
190
|
|
|
throw new Exception\Structure('There is more than one '.$name.' element in the xml-query!', -56); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* "Rough" validation of the received xml request |
196
|
|
|
* |
197
|
|
|
* @param array $options |
198
|
|
|
* @throws Exception\Data |
199
|
|
|
* @throws Exception\Structure |
200
|
|
|
*/ |
201
|
|
|
public function validate_request($options) |
202
|
|
|
{ |
203
|
|
|
$this->validate_element('DateTime'); |
204
|
|
|
$this->validate_element('Sign'); |
205
|
|
|
$this->validate_element('ServiceId'); |
206
|
|
|
|
207
|
|
|
// compare received value ServiceId with option ServiceId |
208
|
|
|
if (intval($options['ServiceId']) != intval($this->ServiceId)) |
209
|
|
|
{ |
210
|
|
|
throw new Exception\Data('This request is not for our ServiceId!', -58); |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Validation of xml-element |
216
|
|
|
* |
217
|
|
|
* @param string $name |
218
|
|
|
*/ |
219
|
|
|
public function validate_element($name) |
220
|
|
|
{ |
221
|
|
|
if ( ! isset($this->$name)) |
222
|
|
|
{ |
223
|
|
|
throw new Exception\Structure('There is no '.$name.' element in the xml request!', -57); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Verify signature of request |
229
|
|
|
* |
230
|
|
|
* @param array $options |
231
|
|
|
*/ |
232
|
|
|
public function verify_sign($options) |
233
|
|
|
{ |
234
|
|
|
$sign = new Sign(); |
235
|
|
|
$sign->verify( |
236
|
|
|
$this->raw_request->str(), |
237
|
|
|
$this->Sign, |
238
|
|
|
$options |
239
|
|
|
); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
} |
243
|
|
|
|