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