diff --git a/src/AtolOnlineClient/AtolOnlineApi.php b/src/AtolOnlineClient/AtolOnlineApi.php index 10c2325..69502f5 100644 --- a/src/AtolOnlineClient/AtolOnlineApi.php +++ b/src/AtolOnlineClient/AtolOnlineApi.php @@ -12,7 +12,6 @@ use Psr\Log\LoggerInterface; class AtolOnlineApi { public const API_VERSION_V4 = 'v4'; - public const API_VERSION_V3 = 'v3'; public const TOKEN_CACHE_KEY = 'crm_fiscal_atol_online_token'; public const TOKEN_CACHE_TIME = 86400; @@ -94,7 +93,7 @@ class AtolOnlineApi */ public function getVersion(): string { - return $this->connection->version ?: self::API_VERSION_V3; + return $this->connection->version ?: self::API_VERSION_V4; } /** @@ -191,32 +190,19 @@ class AtolOnlineApi if ($response) { $response = json_decode($response->getBody(), true); - if ($this->connection->isVersion4()) { - if ($this->cache && !isset($response['error'])) { - $this->cache->save($this->getTokenCacheKey(), $response['token'], self::TOKEN_CACHE_TIME); - } - - if ($this->logger && isset($response['error'])) { - $this->logger->error($response['error']['code'] . ' '. $response['error']['text']); - } - - if (!isset($response['error'])) { - return $response['token']; - } - - return false; + if ($this->cache && !isset($response['error'])) { + $this->cache->save($this->getTokenCacheKey(), $response['token'], self::TOKEN_CACHE_TIME); } - if (isset($response['code']) && in_array($response['code'], [0, 1], true)) { - if ($this->cache) { - $this->cache->save($this->getTokenCacheKey(), $response['token'], self::TOKEN_CACHE_TIME); - } + if ($this->logger && isset($response['error'])) { + $this->logger->error($response['error']['code'] . ' '. $response['error']['text']); + } + if (!isset($response['error'])) { return $response['token']; } } - return false; } @@ -236,11 +222,7 @@ class AtolOnlineApi return $url; } - if ($this->getVersion() === self::API_VERSION_V4) { - return $url.'?token='.$token; - } - - return $url.'?tokenid='.$token; + return $url.'?token='.$token; } /** diff --git a/src/AtolOnlineClient/Configuration/Connection.php b/src/AtolOnlineClient/Configuration/Connection.php index a150190..9333ae2 100644 --- a/src/AtolOnlineClient/Configuration/Connection.php +++ b/src/AtolOnlineClient/Configuration/Connection.php @@ -25,7 +25,6 @@ class Connection ]; const versions = [ - AtolOnlineApi::API_VERSION_V3, AtolOnlineApi::API_VERSION_V4, ]; @@ -45,7 +44,7 @@ class Connection public $sno; /** @var string */ - public $version = AtolOnlineApi::API_VERSION_V3; + public $version = AtolOnlineApi::API_VERSION_V4; /** @var bool */ public $testMode = false; @@ -53,7 +52,7 @@ class Connection /** * @return bool */ - public function isTestMode() + public function isTestMode(): bool { return $this->testMode; } @@ -61,7 +60,7 @@ class Connection /** * @param bool $testMode */ - public function setTestMode(bool $testMode) + public function setTestMode(bool $testMode): void { $this->testMode = $testMode; } @@ -85,7 +84,7 @@ class Connection /** * @param ClassMetadata $metadata */ - public static function loadValidatorMetadata(ClassMetadata $metadata) + public static function loadValidatorMetadata(ClassMetadata $metadata): void { $metadata->addPropertyConstraint('login', new Assert\NotBlank()); $metadata->addPropertyConstraint('pass', new Assert\NotBlank()); @@ -111,14 +110,6 @@ class Connection return $this->version; } - /** - * @return bool - */ - public function isVersion3() - { - return $this->getVersion() === AtolOnlineApi::API_VERSION_V3; - } - /** * @return bool */ diff --git a/src/AtolOnlineClient/Request/V3/PaymentReceiptRequest.php b/src/AtolOnlineClient/Request/V3/PaymentReceiptRequest.php deleted file mode 100644 index f27cbdf..0000000 --- a/src/AtolOnlineClient/Request/V3/PaymentReceiptRequest.php +++ /dev/null @@ -1,129 +0,0 @@ -timestamp = (new \DateTime())->format('d.m.Y H:i:s'); - } - - /** - * @return string - */ - public function getTimestamp() - { - return $this->timestamp; - } - - /** - * @param string $timestamp - * - * @return $this - */ - public function setTimestamp($timestamp) - { - $this->timestamp = $timestamp; - - return $this; - } - - /** - * @return string - */ - public function getExternalId() - { - return $this->externalId; - } - - /** - * @param string $externalId - * - * @return $this - */ - public function setExternalId($externalId) - { - $this->externalId = $externalId; - - return $this; - } - - /** - * @return \AtolOnlineClient\Request\V3\ServiceRequest - */ - public function getService() - { - return $this->service; - } - - /** - * @param \AtolOnlineClient\Request\V3\ServiceRequest $service - * - * @return $this - */ - public function setService($service) - { - $this->service = $service; - - return $this; - } - - /** - * @return ReceiptRequest - */ - public function getReceipt() - { - return $this->receipt; - } - - /** - * @param ReceiptRequest $receipt - * - * @return $this - */ - public function setReceipt($receipt) - { - $this->receipt = $receipt; - - return $this; - } -} diff --git a/src/AtolOnlineClient/Request/V3/ReceiptAttributesRequest.php b/src/AtolOnlineClient/Request/V3/ReceiptAttributesRequest.php deleted file mode 100644 index 03fc56d..0000000 --- a/src/AtolOnlineClient/Request/V3/ReceiptAttributesRequest.php +++ /dev/null @@ -1,98 +0,0 @@ -email = $email; - if ($email == null) { - $this->email = ''; - } - - $this->phone = $phone; - if ($phone == null) { - $this->phone = ''; - } - } - - /** - * @return string - */ - public function getSno() - { - return $this->sno; - } - - /** - * @param string $sno - * - * @return $this - */ - public function setSno($sno) - { - $this->sno = $sno; - - return $this; - } - - /** - * @return string - */ - public function getEmail() - { - return $this->email; - } - - /** - * @return string - */ - public function getPhone() - { - return $this->phone; - } -} diff --git a/src/AtolOnlineClient/Request/V3/ReceiptItemRequest.php b/src/AtolOnlineClient/Request/V3/ReceiptItemRequest.php deleted file mode 100644 index ed380a8..0000000 --- a/src/AtolOnlineClient/Request/V3/ReceiptItemRequest.php +++ /dev/null @@ -1,163 +0,0 @@ -name; - } - - /** - * @param string $name - * - * @return $this - */ - public function setName($name) - { - $this->name = $name; - - return $this; - } - - /** - * @return float - */ - public function getPrice() - { - return $this->price; - } - - /** - * @param float $price - * - * @return $this - */ - public function setPrice($price) - { - $this->price = $price; - - return $this; - } - - /** - * @return integer - */ - public function getQuantity() - { - return $this->quantity; - } - - /** - * @param int $quantity - * - * @return $this - */ - public function setQuantity($quantity) - { - $this->quantity = $quantity; - - return $this; - } - - /** - * @return float - */ - public function getSum() - { - return $this->sum; - } - - /** - * @param float $sum - * - * @return $this - */ - public function setSum($sum) - { - $this->sum = $sum; - - return $this; - } - - /** - * @return string - */ - public function getTax() - { - return $this->tax; - } - - /** - * @param string $tax - * - * @return $this - */ - public function setTax($tax) - { - $this->tax = $tax; - - return $this; - } -} diff --git a/src/AtolOnlineClient/Request/V3/ReceiptPaymentRequest.php b/src/AtolOnlineClient/Request/V3/ReceiptPaymentRequest.php deleted file mode 100644 index 72e4ecf..0000000 --- a/src/AtolOnlineClient/Request/V3/ReceiptPaymentRequest.php +++ /dev/null @@ -1,59 +0,0 @@ -type = $type; - $this->sum = $sum; - } - - - /** - * @return int - */ - public function getType() - { - return $this->type; - } - - /** - * @return float - */ - public function getSum() - { - return $this->sum; - } -} diff --git a/src/AtolOnlineClient/Request/V3/ReceiptRequest.php b/src/AtolOnlineClient/Request/V3/ReceiptRequest.php deleted file mode 100644 index 3dd6c0b..0000000 --- a/src/AtolOnlineClient/Request/V3/ReceiptRequest.php +++ /dev/null @@ -1,127 +0,0 @@ -") - */ - private $items; - - /** - * @var float - * required - * @Serializer\Groups({"set", "get"}) - * @Serializer\SerializedName("total") - * @Serializer\Type("float") - */ - private $total; - - /** - * @var \AtolOnlineClient\Request\V3\ReceiptPaymentRequest[] - * "minItems": 1, "maxItems": 10, - * required - * @Serializer\Groups({"set", "get"}) - * @Serializer\SerializedName("payments") - * @Serializer\Type("array") - */ - private $payments; - - /** - * @return ReceiptAttributesRequest - */ - public function getAttributes() - { - return $this->attributes; - } - - /** - * @param ReceiptAttributesRequest $attributes - * - * @return $this - */ - public function setAttributes($attributes) - { - $this->attributes = $attributes; - - return $this; - } - - /** - * @return ReceiptItemRequest[] - */ - public function getItems() - { - return $this->items; - } - - /** - * @param ReceiptItemRequest[] $items - * - * @return $this - */ - public function setItems($items) - { - $this->items = $items; - - return $this; - } - - /** - * @return float - */ - public function getTotal() - { - return $this->total; - } - - /** - * @param float $total - * - * @return $this - */ - public function setTotal($total) - { - $this->total = $total; - - return $this; - } - - /** - * @return \AtolOnlineClient\Request\V3\ReceiptPaymentRequest[] - */ - public function getPayments() - { - return $this->payments; - } - - /** - * @param \AtolOnlineClient\Request\V3\ReceiptPaymentRequest[] $payments - * - * @return $this - */ - public function setPayments($payments) - { - $this->payments = $payments; - - return $this; - } -} diff --git a/src/AtolOnlineClient/Request/V3/ServiceRequest.php b/src/AtolOnlineClient/Request/V3/ServiceRequest.php deleted file mode 100644 index 3a90989..0000000 --- a/src/AtolOnlineClient/Request/V3/ServiceRequest.php +++ /dev/null @@ -1,106 +0,0 @@ -inn = $inn; - $this->paymentAddress = $paymentAddress; - } - - /** - * @return string - */ - public function getCallbackUrl() - { - return $this->callbackUrl; - } - - /** - * @param string $callbackUrl - * - * @return $this - */ - public function setCallbackUrl($callbackUrl) - { - $this->callbackUrl = $callbackUrl; - - return $this; - } - - /** - * @return string - */ - public function getInn() - { - return $this->inn; - } - - /** - * @param string $inn - * - * @return $this - */ - public function setInn($inn) - { - $this->inn = $inn; - - return $this; - } - - /** - * @return string - */ - public function getPaymentAddress() - { - return $this->paymentAddress; - } - - /** - * @param string $paymentAddress - * - * @return $this - */ - public function setPaymentAddress($paymentAddress) - { - $this->paymentAddress = $paymentAddress; - - return $this; - } -} diff --git a/src/AtolOnlineClient/Request/V4/ClientReceiptRequest.php b/src/AtolOnlineClient/Request/V4/ClientReceiptRequest.php index 8113eae..422361b 100644 --- a/src/AtolOnlineClient/Request/V4/ClientReceiptRequest.php +++ b/src/AtolOnlineClient/Request/V4/ClientReceiptRequest.php @@ -7,12 +7,12 @@ use JMS\Serializer\Annotation as Serializer; /** * В запросе обязательно должно быть заполнено хотя бы одно из полей: email или phone. * Если заполнены оба поля, ОФД отправит электронный чек только на email. + * @Serializer\AccessType("public_method") */ class ClientReceiptRequest { /** - * @var string - * required + * @var string|null * @Serializer\Groups({"set", "get"}) * @Serializer\SerializedName("email") * @Serializer\Type("string") @@ -20,8 +20,7 @@ class ClientReceiptRequest private $email; /** - * @var string - * required + * @var string|null * @Serializer\Groups({"set", "get"}) * @Serializer\SerializedName("phone") * @Serializer\Type("string") @@ -30,7 +29,7 @@ class ClientReceiptRequest /** - * @var string + * @var string|null * * @Serializer\Groups({"set", "get"}) * @Serializer\SerializedName("name") @@ -39,7 +38,7 @@ class ClientReceiptRequest private $name; /** - * @var string + * @var string|null * * @Serializer\Groups({"set", "get"}) * @Serializer\SerializedName("inn") @@ -47,56 +46,72 @@ class ClientReceiptRequest */ private $inn; - public function __construct($email, $phone) + public function __construct(string $email = null, string $phone = null) { $this->email = $email; $this->phone = $phone; } /** - * @return string + * @return string|null */ - public function getEmail(): string + public function getEmail(): ?string { return $this->email; } /** - * @return string + * @param string|null $email */ - public function getPhone(): string + public function setEmail(?string $email): void + { + $this->email = $email; + } + + /** + * @return string|null + */ + public function getPhone(): ?string { return $this->phone; } /** - * @return string + * @param string|null $phone */ - public function getName(): string + public function setPhone(?string $phone): void + { + $this->phone = $phone; + } + + /** + * @return string|null + */ + public function getName(): ?string { return $this->name; } /** - * @param string $name + * @param string|null $name */ - public function setName(string $name): void + public function setName(?string $name): void { $this->name = $name; } /** - * @return string + * @return string|null */ - public function getInn(): string + public function getInn(): ?string { return $this->inn; } /** - * @param string $inn + * @param string|null $inn */ - public function setInn(string $inn): void + public function setInn(?string $inn): void { $this->inn = $inn; } diff --git a/src/AtolOnlineClient/Request/V4/CompanyReceiptRequest.php b/src/AtolOnlineClient/Request/V4/CompanyReceiptRequest.php index 881e4eb..e9f84f5 100644 --- a/src/AtolOnlineClient/Request/V4/CompanyReceiptRequest.php +++ b/src/AtolOnlineClient/Request/V4/CompanyReceiptRequest.php @@ -4,6 +4,9 @@ namespace AtolOnlineClient\Request\V4; use JMS\Serializer\Annotation as Serializer; +/** + * @Serializer\AccessType("public_method") + */ class CompanyReceiptRequest { /** @@ -16,7 +19,7 @@ class CompanyReceiptRequest private $email; /** - * @var string + * @var string|null * Поле необязательно, если у организации один тип налогообложения * @Serializer\Groups({"set", "get"}) * @Serializer\SerializedName("sno") @@ -59,17 +62,17 @@ class CompanyReceiptRequest } /** - * @return string + * @return string|null */ - public function getSno(): string + public function getSno(): ?string { return $this->sno; } /** - * @param string $sno + * @param string|null $sno */ - public function setSno(string $sno): void + public function setSno(?string $sno): void { $this->sno = $sno; } diff --git a/src/AtolOnlineClient/Request/V4/PaymentReceiptRequest.php b/src/AtolOnlineClient/Request/V4/PaymentReceiptRequest.php index d164044..881fd0b 100644 --- a/src/AtolOnlineClient/Request/V4/PaymentReceiptRequest.php +++ b/src/AtolOnlineClient/Request/V4/PaymentReceiptRequest.php @@ -4,6 +4,9 @@ namespace AtolOnlineClient\Request\V4; use JMS\Serializer\Annotation as Serializer; +/** + * @Serializer\AccessType("public_method") + */ class PaymentReceiptRequest { /** @@ -44,7 +47,6 @@ class PaymentReceiptRequest private $service; - public function __construct() { $this->timestamp = (new \DateTime())->format('d.m.Y H:i:s'); @@ -90,6 +92,14 @@ class PaymentReceiptRequest return $this->timestamp; } + /** + * @param string $timestamp + */ + public function setTimestamp(string $timestamp): void + { + $this->timestamp = $timestamp; + } + /** * @return ServiceRequest */ diff --git a/src/AtolOnlineClient/Request/V4/ReceiptItemRequest.php b/src/AtolOnlineClient/Request/V4/ReceiptItemRequest.php index 7bedb5e..844da91 100644 --- a/src/AtolOnlineClient/Request/V4/ReceiptItemRequest.php +++ b/src/AtolOnlineClient/Request/V4/ReceiptItemRequest.php @@ -4,6 +4,9 @@ namespace AtolOnlineClient\Request\V4; use JMS\Serializer\Annotation as Serializer; +/** + * @Serializer\AccessType("public_method") + */ class ReceiptItemRequest { @@ -68,7 +71,7 @@ class ReceiptItemRequest private $sum; /** - * @var string + * @var string|null * * required * @Serializer\Groups({"set", "get"}) @@ -88,7 +91,7 @@ class ReceiptItemRequest * «partial_payment» – частичный расчет и кредит. Частичная оплата предмета расчета в момент его передачи с последующей оплатой в кредит. * «credit» – передача в кредит. Передача предмета расчета без его оплаты в момент его передачи с последующей оплатой в кредит. * «credit_payment» – оплата кредита. Оплата предмета расчета после его передачи с оплатой в кредит (оплата кредита). - * @var string + * @var string|null * * @Serializer\Groups({"set", "get"}) * @Serializer\SerializedName("payment_method") @@ -113,7 +116,7 @@ class ReceiptItemRequest * «composite» – составной предмет расчета. О предмете расчета, состоящем из предметов, каждому из которых может быть присвоено значение выше перечисленных признаков. * «another» – иной предмет расчета. О предмете расчета, не относящемуся к выше перечисленным предметам расчета * - * @var string + * @var string|null * * @Serializer\Groups({"set", "get"}) * @Serializer\SerializedName("payment_object") @@ -132,7 +135,7 @@ class ReceiptItemRequest private $vat; /** - * @var string + * @var string|null * @Serializer\Groups({"set", "get"}) * @Serializer\SerializedName("nomenclature_code") * @Serializer\Type("string") @@ -204,49 +207,49 @@ class ReceiptItemRequest } /** - * @return string + * @return string|null */ - public function getMeasurementUnit(): string + public function getMeasurementUnit(): ?string { return $this->measurementUnit; } /** - * @param string $measurementUnit + * @param string|null $measurementUnit */ - public function setMeasurementUnit(string $measurementUnit): void + public function setMeasurementUnit(?string $measurementUnit): void { $this->measurementUnit = $measurementUnit; } /** - * @return string + * @return string|null */ - public function getPaymentMethod(): string + public function getPaymentMethod(): ?string { return $this->paymentMethod; } /** - * @param string $paymentMethod + * @param string|null $paymentMethod */ - public function setPaymentMethod(string $paymentMethod): void + public function setPaymentMethod(?string $paymentMethod): void { $this->paymentMethod = $paymentMethod; } /** - * @return string + * @return string|null */ - public function getPaymentObject(): string + public function getPaymentObject(): ?string { return $this->paymentObject; } /** - * @param string $paymentObject + * @param string|null $paymentObject */ - public function setPaymentObject(string $paymentObject): void + public function setPaymentObject(?string $paymentObject): void { $this->paymentObject = $paymentObject; } @@ -268,17 +271,17 @@ class ReceiptItemRequest } /** - * @return string + * @return string|null */ - public function getNomenclatureCode(): string + public function getNomenclatureCode(): ?string { return $this->nomenclatureCode; } /** - * @param string $nomenclatureCode + * @param string|null $nomenclatureCode */ - public function setNomenclatureCode(string $nomenclatureCode): void + public function setNomenclatureCode(?string $nomenclatureCode): void { $this->nomenclatureCode = $nomenclatureCode; } diff --git a/src/AtolOnlineClient/Request/V4/ReceiptPaymentRequest.php b/src/AtolOnlineClient/Request/V4/ReceiptPaymentRequest.php index cbc7314..9ad48fb 100644 --- a/src/AtolOnlineClient/Request/V4/ReceiptPaymentRequest.php +++ b/src/AtolOnlineClient/Request/V4/ReceiptPaymentRequest.php @@ -4,6 +4,9 @@ namespace AtolOnlineClient\Request\V4; use JMS\Serializer\Annotation as Serializer; +/** + * @Serializer\AccessType("public_method") + */ class ReceiptPaymentRequest { const TYPE_ELECTRON = 1; @@ -49,6 +52,14 @@ class ReceiptPaymentRequest return $this->type; } + /** + * @param int $type + */ + public function setType(int $type): void + { + $this->type = $type; + } + /** * @return float */ @@ -56,4 +67,12 @@ class ReceiptPaymentRequest { return $this->sum; } + + /** + * @param float $sum + */ + public function setSum(float $sum): void + { + $this->sum = $sum; + } } diff --git a/src/AtolOnlineClient/Request/V4/ReceiptRequest.php b/src/AtolOnlineClient/Request/V4/ReceiptRequest.php index 50cf51f..32365a6 100644 --- a/src/AtolOnlineClient/Request/V4/ReceiptRequest.php +++ b/src/AtolOnlineClient/Request/V4/ReceiptRequest.php @@ -4,6 +4,9 @@ namespace AtolOnlineClient\Request\V4; use JMS\Serializer\Annotation as Serializer; +/** + * @Serializer\AccessType("public_method") + */ class ReceiptRequest { diff --git a/src/AtolOnlineClient/Request/V4/ServiceRequest.php b/src/AtolOnlineClient/Request/V4/ServiceRequest.php index d1ef881..957c4cb 100644 --- a/src/AtolOnlineClient/Request/V4/ServiceRequest.php +++ b/src/AtolOnlineClient/Request/V4/ServiceRequest.php @@ -4,6 +4,9 @@ namespace AtolOnlineClient\Request\V4; use JMS\Serializer\Annotation as Serializer; +/** + * @Serializer\AccessType("public_method") + */ class ServiceRequest { diff --git a/src/AtolOnlineClient/Request/V4/VatReceiptRequest.php b/src/AtolOnlineClient/Request/V4/VatReceiptRequest.php index e2b4cf8..6b485e5 100644 --- a/src/AtolOnlineClient/Request/V4/VatReceiptRequest.php +++ b/src/AtolOnlineClient/Request/V4/VatReceiptRequest.php @@ -4,6 +4,9 @@ namespace AtolOnlineClient\Request\V4; use JMS\Serializer\Annotation as Serializer; +/** + * @Serializer\AccessType("public_method") + */ class VatReceiptRequest { /** @@ -12,9 +15,9 @@ class VatReceiptRequest * "none", * "vat0", * "vat10", - * "vat18", + * "vat20", * "vat110", - * "vat118" + * "vat120" * ] * * @var string @@ -26,8 +29,8 @@ class VatReceiptRequest private $type; /** - * @var float - * required + * @var float|null + * * @Serializer\Groups({"set", "get"}) * @Serializer\SerializedName("sum") * @Serializer\Type("float") @@ -53,6 +56,14 @@ class VatReceiptRequest return $this->type; } + /** + * @param string $type + */ + public function setType(string $type): void + { + $this->type = $type; + } + /** * @return float */ @@ -60,4 +71,12 @@ class VatReceiptRequest { return $this->sum; } + + /** + * @param float $sum + */ + public function setSum(float $sum): void + { + $this->sum = $sum; + } } diff --git a/tests/AtolOnlineClient/AtolOnlineApiTest.php b/tests/AtolOnlineClient/AtolOnlineApiTest.php index 23b5af7..bad56d4 100644 --- a/tests/AtolOnlineClient/AtolOnlineApiTest.php +++ b/tests/AtolOnlineClient/AtolOnlineApiTest.php @@ -4,10 +4,8 @@ namespace AtolOnlineClient\AtolOnlineClient; use AtolOnlineClient\AtolOnline; use AtolOnlineClient\AtolOnlineApi; +use AtolOnlineClient\AtolOnlineClient\Traits\PaymentReceiptRequestTrait; use AtolOnlineClient\Configuration\Connection; -use AtolOnlineClient\Request\V4\PaymentReceiptRequest; -use AtolOnlineClient\Request\V4\ReceiptRequest; -use AtolOnlineClient\Request\V4\ServiceRequest; use Doctrine\Common\Cache\ArrayCache; use GuzzleHttp\Client; use GuzzleHttp\Exception\BadResponseException; @@ -26,6 +24,8 @@ use Psr\Log\Test\TestLogger; */ class AtolOnlineApiTest extends TestCase { + use PaymentReceiptRequestTrait; + /** * @return void * @covers \AtolOnlineClient\AtolOnlineApi::__construct @@ -40,8 +40,6 @@ class AtolOnlineApiTest extends TestCase $this->assertInstanceOf(AtolOnlineApi::class, $api); - $this->assertSame(AtolOnlineApi::API_VERSION_V3, $api->getVersion()); - $api->setVersion(AtolOnlineApi::API_VERSION_V4); $this->assertSame(AtolOnlineApi::API_VERSION_V4, $api->getVersion()); @@ -63,7 +61,7 @@ class AtolOnlineApiTest extends TestCase new Response(200, [], $this->getReportSuccessReportV4()), ]; - $request = (new AtolOnline())->serializeOperationRequest($this->getPaymentRecepientRequest()); + $request = (new AtolOnline())->serializeOperationRequest($this->getPaymentReceiptRequest()); $response = $this->getApi($responses)->sell($request); @@ -81,7 +79,7 @@ class AtolOnlineApiTest extends TestCase new Response(200, [], $this->getReportSuccessReportV4()), ]; - $request = (new AtolOnline())->serializeOperationRequest($this->getPaymentRecepientRequest()); + $request = (new AtolOnline())->serializeOperationRequest($this->getPaymentReceiptRequest()); $response = $this->getApi($responses)->sellRefund($request); @@ -208,34 +206,6 @@ class AtolOnlineApiTest extends TestCase $this->assertSame('fj45u923j59ju42395iu9423i59243u0', $this->callMethod($api, 'getToken')); } - /** - * @return void - * @covers \AtolOnlineClient\AtolOnlineApi::getToken - */ - public function testGetTokenSuccessResponseV3(): void - { - $api = $this->getApi([new Response(200, [], $this->getTokenSuccessResponseV3())])->setVersion(AtolOnlineApi::API_VERSION_V3); - - $this->assertSame('fj45u923j59ju42395iu9423i59243u0', $this->callMethod($api, 'getToken')); - } - - /** - * @return void - * @covers \AtolOnlineClient\AtolOnlineApi::getToken - */ - public function testGetTokenSuccessResponseV3WithCache(): void - { - $cache = new ArrayCache(); - - $api = $this->getApi([new Response(200, [], $this->getTokenSuccessResponseV3())])->setVersion(AtolOnlineApi::API_VERSION_V3); - $api->setCache($cache); - - $this->assertSame('fj45u923j59ju42395iu9423i59243u0', $this->callMethod($api, 'getToken')); - $this->assertTrue($cache->contains($this->callMethod($api, 'getTokenCacheKey'))); - - $this->assertSame('fj45u923j59ju42395iu9423i59243u0', $this->callMethod($api, 'getToken')); - } - /** * @return void * @covers \AtolOnlineClient\AtolOnlineApi::buildUrl @@ -245,7 +215,6 @@ class AtolOnlineApiTest extends TestCase $args = ['operation' => 'test']; $this->assertSame('https://online.atol.ru/possystem/v4/group/test', $this->callMethod($this->getApi(), 'buildUrl', $args)); - $this->assertSame('https://online.atol.ru/possystem/v3/group/test', $this->callMethod($this->getApi()->setVersion(AtolOnlineApi::API_VERSION_V3), 'buildUrl', $args)); } /** @@ -257,7 +226,6 @@ class AtolOnlineApiTest extends TestCase $args = ['operation' => 'test', 'token' => 'test']; $this->assertSame('https://online.atol.ru/possystem/v4/group/test?token=test', $this->callMethod($this->getApi(), 'buildUrl', $args)); - $this->assertSame('https://online.atol.ru/possystem/v3/group/test?tokenid=test', $this->callMethod($this->getApi()->setVersion(AtolOnlineApi::API_VERSION_V3), 'buildUrl', $args)); } /** @@ -271,7 +239,7 @@ class AtolOnlineApiTest extends TestCase new Response(200, [], $this->getTokenSuccessResponseV4()) ]; - $request = (new AtolOnline())->serializeOperationRequest($this->getPaymentRecepientRequest()); + $request = (new AtolOnline())->serializeOperationRequest($this->getPaymentReceiptRequest()); $response = $this->callMethod($this->getApi($responses), 'sendOperationRequest', [ 'operation' => 'sell', @@ -294,7 +262,7 @@ class AtolOnlineApiTest extends TestCase new Response(200, [], $this->getTokenSuccessResponseV4()), ]; - $request = (new AtolOnline())->serializeOperationRequest($this->getPaymentRecepientRequest()); + $request = (new AtolOnline())->serializeOperationRequest($this->getPaymentReceiptRequest()); $api = $this->getApi($responses); $api->setCache(new ArrayCache()); @@ -338,7 +306,6 @@ class AtolOnlineApiTest extends TestCase public function testGetTokenCacheKey(): void { $this->assertSame('crm_fiscal_atol_online_token_68526766e6751745b52ae70b7bd3c6fe_v4', $this->callMethod($this->getApi(), 'getTokenCacheKey')); - $this->assertSame('crm_fiscal_atol_online_token_68526766e6751745b52ae70b7bd3c6fe_v3', $this->callMethod($this->getApi()->setVersion(AtolOnlineApi::API_VERSION_V3), 'getTokenCacheKey')); } /** @@ -410,17 +377,6 @@ class AtolOnlineApiTest extends TestCase return $property->getValue($object); } - /** - * @return false|string - */ - private function getTokenSuccessResponseV3() - { - return json_encode([ - 'token' => 'fj45u923j59ju42395iu9423i59243u0', - 'code' => 1, - ]); - } - /** * @return false|string */ @@ -456,24 +412,4 @@ class AtolOnlineApiTest extends TestCase { return file_get_contents(__DIR__.'/../fixtures/success_response_v4_3.json'); } - - /** - * @return PaymentReceiptRequest - */ - private function getPaymentRecepientRequest(): PaymentReceiptRequest - { - $service = new ServiceRequest(); - $service->setCallbackUrl('test.local'); - - $receipt = new ReceiptRequest(); - $receipt->setTotal('100'); - - /** @var PaymentReceiptRequest $request */ - $request = new PaymentReceiptRequest(); - $request->setExternalId('test'); - $request->setService($service); - $request->setReceipt($receipt); - - return $request; - } } diff --git a/tests/AtolOnlineClient/AtolOnlineTest.php b/tests/AtolOnlineClient/AtolOnlineTest.php index bbdc577..0199f30 100644 --- a/tests/AtolOnlineClient/AtolOnlineTest.php +++ b/tests/AtolOnlineClient/AtolOnlineTest.php @@ -3,12 +3,10 @@ namespace AtolOnlineClient\Tests; use AtolOnlineClient\AtolOnline; +use AtolOnlineClient\AtolOnlineClient\Traits\PaymentReceiptRequestTrait; use AtolOnlineClient\Configuration\Connection; use AtolOnlineClient\ConfigurationInterface; use AtolOnlineClient\Exception\InvalidResponseException; -use AtolOnlineClient\Request\V4\PaymentReceiptRequest; -use AtolOnlineClient\Request\V4\ReceiptRequest; -use AtolOnlineClient\Request\V4\ServiceRequest; use GuzzleHttp\Client; use JMS\Serializer\Exception\RuntimeException; use JMS\Serializer\SerializerInterface; @@ -16,6 +14,8 @@ use PHPUnit\Framework\TestCase; class AtolOnlineTest extends TestCase { + use PaymentReceiptRequestTrait; + /** * @var AtolOnline */ @@ -127,6 +127,7 @@ class AtolOnlineTest extends TestCase * @param string|null $message * @param string $html * @covers \AtolOnlineClient\AtolOnline::createInvalidResponseException + * @covers \AtolOnlineClient\Exception\InvalidResponseException * @dataProvider dataInvalidResponse */ public function testCreateInvalidResponseException(?int $code, ?string $message, string $html): void @@ -144,30 +145,24 @@ class AtolOnlineTest extends TestCase /** * @covers \AtolOnlineClient\AtolOnline::serializeOperationRequest + * @covers \AtolOnlineClient\Request\V4\ClientReceiptRequest + * @covers \AtolOnlineClient\Request\V4\CompanyReceiptRequest + * @covers \AtolOnlineClient\Request\V4\PaymentReceiptRequest + * @covers \AtolOnlineClient\Request\V4\ReceiptItemRequest + * @covers \AtolOnlineClient\Request\V4\ReceiptPaymentRequest + * @covers \AtolOnlineClient\Request\V4\ReceiptRequest + * @covers \AtolOnlineClient\Request\V4\ServiceRequest + * @covers \AtolOnlineClient\Request\V4\VatReceiptRequest */ public function testSerializeOperationRequest(): void { - $service = new ServiceRequest(); - $service->setCallbackUrl('test.local'); + $request = $this->getPaymentReceiptRequest(); + $request->setTimestamp('17.07.2019 10:14:22'); - $receipt = new ReceiptRequest(); - $receipt->setTotal('100'); - - /** @var PaymentReceiptRequest $paymentReceipt */ - $paymentReceipt = new PaymentReceiptRequest(); - - $reflection = new \ReflectionClass($paymentReceipt); - $property = $reflection->getProperty('timestamp'); - $property->setAccessible(true); - $property->setValue($paymentReceipt, 1); - - $paymentReceipt->setExternalId('test'); - $paymentReceipt->setService($service); - $paymentReceipt->setReceipt($receipt); - - $request = $this->atol->serializeOperationRequest($paymentReceipt); - - $this->assertEquals('{"external_id":"test","receipt":{"total":100},"timestamp":"1","service":{"callback_url":"test.local"}}', $request); + $this->assertEquals( + '{"external_id":"test","receipt":{"client":{"email":"test@test.local"},"company":{"email":"test@test.local","inn":"11111111","payment_address":"address"},"items":[{"name":"test item","price":100,"quantity":1,"sum":100,"measurement_unit":"kg","payment_method":"advance","payment_object":"agent_commission","vat":{"type":"vat20","sum":20},"nomenclature_code":"00"}],"payments":[{"type":0,"sum":100}],"vats":[{"type":"vat20","sum":20}],"total":100},"timestamp":"17.07.2019 10:14:22","service":{"callback_url":"test.local"}}', + $this->atol->serializeOperationRequest($request) + ); } /** @@ -214,7 +209,6 @@ class AtolOnlineTest extends TestCase public function dataErrorResponse(): array { return [ - ['error_response_v3.json'], ['error_response_v4.json'] ]; } diff --git a/tests/AtolOnlineClient/Configuration/ConnectionTest.php b/tests/AtolOnlineClient/Configuration/ConnectionTest.php index 4947d09..674e60b 100644 --- a/tests/AtolOnlineClient/Configuration/ConnectionTest.php +++ b/tests/AtolOnlineClient/Configuration/ConnectionTest.php @@ -52,13 +52,9 @@ class ConnectionTest extends TestCase * @return void * @covers \AtolOnlineClient\Configuration\Connection::getVersion * @covers \AtolOnlineClient\Configuration\Connection::isVersion4 - * @covers \AtolOnlineClient\Configuration\Connection::isVersion3 */ public function testVersion(): void { - $this->assertEquals(AtolOnlineApi::API_VERSION_V3, $this->connection->getVersion()); - $this->assertTrue($this->connection->isVersion3()); - $this->connection->version = AtolOnlineApi::API_VERSION_V4; $this->assertTrue($this->connection->isVersion4()); } diff --git a/tests/AtolOnlineClient/Request/RequestTest.php b/tests/AtolOnlineClient/Request/RequestTest.php new file mode 100644 index 0000000..59ba7db --- /dev/null +++ b/tests/AtolOnlineClient/Request/RequestTest.php @@ -0,0 +1,102 @@ +build(); + + $request = '{ + "external_id": "17052917561851307", + "receipt": { + "client": { + "name": "name", + "email": "kkt@kkt.ru", + "phone": "88002000600", + "inn": "111111111" + }, + "company": { + "email": "chek@romashka.ru", + "sno": "osn", + "inn": "1234567891", + "payment_address": "http://magazin.ru/" + }, + "items": [ + { + "name": "колбаса Клинский Брауншвейгская с/к в/с ", + "price": 1000, + "quantity": 0.3, + "sum": 300, + "measurement_unit": "кг", + "payment_method": "full_payment", + "payment_object": "commodity", + "vat": { + "type": "vat18" + } + }, + { + "name": "яйцоОкскоекуриноеС0 белое", + "price": 100, + "quantity": 1, + "sum": 100, + "measurement_unit": "Упаковка10 шт.", + "payment_method": "full_payment", + "payment_object": "commodity", + "vat": { + "type": "vat10" + } + } + ], + "payments": [ + { + "type": 1, + "sum": 400 + } + ], + "vats": [ + { + "type": "vat18", + "sum": 45.76 + }, + { + "type": "vat10", + "sum": 9.09 + } + ], + "total": 400 + }, + "service": { + "callback_url": "http://testtest" + }, + "timestamp": "01.02.1713:45:00" +}'; + + /** @var PaymentReceiptRequest $request */ + $request = $serializer->deserialize( + $request, + PaymentReceiptRequest::class, + 'json' + ); + + $this->assertSame('17052917561851307', $request->getExternalId()); + } +} diff --git a/tests/AtolOnlineClient/Response/ResponseTest.php b/tests/AtolOnlineClient/Response/ResponseTest.php index 6b71c30..60ee1d9 100644 --- a/tests/AtolOnlineClient/Response/ResponseTest.php +++ b/tests/AtolOnlineClient/Response/ResponseTest.php @@ -47,6 +47,7 @@ class ResponseTest extends TestCase "external_id": "TRF10601_1", "callback_url": "" }'; + /** @var OperationResponse $response */ $response = $serializer->deserialize( $response, OperationResponse::class, @@ -58,6 +59,6 @@ class ResponseTest extends TestCase 'json' ); - $this->assertTrue(true); + $this->assertSame('4355', $response->getUuid()); } } diff --git a/tests/AtolOnlineClient/Traits/PaymentReceiptRequestTrait.php b/tests/AtolOnlineClient/Traits/PaymentReceiptRequestTrait.php new file mode 100644 index 0000000..e3d48fc --- /dev/null +++ b/tests/AtolOnlineClient/Traits/PaymentReceiptRequestTrait.php @@ -0,0 +1,58 @@ +setCallbackUrl('test.local'); + + $client = new ClientReceiptRequest('test@test.local'); + + $company = new CompanyReceiptRequest(); + $company->setEmail('test@test.local'); + $company->setInn('11111111'); + $company->setPaymentAddress('address'); + + $item = new ReceiptItemRequest(); + $item->setName('test item'); + $item->setPrice(100); + $item->setQuantity(1); + $item->setSum(100); + $item->setMeasurementUnit('kg'); + $item->setPaymentMethod(ReceiptItemRequest::PAYMENT_METHOD_ADVANCE); + $item->setPaymentObject(ReceiptItemRequest::PAYMENT_OBJECT_AGENT_COMMISSION); + $item->setVat(new VatReceiptRequest('vat20', 20)); + $item->setNomenclatureCode('00'); + + $receipt = new ReceiptRequest(); + $receipt->setTotal('100'); + $receipt->setClient($client); + $receipt->setCompany($company); + $receipt->setItems([$item]); + $receipt->setPayments([new ReceiptPaymentRequest(0, 100)]); + $receipt->setVats([new VatReceiptRequest('vat20', 20)]); + + /** @var PaymentReceiptRequest $request */ + $request = new PaymentReceiptRequest(); + $request->setExternalId('test'); + $request->setService($service); + $request->setReceipt($receipt); + + return $request; + } +}