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