|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lexik\Bundle\PayboxBundle\Paybox\System\Base; |
|
4
|
|
|
|
|
5
|
|
|
use Lexik\Bundle\PayboxBundle\Event\PayboxEvents; |
|
6
|
|
|
use Lexik\Bundle\PayboxBundle\Event\PayboxResponseEvent; |
|
7
|
|
|
use Lexik\Bundle\PayboxBundle\Paybox\System\Tools; |
|
8
|
|
|
use Psr\Log\LoggerInterface; |
|
9
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\Request as HttpRequest; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class Response |
|
16
|
|
|
* |
|
17
|
|
|
* @package Lexik\Bundle\PayboxBundle\Paybox\System\Base |
|
18
|
|
|
* |
|
19
|
|
|
* @author Lexik <[email protected]> |
|
20
|
|
|
* @author Olivier Maisonneuve <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
class Response |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var HttpRequest |
|
26
|
|
|
*/ |
|
27
|
|
|
private $request; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var LoggerInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $logger; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var EventDispatcherInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
private $dispatcher; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var array |
|
41
|
|
|
*/ |
|
42
|
|
|
private $data; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
private $signature; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var array |
|
51
|
|
|
*/ |
|
52
|
|
|
private $parameters; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Contructor. |
|
56
|
|
|
* |
|
57
|
|
|
* @param RequestStack $requestStack |
|
58
|
|
|
* @param LoggerInterface $logger |
|
59
|
|
|
* @param EventDispatcherInterface $dispatcher |
|
60
|
|
|
* @param array $parameters |
|
61
|
|
|
*/ |
|
62
|
|
|
public function __construct(RequestStack $requestStack, LoggerInterface $logger, EventDispatcherInterface $dispatcher, array $parameters) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->request = $requestStack->getCurrentRequest(); |
|
65
|
|
|
$this->logger = $logger; |
|
66
|
|
|
$this->dispatcher = $dispatcher; |
|
67
|
|
|
$this->parameters = $parameters; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Returns the GET or POST parameters form the request. |
|
72
|
|
|
* |
|
73
|
|
|
* @return ParameterBag |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function getRequestParameters() |
|
76
|
|
|
{ |
|
77
|
|
|
if ($this->request->isMethod('POST')) { |
|
78
|
|
|
$parameters = $this->request->request; |
|
79
|
|
|
} else { |
|
80
|
|
|
$parameters = $this->request->query; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $parameters; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Gets the signature set in the http request. |
|
88
|
|
|
* |
|
89
|
|
|
* Paybox documentation says : |
|
90
|
|
|
* The Paybox signature is created by encrypting a SHA-1 hash with the private Paybox RSA key. The size |
|
91
|
|
|
* of a SHA-1 hash is 160 bits and the size of the Paybox key is 1024 bits. The signature is always a binary |
|
92
|
|
|
* value of fixed 128 bytes size (172 bytes in Base64 encoding). |
|
93
|
|
|
* |
|
94
|
|
|
* But sometimes, base64 encoded signature are also url encoded. |
|
95
|
|
|
*/ |
|
96
|
|
|
protected function initSignature() |
|
97
|
|
|
{ |
|
98
|
|
|
if (!$this->getRequestParameters()->has($this->parameters['hmac']['signature_name'])) { |
|
99
|
|
|
$this->logger->error('Payment signature not set.'); |
|
100
|
|
|
|
|
101
|
|
|
return false; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$signature = $this->getRequestParameters()->get($this->parameters['hmac']['signature_name']); |
|
105
|
|
|
$signatureLength = strlen($signature); |
|
106
|
|
|
|
|
107
|
|
|
if ($signatureLength > 172) { |
|
108
|
|
|
$this->signature = base64_decode(urldecode($signature)); |
|
109
|
|
|
|
|
110
|
|
|
return true; |
|
111
|
|
|
} elseif ($signatureLength == 172) { |
|
112
|
|
|
$this->signature = base64_decode($signature); |
|
113
|
|
|
|
|
114
|
|
|
return true; |
|
115
|
|
|
} elseif ($signatureLength == 128) { |
|
116
|
|
|
$this->signature = $signature; |
|
117
|
|
|
|
|
118
|
|
|
return true; |
|
119
|
|
|
} else { |
|
120
|
|
|
$this->signature = null; |
|
121
|
|
|
$this->logger->error('Bad signature format.'); |
|
122
|
|
|
|
|
123
|
|
|
return false; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Concatenates all parameters set in the http request. |
|
129
|
|
|
*/ |
|
130
|
|
|
protected function initData() |
|
131
|
|
|
{ |
|
132
|
|
|
foreach ($this->getRequestParameters() as $key => $value) { |
|
133
|
|
|
$this->logger->info(sprintf('%s=%s', $key, $value)); |
|
134
|
|
|
|
|
135
|
|
|
if ($this->parameters['hmac']['signature_name'] !== $key) { |
|
136
|
|
|
$this->data[$key] = urlencode($value); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Verifies the validity of the signature. |
|
143
|
|
|
* |
|
144
|
|
|
* @return bool |
|
145
|
|
|
*/ |
|
146
|
|
|
public function verifySignature() |
|
147
|
|
|
{ |
|
148
|
|
|
$this->logger->info('New IPN call.'); |
|
149
|
|
|
|
|
150
|
|
|
$this->initData(); |
|
151
|
|
|
$this->initSignature(); |
|
152
|
|
|
|
|
153
|
|
|
$file = fopen($this->parameters['public_key'], 'r'); |
|
154
|
|
|
$cert = fread($file, 1024); |
|
155
|
|
|
fclose($file); |
|
156
|
|
|
|
|
157
|
|
|
$publicKey = openssl_pkey_get_public($cert); |
|
158
|
|
|
|
|
159
|
|
|
$result = openssl_verify( |
|
160
|
|
|
Tools::stringify($this->data), |
|
161
|
|
|
$this->signature, |
|
162
|
|
|
$publicKey, |
|
163
|
|
|
'sha1WithRSAEncryption' |
|
164
|
|
|
); |
|
165
|
|
|
|
|
166
|
|
|
$this->logger->info(Tools::stringify($this->data)); |
|
167
|
|
|
$this->logger->info(base64_encode($this->signature)); |
|
168
|
|
|
|
|
169
|
|
|
if ($result == 1) { |
|
170
|
|
|
$this->logger->info('Signature is valid.'); |
|
171
|
|
|
} elseif ($result == 0) { |
|
172
|
|
|
$this->logger->error('Signature is invalid.'); |
|
173
|
|
|
} else { |
|
174
|
|
|
$this->logger->error('Error while verifying Signature.'); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
$result = (1 == $result); |
|
178
|
|
|
|
|
179
|
|
|
openssl_free_key($publicKey); |
|
180
|
|
|
|
|
181
|
|
|
$event = new PayboxResponseEvent($this->data, $result); |
|
182
|
|
|
$this->dispatcher->dispatch(PayboxEvents::PAYBOX_IPN_RESPONSE, $event); |
|
183
|
|
|
|
|
184
|
|
|
return $result; |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|