1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Omnipay\Redsys\Message; |
4
|
|
|
|
5
|
|
|
use AvaiBookSports\Component\RedsysMessages\CatalogInterface; |
6
|
|
|
use AvaiBookSports\Component\RedsysMessages\Exception\CatalogNotFoundException; |
7
|
|
|
use AvaiBookSports\Component\RedsysMessages\Factory; |
8
|
|
|
use AvaiBookSports\Component\RedsysMessages\Loader\CatalogLoader; |
9
|
|
|
use Omnipay\Common\Exception\InvalidResponseException; |
10
|
|
|
use Omnipay\Common\Message\AbstractResponse; |
11
|
|
|
use Omnipay\Common\Message\RequestInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Redsys Complete Purchase Response. |
15
|
|
|
*/ |
16
|
|
|
class CompleteAuthorizeResponse extends AbstractResponse |
17
|
|
|
{ |
18
|
|
|
/** @var array */ |
19
|
|
|
protected $merchantParameters; |
20
|
|
|
|
21
|
|
|
/** @var string */ |
22
|
|
|
protected $returnSignature; |
23
|
|
|
|
24
|
|
|
/** @var bool */ |
25
|
|
|
protected $usingUpcaseParameters = false; |
26
|
|
|
|
27
|
|
|
/** @var bool */ |
28
|
|
|
protected $usingUpcaseResponse = false; |
29
|
|
|
|
30
|
|
|
/** @var CatalogInterface */ |
31
|
|
|
protected $redsysMessages; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Constructor. |
35
|
|
|
* |
36
|
|
|
* @param RequestInterface $request the initiating request |
37
|
|
|
* @param mixed $data |
38
|
|
|
* |
39
|
|
|
* @throws InvalidResponseException If merchant data or order number is missing, or signature does not match |
40
|
|
|
*/ |
41
|
|
|
public function __construct(RequestInterface $request, $data) |
42
|
|
|
{ |
43
|
|
|
parent::__construct($request, $data); |
44
|
|
|
|
45
|
|
|
$security = new Security(); |
46
|
|
|
|
47
|
|
|
try { |
48
|
|
|
$this->redsysMessages = (new Factory(new CatalogLoader()))->createCatalogByLanguage(array_key_exists('language', $this->request->getParameters()) ? $this->request->getParameters()['language'] : 'en'); |
49
|
|
|
} catch (CatalogNotFoundException $e) { |
50
|
|
|
$this->redsysMessages = (new Factory(new CatalogLoader()))->createCatalogByLanguage('en'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (!empty($data['Ds_MerchantParameters'])) { |
54
|
|
|
$this->merchantParameters = $security->decodeMerchantParameters($data['Ds_MerchantParameters']); |
55
|
|
|
} elseif (!empty($data['DS_MERCHANTPARAMETERS'])) { |
56
|
|
|
$this->merchantParameters = $security->decodeMerchantParameters($data['DS_MERCHANTPARAMETERS']); |
57
|
|
|
$this->usingUpcaseResponse = true; |
58
|
|
|
} else { |
59
|
|
|
throw new InvalidResponseException('Invalid response from payment gateway (no data)'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (!empty($this->merchantParameters['Ds_Order'])) { |
63
|
|
|
$order = $this->merchantParameters['Ds_Order']; |
64
|
|
|
} elseif (!empty($this->merchantParameters['DS_ORDER'])) { |
65
|
|
|
$order = $this->merchantParameters['DS_ORDER']; |
66
|
|
|
$this->usingUpcaseParameters = true; |
67
|
|
|
} else { |
68
|
|
|
throw new InvalidResponseException(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$this->returnSignature = $security->createReturnSignature( |
72
|
|
|
$data[$this->usingUpcaseResponse ? 'DS_MERCHANTPARAMETERS' : 'Ds_MerchantParameters'], |
73
|
|
|
$order, |
74
|
|
|
$this->request->getHmacKey() |
|
|
|
|
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
if ($this->returnSignature != $data[$this->usingUpcaseResponse ? 'DS_SIGNATURE' : 'Ds_Signature']) { |
78
|
|
|
throw new InvalidResponseException('Invalid response from payment gateway (signature mismatch)'); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Is the response successful? |
84
|
|
|
* |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
|
|
public function isSuccessful() |
88
|
|
|
{ |
89
|
|
|
$key = $this->usingUpcaseParameters ? 'DS_RESPONSE' : 'Ds_Response'; |
90
|
|
|
|
91
|
|
|
return isset($this->merchantParameters[$key]) |
92
|
|
|
&& is_numeric($this->merchantParameters[$key]) |
93
|
|
|
&& 0 <= $this->merchantParameters[$key] |
94
|
|
|
&& 100 > $this->merchantParameters[$key]; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Is the transaction cancelled by the user? |
99
|
|
|
* |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
|
|
public function isCancelled() |
103
|
|
|
{ |
104
|
|
|
return '9915' === $this->getCode(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get the response data, included the decoded merchant parameters if available. |
109
|
|
|
* |
110
|
|
|
* @return mixed |
111
|
|
|
*/ |
112
|
|
|
public function getData() |
113
|
|
|
{ |
114
|
|
|
$data = parent::getData(); |
115
|
|
|
|
116
|
|
|
return is_array($data) && is_array($this->merchantParameters) |
117
|
|
|
? array_merge($data, $this->merchantParameters) |
118
|
|
|
: $data; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Helper method to get a specific merchant parameter if available. |
123
|
|
|
* |
124
|
|
|
* @param string $key The key to look up |
125
|
|
|
* |
126
|
|
|
* @return mixed|null |
127
|
|
|
*/ |
128
|
|
|
protected function getKey($key) |
129
|
|
|
{ |
130
|
|
|
if ($this->usingUpcaseParameters) { |
131
|
|
|
$key = strtoupper($key); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return isset($this->merchantParameters[$key]) ? $this->merchantParameters[$key] : null; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get the authorisation code if available. |
139
|
|
|
* |
140
|
|
|
* @return string|null |
141
|
|
|
*/ |
142
|
|
|
public function getTransactionReference() |
143
|
|
|
{ |
144
|
|
|
return $this->getKey('Ds_AuthorisationCode'); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get the merchant message if available. |
149
|
|
|
* |
150
|
|
|
* @return string|null A response message from the payment gateway |
151
|
|
|
*/ |
152
|
|
|
public function getMessage() |
153
|
|
|
{ |
154
|
|
|
return $this->redsysMessages->getDsResponseMessage($this->getCode()); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Get the merchant response code if available. |
159
|
|
|
* |
160
|
|
|
* @return string|null |
161
|
|
|
*/ |
162
|
|
|
public function getCode() |
163
|
|
|
{ |
164
|
|
|
return $this->getKey('Ds_Response'); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Get the card type if available. |
169
|
|
|
* |
170
|
|
|
* @return string|null |
171
|
|
|
*/ |
172
|
|
|
public function getCardType() |
173
|
|
|
{ |
174
|
|
|
return $this->getKey('Ds_Card_Type'); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|