diff --git a/README.md b/README.md index 9260bd0..0fc6c34 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,62 @@ # php-dreamkas -Фискализация чека для Дримкас-Ф на php +Фискализация чека для Дримкас-Ф для PHP 7.2 + +Для более старых версий PHP придётся править код на предмет типов у функций. + +## Установка + +``` +composer require devgroup/php-dreamkas +``` + +## Пример кода + +```php +taxMode = TaxMode::MODE_SIMPLE; +$receipt->positions[] = new Position([ + 'name' => 'Билет - тест', + 'quantity' => 2, + 'price' => 210000, // цена в копейках за 1 шт. или 1 грамм +]); +$receipt->payments[] = new Payment([ + 'sum' => 420000, // стоимость оплаты по чеку +]); +$receipt->attributes = new CustomerAttributes([ + 'email' => 'info@devgroup.ru', // почта покупателя + 'phone' => '74996776566', // телефон покупателя +]); + +$receipt->calculateSum(); + + +$response = []; +try { + $response = $api->postReceipt($receipt); +} catch (ValidationException $e) { + // Это исключение кидается, когда информация в чеке не правильная +} catch (ClientException $e) { + echo $e->getResponse()->getBody(); + // Это исключение кидается, когда при передачи чека в Дрикас произошла ошибка. Лучше отправить чек ещё раз + // Если будут дубли - потом отменяйте через $receipt->type = Receipt::TYPE_REFUND; +} + +``` + +Made by DevGroup.ru - [Создание интернет-магазинов](https://devgroup.ru/services/internet-magazin). \ No newline at end of file diff --git a/composer.json b/composer.json index 9dd541c..c45eeca 100644 --- a/composer.json +++ b/composer.json @@ -3,6 +3,7 @@ "description": "Фискализация чека для Дримкас-Ф на php", "type": "library", "require": { + "php": "~7.2.0", "guzzlehttp/guzzle": "~6.3.0" }, "require-dev": { diff --git a/src/Api.php b/src/Api.php index 3ab705d..0e6fb78 100644 --- a/src/Api.php +++ b/src/Api.php @@ -3,9 +3,6 @@ namespace DevGroup\Dreamkas; use GuzzleHttp\Client; -use GuzzleHttp\Middleware; -use GuzzleHttp\Psr7\Request; - /** * Class Api @@ -23,7 +20,7 @@ class Api /** @var Client */ protected $client; - protected $baseUri = [ + protected static $baseUri = [ self::MODE_PRODUCTION => 'https://kabinet.dreamkas.ru/api/', self::MODE_MOCK => 'https://private-anon-2a1e26f7f7-kabinet.apiary-mock.com/api/', self::MODE_DEBUG => 'https://private-anon-2a1e26f7f7-kabinet.apiary-proxy.com/api/', @@ -43,9 +40,9 @@ public function __construct(string $accessToken, int $deviceId, int $mode = self $this->createClient(); } - protected function createClient() + protected function createClient(): void { - $baseUri = $this->baseUri[$this->mode] ?? null; + $baseUri = static::$baseUri[$this->mode] ?? null; if ($baseUri === null) { throw new \RuntimeException('Unknown Dreamkas Api mode'); } diff --git a/src/Configurable.php b/src/Configurable.php index 3cb5859..13b3090 100644 --- a/src/Configurable.php +++ b/src/Configurable.php @@ -30,10 +30,10 @@ public function toArray(): array if ($value === null) { continue; } - if (is_array($value)) { + if (\is_array($value)) { $newValue = []; foreach ($value as $valueKey => $valueItem) { - if ($valueItem instanceof Configurable) { + if ($valueItem instanceof self) { $newValue[$valueKey] = $valueItem->toArray(); } else { $newValue[$valueKey] = $valueItem; @@ -41,7 +41,7 @@ public function toArray(): array } $value = $newValue; } - if ($value instanceof Configurable) { + if ($value instanceof self) { $value = $value->toArray(); } $result[$key] = $value; diff --git a/src/CustomerAttributes.php b/src/CustomerAttributes.php index f07f12d..43e05e3 100644 --- a/src/CustomerAttributes.php +++ b/src/CustomerAttributes.php @@ -5,6 +5,9 @@ use DevGroup\Dreamkas\exceptions\ValidationException; +/** + * Class CustomerAttributes описывает информацию о покупателе, куда ему высылать чек + */ class CustomerAttributes extends Configurable { /** @var string|null */ diff --git a/src/Receipt.php b/src/Receipt.php index cf324af..80fa2d4 100644 --- a/src/Receipt.php +++ b/src/Receipt.php @@ -14,10 +14,12 @@ class Receipt extends Configurable public const TYPE_OUTFLOW = 'OUTFLOW'; public const TYPE_OUTFLOW_REFUND = 'OUTFLOW_REFUND'; + // Тип чека public $type = self::TYPE_SALE; public $timeout = 300; + // Налоговый режим public $taxMode; /** @var Position[] */ @@ -29,6 +31,7 @@ class Receipt extends Configurable /** @var CustomerAttributes|array */ public $attributes = []; + // Общая сумма чека public $total = [ 'priceSum' => 0, ]; @@ -40,7 +43,7 @@ class Receipt extends Configurable public function toArray(): array { $this->calculateSum(); - return parent::toArray(); // TODO: Change the autogenerated stub + return parent::toArray(); } /** @@ -75,7 +78,7 @@ public function validate(): bool throw new ValidationException('No taxMode specified'); } - if (is_array($this->attributes) && \count($this->attributes) === 0) { + if (\is_array($this->attributes) && \count($this->attributes) === 0) { throw new ValidationException('No customer attributes specified'); } if ($this->attributes instanceof CustomerAttributes) { diff --git a/tests/ApiTest.php b/tests/ApiTest.php new file mode 100644 index 0000000..25e95dc --- /dev/null +++ b/tests/ApiTest.php @@ -0,0 +1,82 @@ +json('GET', 'products'); + + $this->assertSame([[]], $result); + } + + public function testPostReceipt() + { + + $api = new Api('FAKE', 123, Api::MODE_MOCK); + + $receipt = new Receipt(); + $receipt->taxMode = TaxMode::MODE_SIMPLE; + $receipt->positions[] = new Position([ + 'name' => 'Билет - тест', + 'quantity' => 2, + 'price' => 210000, + ]); + $receipt->payments[] = new Payment([ + 'sum' => 420000, + ]); + $receipt->attributes = new CustomerAttributes([ + 'email' => 'info@devgroup.ru', + ]); + + $receipt->calculateSum(); + + + $response = []; + try { + $response = $api->postReceipt($receipt); + } catch (ValidationException $e) { + $this->assertFalse(true, 'Validation exception: ' . $e->getMessage()); + } catch (ClientException $e) { + echo $e->getResponse()->getBody(); + $this->assertFalse(true, 'Client exception'); + } + $this->assertArrayHasKey('status', $response); + $this->assertArrayHasKey('id', $response); + $this->assertArrayHasKey('createdAt', $response); + +// + $receipt->type = Receipt::TYPE_REFUND; + $response = []; + try { + $response = $api->postReceipt($receipt); + } catch (ValidationException $e) { + $this->assertFalse(true, 'Validation exception: ' . $e->getMessage()); + } catch (ClientException $e) { + echo $e->getResponse()->getBody(); + $this->assertFalse(true, 'Client exception'); + } + $this->assertArrayHasKey('status', $response); + $this->assertArrayHasKey('id', $response); + $this->assertArrayHasKey('createdAt', $response); + + } + + +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..b3d9bbc --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1 @@ +