1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Omnipay\Redsys\Message; |
6
|
|
|
|
7
|
|
|
use AvaiBookSports\Component\RedsysMessages\Exception\CatalogNotFoundException; |
8
|
|
|
use AvaiBookSports\Component\RedsysMessages\Factory; |
9
|
|
|
use AvaiBookSports\Component\RedsysMessages\Loader\CatalogLoader; |
10
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
11
|
|
|
use Omnipay\Common\Exception\InvalidResponseException; |
12
|
|
|
use Omnipay\Common\Http\Exception\RequestException; |
13
|
|
|
use Omnipay\Common\Message\AbstractResponse; |
14
|
|
|
use Omnipay\Common\Message\RequestInterface; |
15
|
|
|
|
16
|
|
|
class CaptureResponse extends AbstractResponse |
17
|
|
|
{ |
18
|
|
|
protected $returnSignature; |
19
|
|
|
|
20
|
|
|
/** @var bool */ |
21
|
|
|
protected $usingUpcaseResponse = false; |
22
|
|
|
|
23
|
|
|
/** @var CatalogInterface */ |
|
|
|
|
24
|
|
|
protected $redsysMessages; |
25
|
|
|
|
26
|
|
|
public function __construct(RequestInterface $request, $data) |
27
|
|
|
{ |
28
|
|
|
parent::__construct($request, $data); |
29
|
|
|
|
30
|
|
|
$security = new Security(); |
31
|
|
|
|
32
|
|
|
try { |
33
|
|
|
$this->redsysMessages = (new Factory(new CatalogLoader()))->createCatalogByLanguage(array_key_exists('language', $this->request->getParameters()) ? $this->request->getParameters()['language'] : 'en'); |
|
|
|
|
34
|
|
|
} catch (CatalogNotFoundException $e) { |
35
|
|
|
$this->redsysMessages = (new Factory(new CatalogLoader()))->createCatalogByLanguage('en'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if (!isset($data['CODIGO'])) { |
39
|
|
|
throw new InvalidResponseException('Invalid response from payment gateway (no data)'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (!isset($data['OPERACION'])) { |
43
|
|
|
if ('0' == $data['CODIGO']) { |
44
|
|
|
throw new InvalidResponseException('Invalid response from payment gateway (no data)'); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// Exceeder API rate limit |
49
|
|
|
if ('SIS0295' == $data['CODIGO'] || '9295' == $data['CODIGO']) { |
50
|
|
|
throw new RequestException('Too many requests. "'.$data['CODIGO'].'"', MessageFactoryDiscovery::find()->createRequest('POST', $this->getRequest()->getEndpoint(), ['SOAPAction' => 'trataPeticion'])); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (isset($data['OPERACION']['DS_ORDER'])) { |
54
|
|
|
$this->usingUpcaseResponse = true; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
|
59
|
|
|
if (!empty($data['OPERACION'])) { |
60
|
|
|
if (!empty($data['OPERACION']['Ds_CardNumber'])) { |
61
|
|
|
$signature_keys = [ |
62
|
|
|
'Ds_Amount', |
63
|
|
|
'Ds_Order', |
64
|
|
|
'Ds_MerchantCode', |
65
|
|
|
'Ds_Currency', |
66
|
|
|
'Ds_Response', |
67
|
|
|
'Ds_CardNumber', |
68
|
|
|
'Ds_TransactionType', |
69
|
|
|
'Ds_SecurePayment', |
70
|
|
|
]; |
71
|
|
|
} else { |
72
|
|
|
$signature_keys = [ |
73
|
|
|
'Ds_Amount', |
74
|
|
|
'Ds_Order', |
75
|
|
|
'Ds_MerchantCode', |
76
|
|
|
'Ds_Currency', |
77
|
|
|
'Ds_Response', |
78
|
|
|
'Ds_TransactionType', |
79
|
|
|
'Ds_SecurePayment', |
80
|
|
|
]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$signature_data = ''; |
84
|
|
|
foreach ($signature_keys as $key) { |
85
|
|
|
$value = $this->getKey($key); |
86
|
|
|
if (null === $value) { |
87
|
|
|
throw new InvalidResponseException('Invalid response from payment gateway (missing data)'); |
88
|
|
|
} |
89
|
|
|
$signature_data .= $value; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$this->returnSignature = $security->createSignature( |
93
|
|
|
$signature_data, |
94
|
|
|
$this->getKey('Ds_Order'), |
95
|
|
|
$this->request->getHmacKey() |
|
|
|
|
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
if ($this->returnSignature != $this->getKey('Ds_Signature')) { |
99
|
|
|
throw new InvalidResponseException('Invalid response from payment gateway (signature mismatch)'); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
|
public function isSuccessful() |
108
|
|
|
{ |
109
|
|
|
$response_code = $this->getKey('Ds_Response'); |
110
|
|
|
|
111
|
|
|
if (isset($this->data['CODIGO']) && 'SIS0060' === $this->data['CODIGO']) { |
112
|
|
|
return true; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
// check for field existence as well as value |
116
|
|
|
return isset($this->data['CODIGO']) |
117
|
|
|
&& '0' == $this->data['CODIGO'] |
118
|
|
|
&& null !== $response_code |
119
|
|
|
&& is_numeric($response_code) |
120
|
|
|
&& 900 == $response_code; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Helper method to get a specific response parameter if available. |
125
|
|
|
* |
126
|
|
|
* @param string $key The key to look up |
127
|
|
|
* |
128
|
|
|
* @return mixed|null |
129
|
|
|
*/ |
130
|
|
|
protected function getKey($key) |
131
|
|
|
{ |
132
|
|
|
if ($this->usingUpcaseResponse) { |
133
|
|
|
$key = strtoupper($key); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return isset($this->data['OPERACION'][$key]) ? $this->data['OPERACION'][$key] : null; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get the authorisation code if available. |
141
|
|
|
* |
142
|
|
|
* @return string|null |
143
|
|
|
*/ |
144
|
|
|
public function getTransactionReference() |
145
|
|
|
{ |
146
|
|
|
return $this->getKey('Ds_AuthorisationCode') ?? $this->request->getParameters()['transactionId']; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get the merchant message if available. |
151
|
|
|
* |
152
|
|
|
* @return string|null A response message from the payment gateway |
153
|
|
|
*/ |
154
|
|
|
public function getMessage() |
155
|
|
|
{ |
156
|
|
|
$message = $this->redsysMessages->getDsResponseMessage($this->getCode()); |
157
|
|
|
|
158
|
|
|
if (null === $message) { |
159
|
|
|
$message = $this->redsysMessages->getErrorMessage($this->getCode()); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return $message; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Get the merchant response code if available. |
167
|
|
|
* |
168
|
|
|
* @return string|null |
169
|
|
|
*/ |
170
|
|
|
public function getCode() |
171
|
|
|
{ |
172
|
|
|
$code = $this->getKey('Ds_Response'); |
173
|
|
|
|
174
|
|
|
if (null === $code) { |
175
|
|
|
$code = $this->data['CODIGO']; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $code; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Get the merchant data if available. |
183
|
|
|
* |
184
|
|
|
* @return string|null |
185
|
|
|
*/ |
186
|
|
|
public function getMerchantData() |
187
|
|
|
{ |
188
|
|
|
return $this->getKey('Ds_MerchantData'); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Get the card country if available. |
193
|
|
|
* |
194
|
|
|
* @return string|null ISO 3166-1 (3-digit numeric) format, if supplied |
195
|
|
|
*/ |
196
|
|
|
public function getCardCountry() |
197
|
|
|
{ |
198
|
|
|
return $this->getKey('Ds_Card_Country'); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths