@@ -172,7 +172,7 @@ class Kkt extends Client
*/
public function setCallbackUrl ( string $url )
{
$this -> kkt_config [ 'prod' ][ 'callback_url' ] = $url ;
$this -> kkt_config [ $this -> isTestMode () ? 'test' : 'prod' ][ 'callback_url' ] = $url ;
return $this ;
}
@@ -222,104 +222,115 @@ class Kkt extends Client
* Регистрирует документ прихода
*
* @param \AtolOnline\Entities\Document $document
* @param string|null $external_id Уникальный код документа (если не указан, то будет создан UUID)
* @return \AtolOnline\Api\KktResponse
* @throws \AtolOnline\Exceptions\AtolWrongDocumentTypeException Некорректный тип документа
* @throws \AtolOnline\Exceptions\AtolCorrectionInfoException В документе есть данные коррекции
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function sell ( Document $document )
public function sell ( Document $document , ? string $external_id = null )
{
if ( $document -> getCorrectionInfo ()) {
throw new AtolCorrectionInfoException ( 'Некорректная операция над документом коррекции' );
}
return $this -> registerDocument ( 'sell' , 'receipt' , $document );
return $this -> registerDocument ( 'sell' , 'receipt' , $document , $external_id );
}
/**
* Регистрирует документ возврата прихода
*
* @param \AtolOnline\Entities\Document $document
* @param string|null $external_id Уникальный код документа (если не указан, то будет создан UUID)
* @return \AtolOnline\Api\KktResponse
* @throws \AtolOnline\Exceptions\AtolPriceTooHighException Слишком большая сумма
* @throws \AtolOnline\Exceptions\AtolTooManyVatsException Слишком много ставок НДС
* @throws \AtolOnline\Exceptions\AtolWrongDocumentTypeException Некорректный тип документа
* @throws \AtolOnline\Exceptions\AtolCorrectionInfoException В документе есть данные коррекции
*/
public function sellRefund ( Document $document )
public function sellRefund ( Document $document , ? string $external_id = null )
{
if ( $document -> getCorrectionInfo ()) {
throw new AtolCorrectionInfoException ( 'Некорректная операция над документом коррекции' );
}
return $this -> registerDocument ( 'sell_refund' , 'receipt' , $document -> clearVats ());
return $this -> registerDocument ( 'sell_refund' , 'receipt' , $document -> clearVats (), $external_id );
}
/**
* Регистрирует документ коррекции прихода
*
* @param \AtolOnline\Entities\Document $document
* @param string|null $external_id Уникальный код документа (если не указан, то будет создан UUID)
* @return \AtolOnline\Api\KktResponse
* @throws \AtolOnline\Exceptions\AtolWrongDocumentTypeException Некорректный тип документа
* @throws \AtolOnline\Exceptions\AtolCorrectionInfoException В документе отсутствуют данные коррекции
* @throws \AtolOnline\Exceptions\AtolTooManyItemsException Слишком много предметов расчёта
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function sellCorrection ( Document $document )
public function sellCorrection ( Document $document , ? string $external_id = null )
{
if ( ! $document -> getCorrectionInfo ()) {
throw new AtolCorrectionInfoException ();
}
$document -> setClient ( null ) -> setItems ([]);
return $this -> registerDocument ( 'sell_correction' , 'correction' , $document );
return $this -> registerDocument ( 'sell_correction' , 'correction' , $document , $external_id );
}
/**
* Регистрирует документ расхода
*
* @param \AtolOnline\Entities\Document $document
* @param string|null $external_id Уникальный код документа (если не указан, то будет создан UUID)
* @return \AtolOnline\Api\KktResponse
* @throws \AtolOnline\Exceptions\AtolWrongDocumentTypeException Некорректный тип документа
* @throws \AtolOnline\Exceptions\AtolCorrectionInfoException В документе есть данные коррекции
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function buy ( Document $document )
public function buy ( Document $document , ? string $external_id = null )
{
if ( $document -> getCorrectionInfo ()) {
throw new AtolCorrectionInfoException ( 'Некорректная операция над документом коррекции' );
}
return $this -> registerDocument ( 'buy' , 'receipt' , $document );
return $this -> registerDocument ( 'buy' , 'receipt' , $document , $external_id );
}
/**
* Регистрирует документ возврата расхода
*
* @param \AtolOnline\Entities\Document $document
* @param string|null $external_id Уникальный код документа (если не указан, то будет создан UUID)
* @return \AtolOnline\Api\KktResponse
* @throws \AtolOnline\Exceptions\AtolPriceTooHighException Слишком большая сумма
* @throws \AtolOnline\Exceptions\AtolTooManyVatsException Слишком много ставок НДС
* @throws \AtolOnline\Exceptions\AtolWrongDocumentTypeException Некорректный тип документа
* @throws \AtolOnline\Exceptions\AtolCorrectionInfoException В документе есть данные коррекции
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function buyRefund ( Document $document )
public function buyRefund ( Document $document , ? string $external_id = null )
{
if ( $document -> getCorrectionInfo ()) {
throw new AtolCorrectionInfoException ( 'Некорректная операция над документом коррекции' );
}
return $this -> registerDocument ( 'buy_refund' , 'receipt' , $document -> clearVats ());
return $this -> registerDocument ( 'buy_refund' , 'receipt' , $document -> clearVats (), $external_id );
}
/**
* Регистрирует документ коррекции расхода
*
* @param Document $document
* @param Document $document
* @param string|null $external_id Уникальный код документа (если не указан, то будет создан UUID)
* @return \AtolOnline\Api\KktResponse
* @throws \AtolOnline\Exceptions\AtolWrongDocumentTypeException Некорректный тип документа
* @throws \AtolOnline\Exceptions\AtolCorrectionInfoException В документе отсутствуют данные коррекции
* @throws \AtolOnline\Exceptions\AtolTooManyItemsException Слишком много предметов расчёта
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function buyCorrection ( Document $document )
public function buyCorrection ( Document $document , ? string $external_id = null )
{
if ( ! $document -> getCorrectionInfo ()) {
throw new AtolCorrectionInfoException ();
}
$document -> setClient ( null ) -> setItems ([]);
return $this -> registerDocument ( 'buy_correction' , 'correction' , $document );
return $this -> registerDocument ( 'buy_correction' , 'correction' , $document , $external_id );
}
/**
@@ -364,6 +375,28 @@ class Kkt extends Client
return $response ;
}
/**
* Возвращает текущий токен авторизации
*
* @return string
*/
public function getAuthToken () : ? string
{
return $this -> auth_token ;
}
/**
* Устанавливает заранее известный токен авторизации
*
* @param string|null $auth_token
* @return $this
*/
public function setAuthToken ( ? string $auth_token )
{
$this -> auth_token = $auth_token ;
return $this ;
}
/**
* Сбрасывает настройки К К Т по умолчанию
*/
@@ -390,7 +423,7 @@ class Kkt extends Client
{
$headers [ 'Content-type' ] = 'application/json; charset=utf-8' ;
if ( $this -> getAuthToken ()) {
$headers [ 'Token' ] = $this -> a uth_t oken;
$headers [ 'Token' ] = $this -> getA uthT oken() ;
}
return $headers ;
}
@@ -429,6 +462,7 @@ class Kkt extends Client
* @param mixed $data Данные для передачи
* @param array|null $options Параметры Guzzle
* @return \AtolOnline\Api\KktResponse
* @throws \GuzzleHttp\Exception\GuzzleException
* @see https://guzzle.readthedocs.io/en/latest/request-options.html
*/
protected function sendAtolRequest ( string $http_method , string $api_method , $data = null , array $options = null )
@@ -449,6 +483,7 @@ class Kkt extends Client
* Производит авторизацию на К К Т и получает токен доступа для дальнейших HTTP-запросов
*
* @return bool
* @throws \GuzzleHttp\Exception\GuzzleException
*/
protected function auth ()
{
@@ -468,36 +503,27 @@ class Kkt extends Client
/**
* Отправляет документ на регистрацию
*
* @param string $api_method Метод API
* @param string $type Тип документа: receipt, correction
* @param \AtolOnline\Entities\Document $document Объект документа
* @param string $api_method Метод API
* @param string $type Тип документа: receipt, correction
* @param \AtolOnline\Entities\Document $document Объект документа
* @param string|null $external_id Уникальный код документа (если не указан, то будет создан UUID)
* @return \AtolOnline\Api\KktResponse
* @throws \AtolOnline\Exceptions\AtolWrongDocumentTypeException Некорректный тип документа
* @throws \Exception
* @throws \AtolOnline\Exceptions\AtolWrongDocumentTypeException
* @throws \GuzzleHttp\Exception\Guzzle Exception
*/
protected function registerDocument ( string $api_method , string $type , Document $document )
protected function registerDocument ( string $api_method , string $type , Document $document , ? string $external_id = null )
{
$type = trim ( $type );
if ( ! in_array ( $type , [ 'receipt' , 'correction' ])) {
throw new AtolWrongDocumentTypeException ( $type );
}
$this -> auth ();
$data = [
'timestamp' => date ( 'd.m.y H:i:s' ),
'external_id' => Uuid :: uuid4 () -> toString (),
'service' => [ 'callback_url' => $this -> getCallbackUrl ()],
$type => $document ,
];
$data ['timestamp' ] = date ( 'd.m.y H:i:s' );
$data [ 'external_id' ] = $external_id ? : Uuid :: uuid4 () -> toString ();
$data [ $type ] = $document ;
if ( $this -> getCallbackUrl ()) {
$data [ 'service' ] = [ 'callback_url' => $this -> getCallbackUrl ()];
}
return $this -> sendAtolRequest ( 'POST' , trim ( $api_method ), $data );
}
/**
* Возвращает текущий токен авторизации
*
* @return string
*/
protected function getAuthToken ()
{
return $this -> auth_token ;
}
}