Skip to content

Commit

Permalink
First installation of paynet
Browse files Browse the repository at this point in the history
  • Loading branch information
shaxzodbek-uzb committed Apr 11, 2019
1 parent 1ce3d20 commit eaf28ac
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 212 deletions.
15 changes: 15 additions & 0 deletions src/Http/Classes/DataFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}
9 changes: 3 additions & 6 deletions src/Http/Classes/Paynet/Merchant.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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;
}
Expand Down
189 changes: 66 additions & 123 deletions src/Http/Classes/Paynet/Paynet.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,131 +10,116 @@
use Goodoneuz\PayUz\Services\PaymentService;
use Goodoneuz\PayUz\Http\Classes\PaymentException;

class PaynetController
class Paynet
{
public $config;
public $request;
public $response;
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 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>".
"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">".
"<soapenv:Body>".
"<ns2:CheckTransactionResult xmlns:ns2=\"http://uws.provider.com/\">".
return "<ns2:CheckTransactionResult xmlns:ns2=\"http://uws.provider.com/\">".
"<errorMsg>Success</errorMsg>".
"<status>0</status>".
"<timeStamp>".$this->toDateTimeWithTimeZone(now())."</timeStamp>".
"<timeStamp>".DataFormat::toDateTimeWithTimeZone(now())."</timeStamp>".
"<providerTrnId>".$this->request->params['transactionId']."</providerTrnId>".
"<transactionState>" . $transactionState . "</transactionState>".
"<transactionStateErrorStatus>0</transactionStateErrorStatus>".
"<transactionStateErrorMsg>Success</transactionStateErrorMsg>".
"</ns2:CheckTransactionResult>".
"</soapenv:Body>".
"</soapenv:Envelope>";
"</ns2:CheckTransactionResult>";

}


private function PerformTransaction()
{
if ($this->getTransactionBySystemTransactionId())
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>".
"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">".
"<soapenv:Body>".
"<ns2:PerformTransactionResult xmlns:ns2=\"http://uws.provider.com/\">".
"<errorMsg>bor trans</errorMsg>".
return "<ns2:PerformTransactionResult xmlns:ns2=\"http://uws.provider.com/\">".
"<errorMsg>transaction found</errorMsg>".
"<status>201</status>".
"<timeStamp>".$this->toDateTimeWithTimeZone(now())."</timeStamp>".
"<timeStamp>".DataFormat::toDateTimeWithTimeZone(now())."</timeStamp>".
"<providerTrnId>" . $this->request->params['transactionId'] . "</providerTrnId>".
"</ns2:PerformTransactionResult>".
"</soapenv:Body>".
"</soapenv:Envelope>";
"</ns2:PerformTransactionResult>";

$model = PaymentService::convertKeyToModel($this->request->params['key']);

// TODO: check if user not found return status 302;

if (is_null($model)) {
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>".
"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">".
"<soapenv:Body>".
"<ns2:PerformTransactionResult xmlns:ns2=\"http://uws.provider.com/\">".
"<errorMsg>Not Found</errorMsg>".
return "<ns2:PerformTransactionResult xmlns:ns2=\"http://uws.provider.com/\">".
"<errorMsg>Model not found</errorMsg>".
"<status>302</status>".
"<timeStamp>".$this->toDateTimeWithTimeZone(now())."</timeStamp>".
"<timeStamp>".DataFormat::toDateTimeWithTimeZone(now())."</timeStamp>".
"<providerTrnId>0</providerTrnId>".
"</ns2:PerformTransactionResult>".
"</soapenv:Body>".
"</soapenv:Envelope>";
"</ns2:PerformTransactionResult>";
}
// 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 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>".
"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">".
"<soapenv:Body>".
"<ns2:PerformTransactionResult xmlns:ns2=\"http://uws.provider.com/\">".
return "<ns2:PerformTransactionResult xmlns:ns2=\"http://uws.provider.com/\">".
"<errorMsg>Success</errorMsg>".
"<status>0</status>".
"<timeStamp>".$this->toDateTimeWithTimeZone(now())."</timeStamp>".
"<timeStamp>".DataFormat::toDateTimeWithTimeZone(now())."</timeStamp>".
"<providerTrnId>" . $this->request->params['transactionId'] . "</providerTrnId>".
"</ns2:PerformTransactionResult>".
"</soapenv:Body>".
"</soapenv:Envelope>";
"</ns2:PerformTransactionResult>";
}

