|
| 1 | +<?php |
| 2 | +namespace Goodoneuz\PayUz\Http\Classes\Paynet; |
| 3 | + |
| 4 | +use App; |
| 5 | +use Goodoneuz\PayUz\Http\Classes\DataFormat; |
| 6 | +use Goodoneuz\PayUz\Models\PaymentSystem; |
| 7 | +use Goodoneuz\PayUz\Models\PaymentSystemParam; |
| 8 | +use Goodoneuz\PayUz\Models\Transaction; |
| 9 | +use Goodoneuz\PayUz\Services\PaymentSystemService; |
| 10 | +use Goodoneuz\PayUz\Services\PaymentService; |
| 11 | +use Goodoneuz\PayUz\Http\Classes\PaymentException; |
| 12 | + |
| 13 | +class PaynetController |
| 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($request); |
| 25 | + $this->merchant = new Merchant($this->config, $this->request); |
| 26 | + } |
| 27 | + public function run(){ |
| 28 | + $this->merchant->Authorize(); |
| 29 | + switch ($this->request->params['method']) { |
| 30 | + case Request::METHOD_CheckTransaction: |
| 31 | + $this->CheckTransaction(); |
| 32 | + break; |
| 33 | + case Request::METHOD_PerformTransaction: |
| 34 | + $this->PerformTransaction(); |
| 35 | + break; |
| 36 | + case Request::METHOD_CancelTransaction: |
| 37 | + $this->CancelTransaction(); |
| 38 | + break; |
| 39 | + case Request::METHOD_GetStatement: |
| 40 | + $this->GetStatement(); |
| 41 | + break; |
| 42 | + case Request::METHOD_GetInformation: |
| 43 | + $this->GetInformation(); |
| 44 | + break; |
| 45 | + default: |
| 46 | + throw new PaynetException( |
| 47 | + null, |
| 48 | + 'Method not found.', |
| 49 | + PaynetException::ERROR_METHOD_NOT_FOUND |
| 50 | + |
| 51 | + ); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private function CheckTransaction() |
| 56 | + { |
| 57 | + $transaction = $this->getTransactionBySystemTransactionId(); |
| 58 | + $transactionState = ($transaction->state == Transaction::STATE_CANCELLED) ? 2 : 1; |
| 59 | + |
| 60 | + return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>". |
| 61 | + "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">". |
| 62 | + "<soapenv:Body>". |
| 63 | + "<ns2:CheckTransactionResult xmlns:ns2=\"http://uws.provider.com/\">". |
| 64 | + "<errorMsg>Success</errorMsg>". |
| 65 | + "<status>0</status>". |
| 66 | + "<timeStamp>".$this->toDateTimeWithTimeZone(now())."</timeStamp>". |
| 67 | + "<providerTrnId>".$this->request->params['transactionId']."</providerTrnId>". |
| 68 | + "<transactionState>" . $transactionState . "</transactionState>". |
| 69 | + "<transactionStateErrorStatus>0</transactionStateErrorStatus>". |
| 70 | + "<transactionStateErrorMsg>Success</transactionStateErrorMsg>". |
| 71 | + "</ns2:CheckTransactionResult>". |
| 72 | + "</soapenv:Body>". |
| 73 | + "</soapenv:Envelope>"; |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + private function PerformTransaction() |
| 78 | + { |
| 79 | + if ($this->getTransactionBySystemTransactionId()) |
| 80 | + return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>". |
| 81 | + "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">". |
| 82 | + "<soapenv:Body>". |
| 83 | + "<ns2:PerformTransactionResult xmlns:ns2=\"http://uws.provider.com/\">". |
| 84 | + "<errorMsg>bor trans</errorMsg>". |
| 85 | + "<status>201</status>". |
| 86 | + "<timeStamp>".$this->toDateTimeWithTimeZone(now())."</timeStamp>". |
| 87 | + "<providerTrnId>" . $this->request->params['transactionId'] . "</providerTrnId>". |
| 88 | + "</ns2:PerformTransactionResult>". |
| 89 | + "</soapenv:Body>". |
| 90 | + "</soapenv:Envelope>"; |
| 91 | + |
| 92 | + $model = PaymentService::convertKeyToModel($this->request->params['key']); |
| 93 | + |
| 94 | + // TODO: check if user not found return status 302; |
| 95 | + |
| 96 | + if (is_null($model)) { |
| 97 | + return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>". |
| 98 | + "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">". |
| 99 | + "<soapenv:Body>". |
| 100 | + "<ns2:PerformTransactionResult xmlns:ns2=\"http://uws.provider.com/\">". |
| 101 | + "<errorMsg>Not Found</errorMsg>". |
| 102 | + "<status>302</status>". |
| 103 | + "<timeStamp>".$this->toDateTimeWithTimeZone(now())."</timeStamp>". |
| 104 | + "<providerTrnId>0</providerTrnId>". |
| 105 | + "</ns2:PerformTransactionResult>". |
| 106 | + "</soapenv:Body>". |
| 107 | + "</soapenv:Envelope>"; |
| 108 | + } |
| 109 | + // TODO: check if user be yuridik litso can not return status 501; |
| 110 | + |
| 111 | + $create_time = Format::timestamp(true); |
| 112 | + $transaction = new Transaction(); |
| 113 | + $transaction->payment_system = Transaction::PAYNET; |
| 114 | + $transaction->system_transaction_id = $this->request->params['transactionId']; |
| 115 | + $transaction->system_time = Format::datetime2timestamp($this->request->params['transactionTime']); |
| 116 | + $transaction->system_time_datetime = Format::timestamp2datetime($this->request->params['transactionTime']); |
| 117 | + $transaction->create_time = Format::timestamp(true); |
| 118 | + $transaction->state = Transaction::STATE_CREATED; |
| 119 | + $transaction->amount = 1 * $this->request->params['amount']; |
| 120 | + $transaction->currency_code = Transaction::CODE_UZS; |
| 121 | + $transaction->user_key = $this->request->params['key']; |
| 122 | + $transaction->exported = Transaction::EXPORT_AVAILABLE; |
| 123 | + $transaction->save(); // after save $transaction->id will be populated with the newly created transaction's id. |
| 124 | + |
| 125 | + PaymentService::payListener(null,$transaction,'after-pay'); |
| 126 | + |
| 127 | + return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>". |
| 128 | + "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">". |
| 129 | + "<soapenv:Body>". |
| 130 | + "<ns2:PerformTransactionResult xmlns:ns2=\"http://uws.provider.com/\">". |
| 131 | + "<errorMsg>Success</errorMsg>". |
| 132 | + "<status>0</status>". |
| 133 | + "<timeStamp>".$this->toDateTimeWithTimeZone(now())."</timeStamp>". |
| 134 | + "<providerTrnId>" . $this->request->params['transactionId'] . "</providerTrnId>". |
| 135 | + "</ns2:PerformTransactionResult>". |
| 136 | + "</soapenv:Body>". |
| 137 | + "</soapenv:Envelope>"; |
| 138 | + } |
| 139 | + |
| 140 | + private function CancelTransaction(){ |
| 141 | + |
| 142 | + $transaction = $this->getTransactionBySystemTransactionId(); |
| 143 | + |
| 144 | + if ($transaction == null || $transaction->state == Transaction::STATE_CANCELLED) |
| 145 | + { |
| 146 | + header('content-type: text/xml;'); |
| 147 | + |
| 148 | + return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>". |
| 149 | + "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">". |
| 150 | + "<soapenv:Body>". |
| 151 | + "<ns2:CancelTransactionResult xmlns:ns2=\"http://uws.provider.com/\">". |
| 152 | + "<errorMsg>bekor qilingan</errorMsg>". |
| 153 | + "<status>202</status>". |
| 154 | + "<timeStamp>".$this->toDateTimeWithTimeZone(now())."</timeStamp>". |
| 155 | + "<transactionState>2</transactionState>". |
| 156 | + "</ns2:CancelTransactionResult>". |
| 157 | + "</soapenv:Body>". |
| 158 | + "</soapenv:Envelope>"; |
| 159 | + } |
| 160 | + |
| 161 | + $transaction->state = Transaction::STATE_CANCELLED; |
| 162 | + $transaction->update(); |
| 163 | + PaymentService::payListener(null,$transaction,'cancel-pay'); |
| 164 | + |
| 165 | + return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>". |
| 166 | + "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">". |
| 167 | + "<soapenv:Body>". |
| 168 | + "<ns2:CancelTransactionResult xmlns:ns2=\"http://uws.provider.com/\">". |
| 169 | + "<errorMsg>Success</errorMsg>". |
| 170 | + "<status>0</status>". |
| 171 | + "<timeStamp>".$this->toDateTimeWithTimeZone(now())."</timeStamp>". |
| 172 | + "<transactionState>2</transactionState>". |
| 173 | + "</ns2:CancelTransactionResult>". |
| 174 | + "</soapenv:Body>". |
| 175 | + "</soapenv:Envelope>"; |
| 176 | + } |
| 177 | + |
| 178 | + |
| 179 | + private function GetStatement() |
| 180 | + { |
| 181 | + |
| 182 | + $transactions = Transaction::where('payment_system', Transaction::PAYNET) |
| 183 | + ->where('state','<>',Transaction::STATE_CANCELLED) |
| 184 | + ->where('created_at','<=',$this->toDateTime($this->request->params['dateTo'])) |
| 185 | + ->where('created_at','>=',$this->toDateTime($this->request->params['dateFrom'])) |
| 186 | + ->get(); |
| 187 | + $statements = ''; |
| 188 | + |
| 189 | + foreach ($transactions as $transaction) |
| 190 | + { |
| 191 | + $statements = $statements . |
| 192 | + "<statements>". |
| 193 | + "<amount>" . $transaction->amount . "</amount>". |
| 194 | + "<providerTrnId>" . $transaction->id . "</providerTrnId>". |
| 195 | + "<transactionId>" . $transaction->system_transaction_id . "</transactionId>". |
| 196 | + "<transactionTime>".$this->toDateTimeWithTimeZone($transaction->created_at)."</transactionTime>". |
| 197 | + "</statements>"; |
| 198 | + } |
| 199 | + return "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"http://uws.provider.com/\">". |
| 200 | + "<SOAP-ENV:Body>". |
| 201 | + "<ns1:GetStatementResult>". |
| 202 | + "<errorMsg>Success</errorMsg>". |
| 203 | + "<status>0</status>". |
| 204 | + "<timeStamp>".$this->toDateTimeWithTimeZone(now())."</timeStamp>". |
| 205 | + $statements . |
| 206 | + "</ns1:GetStatementResult>". |
| 207 | + "</SOAP-ENV:Body>". |
| 208 | + "</SOAP-ENV:Envelope>"; |
| 209 | + } |
| 210 | + |
| 211 | + private function GetInformation(){ |
| 212 | + $model = PaymentService::convertKeyToModel($this->request->params['key']); |
| 213 | + |
| 214 | + if ($model) { |
| 215 | + return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>". |
| 216 | + "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">". |
| 217 | + "<soapenv:Body>". |
| 218 | + "<ns2:GetInformationResult xmlns:ns2=\"http://uws.provider.com/\">". |
| 219 | + "<errorMsg>Success</errorMsg>". |
| 220 | + "<status>0</status>". |
| 221 | + "<timeStamp>".$this->toDateTimeWithTimeZone(now())."</timeStamp>". |
| 222 | + "<parameters>". |
| 223 | + "<paramKey>userInfo</paramKey>". |
| 224 | + "<paramValue>".$model->name."</paramValue>". |
| 225 | + "</parameters>". |
| 226 | + "</ns2:GetInformationResult>". |
| 227 | + "</soapenv:Body>". |
| 228 | + "</soapenv:Envelope>"; |
| 229 | + }else{ |
| 230 | + return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>". |
| 231 | + "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">". |
| 232 | + "<soapenv:Body>". |
| 233 | + "<ns2:GetInformationResult xmlns:ns2=\"http://uws.provider.com/\">". |
| 234 | + "<errorMsg>Not Found</errorMsg>". |
| 235 | + "<status>302</status>". |
| 236 | + "<timeStamp>".$this->toDateTimeWithTimeZone(now())."</timeStamp>". |
| 237 | + "</ns2:GetInformationResult>". |
| 238 | + "</soapenv:Body>". |
| 239 | + "</soapenv:Envelope>"; |
| 240 | + } |
| 241 | + } |
| 242 | + |
| 243 | + |
| 244 | + private function getTransactionBySystemTransactionId() |
| 245 | + { |
| 246 | + return Transaction::where('system_transaction_id', $this->request->params['transactionId'])->first(); |
| 247 | + } |
| 248 | + |
| 249 | + /** |
| 250 | + * @param $time '2018-11-06T17:39:31+05:00' |
| 251 | + * @return false|string |
| 252 | + */ |
| 253 | + private function toDateTime($time){ |
| 254 | + return date('Y-m-d H:i:s',strtotime($time)); |
| 255 | + } |
| 256 | + |
| 257 | + /** |
| 258 | + * @param $time '2018-11-06 17:39:31' |
| 259 | + * @return string |
| 260 | + */ |
| 261 | + private function toDateTimeWithTimeZone($time){ |
| 262 | + return date('Y-m-d\TH:i:s',strtotime($time)) . '+05:00'; |
| 263 | + } |
| 264 | +} |
0 commit comments