1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Abstract class for the family of response classes |
5
|
|
|
* |
6
|
|
|
* @package php_EasyPay |
7
|
|
|
* @version 1.1 |
8
|
|
|
* @author Dmitry Shovchko <[email protected]> |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace EasyPay\Provider31; |
13
|
|
|
|
14
|
|
|
use EasyPay\Log as Log; |
15
|
|
|
use EasyPay\Key as Key; |
16
|
|
|
|
17
|
|
|
abstract class Response extends \DomDocument |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
|
|
const TEMPLATE = '<Response><StatusCode></StatusCode><StatusDetail></StatusDetail><DateTime></DateTime></Response>'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var \DOMNode |
27
|
|
|
*/ |
28
|
|
|
protected $Response; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var \DOMElement |
32
|
|
|
*/ |
33
|
|
|
protected $Sign; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Response constructor |
37
|
|
|
* |
38
|
|
|
*/ |
39
|
|
|
public function __construct() |
40
|
|
|
{ |
41
|
|
|
parent::__construct('1.0', 'UTF-8'); |
42
|
|
|
|
43
|
|
|
self::loadXML(self::TEMPLATE); |
|
|
|
|
44
|
|
|
|
45
|
|
|
$this->Response = $this->firstChild; |
46
|
|
|
$this->set_DateTime(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Set DateTime element value by current time |
51
|
|
|
*/ |
52
|
|
|
public function set_DateTime() |
53
|
|
|
{ |
54
|
|
|
$this->setElementValue('DateTime', date('Y-m-d\TH:i:s', time())); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Create new element node |
59
|
|
|
* |
60
|
|
|
* @param string $name |
61
|
|
|
* @param string $value (optional) |
62
|
|
|
*/ |
63
|
|
|
public function createElement($name, $value=NULL) |
64
|
|
|
{ |
65
|
|
|
return parent::createElement($name, $value); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Set node value |
70
|
|
|
* |
71
|
|
|
* @param string $name |
72
|
|
|
* @param string $value |
73
|
|
|
*/ |
74
|
|
|
public function setElementValue($name, $value) |
75
|
|
|
{ |
76
|
|
|
foreach ($this->Response->childNodes as $child) |
77
|
|
|
{ |
78
|
|
|
if ($child->nodeName == $name) |
79
|
|
|
{ |
80
|
|
|
$child->nodeValue = $value; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Dumps response into a string |
87
|
|
|
* |
88
|
|
|
* @return string XML |
89
|
|
|
*/ |
90
|
|
|
public function friendly() |
91
|
|
|
{ |
92
|
|
|
$this->encoding = 'UTF-8'; |
93
|
|
|
$this->formatOutput = true; |
94
|
|
|
//$this->save('/tmp/test1.xml'); |
95
|
|
|
|
96
|
|
|
return $this->saveXML(NULL, LIBXML_NOEMPTYTAG); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Send response |
101
|
|
|
* |
102
|
|
|
* @param array $options |
103
|
|
|
*/ |
104
|
|
|
public function out($options) |
105
|
|
|
{ |
106
|
|
|
$this->sign($options); |
107
|
|
|
|
108
|
|
|
Log::instance()->debug('response sends: '); |
109
|
|
|
Log::instance()->debug($this->friendly()); |
110
|
|
|
|
111
|
|
|
ob_clean(); |
112
|
|
|
header("Content-Type: text/xml; charset=utf-8"); |
113
|
|
|
echo $this->friendly(); |
114
|
|
|
exit; |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Add Sign (if hasn't yet done) |
119
|
|
|
* |
120
|
|
|
* @param array $options |
121
|
|
|
*/ |
122
|
|
|
protected function sign($options) |
123
|
|
|
{ |
124
|
|
|
if (isset($this->Sign)) return; |
125
|
|
|
|
126
|
|
|
if (isset($options['UseSign']) && ($options['UseSign'] === true)) |
127
|
|
|
{ |
128
|
|
|
$this->Sign = self::createElement('Sign'); |
|
|
|
|
129
|
|
|
$this->Response->appendChild($this->Sign); |
130
|
|
|
|
131
|
|
|
$sign = $this->generate_sign($options); |
132
|
|
|
|
133
|
|
|
$this->Sign->nodeValue = $sign; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Generate signature of response |
139
|
|
|
* |
140
|
|
|
* @param array $options |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
public function generate_sign($options) |
144
|
|
|
{ |
145
|
|
|
if ( ! isset($options['ProviderPKey'])) |
146
|
|
|
{ |
147
|
|
|
Log::instance()->error('The parameter ProviderPKey is not set!'); |
148
|
|
|
return null; |
149
|
|
|
} |
150
|
|
|
try |
151
|
|
|
{ |
152
|
|
|
$pkeyid = (new Key())->get($options['ProviderPKey'], 'private'); |
153
|
|
|
} |
154
|
|
|
catch (\Exception $e) |
155
|
|
|
{ |
156
|
|
|
return null; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$pr_key = openssl_pkey_get_private($pkeyid); |
160
|
|
|
if ($pr_key === FALSE) |
161
|
|
|
{ |
162
|
|
|
Log::instance()->error('Can not extract the private key from certificate!'); |
163
|
|
|
return null; |
164
|
|
|
} |
165
|
|
|
if (openssl_sign($this->friendly(), $sign, $pr_key) === FALSE) |
166
|
|
|
{ |
167
|
|
|
Log::instance()->error('Can not generate signature!'); |
168
|
|
|
return null; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return strtoupper(bin2hex($sign)); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|