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