1
|
|
|
<?php |
2
|
|
|
namespace Tzsk\Payu; |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
use Tzsk\Payu\Model\PayuPayment; |
6
|
|
|
|
7
|
|
|
class PaymentVerification |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Command to Pass to the PayuService. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $command = 'verify_payment'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Merchant Key. |
18
|
|
|
* |
19
|
|
|
* @var string|null |
20
|
|
|
*/ |
21
|
|
|
protected $key = null; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Merchant Salt. |
25
|
|
|
* |
26
|
|
|
* @var string|null |
27
|
|
|
*/ |
28
|
|
|
protected $salt = null; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Payu Service URL. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $url = null; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Transaction ID. |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $txn_id = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Original Response. |
46
|
|
|
* |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
protected $response = []; |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* PaymentVerification constructor. |
54
|
|
|
* |
55
|
|
|
* @param $txn_id |
56
|
|
|
*/ |
57
|
|
|
public function __construct($txn_id) |
58
|
|
|
{ |
59
|
|
|
$this->txn_id = $txn_id; |
60
|
|
|
$this->key = config('payu.key'); |
61
|
|
|
$this->salt = config('payu.salt'); |
62
|
|
|
|
63
|
|
|
$env = config('payu.env'); |
64
|
|
|
if ($env != 'test') { |
65
|
|
|
$env = 'info'; |
66
|
|
|
} |
67
|
|
|
$this->url = "https://{$env}.payu.in/merchant/postservice?form=2"; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Request for Verification Status. |
72
|
|
|
* |
73
|
|
|
* @return $this |
74
|
|
|
*/ |
75
|
|
|
public function request() |
76
|
|
|
{ |
77
|
|
|
$this->sendRequest(); |
78
|
|
|
|
79
|
|
|
if (config('payu.driver') != 'database') { |
80
|
|
|
$this->updatePayuTransaction(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Simple data accessor. |
88
|
|
|
* |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
public function simple() |
92
|
|
|
{ |
93
|
|
|
return $this->getResponse(true); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Full Data Accessor. |
98
|
|
|
* |
99
|
|
|
* @return array |
100
|
|
|
*/ |
101
|
|
|
public function full() |
102
|
|
|
{ |
103
|
|
|
return $this->getResponse(false); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get Response according to Simple or Full. |
108
|
|
|
* |
109
|
|
|
* @param boolean $simple |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
|
|
protected function getResponse($simple) |
113
|
|
|
{ |
114
|
|
|
if (empty($this->response['status'])) { |
115
|
|
|
return ['status' => false, 'message' => $this->response['msg']]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if ($simple) { |
119
|
|
|
return $this->getSimpleResponseData(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $this->getFullResponseData(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get the Request Params for Verification. |
127
|
|
|
* |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
public function getVerificationPostFields() |
131
|
|
|
{ |
132
|
|
|
$txn_string = implode("|", $this->txn_id); |
133
|
|
|
$hash_str = $this->key.'|'.$this->command.'|'.$txn_string.'|'.$this->salt; |
134
|
|
|
$hash = strtolower(hash('sha512', $hash_str)); |
135
|
|
|
$params = ['key' => $this->key, 'hash' => $hash, 'var1' => $txn_string, 'command' => $this->command]; |
136
|
|
|
|
137
|
|
|
return http_build_query($params); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Set the Verification Response. |
142
|
|
|
* |
143
|
|
|
* @throws \Exception |
144
|
|
|
*/ |
145
|
|
|
protected function sendRequest() |
146
|
|
|
{ |
147
|
|
|
$post_fields = $this->getVerificationPostFields(); |
148
|
|
|
|
149
|
|
|
$curl = curl_init(); |
150
|
|
|
curl_setopt($curl, CURLOPT_URL, $this->url); |
151
|
|
|
curl_setopt($curl, CURLOPT_POST, 1); |
152
|
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_fields); |
153
|
|
|
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30); |
154
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); |
155
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); |
156
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); |
157
|
|
|
$output = curl_exec($curl); |
158
|
|
|
|
159
|
|
|
if (curl_errno($curl)) { |
160
|
|
|
throw new \Exception(curl_error($curl)); |
161
|
|
|
} |
162
|
|
|
curl_close($curl); |
163
|
|
|
|
164
|
|
|
$this->response = json_decode($output, true); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Update Payu Payments Table Entries. |
169
|
|
|
* |
170
|
|
|
* @return boolean |
171
|
|
|
*/ |
172
|
|
|
protected function updatePayuTransaction() |
173
|
|
|
{ |
174
|
|
|
foreach ($this->txn_id as $item) { |
175
|
|
|
$payu_payment = PayuPayment::where('txnid', $item)->first(); |
176
|
|
|
if ($payu_payment) { |
177
|
|
|
$payu_payment->fill($this->getPayuPaymentAttributes($item))->save(); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return true; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Get Full Response for user. |
186
|
|
|
* |
187
|
|
|
* @return array |
188
|
|
|
*/ |
189
|
|
|
protected function getFullResponseData() |
190
|
|
|
{ |
191
|
|
|
$data = ['status' => true, 'data' => []]; |
192
|
|
|
foreach ($this->txn_id as $item) { |
193
|
|
|
$status = ($this->response['transaction_details'][$item]['status'] == 'success') ? |
194
|
|
|
ProcessPayment::STATUS_COMPLETED : ProcessPayment::STATUS_FAILED; |
195
|
|
|
|
196
|
|
|
$message = $this->getResponseMessage($item); |
197
|
|
|
|
198
|
|
|
$data['data'][$item] = ['status' => $status, 'message' => $message, |
199
|
|
|
'response' => $this->response['transaction_details'][$item]]; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
return $data; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Get Simple Response for user. |
207
|
|
|
* |
208
|
|
|
* @return array |
209
|
|
|
*/ |
210
|
|
|
protected function getSimpleResponseData() |
211
|
|
|
{ |
212
|
|
|
$data = ['status' => true, 'data' => []]; |
213
|
|
|
foreach ($this->txn_id as $item) { |
214
|
|
|
$status = ($this->response['transaction_details'][$item]['status'] == 'success') ? true : false; |
215
|
|
|
|
216
|
|
|
$message = $this->getResponseMessage($item); |
217
|
|
|
|
218
|
|
|
$data['data'][$item] = ['status' => $status, 'message' => $message]; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return $data; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Get Response Message. |
226
|
|
|
* |
227
|
|
|
* @param $item |
228
|
|
|
* @return string |
229
|
|
|
*/ |
230
|
|
|
protected function getResponseMessage($item) |
231
|
|
|
{ |
232
|
|
|
$message = @$this->response['transaction_details'][$item]['error_Message']; |
233
|
|
|
if (count($this->response['transaction_details'][$item]) < 3) { |
234
|
|
|
$message = 'Transaction ID not found.'; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
return $message; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @param $item |
242
|
|
|
* @return mixed |
243
|
|
|
*/ |
244
|
|
|
protected function getPayuPaymentAttributes($item) |
245
|
|
|
{ |
246
|
|
|
$attributes = $this->response['transaction_details'][$item]; |
247
|
|
|
$attributes['status'] = ($this->response['transaction_details'][$item]['status'] == 'success') ? |
248
|
|
|
ProcessPayment::STATUS_COMPLETED : ProcessPayment::STATUS_FAILED; |
249
|
|
|
|
250
|
|
|
return $attributes; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
} |