private function CancelTransaction(){
Expand All @@ -143,36 +128,24 @@ private function CancelTransaction(){

if ($transaction == null || $transaction->state == Transaction::STATE_CANCELLED)
{
header('content-type: text/xml;');

return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>".
"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">".
"<soapenv:Body>".
"<ns2:CancelTransactionResult xmlns:ns2=\"http://uws.provider.com/\">".
return "<ns2:CancelTransactionResult xmlns:ns2=\"http://uws.provider.com/\">".
"<errorMsg>bekor qilingan</errorMsg>".
"<status>202</status>".
"<timeStamp>".$this->toDateTimeWithTimeZone(now())."</timeStamp>".
"<timeStamp>".DataFormat::toDateTimeWithTimeZone(now())."</timeStamp>".
"<transactionState>2</transactionState>".
"</ns2:CancelTransactionResult>".
"</soapenv:Body>".
"</soapenv:Envelope>";
"</ns2:CancelTransactionResult>";
}

$transaction->state = Transaction::STATE_CANCELLED;
$transaction->update();
PaymentService::payListener(null,$transaction,'cancel-pay');

return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>".
"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">".
"<soapenv:Body>".
"<ns2:CancelTransactionResult xmlns:ns2=\"http://uws.provider.com/\">".
return "<ns2:CancelTransactionResult xmlns:ns2=\"http://uws.provider.com/\">".
"<errorMsg>Success</errorMsg>".
"<status>0</status>".
"<timeStamp>".$this->toDateTimeWithTimeZone(now())."</timeStamp>".
"<timeStamp>".DataFormat::toDateTimeWithTimeZone(now())."</timeStamp>".
"<transactionState>2</transactionState>".
"</ns2:CancelTransactionResult>".
"</soapenv:Body>".
"</soapenv:Envelope>";
"</ns2:CancelTransactionResult>";
}


Expand All @@ -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 .
"<statements>".
"<amount>" . $transaction->amount . "</amount>".
"<providerTrnId>" . $transaction->id . "</providerTrnId>".
"<transactionId>" . $transaction->system_transaction_id . "</transactionId>".
"<transactionTime>".$this->toDateTimeWithTimeZone($transaction->created_at)."</transactionTime>".
"<transactionTime>".DataFormat::toDateTimeWithTimeZone($transaction->created_at)."</transactionTime>".
"</statements>";
}
return "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"http://uws.provider.com/\">".
"<SOAP-ENV:Body>".
"<ns1:GetStatementResult>".

return "<ns1:GetStatementResult>".
"<errorMsg>Success</errorMsg>".
"<status>0</status>".
"<timeStamp>".$this->toDateTimeWithTimeZone(now())."</timeStamp>".
"<timeStamp>".DataFormat::toDateTimeWithTimeZone(now())."</timeStamp>".
$statements .
"</ns1:GetStatementResult>".
"</SOAP-ENV:Body>".
"</SOAP-ENV:Envelope>";
"</ns1:GetStatementResult>";
}

private function GetInformation(){
$model = PaymentService::convertKeyToModel($this->request->params['key']);

if ($model) {
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>".
"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">".
"<soapenv:Body>".
"<ns2:GetInformationResult xmlns:ns2=\"http://uws.provider.com/\">".
return "<ns2:GetInformationResult xmlns:ns2=\"http://uws.provider.com/\">".
"<errorMsg>Success</errorMsg>".
"<status>0</status>".
"<timeStamp>".$this->toDateTimeWithTimeZone(now())."</timeStamp>".
"<timeStamp>".DataFormat::toDateTimeWithTimeZone(now())."</timeStamp>".
"<parameters>".
"<paramKey>userInfo</paramKey>".
"<paramValue>".$model->name."</paramValue>".
"</parameters>".
"</ns2:GetInformationResult>".
"</soapenv:Body>".
"</soapenv:Envelope>";
"</ns2:GetInformationResult>";
}else{
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>".
"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">".
"<soapenv:Body>".
"<ns2:GetInformationResult xmlns:ns2=\"http://uws.provider.com/\">".
return "<ns2:GetInformationResult xmlns:ns2=\"http://uws.provider.com/\">".
"<errorMsg>Not Found</errorMsg>".
"<status>302</status>".
"<timeStamp>".$this->toDateTimeWithTimeZone(now())."</timeStamp>".
"</ns2:GetInformationResult>".
"</soapenv:Body>".
"</soapenv:Envelope>";
"<timeStamp>".DataFormat::toDateTimeWithTimeZone(now())."</timeStamp>".
"</ns2:GetInformationResult>";
}
}

Expand All @@ -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';
}
}
Loading

0 comments on commit eaf28ac

Please sign in to comment.