1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Omnipay\SmartPay\Message; |
4
|
|
|
|
5
|
|
|
use Omnipay\Common\Exception\InvalidResponseException; |
6
|
|
|
use Omnipay\Common\Http\Exception; |
7
|
|
|
use Omnipay\Common\Message\AbstractResponse; |
8
|
|
|
use Omnipay\Common\Message\RequestInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Bank Muscat Smart Pay Complete Purchase Response |
12
|
|
|
*/ |
13
|
|
|
class CompletePurchaseResponse extends AbstractResponse |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Constructor |
17
|
|
|
* |
18
|
|
|
* @param RequestInterface $request the initiating request. |
19
|
|
|
* @param mixed $data |
20
|
|
|
* |
21
|
|
|
* @throws InvalidResponseException If merchant data or order number is missing, or signature does not match |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
protected $rawData; |
25
|
|
|
|
26
|
|
|
public function __construct(RequestInterface $request, $data) |
27
|
|
|
{ |
28
|
|
|
parent::__construct($request, $data); |
29
|
|
|
|
30
|
|
|
foreach (['encResp', 'orderNo'] as $key) { |
31
|
|
|
if (empty($data[$key])) { |
32
|
|
|
throw new InvalidResponseException('Invalid response from payment gateway ['.$key.'] missing'); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
try { |
37
|
|
|
$crypto = new Crypto(); |
38
|
|
|
|
39
|
|
|
$decryptedString = $crypto->decrypt($this->data['encResp'], $this->getRequest()->getWorkingKey()); |
|
|
|
|
40
|
|
|
} catch (Exception $exception) { |
41
|
|
|
throw new InvalidResponseException('Invalid response from payment gateway'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if ( empty($decryptedString) ) { |
45
|
|
|
throw new InvalidResponseException('Invalid response from payment gateway'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$orderData = []; |
49
|
|
|
|
50
|
|
|
foreach (explode('&', $decryptedString) as $item) { |
51
|
|
|
[$key, $value] = explode('=', $item); |
52
|
|
|
$orderData[$key] = $value; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
foreach (['order_id', 'tracking_id', 'status_message', 'order_status'] as $key) { |
56
|
|
|
if (empty($orderData[$key])) { |
57
|
|
|
throw new InvalidResponseException('Invalid response from payment gateway ['.$key.'] missing'); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$this->rawData = $data; |
62
|
|
|
$this->data = $orderData; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Is the response successful? |
67
|
|
|
* |
68
|
|
|
* @return boolean |
69
|
|
|
*/ |
70
|
|
|
public function isSuccessful() |
71
|
|
|
{ |
72
|
|
|
return $this->getOrderStatus() === 'Success'; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get the order status if available. |
77
|
|
|
* |
78
|
|
|
* @return null|string |
79
|
|
|
*/ |
80
|
|
|
public function getOrderStatus() |
81
|
|
|
{ |
82
|
|
|
return $this->getKey('order_status'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
public function getMessage() |
87
|
|
|
{ |
88
|
|
|
return $this->getKey('status_message'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the transaction identifier if available. |
93
|
|
|
* |
94
|
|
|
* @return null|string |
95
|
|
|
*/ |
96
|
|
|
public function getTransactionReference() |
97
|
|
|
{ |
98
|
|
|
return $this->getKey('tracking_id'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get the merchant-supplied transaction identifier if available. |
103
|
|
|
* |
104
|
|
|
* @return null|string |
105
|
|
|
*/ |
106
|
|
|
public function getTransactionId() |
107
|
|
|
{ |
108
|
|
|
return $this->getKey('order_id'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get the card number if available. |
113
|
|
|
* |
114
|
|
|
* @return null|string |
115
|
|
|
*/ |
116
|
|
|
public function getCardNumber() |
117
|
|
|
{ |
118
|
|
|
return $this->getKey('merchant_param6'); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get the card type if available. |
123
|
|
|
* |
124
|
|
|
* @return null|string |
125
|
|
|
*/ |
126
|
|
|
public function getCardType() |
127
|
|
|
{ |
128
|
|
|
return $this->getKey('card_type') ?? $this->getKey('card_name'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Get the card expiry if available. |
133
|
|
|
* |
134
|
|
|
* @return null|string |
135
|
|
|
*/ |
136
|
|
|
public function getCardExpiry() |
137
|
|
|
{ |
138
|
|
|
return $this->getKey('merchant_param7'); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Helper method to get a specific response parameter if available. |
144
|
|
|
* |
145
|
|
|
* @param string $key The key to look up |
146
|
|
|
* |
147
|
|
|
* @return null|mixed |
148
|
|
|
*/ |
149
|
|
|
protected function getKey($key) |
150
|
|
|
{ |
151
|
|
|
return $this->data[$key] ?? null; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|