1
|
|
|
<?php |
2
|
|
|
namespace Goodoneuz\PayUz\Http\Classes\Paynet; |
3
|
|
|
|
4
|
|
|
use Goodoneuz\PayUz\Models\Transaction; |
5
|
|
|
use Goodoneuz\PayUz\Models\PaymentSystem; |
6
|
|
|
use Goodoneuz\PayUz\Services\PaymentService; |
7
|
|
|
use Goodoneuz\PayUz\Http\Classes\DataFormat; |
8
|
|
|
use Goodoneuz\PayUz\Http\Classes\BaseGateway; |
9
|
|
|
use Goodoneuz\PayUz\Models\PaymentSystemParam; |
10
|
|
|
use Goodoneuz\PayUz\Http\Classes\PaymentException; |
11
|
|
|
use Goodoneuz\PayUz\Services\PaymentSystemService; |
12
|
|
|
|
13
|
|
|
class Paynet extends BaseGateway |
14
|
|
|
{ |
15
|
|
|
public $config; |
16
|
|
|
public $request; |
17
|
|
|
public $response; |
18
|
|
|
public $merchant; |
19
|
|
|
public function __construct() |
20
|
|
|
{ |
21
|
|
|
$this->config = PaymentSystemService::getPaymentSystemParamsCollect(PaymentSystem::PAYNET); |
22
|
|
|
$this->response = new Response(); |
23
|
|
|
$this->request = new Request($this->response); |
24
|
|
|
$this->response->setRequest($this->request); |
25
|
|
|
$this->merchant = new Merchant($this->config, $this->request, $this->response); |
26
|
|
|
} |
27
|
|
|
public function run(){ |
28
|
|
|
$this->merchant->Authorize(); |
29
|
|
|
switch ($this->request->params['method']) { |
30
|
|
|
case Request::METHOD_CheckTransaction: |
31
|
|
|
$body = Response::makeResponse($this->CheckTransaction()); |
32
|
|
|
break; |
33
|
|
|
case Request::METHOD_PerformTransaction: |
34
|
|
|
$body = Response::makeResponse($this->PerformTransaction()); |
35
|
|
|
break; |
36
|
|
|
case Request::METHOD_CancelTransaction: |
37
|
|
|
$body = Response::makeResponse($this->CancelTransaction()); |
38
|
|
|
break; |
39
|
|
|
case Request::METHOD_GetStatement: |
40
|
|
|
$body = $this->GetStatement(); |
41
|
|
|
break; |
42
|
|
|
case Request::METHOD_GetInformation: |
43
|
|
|
$body = Response::makeResponse($this->GetInformation()); |
44
|
|
|
break; |
45
|
|
|
default: |
46
|
|
|
$this->response->response($this->request, 'Method not found.', PaynetException::ERROR_METHOD_NOT_FOUND); |
47
|
|
|
} |
48
|
|
|
$this->response->response($this->request, $body, Response::SUCCESS); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
private function CheckTransaction() |
53
|
|
|
{ |
54
|
|
|
$transaction = $this->getTransactionBySystemTransactionId(); |
55
|
|
|
$transactionState = ($transaction->state == Transaction::STATE_CANCELLED) ? 2 : 1; |
56
|
|
|
|
57
|
|
|
return "<ns2:CheckTransactionResult xmlns:ns2=\"http://uws.provider.com/\">". |
58
|
|
|
"<errorMsg>Success</errorMsg>". |
59
|
|
|
"<status>0</status>". |
60
|
|
|
"<timeStamp>".DataFormat::toDateTimeWithTimeZone(now())."</timeStamp>". |
61
|
|
|
"<providerTrnId>".$this->request->params['transactionId']."</providerTrnId>". |
62
|
|
|
"<transactionState>" . $transactionState . "</transactionState>". |
63
|
|
|
"<transactionStateErrorStatus>0</transactionStateErrorStatus>". |
64
|
|
|
"<transactionStateErrorMsg>Success</transactionStateErrorMsg>". |
65
|
|
|
"</ns2:CheckTransactionResult>"; |
66
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
private function PerformTransaction() |
71
|
|
|
{ |
72
|
|
|
if ($this->getTransactionBySystemTransactionId()) |
73
|
|
|
return "<ns2:PerformTransactionResult xmlns:ns2=\"http://uws.provider.com/\">". |
74
|
|
|
"<errorMsg>transaction found</errorMsg>". |
75
|
|
|
"<status>201</status>". |
76
|
|
|
"<timeStamp>".DataFormat::toDateTimeWithTimeZone(now())."</timeStamp>". |
77
|
|
|
"<providerTrnId>" . $this->request->params['transactionId'] . "</providerTrnId>". |
78
|
|
|
"</ns2:PerformTransactionResult>"; |
79
|
|
|
|
80
|
|
|
$model = PaymentService::convertKeyToModel($this->request->params['key']); |
81
|
|
|
|
82
|
|
|
// TODO: check if user not found return status 302; |
83
|
|
|
|
84
|
|
|
if (is_null($model)) { |
85
|
|
|
return "<ns2:PerformTransactionResult xmlns:ns2=\"http://uws.provider.com/\">". |
86
|
|
|
"<errorMsg>Model not found</errorMsg>". |
87
|
|
|
"<status>302</status>". |
88
|
|
|
"<timeStamp>".DataFormat::toDateTimeWithTimeZone(now())."</timeStamp>". |
89
|
|
|
"<providerTrnId>0</providerTrnId>". |
90
|
|
|
"</ns2:PerformTransactionResult>"; |
91
|
|
|
} |
92
|
|
|
// TODO: check if user be yuridik litso can not return status 501; |
93
|
|
|
|
94
|
|
|
$create_time = DataFormat::timestamp(true); |
95
|
|
|
|
96
|
|
|
$detail = json_encode(array( |
97
|
|
|
'create_time' => $create_time, |
98
|
|
|
'perform_time' => null, |
99
|
|
|
'cancel_time' => null, |
100
|
|
|
'system_time_datetime' => DataFormat::timestamp2datetime($this->request->params['transactionTime']) |
101
|
|
|
)); |
102
|
|
|
$transaction = Transaction::create([ |
|
|
|
|
103
|
|
|
'payment_system' => PaymentSystem::PAYNET, |
104
|
|
|
'system_transaction_id' => $this->request->params['transactionId'], |
105
|
|
|
'amount' => 1 * $this->request->params['amount'], |
106
|
|
|
'currency_code' => Transaction::CURRENCY_CODE_UZS, |
107
|
|
|
'state' => Transaction::STATE_CREATED, |
108
|
|
|
'updated_time' => 1*$create_time, |
109
|
|
|
'comment' => (isset($this->request->params['error_note'])?$this->request->params['error_note']:''), |
110
|
|
|
'detail' => $detail, |
111
|
|
|
'transactionable_type' => get_class($model), |
112
|
|
|
'transactionable_id' => $model->id |
113
|
|
|
]); |
114
|
|
|
|
115
|
|
|
PaymentService::payListener(null,$transaction,'after-pay'); |
116
|
|
|
|
117
|
|
|
return "<ns2:PerformTransactionResult xmlns:ns2=\"http://uws.provider.com/\">". |
118
|
|
|
"<errorMsg>Success</errorMsg>". |
119
|
|
|
"<status>0</status>". |
120
|
|
|
"<timeStamp>".DataFormat::toDateTimeWithTimeZone(now())."</timeStamp>". |
121
|
|
|
"<providerTrnId>" . $this->request->params['transactionId'] . "</providerTrnId>". |
122
|
|
|
"</ns2:PerformTransactionResult>"; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
private function CancelTransaction(){ |
126
|
|
|
|
127
|
|
|
$transaction = $this->getTransactionBySystemTransactionId(); |
128
|
|
|
|
129
|
|
|
if ($transaction == null || $transaction->state == Transaction::STATE_CANCELLED) |
130
|
|
|
{ |
131
|
|
|
return "<ns2:CancelTransactionResult xmlns:ns2=\"http://uws.provider.com/\">". |
132
|
|
|
"<errorMsg>bekor qilingan</errorMsg>". |
133
|
|
|
"<status>202</status>". |
134
|
|
|
"<timeStamp>".DataFormat::toDateTimeWithTimeZone(now())."</timeStamp>". |
135
|
|
|
"<transactionState>2</transactionState>". |
136
|
|
|
"</ns2:CancelTransactionResult>"; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$transaction->state = Transaction::STATE_CANCELLED; |
140
|
|
|
$transaction->update(); |
141
|
|
|
PaymentService::payListener(null,$transaction,'cancel-pay'); |
142
|
|
|
|
143
|
|
|
return "<ns2:CancelTransactionResult xmlns:ns2=\"http://uws.provider.com/\">". |
144
|
|
|
"<errorMsg>Success</errorMsg>". |
145
|
|
|
"<status>0</status>". |
146
|
|
|
"<timeStamp>".DataFormat::toDateTimeWithTimeZone(now())."</timeStamp>". |
147
|
|
|
"<transactionState>2</transactionState>". |
148
|
|
|
"</ns2:CancelTransactionResult>"; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
|
152
|
|
|
private function GetStatement() |
153
|
|
|
{ |
154
|
|
|
|
155
|
|
|
$transactions = Transaction::where('payment_system', PaymentSystem::PAYNET) |
156
|
|
|
->where('state','<>',Transaction::STATE_CANCELLED) |
157
|
|
|
->where('created_at','<=',DataFormat::toDateTime($this->request->params['dateTo'])) |
158
|
|
|
->where('created_at','>=',DataFormat::toDateTime($this->request->params['dateFrom'])) |
159
|
|
|
->get(); |
160
|
|
|
$statements = ''; |
161
|
|
|
|
162
|
|
|
foreach ($transactions as $transaction){ |
163
|
|
|
$statements = $statements . |
164
|
|
|
"<statements>". |
165
|
|
|
"<amount>" . $transaction->amount . "</amount>". |
166
|
|
|
"<providerTrnId>" . $transaction->id . "</providerTrnId>". |
167
|
|
|
"<transactionId>" . $transaction->system_transaction_id . "</transactionId>". |
168
|
|
|
"<transactionTime>".DataFormat::toDateTimeWithTimeZone($transaction->created_at)."</transactionTime>". |
169
|
|
|
"</statements>"; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"http://uws.provider.com/\">". |
173
|
|
|
"<SOAP-ENV:Body>". |
174
|
|
|
"<ns1:GetStatementResult>". |
175
|
|
|
"<errorMsg>Success</errorMsg>". |
176
|
|
|
"<status>0</status>". |
177
|
|
|
"<timeStamp>".DataFormat::toDateTimeWithTimeZone(now())."</timeStamp>". |
178
|
|
|
$statements . |
179
|
|
|
"</ns1:GetStatementResult>". |
180
|
|
|
"</SOAP-ENV:Body>". |
181
|
|
|
"</SOAP-ENV:Envelope>"; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
private function GetInformation(){ |
185
|
|
|
$model = PaymentService::convertKeyToModel($this->request->params['key']); |
186
|
|
|
|
187
|
|
|
if ($model) { |
188
|
|
|
return "<ns2:GetInformationResult xmlns:ns2=\"http://uws.provider.com/\">". |
189
|
|
|
"<errorMsg>Success</errorMsg>". |
190
|
|
|
"<status>0</status>". |
191
|
|
|
"<timeStamp>".DataFormat::toDateTimeWithTimeZone(now())."</timeStamp>". |
192
|
|
|
"<parameters>". |
193
|
|
|
"<paramKey>userInfo</paramKey>". |
194
|
|
|
"<paramValue>".$model->name."</paramValue>". |
195
|
|
|
"</parameters>". |
196
|
|
|
"</ns2:GetInformationResult>"; |
197
|
|
|
}else{ |
198
|
|
|
return "<ns2:GetInformationResult xmlns:ns2=\"http://uws.provider.com/\">". |
199
|
|
|
"<errorMsg>Not Found</errorMsg>". |
200
|
|
|
"<status>302</status>". |
201
|
|
|
"<timeStamp>".DataFormat::toDateTimeWithTimeZone(now())."</timeStamp>". |
202
|
|
|
"</ns2:GetInformationResult>"; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
|
207
|
|
|
private function getTransactionBySystemTransactionId() |
208
|
|
|
{ |
209
|
|
|
return Transaction::where('system_transaction_id', $this->request->params['transactionId'])->first(); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: