├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── composer.json ├── phpunit.xml ├── src ├── Associate.php ├── CheckDataFactory.php ├── Client.php ├── Config.php ├── Contracts │ ├── ClientInterface.php │ ├── ModulposCashierInterface.php │ ├── ModulposOrderInterface.php │ ├── ModulposOrderItemInterface.php │ └── ModulposPaymentItemInterface.php ├── Entity │ ├── AbstractEntity.php │ ├── Cashier.php │ ├── Order.php │ ├── OrderItem.php │ └── PaymentItem.php └── Exceptions │ ├── ItemsNotFound.php │ ├── MethodNotFound.php │ ├── PaymentMethodNotAllowed.php │ ├── PaymentObjectNotAllowed.php │ ├── RequiredParameterNotFound.php │ ├── TaxModeNotAllowed.php │ ├── TypeOperationsNotAllowed.php │ └── VatTagNotAllowed.php └── tests ├── AssociateTest.php ├── CheckDataFactoryTest.php ├── ClientTest.php ├── ConfigTest.php ├── Entity ├── CashierTest.php ├── OrderItemTest.php ├── OrderTest.php └── PaymentItemTest.php └── TestCase.php /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | vendor 3 | composer.lock -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | php: 3 | - 7.1 4 | - 7.2 5 | - 7.3 6 | before_install: 7 | - sudo pip install codecov 8 | before_script: 9 | - composer install --dev 10 | script: 11 | - phpunit --coverage-clover=coverage.xml 12 | after_success: 13 | - codecov 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Anton Kartsev 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PHP клиент для API автоматической фискализации чеков интернет-магазинов Модуль.Кассы 2 | [![](https://img.shields.io/packagist/l/bigperson/modulpos-php-api-client.svg?style=flat-square)](https://github.com/bigperson/modulpos-php-api-client/blob/master/LICENSE) 3 | [![](https://img.shields.io/packagist/dt/bigperson/modulpos-php-api-client.svg?style=flat-square)](https://packagist.org/packages/bigperson/modulpos-php-api-client) 4 | [![](https://img.shields.io/packagist/v/bigperson/modulpos-php-api-client.svg?style=flat-square)](https://packagist.org/packages/bigperson/modulpos-php-api-client) 5 | [![](https://img.shields.io/travis/bigperson/modulpos-php-api-client.svg?style=flat-square)](https://travis-ci.org/bigperson/modulpos-php-api-client) 6 | [![](https://img.shields.io/codecov/c/github/bigperson/modulpos-php-api-client.svg?style=flat-square)](https://codecov.io/gh/bigperson/modulpos-php-api-client) 7 | [![StyleCI](https://styleci.io/repos/98306851/shield?branch=master)](https://styleci.io/repos/98306851) 8 | 9 | Пакет предоставляет удобный интерфейс для общения с API Модуль.Кассы для отправки данных чеков в сервис фискализации. 10 | Пакет упрощает разработку модулей интеграции интернет-магазина с севисом фискализации Модуль.Кассы. 11 | 12 | Часть описания дублирует оригинал [документации по API Модуль.Кассы](http://modulkassa.ru/upload/medialibrary/abb/api-avtomaticheskoy-fiskalizatsii-chekov-internet_magazinov-_ver.1.2_.pdf) 13 | 14 | 15 | ## Требования 16 | * php ^7.1 17 | * guzzlehttp/guzzle (или любой клиент следующий интерфейсу `\GuzzleHttp\ClientInterface`) 18 | * ext-json 19 | * curl 20 | 21 | ## Установка 22 | Вы можете установить данный пакет с помощью сomposer: 23 | 24 | ``` 25 | composer require bigperson/modulpos-php-api-client 26 | ``` 27 | 28 | Для PHP 5.4-7.0 можеете использовать предыдущую версию 29 | ``` 30 | composer require bigperson/modulpos-php-api-client 1.0 31 | ``` 32 | 33 | ## Использование 34 | Схема процесса фискализации подробна описана в [документации к API](https://support.modulkassa.ru/upload/medialibrary/abb/API%20%D0%B0%D0%B2%D1%82%D0%BE%D0%BC%D0%B0%D1%82%D0%B8%D1%87%D0%B5%D1%81%D0%BA%D0%BE%D0%B9%20%D1%84%D0%B8%D1%81%D0%BA%D0%B0%D0%BB%D0%B8%D0%B7%D0%B0%D1%86%D0%B8%D0%B8%20%D1%87%D0%B5%D0%BA%D0%BE%D0%B2%20%D0%B8%D0%BD%D1%82%D0%B5%D1%80%D0%BD%D0%B5%D1%82-%D0%BC%D0%B0%D0%B3%D0%B0%D0%B7%D0%B8%D0%BD%D0%BE%D0%B2%20(ver.1.4).pdf). В кратце необходимо связать точку продаж с интернет магазином, настроить отправку данных чеков и проверить статус отправленного чека. 35 | 36 | ### Создания связки аккаунта и розничной точки 37 | Для начала необходимо в личном кабинете Модуль.Кассы создать розничную точку продаж, активировать у неё функцию `Использовать для печати документов интернет-магазина` и получить идентификатор `uuid`. Далее вызываем связку 38 | 39 | ```php 40 | $login = 'test@test.ru'; // Логин от аккаунта Модуль.Кассы 41 | $password = 'password'; // Пароль от аккаунта Модуль.Кассы 42 | $retailPointUuid = 'uuid'; // Идентификатор розничной точки 43 | $testMode = true; // Тестовый режим 44 | $associate = new \Bigperson\ModulposApiClient\Associate($login, $password, $retailPointUuid, $testMode); 45 | $result = $associate->init(); 46 | ``` 47 | 48 | В `$result` получим массив с данным `userName` и `password` которые будут использоватся для дальнейших обращений к API. Их нужно где-нибудь сохранить, например в базе данных. 49 | 50 | ### Отправка данных чека на сервер фискализации (создание документа) 51 | Для начала необходимо сформировать данные самого чека. Для этого достаточно для ваших моделей инплементировать интерфейсы ModulposOrderInterface для заказа, ModulposOrderItemInterface для товара в заказе, ModulposPaymentItemInterface для способа оплаты. Также вы можете использовать entity из пакета, или отнаследовать от них собственные классы переопределив методы на собственные. 52 | ```php 53 | use Bigperson\ModulposApiClient\Entity\Order; 54 | use Bigperson\ModulposApiClient\Entity\Cashier; 55 | use Bigperson\ModulposApiClient\Entity\OrderItem; 56 | use Bigperson\ModulposApiClient\Entity\PaymentItem; 57 | 58 | $dateTime = new \DateTime('NOW'); 59 | // Создаем заказ 60 | $order = Order::create([ 61 | 'documentUuid' => uniqid(), 62 | 'checkoutDateTime' => $dateTime->format(DATE_RFC3339), 63 | 'orderId' => rand(100000, 999999), 64 | 'typeOperation' => 'SALE', 65 | 'customerContact' => 'test@example.com', 66 | ]); 67 | 68 | // Созадем товары 69 | $orderItem1 = OrderItem::create([ 70 | 'price' => 100, 71 | 'quantity' => 1, 72 | 'vatTag' => OrderItem::VAT_NO, 73 | 'name' => 'Test Product1' 74 | ]); 75 | 76 | $orderItem2 = OrderItem::create([ 77 | 'price' => 200, 78 | 'quantity' => 1, 79 | 'vatTag' => OrderItem::VAT_NO, 80 | 'name' => 'Test Product2' 81 | ]); 82 | 83 | //Создаем способ оплаты 84 | $paymentItem = PaymentItem::create([ 85 | 'type' => 'CARD', 86 | 'sum' => 300 87 | ]); 88 | 89 | // Добавляем товары и способ оплаты к заказу 90 | $order->addItem($orderItem1); 91 | $order->addItem($orderItem2); 92 | $order->addPaymentItem($paymentItem); 93 | 94 | //Создаем кассира 95 | $cashier = Cashier::create([ 96 | 'name' => 'Test Cashier', 97 | 'inn' => '123456789012', 98 | 'position' => 'salesman', 99 | ]); 100 | ``` 101 | 102 | Далее объект заказа необходимо передать клиенту, также вы можете передать `responseURL` и печатать ли чек на кассе: 103 | ```php 104 | $login = 'test@test.ru'; // Логин полученный на первом шаге 105 | $password = 'password'; // Пароль полученный на первом шаге 106 | $testMode = true; // Тестовый режим 107 | $client = new \Bigperson\ModulposApiClient\Client($login, $password, $testMode); 108 | $responseUrl = 'https://internet.shop.ru/order/982340931/checkout?completed=1'; 109 | $printReceipt = true; // Печатать ли чек на кассе 110 | $result = $client->sendCheck($order, $responseUrl, $printReceipt, $cashier); 111 | ``` 112 | Все параметры кроме $order - опциональные. Если не передан объект `ModulposCashierInterface` 113 | то будут использованны данные из настроек торговой точки. 114 | 115 | В ответ придет массив со статусом обработки документа и фискального накопителя. 116 | 117 | ### Проверка статуса документа 118 | Если при передаче данных чека был передан `responseURL`, то на него придет результат фискализации, если параметр задан не был, то вы можете самостоятельно проверить статус документа: 119 | ```php 120 | $login = 'test@test.ru'; // Логин полученный на первом шаге 121 | $password = 'password'; // Пароль полученный на первом шаге 122 | $testMode = true; // Тестовый режим 123 | $documentId = 'efbafcdd-113a-45db-8fb9-718b1fdc3524'; // id документа 124 | $client = new \Bigperson\ModulposApiClient\Client($login, $password, $testMode); 125 | $result = $client->getStatusDocumentById($documentId); 126 | ``` 127 | В ответ придет массив со статусом `status`, который может принимать значения: 128 | * QUEUED - документ принят в очередь на обработку; 129 | * PENDING - документ получен кассой для печати; 130 | * PRINTED - фискализирован успешно; 131 | * COMPLETED - результат фискализации отправлен (если было заполнено поле responseURL) в сервис источник; 132 | * FAILED - ошибка при фискализации. 133 | 134 | 135 | Также в массив придет `fnState` - статус фискального накопителя, может принимать значения: 136 | 137 | * ready ​- соединение с фискальным накопителем установлено, состояние позволяет фискализировать чеки 138 | * associated​ - клиент успешно связан с розничной точкой, но касса еще ни разу не вышла на связь и не сообщила свое состояние 139 | * failed ​- Проблемы получения статуса фискального накопителя. Этот статус не препятствует добавлению документов для фискализации. Все документы будут добавлены в очередь на сервере и дождутся момента когда касса будет в состоянии их фискализировать 140 | 141 | Кроме того вы можете вызвать отдельно метод проверки статуса фискального накопителя (сервиса фискализации): 142 | ```php 143 | $client = new \Bigperson\ModulposApiClient\Client($login, $password, $testMode); 144 | $result = $client->getStatusFiscalService(); 145 | ``` 146 | 147 | ## Развитие пакета 148 | С целью активного развития пакета, рекомендуем создавать пулл-реквесты, а не только баг-репорты ([issues](https://github.com/bigperson/modulpos-php-api-client/issues)). 149 | По любым проблемам рекомендуем открывать Баг-репорты с подробным описанием проблемы и последовательностью действия для воспроизведения бага. 150 | 151 | ## Лицензия 152 | [MIT](https://raw.githubusercontent.com/bigperson/modulpos-php-api-client/master/LICENSE) 153 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bigperson/modulpos-php-api-client", 3 | "description": "PHP client for modulpos API Fiscal Service", 4 | "type": "library", 5 | "require": { 6 | "php": "^7.1", 7 | "guzzlehttp/guzzle": "^6.0", 8 | "ext-json": "*" 9 | }, 10 | "require-dev": { 11 | "phpunit/phpunit": "^7.0|^8.0" 12 | }, 13 | "license": "MIT", 14 | "authors": [ 15 | { 16 | "name": "Anton Kartsev", 17 | "email": "anton@alarmcrm.ru" 18 | } 19 | ], 20 | "autoload": { 21 | "psr-4": { 22 | "Bigperson\\ModulposApiClient\\": "src" 23 | } 24 | }, 25 | "autoload-dev": { 26 | "psr-4": { 27 | "Tests\\": "tests/" 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | ./tests 15 | 16 | 17 | 18 | 19 | ./src 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /src/Associate.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | declare(strict_types=1); 11 | 12 | namespace Bigperson\ModulposApiClient; 13 | 14 | use GuzzleHttp\ClientInterface; 15 | 16 | /** 17 | * Class Associate 18 | * Класс 19 | */ 20 | class Associate 21 | { 22 | /** 23 | * URL для запроса на ассоциацию. 24 | */ 25 | const ASSOCIATE_URL = '/v1/associate/'; 26 | 27 | /** 28 | * @var string 29 | */ 30 | private $login; 31 | 32 | /** 33 | * @var string 34 | */ 35 | private $password; 36 | 37 | /** 38 | * @var string 39 | */ 40 | private $retailPointUuid; 41 | 42 | /** 43 | * @var ClientInterface 44 | */ 45 | private $client; 46 | 47 | /** 48 | * @var bool 49 | */ 50 | private $testMode; 51 | 52 | /** 53 | * Associate constructor. 54 | * 55 | * Данные учетной записи Модуль.Касса используются только один раз - для 56 | * создания связки аккаунта и розничной точки. 57 | * 58 | * @param string $login 59 | * @param string $password 60 | * @param string $retailPointUuid 61 | * @param ClientInterface $client 62 | * @param bool $testMode 63 | */ 64 | public function __construct(string $login, string $password, string $retailPointUuid, bool $testMode = false, ?ClientInterface $client = null) 65 | { 66 | $this->login = $login; 67 | $this->password = $password; 68 | $this->retailPointUuid = $retailPointUuid; 69 | $this->testMode = $testMode; 70 | $this->client = $client ?? new \GuzzleHttp\Client(); 71 | } 72 | 73 | /** 74 | * Инициализация (связка) интернет-магазина с розничной точкой. 75 | * 76 | * После вызова метода связки, в ответе 77 | * выдается логин и пароль которые потом нужно использовать для всех обращений а 78 | * API. Данный метод надо вызывать единожды на на интеграцию - полученные учетные 79 | * данные нужно сохранить и вызывать все остальные методы с ними. Повторный вызов 80 | * нужно делать только при смене розничной точки, утери или компрометации 81 | * полученных учетных данных. 82 | * 83 | * @return array $response 84 | */ 85 | public function init() 86 | { 87 | $authParams = ['auth' => [$this->login, $this->password]]; 88 | $response = $this->client->request('POST', $this->getAssociateUrl(), $authParams); 89 | $result = $response->getBody(); 90 | 91 | return json_decode($result->getContents(), true); 92 | } 93 | 94 | /** 95 | * @return string 96 | */ 97 | private function getAssociateUrl() 98 | { 99 | return Config::getBaseUrl($this->testMode).self::ASSOCIATE_URL.$this->retailPointUuid; 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /src/CheckDataFactory.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | declare(strict_types=1); 11 | 12 | namespace Bigperson\ModulposApiClient; 13 | 14 | use Bigperson\ModulposApiClient\Contracts\ModulposCashierInterface; 15 | use Bigperson\ModulposApiClient\Contracts\ModulposOrderInterface; 16 | use Bigperson\ModulposApiClient\Exceptions\ItemsNotFound; 17 | use Bigperson\ModulposApiClient\Exceptions\RequiredParameterNotFound; 18 | 19 | /** 20 | * Class CheckDataFactory. 21 | * 22 | * Фабрика преобразует объект интерфейса ModulposOrderInterface в массив 23 | * который можно использовать для отправки данных в API модулькассы 24 | */ 25 | class CheckDataFactory 26 | { 27 | /** 28 | * @param ModulposOrderInterface $order 29 | * @param null $responseUrl URL для подтверждения успешной фискализации на стороне Интернет-магазина 30 | * @param bool $printReceipt Печатать ли бумажный чек на кассе при фискализации 31 | * @param ModulposCashierInterface $cashier Информация о кассире 32 | * 33 | * @return array 34 | */ 35 | public static function convertToArray( 36 | ModulposOrderInterface $order, 37 | $responseUrl = null, 38 | $printReceipt = false, 39 | $cashier = null 40 | ) { 41 | self::validate($order); 42 | 43 | $checkData = [ 44 | 'id' => $order->getDocumentUuid(), 45 | 'checkoutDateTime' => $order->getCheckoutDateTime(), 46 | 'docNum' => $order->getOrderId(), 47 | 'docType' => $order->getTypeOperation(), 48 | 'printReceipt' => $printReceipt, 49 | 'responseURL' => $responseUrl, 50 | 'email' => $order->getCustomerContact(), 51 | 'taxMode' => $order->getTaxMode(), 52 | ]; 53 | 54 | if ($cashier) { 55 | $checkData['cashierName'] = $cashier->getName(); 56 | $checkData['cashierInn'] = $cashier->getInn(); 57 | $checkData['cashierPosition'] = $cashier->getPosition(); 58 | } 59 | 60 | foreach ($order->getItems() as $item) { 61 | /** @var \Bigperson\ModulposApiClient\Contracts\ModulposOrderItemInterface $item */ 62 | $itemData = [ 63 | 'name' => $item->getName(), 64 | 'price' => $item->getPrice(), 65 | 'quantity' => $item->getQuantity(), 66 | 'vatTag' => $item->getVatTag(), 67 | 'paymentObject' => $item->getPaymentObject(), 68 | 'paymentMethod' => $item->getPaymentMethod(), 69 | ]; 70 | $checkData['inventPositions'][] = $itemData; 71 | } 72 | 73 | foreach ($order->getPaymentItems() as $paymentItem) { 74 | /** @var \Bigperson\ModulposApiClient\Contracts\ModulposPaymentItemInterface $paymentItem */ 75 | $paymentItemData = [ 76 | 'paymentType' => $paymentItem->getType(), 77 | 'sum' => $paymentItem->getSum(), 78 | ]; 79 | 80 | $checkData['moneyPositions'][] = $paymentItemData; 81 | } 82 | 83 | return $checkData; 84 | } 85 | 86 | /** 87 | * @param \Bigperson\ModulposApiClient\Contracts\ModulposOrderInterface $order 88 | * 89 | * @throws \Bigperson\ModulposApiClient\Exceptions\ItemsNotFound 90 | * @throws \Bigperson\ModulposApiClient\Exceptions\RequiredParameterNotFound 91 | */ 92 | private static function validate(ModulposOrderInterface $order) 93 | { 94 | if (!$order->getDocumentUuid()) { 95 | throw new RequiredParameterNotFound('documentUuid is required'); 96 | } 97 | 98 | if (!$order->getCheckoutDateTime()) { 99 | throw new RequiredParameterNotFound('checkoutDateTime is required'); 100 | } 101 | 102 | if (!$order->getOrderId()) { 103 | throw new RequiredParameterNotFound('orderId is required'); 104 | } 105 | 106 | if (!$order->getTypeOperation()) { 107 | throw new RequiredParameterNotFound('typeOperation is required'); 108 | } 109 | 110 | if (!$order->getCustomerContact()) { 111 | throw new RequiredParameterNotFound('customerContact is required'); 112 | } 113 | 114 | $items = $order->getItems(); 115 | 116 | if (empty($items)) { 117 | throw new ItemsNotFound('orderItems is required'); 118 | } 119 | 120 | foreach ($items as $item) { 121 | /** @var \Bigperson\ModulposApiClient\Contracts\ModulposOrderItemInterface $item */ 122 | if (!$item->getPrice()) { 123 | throw new RequiredParameterNotFound('price in orderItem is required'); 124 | } 125 | if (!$item->getVatTag()) { 126 | throw new RequiredParameterNotFound('vatTag in orderItem is required'); 127 | } 128 | if (!$item->getQuantity()) { 129 | throw new RequiredParameterNotFound('quantity in orderItem is required'); 130 | } 131 | if (!$item->getName()) { 132 | throw new RequiredParameterNotFound('name in orderItem is required'); 133 | } 134 | } 135 | 136 | $paymentItems = $order->getPaymentItems(); 137 | 138 | if (empty($paymentItems)) { 139 | throw new ItemsNotFound('paymentItems is required'); 140 | } 141 | 142 | foreach ($paymentItems as $paymentItem) { 143 | /** @var \Bigperson\ModulposApiClient\Contracts\ModulposPaymentItemInterface $paymentItem */ 144 | if (!$paymentItem->getType()) { 145 | throw new RequiredParameterNotFound('type in paymentItem is required'); 146 | } 147 | if (!$paymentItem->getSum()) { 148 | throw new RequiredParameterNotFound('sum in paymentItem is required'); 149 | } 150 | } 151 | } 152 | } 153 | -------------------------------------------------------------------------------- /src/Client.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | declare(strict_types=1); 11 | 12 | namespace Bigperson\ModulposApiClient; 13 | 14 | use Bigperson\ModulposApiClient\Contracts\ClientInterface; 15 | use Bigperson\ModulposApiClient\Contracts\ModulposCashierInterface; 16 | use Bigperson\ModulposApiClient\Contracts\ModulposOrderInterface; 17 | 18 | /** 19 | * Class Client. 20 | */ 21 | class Client implements ClientInterface 22 | { 23 | const STATUS_URI = '/v1/status'; 24 | const SEND_CHECK_DATA_URI = '/v1/doc'; 25 | /** 26 | * @var string 27 | */ 28 | private $login; 29 | 30 | /** 31 | * @var string 32 | */ 33 | private $password; 34 | 35 | /** 36 | * @var bool 37 | */ 38 | private $testMode; 39 | 40 | /** 41 | * @var \GuzzleHttp\Client|\GuzzleHttp\ClientInterface 42 | */ 43 | private $client; 44 | 45 | /** 46 | * Client constructor. 47 | * 48 | * В конструктор необходимо передать данные полученные 49 | * в результате ассоциализации интернет-магазина 50 | * с розничной точкой 51 | * 52 | * @param string $login 53 | * @param string $password 54 | * @param bool $testMode 55 | * @param \GuzzleHttp\ClientInterface $client 56 | * 57 | * @see \Bigperson\ModulposApiClient\Associate::init() 58 | */ 59 | public function __construct(string $login, string $password, bool $testMode = false, \GuzzleHttp\ClientInterface $client = null) 60 | { 61 | $this->login = $login; 62 | $this->password = $password; 63 | $this->testMode = $testMode; 64 | $this->client = $client ?? new \GuzzleHttp\Client(); 65 | } 66 | 67 | /** 68 | * Опрос готовности сервиса фискализации. 69 | * 70 | * @return array ['status', 'statusDateTime'] 71 | */ 72 | public function getStatusFiscalService() 73 | { 74 | $url = Config::getBaseUrl($this->testMode).self::STATUS_URI; 75 | 76 | return $this->send('GET', $url); 77 | } 78 | 79 | /** 80 | * Отправка данных чека на сервер фискализации (создание документа). 81 | * 82 | * @see https://support.modulkassa.ru/upload/medialibrary/abb/API%20%D0%B0%D0%B2%D1%82%D0%BE%D0%BC%D0%B0%D1%82%D0%B8%D1%87%D0%B5%D1%81%D0%BA%D0%BE%D0%B9%20%D1%84%D0%B8%D1%81%D0%BA%D0%B0%D0%BB%D0%B8%D0%B7%D0%B0%D1%86%D0%B8%D0%B8%20%D1%87%D0%B5%D0%BA%D0%BE%D0%B2%20%D0%B8%D0%BD%D1%82%D0%B5%D1%80%D0%BD%D0%B5%D1%82-%D0%BC%D0%B0%D0%B3%D0%B0%D0%B7%D0%B8%D0%BD%D0%BE%D0%B2%20(ver.1.4).pdf 83 | * 84 | * @param ModulposOrderInterface $order 85 | * @param null $responseUrl 86 | * @param bool $printReceipt 87 | * @param ModulposCashierInterface|null $cashier 88 | * 89 | * @return array|bool|float|int|string 90 | */ 91 | public function sendCheck(ModulposOrderInterface $order, $responseUrl = null, $printReceipt = false, ModulposCashierInterface $cashier = null) 92 | { 93 | $url = Config::getBaseUrl($this->testMode).self::SEND_CHECK_DATA_URI; 94 | $checkData = CheckDataFactory::convertToArray($order, $responseUrl, $printReceipt, $cashier); 95 | 96 | return $this->send('POST', $url, $checkData); 97 | } 98 | 99 | /** 100 | * Проверка статуса документа. 101 | * 102 | * @param $documentId 103 | * 104 | * @return array 105 | */ 106 | public function getStatusDocumentById($documentId) 107 | { 108 | $url = Config::getBaseUrl($this->testMode).self::SEND_CHECK_DATA_URI.'/'.$documentId.'/status'; 109 | 110 | return $this->send('GET', $url); 111 | } 112 | 113 | /** 114 | * @param string $method 115 | * @param string $url 116 | * @param array $data 117 | * 118 | * @return array 119 | */ 120 | private function send(string $method, string $url, array $data = []): array 121 | { 122 | $authParams = ['auth' => [$this->login, $this->password], 'json' => $data]; 123 | $response = $this->client->request($method, $url, $authParams); 124 | 125 | return json_decode($response->getBody()->getContents(), true); 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /src/Config.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | declare(strict_types=1); 11 | 12 | namespace Bigperson\ModulposApiClient; 13 | 14 | /** 15 | * Class Config. 16 | */ 17 | class Config 18 | { 19 | /** 20 | * Базовый URL сервиса. 21 | */ 22 | const BASE_URI = 'https://my.modulkassa.ru/api/fn'; 23 | 24 | /** 25 | * Базовый URL сервиса для тестирования. 26 | */ 27 | const BASE_TEST_URI = 'https://demo-fn.avanpos.com/fn'; 28 | 29 | /** 30 | * Возвращает базовый URL. 31 | * 32 | * @param bool $testMode 33 | * 34 | * @return string 35 | */ 36 | public static function getBaseUrl($testMode = false) 37 | { 38 | if ($testMode) { 39 | return self::BASE_TEST_URI; 40 | } 41 | 42 | return self::BASE_URI; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/Contracts/ClientInterface.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | declare(strict_types=1); 11 | 12 | namespace Bigperson\ModulposApiClient\Contracts; 13 | 14 | /** 15 | * Interface ClientInterface. 16 | */ 17 | interface ClientInterface 18 | { 19 | /** 20 | * Опрос готовности сервиса фискализации. 21 | * 22 | * @return array ['status', 'statusDateTime'] 23 | */ 24 | public function getStatusFiscalService(); 25 | 26 | /** 27 | * Отправка данных чека на сервер фискализации (создание документа). 28 | * 29 | * @see http://modulkassa.ru/upload/medialibrary/abb/api-avtomaticheskoy-fiskalizatsii-chekov-internet_magazinov-_ver.1.2_.pdf 30 | * 31 | * @param ModulposOrderInterface $order 32 | * 33 | * @return array 34 | */ 35 | public function sendCheck(ModulposOrderInterface $order); 36 | 37 | /** 38 | * Проверка статуса документа. 39 | * 40 | * @param $documentId 41 | * 42 | * @return array 43 | */ 44 | public function getStatusDocumentById($documentId); 45 | } 46 | -------------------------------------------------------------------------------- /src/Contracts/ModulposCashierInterface.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | declare(strict_types=1); 11 | 12 | namespace Bigperson\ModulposApiClient\Contracts; 13 | 14 | /** 15 | * Interface ModulposOrderItemInterface. 16 | */ 17 | interface ModulposCashierInterface 18 | { 19 | /** 20 | * Имя кассира. 21 | * 22 | * @return string|null 23 | */ 24 | public function getName(); 25 | 26 | /** 27 | * ИНН кассира (используется валидатор ИНН). 28 | * 29 | * @return float 30 | */ 31 | public function getInn(); 32 | 33 | /** 34 | * Должность кассира (до 100 символов). 35 | * 36 | * @return int 37 | */ 38 | public function getPosition(); 39 | } 40 | -------------------------------------------------------------------------------- /src/Contracts/ModulposOrderInterface.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | declare(strict_types=1); 11 | 12 | namespace Bigperson\ModulposApiClient\Contracts; 13 | 14 | /** 15 | * Interface ModulposOrderInterface. 16 | */ 17 | interface ModulposOrderInterface 18 | { 19 | /** 20 | * Уникальный номер документа чека 21 | * Определяется на отправляющей стороне, для исключения ситуации 22 | * с дублированием документов в случае потери ответа сервера. 23 | * Id должен быть достаточно большой строкой из букв и чисел 24 | * для сведения к минимуму возможности коллизий. 25 | * 26 | * @return string 27 | */ 28 | public function getDocumentUuid(); 29 | 30 | /** 31 | * Уникальный номер заказа в системе магазина. 32 | * 33 | * @return string 34 | */ 35 | public function getOrderId(); 36 | 37 | /** 38 | * Адрес электронной почты или номер телефона плательщика. 39 | * 40 | * @return string 41 | */ 42 | public function getCustomerContact(); 43 | 44 | /** 45 | * Тип операции 46 | * SALE - продажа 47 | * RETURN - возврат 48 | * 49 | * @return string 50 | */ 51 | public function getTypeOperation(); 52 | 53 | /** 54 | * Товары/услуги в чеке. 55 | * 56 | * @return array|ModulposOrderItemInterface[] массив объектов ModulposOrderItem 57 | */ 58 | public function getItems(); 59 | 60 | /** 61 | * Способы оплаты в чеке. 62 | * 63 | * @return array|ModulposPaymentItemInterface[] массив объектов ModulposPaymentItem 64 | */ 65 | public function getPaymentItems(); 66 | 67 | /** 68 | * Дата и время оплаты в формате DATE_RFC3339. 69 | * 70 | * @return string 71 | */ 72 | public function getCheckoutDateTime(); 73 | 74 | /** 75 | * Применяемая система налогообложения. 76 | * 77 | * @return string 78 | */ 79 | public function getTaxMode(); 80 | } 81 | -------------------------------------------------------------------------------- /src/Contracts/ModulposOrderItemInterface.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | declare(strict_types=1); 11 | 12 | namespace Bigperson\ModulposApiClient\Contracts; 13 | 14 | /** 15 | * Interface ModulposOrderItemInterface. 16 | */ 17 | interface ModulposOrderItemInterface 18 | { 19 | const VAT_NO = 1105; // НДС не облагается 20 | const VAT_0 = 1104; // НДС 0% 21 | const VAT_10 = 1103; // НДС 10% 22 | const VAT_18 = 1102; // НДС 18% (с 1-го января 2019 - 20%), сохранено для обратной совместимости 23 | const VAT_20 = 1102; // НДС 20% 24 | const VAT_10_110 = 1107; // НДС с рассч. ставкой 10% 25 | const VAT_18_118 = 1106; // НДС с рассч. ставкой 18% (с 1-го января 2019 - 20%), сохранено для обратной совместимости 26 | const VAT_20_120 = 1106; // НДС с рассч. ставкой 20% 27 | 28 | /** 29 | * Цена товара с учётом всех скидок и наценок. 30 | * 31 | * @return float 32 | */ 33 | public function getPrice(); 34 | 35 | /** 36 | * Количество товара. 37 | * 38 | * @return float 39 | */ 40 | public function getQuantity(); 41 | 42 | /** 43 | * Ставка НДС. 44 | * 45 | * См. константы VAT_*. 46 | * 47 | * @return int 48 | */ 49 | public function getVatTag(); 50 | 51 | /** 52 | * Название товара. 128 символов в UTF-8. 53 | * 54 | * @return string 55 | */ 56 | public function getName(); 57 | 58 | /** 59 | * Предмет расчета. 60 | * 61 | * @return string 62 | */ 63 | public function getPaymentObject(); 64 | 65 | /** 66 | * Признак расчета. 67 | * 68 | * @return string 69 | */ 70 | public function getPaymentMethod(); 71 | } 72 | -------------------------------------------------------------------------------- /src/Contracts/ModulposPaymentItemInterface.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | declare(strict_types=1); 11 | 12 | namespace Bigperson\ModulposApiClient\Contracts; 13 | 14 | /** 15 | * Interface ModulposPaymentItemInterface. 16 | */ 17 | interface ModulposPaymentItemInterface 18 | { 19 | /** 20 | * Тип оплаты 21 | * CARD - безналичная оплата 22 | * CASH - оплата наличными. 23 | * 24 | * @return string 25 | */ 26 | public function getType(); 27 | 28 | /** 29 | * Сумма выбранного типа оплаты. 30 | * 31 | * @return float 32 | */ 33 | public function getSum(); 34 | } 35 | -------------------------------------------------------------------------------- /src/Entity/AbstractEntity.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | declare(strict_types=1); 11 | 12 | namespace Bigperson\ModulposApiClient\Entity; 13 | 14 | use Bigperson\ModulposApiClient\Exceptions\MethodNotFound; 15 | 16 | /** 17 | * Class AbstractEntity. 18 | */ 19 | abstract class AbstractEntity 20 | { 21 | /** 22 | * @param array $params 23 | * 24 | * @throws \Bigperson\ModulposApiClient\Exceptions\MethodNotFound 25 | * 26 | * @return static 27 | */ 28 | public static function create(array $params) 29 | { 30 | $item = new static(); 31 | foreach ($params as $key => $param) { 32 | $methodName = 'set'.$key; 33 | if (method_exists($item, $methodName)) { 34 | $item->$methodName($param); 35 | } else { 36 | throw new MethodNotFound("Method is $methodName not found"); 37 | } 38 | } 39 | 40 | return $item; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/Entity/Cashier.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | declare(strict_types=1); 11 | 12 | namespace Bigperson\ModulposApiClient\Entity; 13 | 14 | use Bigperson\ModulposApiClient\Contracts\ModulposCashierInterface; 15 | 16 | /** 17 | * Class Order. 18 | */ 19 | class Cashier extends AbstractEntity implements ModulposCashierInterface 20 | { 21 | /** 22 | * @var string|null 23 | */ 24 | protected $name; 25 | 26 | /** 27 | * @var string|null 28 | */ 29 | protected $inn; 30 | 31 | /** 32 | * @var string|null 33 | */ 34 | protected $position; 35 | 36 | /** 37 | * Имя кассира. 38 | * 39 | * @return string|null 40 | */ 41 | public function getName() 42 | { 43 | return $this->name; 44 | } 45 | 46 | /** 47 | * ИНН кассира (используется валидатор ИНН). 48 | * 49 | * @return string|null 50 | */ 51 | public function getInn() 52 | { 53 | return $this->inn; 54 | } 55 | 56 | /** 57 | * Должность кассира (до 100 символов). 58 | * 59 | * @return string|null 60 | */ 61 | public function getPosition() 62 | { 63 | return $this->position; 64 | } 65 | 66 | /** 67 | * @param null|string $name 68 | * 69 | * @return $this 70 | */ 71 | public function setName($name) 72 | { 73 | $this->name = $name; 74 | 75 | return $this; 76 | } 77 | 78 | /** 79 | * @param null|string $inn 80 | * 81 | * @return $this 82 | */ 83 | public function setInn($inn) 84 | { 85 | $this->inn = $inn; 86 | 87 | return $this; 88 | } 89 | 90 | /** 91 | * @param $position 92 | * 93 | * @return $this 94 | */ 95 | public function setPosition($position) 96 | { 97 | $this->position = $position; 98 | 99 | return $this; 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /src/Entity/Order.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | declare(strict_types=1); 11 | 12 | namespace Bigperson\ModulposApiClient\Entity; 13 | 14 | use Bigperson\ModulposApiClient\Contracts\ModulposOrderInterface; 15 | use Bigperson\ModulposApiClient\Exceptions\TaxModeNotAllowed; 16 | use Bigperson\ModulposApiClient\Exceptions\TypeOperationsNotAllowed; 17 | 18 | /** 19 | * Class Order. 20 | */ 21 | class Order extends AbstractEntity implements ModulposOrderInterface 22 | { 23 | protected $allowedTypeOperations = [ 24 | 'SALE', 25 | 'RETURN', 26 | ]; 27 | 28 | /** 29 | * @var array 30 | */ 31 | protected $allowedTaxModes = [ 32 | 'COMMON', 33 | 'SIMPLIFIED', 34 | 'SIMPLIFIED_WITH_EXPENSE', 35 | 'ENVD', 36 | 'PATENT', 37 | 'COMMON_AGRICULTURAL', 38 | ]; 39 | 40 | /** 41 | * @var string 42 | */ 43 | protected $documentUuid; 44 | 45 | /** 46 | * @var string 47 | */ 48 | protected $orderId; 49 | 50 | /** 51 | * @var string 52 | */ 53 | protected $customerContact; 54 | 55 | /** 56 | * @var string 57 | */ 58 | protected $typeOperation; 59 | 60 | /** 61 | * @var iterable ModulposOrderItemInterface 62 | */ 63 | protected $items; 64 | 65 | /** 66 | * @var iterable ModulposPaymentItemInterface 67 | */ 68 | protected $paymentItems; 69 | 70 | /** 71 | * @var string 72 | */ 73 | protected $checkoutDateTime; 74 | 75 | /** 76 | * @var string 77 | */ 78 | protected $taxMode; 79 | 80 | /** 81 | * @return string 82 | */ 83 | public function getDocumentUuid() 84 | { 85 | return $this->documentUuid; 86 | } 87 | 88 | /** 89 | * @param string $documentUuid 90 | */ 91 | public function setDocumentUuid($documentUuid) 92 | { 93 | $this->documentUuid = $documentUuid; 94 | } 95 | 96 | /** 97 | * @return string 98 | */ 99 | public function getOrderId() 100 | { 101 | return $this->orderId; 102 | } 103 | 104 | /** 105 | * @param string $orderId 106 | */ 107 | public function setOrderId($orderId) 108 | { 109 | $this->orderId = $orderId; 110 | } 111 | 112 | /** 113 | * @return string 114 | */ 115 | public function getCustomerContact() 116 | { 117 | return $this->customerContact; 118 | } 119 | 120 | /** 121 | * @param string $customerContact 122 | */ 123 | public function setCustomerContact($customerContact) 124 | { 125 | $this->customerContact = $customerContact; 126 | } 127 | 128 | /** 129 | * @return string 130 | */ 131 | public function getTypeOperation() 132 | { 133 | return $this->typeOperation; 134 | } 135 | 136 | /** 137 | * @param string $typeOperation 138 | * 139 | * @throws \Bigperson\ModulposApiClient\Exceptions\TypeOperationsNotAllowed 140 | */ 141 | public function setTypeOperation($typeOperation) 142 | { 143 | if (!in_array($typeOperation, $this->allowedTypeOperations)) { 144 | throw new TypeOperationsNotAllowed("$typeOperation is not allowed"); 145 | } 146 | 147 | $this->typeOperation = $typeOperation; 148 | } 149 | 150 | /** 151 | * @return iterable 152 | */ 153 | public function getItems() 154 | { 155 | return $this->items; 156 | } 157 | 158 | /** 159 | * @param OrderItem $item 160 | */ 161 | public function addItem(OrderItem $item) 162 | { 163 | $this->items[] = $item; 164 | } 165 | 166 | /** 167 | * @return iterable 168 | */ 169 | public function getPaymentItems() 170 | { 171 | return $this->paymentItems; 172 | } 173 | 174 | /** 175 | * @param PaymentItem $paymentItem 176 | */ 177 | public function addPaymentItem($paymentItem) 178 | { 179 | $this->paymentItems[] = $paymentItem; 180 | } 181 | 182 | /** 183 | * @return string 184 | */ 185 | public function getCheckoutDateTime() 186 | { 187 | return $this->checkoutDateTime; 188 | } 189 | 190 | /** 191 | * @param string $checkoutDateTime 192 | */ 193 | public function setCheckoutDateTime($checkoutDateTime) 194 | { 195 | $this->checkoutDateTime = $checkoutDateTime; 196 | } 197 | 198 | /** 199 | * @return string 200 | */ 201 | public function getTaxMode() 202 | { 203 | return $this->taxMode; 204 | } 205 | 206 | /** 207 | * @param string $taxMode 208 | */ 209 | public function setTaxMode($taxMode) 210 | { 211 | if (!in_array($taxMode, $this->allowedTaxModes)) { 212 | throw new TaxModeNotAllowed("$taxMode is not allowed"); 213 | } 214 | $this->taxMode = $taxMode; 215 | } 216 | } 217 | -------------------------------------------------------------------------------- /src/Entity/OrderItem.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | declare(strict_types=1); 11 | 12 | namespace Bigperson\ModulposApiClient\Entity; 13 | 14 | use Bigperson\ModulposApiClient\Contracts\ModulposOrderItemInterface; 15 | use Bigperson\ModulposApiClient\Exceptions\PaymentMethodNotAllowed; 16 | use Bigperson\ModulposApiClient\Exceptions\PaymentObjectNotAllowed; 17 | use Bigperson\ModulposApiClient\Exceptions\VatTagNotAllowed; 18 | 19 | /** 20 | * Class OrderItem. 21 | */ 22 | class OrderItem extends AbstractEntity implements ModulposOrderItemInterface 23 | { 24 | /** 25 | * @var float 26 | */ 27 | protected $price; 28 | 29 | /** 30 | * @var int 31 | */ 32 | protected $quantity; 33 | 34 | /** 35 | * @var string 36 | */ 37 | protected $vatTag; 38 | 39 | /** 40 | * @var string 41 | */ 42 | protected $name; 43 | 44 | /** 45 | * @var string 46 | */ 47 | protected $paymentObject = 'commodity'; 48 | 49 | /** 50 | * @var string 51 | */ 52 | protected $paymentMethod = 'full_payment'; 53 | 54 | /** 55 | * @var array 56 | */ 57 | protected $allowedVatTags = [ 58 | self::VAT_NO, 59 | self::VAT_0, 60 | self::VAT_10, 61 | self::VAT_18, 62 | self::VAT_20, 63 | self::VAT_10_110, 64 | self::VAT_18_118, 65 | self::VAT_20_120, 66 | ]; 67 | 68 | /** 69 | * @var array 70 | */ 71 | protected $allowedPaymentObject = [ 72 | 'commodity', 73 | 'excise', 74 | 'job', 75 | 'service', 76 | 'gambling_bet', 77 | 'gambling_prize', 78 | 'lottery', 79 | 'lottery_prize', 80 | 'intellectual_activity', 81 | 'payment', 82 | 'agent_commission', 83 | 'composite', 84 | 'another', 85 | ]; 86 | 87 | /** 88 | * @var array 89 | */ 90 | protected $allowedPaymentMethod = [ 91 | 'full_prepayment', 92 | 'prepayment', 93 | 'advance', 94 | 'full_payment', 95 | 'partial_payment', 96 | 'credit', 97 | 'credit_payment', 98 | ]; 99 | 100 | /** 101 | * @return float 102 | */ 103 | public function getPrice() 104 | { 105 | return $this->price; 106 | } 107 | 108 | /** 109 | * @param float $price 110 | */ 111 | public function setPrice($price) 112 | { 113 | $this->price = $price; 114 | } 115 | 116 | /** 117 | * @return int 118 | */ 119 | public function getQuantity() 120 | { 121 | return $this->quantity; 122 | } 123 | 124 | /** 125 | * @param int $quantity 126 | */ 127 | public function setQuantity($quantity) 128 | { 129 | $this->quantity = $quantity; 130 | } 131 | 132 | /** 133 | * @return string 134 | */ 135 | public function getVatTag() 136 | { 137 | return $this->vatTag; 138 | } 139 | 140 | /** 141 | * @param int $vatTag 142 | * 143 | * @throws \Bigperson\ModulposApiClient\Exceptions\VatTagNotAllowed 144 | */ 145 | public function setVatTag($vatTag) 146 | { 147 | if (!in_array($vatTag, $this->allowedVatTags)) { 148 | throw new VatTagNotAllowed("$vatTag is not allowed"); 149 | } 150 | 151 | $this->vatTag = $vatTag; 152 | } 153 | 154 | /** 155 | * @return string 156 | */ 157 | public function getName() 158 | { 159 | return $this->name; 160 | } 161 | 162 | /** 163 | * @param string $name 164 | */ 165 | public function setName($name) 166 | { 167 | $this->name = $name; 168 | } 169 | 170 | /** 171 | * @return string 172 | */ 173 | public function getPaymentObject() 174 | { 175 | return $this->paymentObject; 176 | } 177 | 178 | /** 179 | * @param $paymentObject 180 | * 181 | * @throws PaymentObjectNotAllowed 182 | */ 183 | public function setPaymentObject($paymentObject) 184 | { 185 | if (!in_array($paymentObject, $this->allowedPaymentObject)) { 186 | throw new PaymentObjectNotAllowed("$paymentObject is not allowed"); 187 | } 188 | 189 | $this->paymentObject = $paymentObject; 190 | } 191 | 192 | /** 193 | * @return string 194 | */ 195 | public function getPaymentMethod() 196 | { 197 | return $this->paymentMethod; 198 | } 199 | 200 | /** 201 | * @param $paymentMethod 202 | * 203 | * @throws PaymentMethodNotAllowed 204 | */ 205 | public function setPaymentMethod($paymentMethod) 206 | { 207 | if (!in_array($paymentMethod, $this->allowedPaymentMethod)) { 208 | throw new PaymentMethodNotAllowed("$paymentMethod is not allowed"); 209 | } 210 | 211 | $this->paymentMethod = $paymentMethod; 212 | } 213 | } 214 | -------------------------------------------------------------------------------- /src/Entity/PaymentItem.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | declare(strict_types=1); 11 | 12 | namespace Bigperson\ModulposApiClient\Entity; 13 | 14 | use Bigperson\ModulposApiClient\Contracts\ModulposPaymentItemInterface; 15 | use Bigperson\ModulposApiClient\Exceptions\TypeOperationsNotAllowed; 16 | 17 | /** 18 | * Class PaymentItem. 19 | */ 20 | class PaymentItem extends AbstractEntity implements ModulposPaymentItemInterface 21 | { 22 | /** 23 | * @var array 24 | */ 25 | protected $allowedTypes = [ 26 | 'CARD', 27 | 'CASH', 28 | ]; 29 | 30 | /** 31 | * @var string 32 | */ 33 | protected $type; 34 | 35 | /** 36 | * @var float 37 | */ 38 | protected $sum; 39 | 40 | /** 41 | * @return string 42 | */ 43 | public function getType() 44 | { 45 | return $this->type; 46 | } 47 | 48 | /** 49 | * @param string $type 50 | * 51 | * @throws \Bigperson\ModulposApiClient\Exceptions\TypeOperationsNotAllowed 52 | */ 53 | public function setType($type) 54 | { 55 | if (!in_array($type, $this->allowedTypes)) { 56 | throw new TypeOperationsNotAllowed("$type is not allowed"); 57 | } 58 | 59 | $this->type = $type; 60 | } 61 | 62 | /** 63 | * @return float 64 | */ 65 | public function getSum() 66 | { 67 | return $this->sum; 68 | } 69 | 70 | /** 71 | * @param float $sum 72 | */ 73 | public function setSum($sum) 74 | { 75 | $this->sum = $sum; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/Exceptions/ItemsNotFound.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | declare(strict_types=1); 11 | 12 | namespace Bigperson\ModulposApiClient\Exceptions; 13 | 14 | /** 15 | * Class ItemsNotFound. 16 | */ 17 | class ItemsNotFound extends \Exception 18 | { 19 | } 20 | -------------------------------------------------------------------------------- /src/Exceptions/MethodNotFound.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | declare(strict_types=1); 11 | 12 | namespace Bigperson\ModulposApiClient\Exceptions; 13 | 14 | /** 15 | * Class MethodNotFound. 16 | */ 17 | class MethodNotFound extends \Exception 18 | { 19 | } 20 | -------------------------------------------------------------------------------- /src/Exceptions/PaymentMethodNotAllowed.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | declare(strict_types=1); 11 | 12 | namespace Bigperson\ModulposApiClient\Exceptions; 13 | 14 | /** 15 | * Class PaymentMethodNotAllowed. 16 | */ 17 | class PaymentMethodNotAllowed extends \Exception 18 | { 19 | } 20 | -------------------------------------------------------------------------------- /src/Exceptions/PaymentObjectNotAllowed.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | declare(strict_types=1); 11 | 12 | namespace Bigperson\ModulposApiClient\Exceptions; 13 | 14 | /** 15 | * Class PaymentObjectNotAllowed. 16 | */ 17 | class PaymentObjectNotAllowed extends \Exception 18 | { 19 | } 20 | -------------------------------------------------------------------------------- /src/Exceptions/RequiredParameterNotFound.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | declare(strict_types=1); 11 | 12 | namespace Bigperson\ModulposApiClient\Exceptions; 13 | 14 | /** 15 | * Class RequiredParameterNotFound. 16 | */ 17 | class RequiredParameterNotFound extends \Exception 18 | { 19 | } 20 | -------------------------------------------------------------------------------- /src/Exceptions/TaxModeNotAllowed.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | declare(strict_types=1); 11 | 12 | namespace Bigperson\ModulposApiClient\Exceptions; 13 | 14 | /** 15 | * Class TaxModeNotAllowed. 16 | */ 17 | class TaxModeNotAllowed extends \Exception 18 | { 19 | } 20 | -------------------------------------------------------------------------------- /src/Exceptions/TypeOperationsNotAllowed.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | declare(strict_types=1); 11 | 12 | namespace Bigperson\ModulposApiClient\Exceptions; 13 | 14 | /** 15 | * Class TypeOperationsNotAllowed. 16 | */ 17 | class TypeOperationsNotAllowed extends \Exception 18 | { 19 | } 20 | -------------------------------------------------------------------------------- /src/Exceptions/VatTagNotAllowed.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | declare(strict_types=1); 11 | 12 | namespace Bigperson\ModulposApiClient\Exceptions; 13 | 14 | /** 15 | * Class TypeOperationsNotAllowed. 16 | */ 17 | class VatTagNotAllowed extends \Exception 18 | { 19 | } 20 | -------------------------------------------------------------------------------- /tests/AssociateTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | declare(strict_types=1); 11 | 12 | namespace Tests; 13 | 14 | use Bigperson\ModulposApiClient\Associate; 15 | 16 | /** 17 | * Class AssociateTest. 18 | */ 19 | class AssociateTest extends TestCase 20 | { 21 | public function testAssociateRetailPointAndOnlineShop(): array 22 | { 23 | $login = getenv('MODULPOS_LOGIN'); 24 | $password = getenv('MODULPOS_PASSWORD'); 25 | $retailPointUuid = getenv('MODULPOS_RETAIL_POINT_UUID'); 26 | $associate = new Associate($login, $password, $retailPointUuid, true); 27 | $result = $associate->init(); 28 | $this->assertTrue(is_array($result)); 29 | $this->assertTrue(is_string($result['userName'])); 30 | $this->assertTrue(is_string($result['password'])); 31 | 32 | return $result; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /tests/CheckDataFactoryTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | declare(strict_types=1); 11 | 12 | namespace Tests; 13 | 14 | use Bigperson\ModulposApiClient\CheckDataFactory; 15 | use Bigperson\ModulposApiClient\Entity\Cashier; 16 | use Bigperson\ModulposApiClient\Entity\Order; 17 | use Bigperson\ModulposApiClient\Entity\OrderItem; 18 | use Bigperson\ModulposApiClient\Entity\PaymentItem; 19 | use Bigperson\ModulposApiClient\Exceptions\RequiredParameterNotFound; 20 | 21 | /** 22 | * Class CheckDataFactoryTest. 23 | */ 24 | class CheckDataFactoryTest extends TestCase 25 | { 26 | public function testConvertOrderToArray(): void 27 | { 28 | date_default_timezone_set('Europe/Moscow'); 29 | $dateTime = new \DateTime('NOW'); 30 | 31 | $order = Order::create([ 32 | 'documentUuid' => uniqid(), 33 | 'checkoutDateTime' => $dateTime->format(DATE_RFC3339), 34 | 'orderId' => rand(100000, 999999), 35 | 'typeOperation' => 'SALE', 36 | 'customerContact' => 'test@example.com', 37 | ]); 38 | 39 | $cashier = Cashier::create([ 40 | 'name' => 'Test Cashier', 41 | 'inn' => '123456789012', 42 | 'position' => 'salesman', 43 | ]); 44 | 45 | $orderItem1 = OrderItem::create([ 46 | 'price' => 100, 47 | 'quantity' => 1, 48 | 'vatTag' => OrderItem::VAT_NO, 49 | 'name' => 'Test Product1', 50 | ]); 51 | 52 | $orderItem2 = OrderItem::create([ 53 | 'price' => 200, 54 | 'quantity' => 1, 55 | 'vatTag' => OrderItem::VAT_NO, 56 | 'name' => 'Test Product2', 57 | ]); 58 | 59 | $paymentItem = PaymentItem::create([ 60 | 'type' => 'CARD', 61 | 'sum' => 300, 62 | ]); 63 | 64 | $order->addItem($orderItem1); 65 | $order->addItem($orderItem2); 66 | $order->addPaymentItem($paymentItem); 67 | 68 | $checkData = CheckDataFactory::convertToArray($order, null, false, $cashier); 69 | 70 | $this->assertTrue(is_array($checkData)); 71 | 72 | $this->assertNotEmpty($checkData); 73 | } 74 | 75 | public function testValidateRequiredParameterNotFound(): void 76 | { 77 | $order = new Order(); 78 | 79 | try { 80 | $checkData = CheckDataFactory::convertToArray($order); 81 | } catch (\Exception $exception) { 82 | $this->assertTrue($exception instanceof RequiredParameterNotFound); 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /tests/ClientTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | declare(strict_types=1); 11 | 12 | namespace Tests; 13 | 14 | use Bigperson\ModulposApiClient\Associate; 15 | use Bigperson\ModulposApiClient\Client; 16 | use Bigperson\ModulposApiClient\Entity\Order; 17 | use Bigperson\ModulposApiClient\Entity\OrderItem; 18 | use Bigperson\ModulposApiClient\Entity\PaymentItem; 19 | 20 | /** 21 | * Class ClientTest. 22 | */ 23 | class ClientTest extends TestCase 24 | { 25 | /** 26 | * @var string 27 | */ 28 | protected static $login; 29 | 30 | /** 31 | * @var string 32 | */ 33 | protected static $password; 34 | 35 | /** 36 | * @var string 37 | */ 38 | protected static $documentId; 39 | 40 | /** 41 | * Связывание торговой точки из данных переменных окружения. 42 | */ 43 | public static function setUpBeforeClass(): void 44 | { 45 | $login = getenv('MODULPOS_LOGIN'); 46 | $password = getenv('MODULPOS_PASSWORD'); 47 | $retailPointUuid = getenv('MODULPOS_RETAIL_POINT_UUID'); 48 | 49 | $associate = new Associate($login, $password, $retailPointUuid, true); 50 | 51 | $result = $associate->init(); 52 | 53 | self::$login = $result['userName']; 54 | self::$password = $result['password']; 55 | 56 | parent::setUpBeforeClass(); 57 | } 58 | 59 | /** 60 | * Проверка статуса сервиса фискализации. 61 | * 62 | * @return void 63 | */ 64 | public function testGetStatusFiscalService(): void 65 | { 66 | $client = new Client(self::$login, self::$password, true); 67 | 68 | $result = $client->getStatusFiscalService(); 69 | 70 | $this->assertTrue(is_array($result)); 71 | 72 | $statuses = [ 73 | 'READY', 74 | 'ASSOCIATED', 75 | 'FAILED', 76 | ]; 77 | 78 | $this->assertTrue(in_array($result['status'], $statuses)); 79 | 80 | $this->assertTrue(is_string($result['dateTime'])); 81 | } 82 | 83 | /** 84 | * Проверка отправки данных чека на сервер фискализации. 85 | * 86 | * @return void 87 | */ 88 | public function testSendCheck(): void 89 | { 90 | $this->assertEmpty(self::$documentId); 91 | 92 | $client = new Client(self::$login, self::$password, true); 93 | 94 | date_default_timezone_set('Europe/Moscow'); 95 | $dateTime = new \DateTime('NOW'); 96 | 97 | self::$documentId = uniqid(); 98 | 99 | $order = Order::create([ 100 | 'documentUuid' => self::$documentId, 101 | 'checkoutDateTime' => $dateTime->format(DATE_RFC3339), 102 | 'orderId' => rand(100000, 999999), 103 | 'typeOperation' => 'SALE', 104 | 'customerContact' => 'test@example.com', 105 | ]); 106 | 107 | $orderItem1 = OrderItem::create([ 108 | 'price' => 100, 109 | 'quantity' => 1, 110 | 'vatTag' => OrderItem::VAT_NO, 111 | 'name' => 'Test Product1', 112 | ]); 113 | 114 | $orderItem2 = OrderItem::create([ 115 | 'price' => 200, 116 | 'quantity' => 1, 117 | 'vatTag' => OrderItem::VAT_NO, 118 | 'name' => 'Test Product2', 119 | ]); 120 | 121 | $paymentItem = PaymentItem::create([ 122 | 'type' => 'CARD', 123 | 'sum' => 300, 124 | ]); 125 | 126 | $order->addItem($orderItem1); 127 | $order->addItem($orderItem2); 128 | $order->addPaymentItem($paymentItem); 129 | 130 | $responseUrl = 'https://internet.shop.ru/order/982340931/checkout?completed=1'; 131 | 132 | $printReceipt = true; 133 | 134 | $result = $client->sendCheck($order, $responseUrl, $printReceipt); 135 | 136 | $this->assertTrue(is_array($result)); 137 | 138 | $statuses = [ 139 | 'QUEUED', 140 | 'PENDING', 141 | 'PRINTED', 142 | 'COMPLETED', 143 | 'FAILED', 144 | ]; 145 | 146 | $this->assertTrue(in_array($result['status'], $statuses)); 147 | } 148 | 149 | /** 150 | * Проверка статуса документа (чека). 151 | * 152 | * @return void 153 | */ 154 | public function testGetDocumentStatus(): void 155 | { 156 | $this->assertNotEmpty(self::$documentId); 157 | 158 | $client = new Client(self::$login, self::$password, true); 159 | 160 | $result = $client->getStatusDocumentById(self::$documentId); 161 | 162 | $this->assertTrue(is_array($result)); 163 | 164 | $statuses = [ 165 | 'QUEUED', 166 | 'PENDING', 167 | 'PRINTED', 168 | 'COMPLETED', 169 | 'FAILED', 170 | ]; 171 | 172 | $this->assertTrue(in_array($result['status'], $statuses)); 173 | } 174 | } 175 | -------------------------------------------------------------------------------- /tests/ConfigTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | declare(strict_types=1); 11 | 12 | namespace Tests; 13 | 14 | use Bigperson\ModulposApiClient\Config; 15 | 16 | /** 17 | * Class ConfigTest. 18 | */ 19 | class ConfigTest extends TestCase 20 | { 21 | public function testGetBaseUrlTestMode(): void 22 | { 23 | $baseUrl = Config::getBaseUrl(true); 24 | 25 | $this->assertTrue($baseUrl === Config::BASE_TEST_URI); 26 | } 27 | 28 | public function testGetBaseUrlWorkMode(): void 29 | { 30 | $baseUrl = Config::getBaseUrl(); 31 | 32 | $this->assertTrue($baseUrl === Config::BASE_URI); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /tests/Entity/CashierTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | declare(strict_types=1); 11 | 12 | namespace Tests\Order; 13 | 14 | use Bigperson\ModulposApiClient\Entity\Cashier; 15 | use Bigperson\ModulposApiClient\Exceptions\MethodNotFound; 16 | use Tests\TestCase; 17 | 18 | /** 19 | * Class CashierTest. 20 | */ 21 | class CashierTest extends TestCase 22 | { 23 | private $cashierName = 'test name'; 24 | private $inn = '2123213213'; 25 | private $position = 'salesman'; 26 | 27 | public function testOrderCanBeCreated(): void 28 | { 29 | $order = new Cashier(); 30 | $order->setName($this->cashierName); 31 | $order->setInn($this->inn); 32 | $order->setPosition($this->position); 33 | 34 | $this->assertEquals($order->getName(), $this->cashierName); 35 | $this->assertEquals($order->getInn(), $this->inn); 36 | $this->assertEquals($order->getPosition(), $this->position); 37 | } 38 | 39 | public function testOrderCanBeCreatedByArray(): void 40 | { 41 | $cashier = Cashier::create([ 42 | 'name' => $this->cashierName, 43 | 'inn' => $this->inn, 44 | 'position' => $this->position, 45 | ]); 46 | 47 | $this->assertEquals($cashier->getName(), $this->cashierName); 48 | $this->assertEquals($cashier->getInn(), $this->inn); 49 | $this->assertEquals($cashier->getPosition(), $this->position); 50 | } 51 | 52 | public function testOrderCanNotBeCreatedByArray(): void 53 | { 54 | try { 55 | Cashier::create([ 56 | 'name' => $this->cashierName, 57 | 'inn' => $this->inn, 58 | 'position' => $this->position, 59 | 'methodNotAllowed' => 'methodNotAllowed', 60 | ]); 61 | } catch (\Exception $exception) { 62 | $this->assertTrue($exception instanceof MethodNotFound); 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /tests/Entity/OrderItemTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | declare(strict_types=1); 11 | 12 | namespace Tests\Order; 13 | 14 | use Bigperson\ModulposApiClient\Entity\OrderItem; 15 | use Bigperson\ModulposApiClient\Exceptions\MethodNotFound; 16 | use Bigperson\ModulposApiClient\Exceptions\PaymentMethodNotAllowed; 17 | use Bigperson\ModulposApiClient\Exceptions\PaymentObjectNotAllowed; 18 | use Bigperson\ModulposApiClient\Exceptions\VatTagNotAllowed; 19 | use Tests\TestCase; 20 | 21 | /** 22 | * Class OrderItemTest. 23 | */ 24 | class OrderItemTest extends TestCase 25 | { 26 | private $price; 27 | private $quantity; 28 | private $vatTag; 29 | private $name; 30 | private $paymentMethod; 31 | private $paymentObject; 32 | 33 | public function setUp(): void 34 | { 35 | $this->price = 32.21; 36 | $this->quantity = rand(1, 10); 37 | $this->vatTag = OrderItem::VAT_NO; 38 | $this->name = 'Test product'; 39 | $this->paymentMethod = 'full_prepayment'; 40 | $this->paymentObject = 'commodity'; 41 | 42 | parent::setUp(); 43 | } 44 | 45 | public function testOrderItemCanBeCreated(): void 46 | { 47 | $order = new OrderItem(); 48 | $order->setPrice($this->price); 49 | $order->setName($this->name); 50 | $order->setQuantity($this->quantity); 51 | $order->setVatTag($this->vatTag); 52 | 53 | $this->assertEquals($order->getName(), $this->name); 54 | $this->assertEquals($order->getPrice(), $this->price); 55 | $this->assertEquals($order->getQuantity(), $this->quantity); 56 | $this->assertEquals($order->getVatTag(), $this->vatTag); 57 | } 58 | 59 | public function testOrderItemCanBeCreatedByArray(): void 60 | { 61 | $order = OrderItem::create([ 62 | 'price' => $this->price, 63 | 'name' => $this->name, 64 | 'quantity' => $this->quantity, 65 | 'vatTag' => $this->vatTag, 66 | 'paymentMethod'=> $this->paymentMethod, 67 | 'paymentObject'=> $this->paymentObject, 68 | ]); 69 | 70 | $this->assertEquals($order->getName(), $this->name); 71 | $this->assertEquals($order->getPrice(), $this->price); 72 | $this->assertEquals($order->getQuantity(), $this->quantity); 73 | $this->assertEquals($order->getVatTag(), $this->vatTag); 74 | } 75 | 76 | public function testOrderItemCanNotBeCreatedByArray(): void 77 | { 78 | try { 79 | $order = OrderItem::create([ 80 | 'price' => $this->price, 81 | 'name' => $this->name, 82 | 'quantity' => $this->quantity, 83 | 'vatTag' => $this->vatTag, 84 | 'methodNotAllowed' => 'methodNotAllowed', 85 | ]); 86 | } catch (\Exception $exception) { 87 | $this->assertTrue($exception instanceof MethodNotFound); 88 | } 89 | } 90 | 91 | public function testOrderItemCanNotSetVatTag(): void 92 | { 93 | try { 94 | $order = new OrderItem(); 95 | $order->setVatTag('NONE'); 96 | } catch (\Exception $exception) { 97 | $this->assertTrue($exception instanceof VatTagNotAllowed); 98 | } 99 | } 100 | 101 | public function testOrderItemCanNotSetPaymentMethod(): void 102 | { 103 | try { 104 | $order = new OrderItem(); 105 | $order->setPaymentMethod('NONE'); 106 | } catch (\Exception $exception) { 107 | $this->assertTrue($exception instanceof PaymentMethodNotAllowed); 108 | } 109 | } 110 | 111 | public function testOrderItemCanNotSetPaymentObject(): void 112 | { 113 | try { 114 | $order = new OrderItem(); 115 | $order->setPaymentObject('NONE'); 116 | } catch (\Exception $exception) { 117 | $this->assertTrue($exception instanceof PaymentObjectNotAllowed); 118 | } 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /tests/Entity/OrderTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | declare(strict_types=1); 11 | 12 | namespace Tests\Order; 13 | 14 | use Bigperson\ModulposApiClient\Entity\Order; 15 | use Bigperson\ModulposApiClient\Entity\OrderItem; 16 | use Bigperson\ModulposApiClient\Entity\PaymentItem; 17 | use Bigperson\ModulposApiClient\Exceptions\MethodNotFound; 18 | use Bigperson\ModulposApiClient\Exceptions\TaxModeNotAllowed; 19 | use Bigperson\ModulposApiClient\Exceptions\TypeOperationsNotAllowed; 20 | use Tests\TestCase; 21 | 22 | /** 23 | * Class OrderTest. 24 | */ 25 | class OrderTest extends TestCase 26 | { 27 | private $uuid; 28 | private $orderId; 29 | private $customerContact; 30 | private $typeOperation; 31 | /** 32 | * @var \DateTime 33 | */ 34 | private $checkoutDateTime; 35 | 36 | public function setUp(): void 37 | { 38 | $this->uuid = uniqid(); 39 | $this->orderId = rand(1000, 9999); 40 | $this->customerContact = 'test@test.ru'; 41 | $this->typeOperation = 'SALE'; 42 | date_default_timezone_set('Europe/Moscow'); 43 | $this->checkoutDateTime = new \DateTime('NOW'); 44 | $this->taxMode = 'COMMON'; 45 | 46 | parent::setUp(); 47 | } 48 | 49 | public function testOrderCanBeCreated(): void 50 | { 51 | $order = new Order(); 52 | $order->setDocumentUuid($this->uuid); 53 | $order->setOrderId($this->orderId); 54 | $order->setCustomerContact($this->customerContact); 55 | $order->setTypeOperation($this->typeOperation); 56 | $order->setCheckoutDateTime($this->checkoutDateTime->format(DATE_RFC3339)); 57 | $order->setTaxMode($this->taxMode); 58 | 59 | $this->assertEquals($order->getDocumentUuid(), $this->uuid); 60 | $this->assertEquals($order->getOrderId(), $this->orderId); 61 | $this->assertEquals($order->getCustomerContact(), $this->customerContact); 62 | $this->assertEquals($order->getTypeOperation(), $this->typeOperation); 63 | $this->assertEquals($order->getCheckoutDateTime(), $this->checkoutDateTime->format(DATE_RFC3339)); 64 | $this->assertEquals($order->getTaxMode(), $this->taxMode); 65 | } 66 | 67 | public function testOrderCanBeCreatedByArray(): void 68 | { 69 | $order = Order::create([ 70 | 'documentUuid' => $this->uuid, 71 | 'orderId' => $this->orderId, 72 | 'customerContact' => $this->customerContact, 73 | 'typeOperation' => $this->typeOperation, 74 | 'checkoutDateTime' => $this->checkoutDateTime->format(DATE_RFC3339), 75 | 'taxMode' => $this->taxMode, 76 | ]); 77 | 78 | $this->assertEquals($order->getDocumentUuid(), $this->uuid); 79 | $this->assertEquals($order->getOrderId(), $this->orderId); 80 | $this->assertEquals($order->getCustomerContact(), $this->customerContact); 81 | $this->assertEquals($order->getTypeOperation(), $this->typeOperation); 82 | $this->assertEquals($order->getCheckoutDateTime(), $this->checkoutDateTime->format(DATE_RFC3339)); 83 | $this->assertEquals($order->getTaxMode(), $this->taxMode); 84 | } 85 | 86 | public function testOrderCanNotBeCreatedByArray(): void 87 | { 88 | try { 89 | $order = Order::create([ 90 | 'documentUuid' => $this->uuid, 91 | 'orderId' => $this->orderId, 92 | 'customerContact' => $this->customerContact, 93 | 'typeOperation' => $this->typeOperation, 94 | 'methodNotAllowed' => 'methodNotAllowed', 95 | ]); 96 | } catch (\Exception $exception) { 97 | $this->assertTrue($exception instanceof MethodNotFound); 98 | } 99 | } 100 | 101 | public function testOrderCanNotSetTypeOperator(): void 102 | { 103 | try { 104 | $order = new Order(); 105 | $order->setTypeOperation('NONE'); 106 | } catch (\Exception $exception) { 107 | $this->assertTrue($exception instanceof TypeOperationsNotAllowed); 108 | } 109 | } 110 | 111 | public function testOrderCanNotSetTaxMode(): void 112 | { 113 | try { 114 | $order = new Order(); 115 | $order->setTaxMode('NONE'); 116 | } catch (\Exception $exception) { 117 | $this->assertTrue($exception instanceof TaxModeNotAllowed); 118 | } 119 | } 120 | 121 | public function testOrderItemsCanAdd(): void 122 | { 123 | $order = new Order(); 124 | $orderItem1 = new OrderItem(); 125 | $orderItem2 = new OrderItem(); 126 | 127 | $order->addItem($orderItem1); 128 | $order->addItem($orderItem2); 129 | 130 | $this->assertEquals($order->getItems(), [$orderItem1, $orderItem2]); 131 | } 132 | 133 | public function testPaymentItemsCanAdd(): void 134 | { 135 | $order = new Order(); 136 | $paymentItem1 = new PaymentItem(); 137 | $paymentItem2 = new PaymentItem(); 138 | 139 | $order->addPaymentItem($paymentItem1); 140 | $order->addPaymentItem($paymentItem2); 141 | 142 | $this->assertEquals($order->getPaymentItems(), [$paymentItem1, $paymentItem2]); 143 | } 144 | } 145 | -------------------------------------------------------------------------------- /tests/Entity/PaymentItemTest.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | declare(strict_types=1); 11 | 12 | namespace Tests\Order; 13 | 14 | use Bigperson\ModulposApiClient\Entity\PaymentItem; 15 | use Bigperson\ModulposApiClient\Exceptions\MethodNotFound; 16 | use Bigperson\ModulposApiClient\Exceptions\TypeOperationsNotAllowed; 17 | use Tests\TestCase; 18 | 19 | /** 20 | * Class PaymentItemTest. 21 | */ 22 | class PaymentItemTest extends TestCase 23 | { 24 | private $type; 25 | private $sum; 26 | 27 | public function setUp(): void 28 | { 29 | $this->type = 'CARD'; 30 | $this->sum = 100; 31 | 32 | parent::setUp(); 33 | } 34 | 35 | public function testPaymentItemCanBeCreated(): void 36 | { 37 | $item = new PaymentItem(); 38 | $item->setType($this->type); 39 | $item->setSum($this->sum); 40 | 41 | $this->assertEquals($item->getType(), $this->type); 42 | $this->assertEquals($item->getSum(), $this->sum); 43 | } 44 | 45 | public function testPaymentItemCanBeCreatedByArray(): void 46 | { 47 | $item = PaymentItem::create([ 48 | 'type' => $this->type, 49 | 'sum' => $this->sum, 50 | ]); 51 | 52 | $this->assertEquals($item->getType(), $this->type); 53 | $this->assertEquals($item->getSum(), $this->sum); 54 | } 55 | 56 | public function testPaymentItemCanNotBeCreatedByArray(): void 57 | { 58 | try { 59 | $item = PaymentItem::create([ 60 | 'type' => $this->type, 61 | 'sum' => $this->sum, 62 | 'methodNotAllowed' => 'methodNotAllowed', 63 | ]); 64 | } catch (\Exception $exception) { 65 | $this->assertTrue($exception instanceof MethodNotFound); 66 | } 67 | } 68 | 69 | public function testPaymentItemCanNotSetType(): void 70 | { 71 | try { 72 | $item = new PaymentItem(); 73 | $item->setType('NONE'); 74 | } catch (\Exception $exception) { 75 | $this->assertTrue($exception instanceof TypeOperationsNotAllowed); 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /tests/TestCase.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | declare(strict_types=1); 11 | 12 | namespace Tests; 13 | 14 | use PHPUnit\Framework\TestCase as UnitTestCase; 15 | 16 | /** 17 | * Class TestCase. 18 | */ 19 | class TestCase extends UnitTestCase 20 | { 21 | } 22 | --------------------------------------------------------------------------------