8 Commits

7 changed files with 71 additions and 103 deletions

View File

@@ -9,7 +9,8 @@
namespace AtolOnline\Api;
use AtolOnline\{Entities\Document,
use AtolOnline\{Entities\Company,
Entities\Document,
Exceptions\AtolCorrectionInfoException,
Exceptions\AtolInvalidUuidException,
Exceptions\AtolKktLoginEmptyException,
@@ -172,7 +173,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;
}
@@ -390,7 +391,7 @@ class Kkt extends Client
{
$headers['Content-type'] = 'application/json; charset=utf-8';
if ($this->getAuthToken()) {
$headers['Token'] = $this->auth_token;
$headers['Token'] = $this->getAuthToken();
}
return $headers;
}
@@ -429,6 +430,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 +451,7 @@ class Kkt extends Client
* Производит авторизацию на ККТ и получает токен доступа для дальнейших HTTP-запросов
*
* @return bool
* @throws \GuzzleHttp\Exception\GuzzleException
*/
protected function auth()
{
@@ -482,6 +485,12 @@ class Kkt extends Client
throw new AtolWrongDocumentTypeException($type);
}
$this->auth();
if ($this->isTestMode()) {
$document->setCompany((new Company())
->setInn('5544332219')
->setPaymentAddress('https://v4.online.atol.ru')
);
}
$data = [
'timestamp' => date('d.m.y H:i:s'),
'external_id' => Uuid::uuid4()->toString(),
@@ -496,7 +505,7 @@ class Kkt extends Client
*
* @return string
*/
protected function getAuthToken()
public function getAuthToken(): ?string
{
return $this->auth_token;
}

View File

@@ -81,9 +81,9 @@ class KktResponse implements JsonSerializable
/**
* Возвращает объект результата запроса
*
* @return \stdClass
* @return stdClass|null
*/
public function getContent(): stdClass
public function getContent(): ?stdClass
{
return $this->content;
}

View File

@@ -199,9 +199,9 @@ class Document extends Entity
/**
* Возвращает заданного клиента (покупателя)
*
* @return Client
* @return Client|null
*/
public function getClient(): Client
public function getClient(): ?Client
{
return $this->client;
}
@@ -221,9 +221,9 @@ class Document extends Entity
/**
* Возвращает заданную компанию (продавца)
*
* @return Company
* @return Company|null
*/
public function getCompany(): Company
public function getCompany(): ?Company
{
return $this->company;
}
@@ -388,7 +388,7 @@ class Document extends Entity
$doc->payments->add($payment);
}
}
if ($array['total'] != $doc->calcTotal()) {
if (isset($array['total']) && $array['total'] != $doc->calcTotal()) {
throw new AtolException('Real total sum not equals to provided in JSON one');
}
return $doc;
@@ -401,16 +401,24 @@ class Document extends Entity
*/
public function jsonSerialize()
{
if ($this->getCompany()) {
$json['company'] = $this->getCompany()->jsonSerialize(); // обязательно
}
if ($this->getPayments()) {
$json['payments'] = $this->payments->jsonSerialize(); // обязательно
}
if ($this->getCashier()) {
$json['cashier'] = $this->getCashier();
}
if ($this->getCorrectionInfo()) {
$json['correction_info'] = $this->getCorrectionInfo()->jsonSerialize(); // обязательно для коррекционных
} else {
if ($this->getClient()) {
$json['client'] = $this->getClient()->jsonSerialize(); // обязательно для некоррекционных
}
if ($this->getItems()) {
$json['items'] = $this->items->jsonSerialize(); // обязательно для некоррекционных
}
$json['total'] = $this->calcTotal(); // обязательно для некоррекционных
}
if ($this->getVats()) {

View File

@@ -42,7 +42,22 @@ class BasicTestCase extends TestCase
*/
public function tearDown(): void
{
//parent::tearDown();
}
/**
* Возвращает случайную строку указанной длины
*
* @param int $length
* @return string
*/
protected static function randomString($length = 8)
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$string = '';
for ($i = 0; $i < $length; $i++) {
$string .= $characters[mt_rand(0, strlen($characters) - 1)];
}
return $string;
}
}

View File

@@ -85,10 +85,7 @@ class ItemTest extends BasicTestCase
/**
* Тестирует установку ставки НДС разными путями
*
* @throws AtolOnline\Exceptions\AtolNameTooLongException
* @throws AtolOnline\Exceptions\AtolPriceTooHighException
* @throws AtolOnline\Exceptions\AtolTooManyException
* @throws AtolOnline\Exceptions\AtolUnitTooLongException
* @throws \AtolOnline\Exceptions\AtolPriceTooHighException
*/
public function testSetVat()
{
@@ -102,25 +99,21 @@ class ItemTest extends BasicTestCase
/**
* Тестирует исключение о слишком длинном наименовании
*
* @throws AtolOnline\Exceptions\AtolNameTooLongException
* @throws AtolOnline\Exceptions\AtolPriceTooHighException
* @throws AtolOnline\Exceptions\AtolTooManyException
* @throws AtolOnline\Exceptions\AtolUnitTooLongException
* @throws \AtolOnline\Exceptions\AtolNameTooLongException
*/
public function testAtolNameTooLongException()
{
$item = new Item();
$this->expectException(AtolNameTooLongException::class);
$item->setName('Банан Банан Банан Банан Банан Банан Банан Банан Банан Банан Банан Банан');
$item->setName(self::randomString(130));
}
/**
* Тестирует исключение о слишком высоком количестве
*
* @throws AtolOnline\Exceptions\AtolNameTooLongException
* @throws AtolOnline\Exceptions\AtolTooManyException
* @throws AtolOnline\Exceptions\AtolPriceTooHighException
* @throws AtolOnline\Exceptions\AtolUnitTooLongException
* @throws \AtolOnline\Exceptions\AtolPriceTooHighException
* @throws \AtolOnline\Exceptions\AtolTooManyException
* @throws \AtolOnline\Exceptions\AtolUnitTooLongException
*/
public function testAtolQuantityTooHighException()
{
@@ -132,10 +125,7 @@ class ItemTest extends BasicTestCase
/**
* Тестирует исключение о слишком высокой цене
*
* @throws AtolOnline\Exceptions\AtolPriceTooHighException
* @throws AtolOnline\Exceptions\AtolNameTooLongException
* @throws AtolOnline\Exceptions\AtolTooManyException
* @throws AtolOnline\Exceptions\AtolUnitTooLongException
* @throws \AtolOnline\Exceptions\AtolPriceTooHighException
*/
public function testAtolPriceTooHighException()
{
@@ -147,11 +137,7 @@ class ItemTest extends BasicTestCase
/**
* Тестирует исключение о слишком длинных польз. данных
*
* @throws AtolOnline\Exceptions\AtolUserdataTooLongException
* @throws AtolOnline\Exceptions\AtolPriceTooHighException
* @throws AtolOnline\Exceptions\AtolNameTooLongException
* @throws AtolOnline\Exceptions\AtolTooManyException
* @throws AtolOnline\Exceptions\AtolUnitTooLongException
* @throws \AtolOnline\Exceptions\AtolUserdataTooLongException
*/
public function testAtolUserdataTooLongException()
{
@@ -163,10 +149,7 @@ class ItemTest extends BasicTestCase
/**
* Тестирует исключение о слишком длинной единице измерения
*
* @throws AtolOnline\Exceptions\AtolNameTooLongException
* @throws AtolOnline\Exceptions\AtolPriceTooHighException
* @throws AtolOnline\Exceptions\AtolTooManyException
* @throws AtolOnline\Exceptions\AtolUnitTooLongException
* @throws \AtolOnline\Exceptions\AtolUnitTooLongException
*/
public function testAtolUnitTooLongException()
{

View File

@@ -22,12 +22,6 @@ class ClientTest extends BasicTestCase
{
/**
* Тестирует установку параметров
*
* @throws \AtolOnline\Exceptions\AtolEmailTooLongException
* @throws \AtolOnline\Exceptions\AtolEmailValidateException
* @throws \AtolOnline\Exceptions\AtolNameTooLongException
* @throws \AtolOnline\Exceptions\AtolPhoneTooLongException
* @throws \AtolOnline\Exceptions\AtolInnWrongLengthException
*/
public function testConstructor()
{
@@ -48,30 +42,18 @@ class ClientTest extends BasicTestCase
* Тестирует исключение о слишком длинном имени
*
* @throws \AtolOnline\Exceptions\AtolNameTooLongException
* @throws \AtolOnline\Exceptions\AtolEmailTooLongException
* @throws \AtolOnline\Exceptions\AtolEmailValidateException
* @throws \AtolOnline\Exceptions\AtolPhoneTooLongException
* @throws \AtolOnline\Exceptions\AtolInnWrongLengthException
*/
public function testAtolNameTooLongException()
{
$customer = new Client();
$this->expectException(AtolNameTooLongException::class);
$customer->setName('John Doe John Doe John Doe John Doe John Doe '.
'John Doe John Doe John Doe John Doe John Doe John Doe John Doe John '.
'Doe John Doe John Doe John Doe John DoeJohn Doe John Doe John Doe '.
'John Doe John Doe John Doe John Doe John Doe John Doe John Doe John '.
'Doe John Doe John Doe John Doe John Doe John Doe John Doe');
$customer->setName(self::randomString(257));
}
/**
* Тестирует исключение о слишком длинном телефоне
*
* @throws \AtolOnline\Exceptions\AtolPhoneTooLongException
* @throws \AtolOnline\Exceptions\AtolNameTooLongException
* @throws \AtolOnline\Exceptions\AtolEmailTooLongException
* @throws \AtolOnline\Exceptions\AtolEmailValidateException
* @throws \AtolOnline\Exceptions\AtolInnWrongLengthException
*/
public function testAtolPhoneTooLongException()
{
@@ -84,42 +66,32 @@ class ClientTest extends BasicTestCase
* Тестирует исключение о слишком длинной почте
*
* @throws \AtolOnline\Exceptions\AtolEmailTooLongException
* @throws \AtolOnline\Exceptions\AtolPhoneTooLongException
* @throws \AtolOnline\Exceptions\AtolNameTooLongException
* @throws \AtolOnline\Exceptions\AtolEmailValidateException
* @throws \AtolOnline\Exceptions\AtolInnWrongLengthException
*/
public function testAtolEmailTooLongException()
{
$customer = new Client();
$this->expectException(AtolEmailTooLongException::class);
$customer->setEmail('johnjohnjohnjohnjohnjohndoedoedoedoe@exampleexampleexampleexample.com');
$customer->setEmail(self::randomString(65));
}
/**
* Тестирует исключение о некорректной почте
*
* @throws \AtolOnline\Exceptions\AtolEmailValidateException
* @throws \AtolOnline\Exceptions\AtolEmailTooLongException
* @throws \AtolOnline\Exceptions\AtolPhoneTooLongException
* @throws \AtolOnline\Exceptions\AtolNameTooLongException
* @throws \AtolOnline\Exceptions\AtolInnWrongLengthException
* @throws \AtolOnline\Exceptions\AtolEmailValidateException
*/
public function testAtolEmailValidateException()
{
$customer = new Client();
$this->expectException(AtolEmailValidateException::class);
$customer->setEmail('John Doe');
$customer->setEmail(self::randomString(15));
}
/**
* Тестирует исключение о некорректной длине ИНН
*
* @throws \AtolOnline\Exceptions\AtolInnWrongLengthException
* @throws \AtolOnline\Exceptions\AtolEmailTooLongException
* @throws \AtolOnline\Exceptions\AtolEmailValidateException
* @throws \AtolOnline\Exceptions\AtolNameTooLongException
* @throws \AtolOnline\Exceptions\AtolPhoneTooLongException
*/
public function testAtolInnWrongLengthException()
{

View File

@@ -22,11 +22,6 @@ class CompanyTest extends BasicTestCase
{
/**
* Тестирует установку параметров через конструктор
*
* @throws AtolOnline\Exceptions\AtolEmailTooLongException
* @throws AtolOnline\Exceptions\AtolEmailValidateException
* @throws AtolOnline\Exceptions\AtolInnWrongLengthException
* @throws AtolOnline\Exceptions\AtolPaymentAddressTooLongException
*/
public function testConstructor()
{
@@ -46,10 +41,7 @@ class CompanyTest extends BasicTestCase
/**
* Тестирует исключение о некорректной длине ИНН
*
* @throws AtolOnline\Exceptions\AtolInnWrongLengthException
* @throws AtolOnline\Exceptions\AtolEmailTooLongException
* @throws AtolOnline\Exceptions\AtolEmailValidateException
* @throws AtolOnline\Exceptions\AtolPaymentAddressTooLongException
* @throws \AtolOnline\Exceptions\AtolInnWrongLengthException
*/
public function testAtolInnWrongLengthException()
{
@@ -62,49 +54,38 @@ class CompanyTest extends BasicTestCase
/**
* Тестирует исключение о слишком длинном платёжном адресе
*
* @throws AtolOnline\Exceptions\AtolPaymentAddressTooLongException
* @throws AtolOnline\Exceptions\AtolEmailTooLongException
* @throws AtolOnline\Exceptions\AtolEmailValidateException
* @throws AtolOnline\Exceptions\AtolInnWrongLengthException
* @throws \AtolOnline\Exceptions\AtolPaymentAddressTooLongException
*/
public function testAtolPaymentAddressTooLongException()
{
$company = new Company();
$this->expectException(AtolPaymentAddressTooLongException::class);
$company->setPaymentAddress('John Doe John Doe John Doe John Doe '.
'John Doe John Doe John Doe John Doe John Doe John Doe John Doe John Doe John '.
'Doe John Doe John Doe John Doe John DoeJohn Doe John Doe John Doe John Doe '.
'John Doe John Doe John Doe John Doe John Doe John Doe John Doe John Doe John '.
'Doe John Doe John Doe John Doe John Doe');
$company->setPaymentAddress(self::randomString(257));
}
/**
* Тестирует исключение о слишком длинной почте
*
* @throws AtolOnline\Exceptions\AtolEmailTooLongException
* @throws AtolOnline\Exceptions\AtolEmailValidateException
* @throws AtolOnline\Exceptions\AtolInnWrongLengthException
* @throws AtolOnline\Exceptions\AtolPaymentAddressTooLongException
* @throws \AtolOnline\Exceptions\AtolEmailTooLongException
* @throws \AtolOnline\Exceptions\AtolEmailValidateException
*/
public function testAtolEmailTooLongException()
{
$company = new Company();
$this->expectException(AtolEmailTooLongException::class);
$company->setEmail('johnjohnjohnjohnjohnjohndoedoedoedoe@exampleexampleexampleexample.com');
$company->setEmail(self::randomString(65));
}
/**
* Тестирует исключение о некорректной почте
*
* @throws AtolOnline\Exceptions\AtolEmailValidateException
* @throws AtolOnline\Exceptions\AtolEmailTooLongException
* @throws AtolOnline\Exceptions\AtolInnWrongLengthException
* @throws AtolOnline\Exceptions\AtolPaymentAddressTooLongException
* @throws \AtolOnline\Exceptions\AtolEmailTooLongException
* @throws \AtolOnline\Exceptions\AtolEmailValidateException
*/
public function testAtolEmailValidateException()
{
$company = new Company();
$this->expectException(AtolEmailValidateException::class);
$company->setEmail('John Doe');
$company->setEmail(self::randomString(15));
}
}