From eaf28ac8ec612ae840fe2dd529db28df9917789c Mon Sep 17 00:00:00 2001 From: Shaxzodbek Qambaraliyev Date: Thu, 11 Apr 2019 15:39:37 +0500 Subject: [PATCH] First installation of paynet --- src/Http/Classes/DataFormat.php | 15 +++ src/Http/Classes/Paynet/Merchant.php | 9 +- src/Http/Classes/Paynet/Paynet.php | 189 ++++++++++----------------- src/Http/Classes/Paynet/Request.php | 97 ++++++-------- src/Http/Classes/Paynet/Response.php | 52 ++++---- src/PayUz.php | 4 + 6 files changed, 154 insertions(+), 212 deletions(-) diff --git a/src/Http/Classes/DataFormat.php b/src/Http/Classes/DataFormat.php index 2483b14..c457cf8 100644 --- a/src/Http/Classes/DataFormat.php +++ b/src/Http/Classes/DataFormat.php @@ -96,4 +96,19 @@ public static function datetime2timestamp($datetime) return $datetime; } + /** + * @param $time '2018-11-06T17:39:31+05:00' + * @return false|string + */ + public static function toDateTime($time){ + return date('Y-m-d H:i:s',strtotime($time)); + } + + /** + * @param $time '2018-11-06 17:39:31' + * @return string + */ + public static function toDateTimeWithTimeZone($time){ + return date('Y-m-d\TH:i:s',strtotime($time)) . '+05:00'; + } } \ No newline at end of file diff --git a/src/Http/Classes/Paynet/Merchant.php b/src/Http/Classes/Paynet/Merchant.php index d218dbd..6d511ed 100644 --- a/src/Http/Classes/Paynet/Merchant.php +++ b/src/Http/Classes/Paynet/Merchant.php @@ -7,10 +7,11 @@ class Merchant public $config; public $request; - public function __construct($config, $request) + public function __construct($config, $request, $response) { $this->config = $config; $this->request = $request; + $this->response = $response; } public function Authorize() @@ -20,11 +21,7 @@ public function Authorize() if ($this->config['login'] != $this->request->params['account']['login'] || $this->config['password'] != $this->request->params['account']['password']) { - throw new PaynetException( - $this->request, - 'Insufficient privilege to perform this method.', - PaynetException::ERROR_INSUFFICIENT_PRIVILEGE - ); + $this->resonse->response($this->request, 'Insufficient privilege to perform this method.', Response::ERROR_INSUFFICIENT_PRIVILEGE); } return true; } diff --git a/src/Http/Classes/Paynet/Paynet.php b/src/Http/Classes/Paynet/Paynet.php index 82b8d04..ac27b31 100644 --- a/src/Http/Classes/Paynet/Paynet.php +++ b/src/Http/Classes/Paynet/Paynet.php @@ -10,7 +10,7 @@ use Goodoneuz\PayUz\Services\PaymentService; use Goodoneuz\PayUz\Http\Classes\PaymentException; -class PaynetController +class Paynet { public $config; public $request; @@ -18,123 +18,108 @@ class PaynetController public $merchant; public function __construct() { - $this->config = PaymentSystemService::getPaymentSystemParamsCollect(PaymentSystem::PAYNET);; + $this->config = PaymentSystemService::getPaymentSystemParamsCollect(PaymentSystem::PAYNET); $this->response = new Response(); $this->request = new Request($this->response); $this->response->setRequest($request); - $this->merchant = new Merchant($this->config, $this->request); + $this->merchant = new Merchant($this->config, $this->request, $this->response); } public function run(){ $this->merchant->Authorize(); switch ($this->request->params['method']) { case Request::METHOD_CheckTransaction: - $this->CheckTransaction(); + $body = $this->CheckTransaction(); break; case Request::METHOD_PerformTransaction: - $this->PerformTransaction(); + $body = $this->PerformTransaction(); break; case Request::METHOD_CancelTransaction: - $this->CancelTransaction(); + $body = $this->CancelTransaction(); break; case Request::METHOD_GetStatement: - $this->GetStatement(); + $body = $this->GetStatement(); break; case Request::METHOD_GetInformation: - $this->GetInformation(); + $body = $this->GetInformation(); break; default: - throw new PaynetException( - null, - 'Method not found.', - PaynetException::ERROR_METHOD_NOT_FOUND - - ); + $this->response->response($this->request, 'Method not found.', PaynetException::ERROR_METHOD_NOT_FOUND); } + $this->response->response($this->request, $body, Response::SUCCESS); } + private function CheckTransaction() { $transaction = $this->getTransactionBySystemTransactionId(); $transactionState = ($transaction->state == Transaction::STATE_CANCELLED) ? 2 : 1; - return "". - "". - "". - "". + return "". "Success". "0". - "".$this->toDateTimeWithTimeZone(now())."". + "".DataFormat::toDateTimeWithTimeZone(now())."". "".$this->request->params['transactionId']."". "" . $transactionState . "". "0". "Success". - "". - "". - ""; + ""; + } private function PerformTransaction() { if ($this->getTransactionBySystemTransactionId()) - return "". - "". - "". - "". - "bor trans". + return "". + "transaction found". "201". - "".$this->toDateTimeWithTimeZone(now())."". + "".DataFormat::toDateTimeWithTimeZone(now())."". "" . $this->request->params['transactionId'] . "". - "". - "". - ""; + ""; $model = PaymentService::convertKeyToModel($this->request->params['key']); // TODO: check if user not found return status 302; if (is_null($model)) { - return "". - "". - "". - "". - "Not Found". + return "". + "Model not found". "302". - "".$this->toDateTimeWithTimeZone(now())."". + "".DataFormat::toDateTimeWithTimeZone(now())."". "0". - "". - "". - ""; + ""; } // TODO: check if user be yuridik litso can not return status 501; - $create_time = Format::timestamp(true); - $transaction = new Transaction(); - $transaction->payment_system = Transaction::PAYNET; - $transaction->system_transaction_id = $this->request->params['transactionId']; - $transaction->system_time = Format::datetime2timestamp($this->request->params['transactionTime']); - $transaction->system_time_datetime = Format::timestamp2datetime($this->request->params['transactionTime']); - $transaction->create_time = Format::timestamp(true); - $transaction->state = Transaction::STATE_CREATED; - $transaction->amount = 1 * $this->request->params['amount']; - $transaction->currency_code = Transaction::CODE_UZS; - $transaction->user_key = $this->request->params['key']; - $transaction->exported = Transaction::EXPORT_AVAILABLE; - $transaction->save(); // after save $transaction->id will be populated with the newly created transaction's id. + $create_time = DataFormat::timestamp(true); + $detail = json_encode(array( + 'create_time' => $create_time, + 'perform_time' => null, + 'cancel_time' => null, + 'system_time_datetime' => DataFormat::timestamp2datetime($this->request->params['transactionTime']) + )); + $transaction = Transaction::create([ + 'payment_system' => Transaction::PAYNET, + 'system_transaction_id' => $this->request->params['transactionId'], + 'amount' => 1 * $this->request->params['amount'], + 'currency_code' => Transaction::CODE_UZS, + 'state' => Transaction::STATE_CREATED, + 'updated_time' => 1*$create_time, + 'comment' => (isset($this->request->params['error_note'])?$this->request->params['error_note']:''), + 'detail' => $detail, + 'transactionable_type' => get_class($model), + 'transactionable_id' => $model->id + ]); + PaymentService::payListener(null,$transaction,'after-pay'); - return "". - "". - "". - "". + return "". "Success". "0". - "".$this->toDateTimeWithTimeZone(now())."". + "".DataFormat::toDateTimeWithTimeZone(now())."". "" . $this->request->params['transactionId'] . "". - "". - "". - ""; + ""; } private function CancelTransaction(){ @@ -143,36 +128,24 @@ private function CancelTransaction(){ if ($transaction == null || $transaction->state == Transaction::STATE_CANCELLED) { - header('content-type: text/xml;'); - - return "". - "". - "". - "". + return "". "bekor qilingan". "202". - "".$this->toDateTimeWithTimeZone(now())."". + "".DataFormat::toDateTimeWithTimeZone(now())."". "2". - "". - "". - ""; + ""; } $transaction->state = Transaction::STATE_CANCELLED; $transaction->update(); PaymentService::payListener(null,$transaction,'cancel-pay'); - return "". - "". - "". - "". + return "". "Success". "0". - "".$this->toDateTimeWithTimeZone(now())."". + "".DataFormat::toDateTimeWithTimeZone(now())."". "2". - "". - "". - ""; + ""; } @@ -181,62 +154,48 @@ private function GetStatement() $transactions = Transaction::where('payment_system', Transaction::PAYNET) ->where('state','<>',Transaction::STATE_CANCELLED) - ->where('created_at','<=',$this->toDateTime($this->request->params['dateTo'])) - ->where('created_at','>=',$this->toDateTime($this->request->params['dateFrom'])) + ->where('created_at','<=',DataFormat::toDateTime($this->request->params['dateTo'])) + ->where('created_at','>=',DataFormat::toDateTime($this->request->params['dateFrom'])) ->get(); $statements = ''; - foreach ($transactions as $transaction) - { + foreach ($transactions as $transaction){ $statements = $statements . "". "" . $transaction->amount . "". "" . $transaction->id . "". "" . $transaction->system_transaction_id . "". - "".$this->toDateTimeWithTimeZone($transaction->created_at)."". + "".DataFormat::toDateTimeWithTimeZone($transaction->created_at)."". ""; } - return "". - "". - "". + + return "". "Success". "0". - "".$this->toDateTimeWithTimeZone(now())."". + "".DataFormat::toDateTimeWithTimeZone(now())."". $statements . - "". - "". - ""; + ""; } private function GetInformation(){ $model = PaymentService::convertKeyToModel($this->request->params['key']); if ($model) { - return "". - "". - "". - "". + return "". "Success". "0". - "".$this->toDateTimeWithTimeZone(now())."". + "".DataFormat::toDateTimeWithTimeZone(now())."". "". "userInfo". "".$model->name."". "". - "". - "". - ""; + ""; }else{ - return "". - "". - "". - "". + return "". "Not Found". "302". - "".$this->toDateTimeWithTimeZone(now())."". - "". - "". - ""; + "".DataFormat::toDateTimeWithTimeZone(now())."". + ""; } } @@ -245,20 +204,4 @@ private function getTransactionBySystemTransactionId() { return Transaction::where('system_transaction_id', $this->request->params['transactionId'])->first(); } - - /** - * @param $time '2018-11-06T17:39:31+05:00' - * @return false|string - */ - private function toDateTime($time){ - return date('Y-m-d H:i:s',strtotime($time)); - } - - /** - * @param $time '2018-11-06 17:39:31' - * @return string - */ - private function toDateTimeWithTimeZone($time){ - return date('Y-m-d\TH:i:s',strtotime($time)) . '+05:00'; - } } diff --git a/src/Http/Classes/Paynet/Request.php b/src/Http/Classes/Paynet/Request.php index 73b94a0..5092fe0 100644 --- a/src/Http/Classes/Paynet/Request.php +++ b/src/Http/Classes/Paynet/Request.php @@ -8,11 +8,9 @@ namespace Goodoneuz\PayUz\Http\Classes\Paynet; - class Request { - public $method; const ARGUMENTS_PerformTransaction = 'PerformTransactionArguments'; @@ -27,114 +25,103 @@ class Request const METHOD_CancelTransaction = 'CancelTransaction'; const METHOD_GetInformation = 'GetInformation'; - public $params; + private $response; - - public function __construct() + public function __construct($response) { - $request_body = file_get_contents('php://input'); - $clean_xml = str_ireplace(['soapenv:', 'soap:','xmlns:','xsi:','ns1:'], '', $request_body); - $xml = simplexml_load_string($clean_xml); - if ($xml){ - $body = $xml->Body; - } else - throw new PaynetException(null, 'Error in request', PaynetException::ERROR_INVALID_JSON_RPC_OBJECT); - $json_params = json_decode(json_encode($body),1); + $this->response = $response; + $this->params = []; + $arr_params = $this->getRequestArray(); + $this->loadAccount($arr_params); - foreach ($json_params as $key => $value){ + foreach ($arr_params as $key => $value){ switch ($key){ case self::ARGUMENTS_PerformTransaction: - $this->paramsPerformTransaction($json_params[self::ARGUMENTS_PerformTransaction]); + $this->paramsPerformTransaction($arr_params[self::ARGUMENTS_PerformTransaction]); break; case self::ARGUMENTS_CheckTransaction: - $this->paramsCheckTransaction($json_params[self::ARGUMENTS_CheckTransaction]); + $this->paramsCheckTransaction($arr_params[self::ARGUMENTS_CheckTransaction]); break; case self::ARGUMENTS_GetStatement: - $this->paramsStament($json_params[self::ARGUMENTS_GetStatement]); + $this->paramsStament($arr_params[self::ARGUMENTS_GetStatement]); break; case self::ARGUMENTS_CancelTransaction: - $this->paramsCancel($json_params[self::ARGUMENTS_CancelTransaction]); + $this->paramsCancel($arr_params[self::ARGUMENTS_CancelTransaction]); break; case self::ARGUMENTS_GetInformation: - $this->paramsInformation($json_params[self::ARGUMENTS_GetInformation]); + $this->paramsInformation($arr_params[self::ARGUMENTS_GetInformation]); break; default: - throw new PaynetException(null, 'Error in method type', PaynetException::ERROR_METHOD_NOT_FOUND); + $this->response->response($this,'Error in request', Response::ERROR_METHOD_NOT_FOUND); } - Log::info($key); } } + public function loadAccount($arr_params){ + $this->params['account'] = [ + 'login' => $arr_params['username'], + 'password' => $arr_params['password'] + ]; + $this->params['serviceId'] = $arr_params['serviceId']; + } + public function getRequestArray(){ + $request_body = file_get_contents('php://input'); + $clean_xml = str_ireplace(['soapenv:', 'soap:','xmlns:','xsi:','ns1:'], '', $request_body); + $xml = simplexml_load_string($clean_xml); + $body = null; + if ($xml) + $body = $xml->Body; + else + $this->response->response($this,'Error in request', Response::ERROR_INVALID_JSON_RPC_OBJECT); + return json_decode(json_encode($body),1); + } public function paramsPerformTransaction($par){ - $this->params = [ + $res = [ 'method' => self::METHOD_PerformTransaction, - 'account' => [ - 'login' => $par['username'], - 'password' => $par['password'] - ], 'amount' => $par['amount'], - 'serviceId' => $par['serviceId'], 'transactionId' => $par['transactionId'], 'transactionTime' => $par['transactionTime'], - 'user_key' => $par['parameters']['paramValue'] + 'key' => $par['parameters']['paramValue'] ]; + $this->params = array_merge($this->params, $res); } public function paramsCheckTransaction($par){ - $this->params = [ + $res = [ 'method' => self::METHOD_CheckTransaction, - 'account' => [ - 'login' => $par['username'], - 'password' => $par['password'] - ], - 'serviceId' => $par['serviceId'], 'transactionId' => $par['transactionId'], 'transactionTime' => $par['transactionTime'], ]; + $this->params = array_merge($this->params, $res); } private function paramsStament($par) { - $this->params = [ + $res = [ 'method' => self::METHOD_GetStatement, - 'account' => [ - 'login' => $par['username'], - 'password' => $par['password'] - ], - 'serviceId' => $par['serviceId'], 'dateFrom' => $par['dateFrom'], 'dateTo' => $par['dateTo'] ]; + $this->params = array_merge($this->params, $res); } private function paramsCancel($par) { - $res = []; - $res1 = [ + $res = [ 'method' => self::METHOD_CancelTransaction, - 'account' => [ - 'login' => $par['username'], - 'password' => $par['password'] - ], - 'serviceId' => $par['serviceId'], 'transactionId' => $par['transactionId'], 'transactionTime' => $par['transactionTime'] ]; - $this->params = array_merge($res1, $res); + $this->params = array_merge($this->params, $res); } private function paramsInformation($par) { $res = [ 'method' => self::METHOD_GetInformation, - 'account' => [ - 'login' => $par['username'], - 'password' => $par['password'] - ], - 'serviceId' => $par['serviceId'], - 'user_key' => $par['parameters']['paramValue'] + 'key' => $par['parameters']['paramValue'] ]; - $this->params = $res; + $this->params = array_merge($this->params, $res); } } diff --git a/src/Http/Classes/Paynet/Response.php b/src/Http/Classes/Paynet/Response.php index e51a427..266663c 100644 --- a/src/Http/Classes/Paynet/Response.php +++ b/src/Http/Classes/Paynet/Response.php @@ -10,6 +10,7 @@ use App\Transaction; use Carbon\Carbon; +use Goodoneuz\PayUz\Http\Classes\PaymentException; class Response { @@ -25,36 +26,31 @@ class Response const SUCCESS = 0; public $request; - public $error; - public $data; - - public function __construct($request, $message, $code, $data = null) - { - $this->request = $request; - $this->message = $message; - $this->code = $code; - $this->data = $data; - - $this->error = ['code' => $this->code]; - - if ($this->message) { - $this->error['message'] = $this->message; - } - - if ($this->data) { - $this->error['data'] = $this->data; - } + public $body; + public $code; + + public function response($request, $body, $code){ + $this->request = $request; + $this->body = $this->makeResponse($body); + $this->code = $code; + throw new PaymentException($this); } - - public function send() - { - $response = ''; - if ($this->request == null){ - echo 'error'; - exit(); - } + public function makeResponse($body){ + return "". + "". + "". + $body. + "". + ""; + } + public function send(){ header('content-type: text/xml;'); - echo $response; + + if ($this->request == null) + echo 'error'; + else + echo $this->body; + exit(); } diff --git a/src/PayUz.php b/src/PayUz.php index f8de134..fadb003 100644 --- a/src/PayUz.php +++ b/src/PayUz.php @@ -3,6 +3,7 @@ namespace Goodoneuz\PayUz; use Goodoneuz\PayUz\Http\Classes\Click\Click; +use Goodoneuz\PayUz\Http\Classes\Paynet\Paynet; use Goodoneuz\PayUz\Http\Classes\Payme\Payme; use Goodoneuz\PayUz\Http\Classes\PaymentException; use Goodoneuz\PayUz\Models\PaymentSystem; @@ -35,6 +36,9 @@ public function driver($driver = null){ case PaymentSystem::CLICK: $this->driverClass = Click::class; break; + case PaymentSystem::PAYNET: + $this->driverClass = Paynet::class; + break; } return $this; }