|
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
|
|
|
* Original Response. |
|
39
|
|
|
* |
|
40
|
|
|
* @var array |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $response = []; |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* PaymentVerification constructor. |
|
47
|
|
|
* |
|
48
|
|
|
* @param $txn_id |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct($txn_id) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->txn_id = $txn_id; |
|
|
|
|
|
|
53
|
|
|
$this->key = config('payu.key'); |
|
54
|
|
|
$this->salt = config('payu.salt'); |
|
55
|
|
|
|
|
56
|
|
|
$env = config('payu.env'); |
|
57
|
|
|
if ($env != 'test') { |
|
58
|
|
|
$env = 'info'; |
|
59
|
|
|
} |
|
60
|
|
|
$this->url = "https://{$env}.payu.in/merchant/postservice?form=2"; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Request for Verification Status. |
|
65
|
|
|
* |
|
66
|
|
|
* @return $this |
|
67
|
|
|
*/ |
|
68
|
|
|
public function request() |
|
69
|
|
|
{ |
|
70
|
|
|
$this->sendRequest(); |
|
71
|
|
|
|
|
72
|
|
|
$this->updatePayuTransaction(); |
|
73
|
|
|
|
|
74
|
|
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Simple data accessor. |
|
79
|
|
|
* |
|
80
|
|
|
* @return array |
|
81
|
|
|
*/ |
|
82
|
|
|
public function simple() |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->getSimpleResponseData(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Full Data Accessor. |
|
89
|
|
|
* |
|
90
|
|
|
* @return array |
|
91
|
|
|
*/ |
|
92
|
|
|
public function full() |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->getFullResponseData(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Get the Request Params for Verification. |
|
99
|
|
|
* |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getVerificationPostFields() |
|
103
|
|
|
{ |
|
104
|
|
|
$txn_string = implode("|", $this->txn_id); |
|
105
|
|
|
$hash_str = $this->key . '|' . $this->command . '|' . $txn_string . '|' . $this->salt ; |
|
106
|
|
|
$hash = strtolower(hash('sha512', $hash_str)); |
|
107
|
|
|
$params = ['key' => $this->key , 'hash' => $hash , 'var1' => $txn_string, 'command' => $this->command]; |
|
108
|
|
|
|
|
109
|
|
|
return http_build_query($params); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Set the Verification Response. |
|
114
|
|
|
* |
|
115
|
|
|
* @throws \Exception |
|
116
|
|
|
*/ |
|
117
|
|
|
protected function sendRequest() |
|
118
|
|
|
{ |
|
119
|
|
|
$post_fields = $this->getVerificationPostFields(); |
|
120
|
|
|
|
|
121
|
|
|
$curl = curl_init(); |
|
122
|
|
|
curl_setopt($curl, CURLOPT_URL, $this->url); |
|
123
|
|
|
curl_setopt($curl, CURLOPT_POST, 1); |
|
124
|
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_fields); |
|
125
|
|
|
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30); |
|
126
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); |
|
127
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); |
|
128
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); |
|
129
|
|
|
$output = curl_exec($curl); |
|
130
|
|
|
|
|
131
|
|
|
if(curl_errno($curl)) { |
|
132
|
|
|
throw new \Exception(curl_error($curl)); |
|
133
|
|
|
} |
|
134
|
|
|
curl_close($curl); |
|
135
|
|
|
|
|
136
|
|
|
$this->response = json_decode($output, 1); |
|
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Update Payu Payments Table Entries. |
|
141
|
|
|
* |
|
142
|
|
|
* @return boolean |
|
143
|
|
|
*/ |
|
144
|
|
|
protected function updatePayuTransaction() |
|
145
|
|
|
{ |
|
146
|
|
|
if (config('payu.driver') != 'database') { |
|
147
|
|
|
return false; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
foreach ($this->txn_id as $item) { |
|
151
|
|
|
$payu_payment = PayuPayment::where('txnid', $item)->first(); |
|
152
|
|
|
if ($payu_payment) { |
|
153
|
|
|
$attributes = $this->response['transaction_details'][$item]; |
|
154
|
|
|
$attributes['status'] = ($this->response['transaction_details'][$item]['status'] == 'success') ? |
|
155
|
|
|
ProcessPayment::STATUS_COMPLETED : ProcessPayment::STATUS_FAILED; |
|
156
|
|
|
|
|
157
|
|
|
$payu_payment->fill($attributes)->save(); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
return true; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Get Full Response for user. |
|
166
|
|
|
* |
|
167
|
|
|
* @return array |
|
168
|
|
|
*/ |
|
169
|
|
|
protected function getFullResponseData() |
|
170
|
|
|
{ |
|
171
|
|
View Code Duplication |
if (empty($this->response['status'])) { |
|
|
|
|
|
|
172
|
|
|
return $this->full_response = ['status' => false, 'message' => $this->response['msg']]; |
|
|
|
|
|
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
$data = ['status' => true, 'data' => []]; |
|
176
|
|
|
foreach ($this->txn_id as $item) { |
|
177
|
|
|
$status = ($this->response['transaction_details'][$item]['status'] == 'success') ? |
|
178
|
|
|
ProcessPayment::STATUS_COMPLETED : ProcessPayment::STATUS_FAILED; |
|
179
|
|
|
|
|
180
|
|
|
$message = @$this->response['transaction_details'][$item]['error_Message']; |
|
181
|
|
|
if (count($this->response['transaction_details'][$item]) < 3) { |
|
182
|
|
|
$message = 'Transaction ID not found.'; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
$data['data'][$item] = ['status' => $status, 'message' => $message, |
|
186
|
|
|
'response' => $this->response['transaction_details'][$item]]; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
return $data; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Get Simple Response for user. |
|
194
|
|
|
* |
|
195
|
|
|
* @return array |
|
196
|
|
|
*/ |
|
197
|
|
|
protected function getSimpleResponseData() |
|
198
|
|
|
{ |
|
199
|
|
View Code Duplication |
if (empty($this->response['status'])) { |
|
|
|
|
|
|
200
|
|
|
return ['status' => false, 'message' => $this->response['msg']]; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
$data = ['status' => true, 'data' => []]; |
|
204
|
|
|
foreach ($this->txn_id as $item) { |
|
205
|
|
|
$status = ($this->response['transaction_details'][$item]['status'] == 'success') ? true : false; |
|
206
|
|
|
|
|
207
|
|
|
$message = @$this->response['transaction_details'][$item]['error_Message']; |
|
208
|
|
|
if (count($this->response['transaction_details'][$item]) < 3) { |
|
209
|
|
|
$message = 'Transaction ID not found.'; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
$data['data'][$item] = ['status' => $status, 'message' => $message]; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
return $data; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: