Skip to content

Commit 0ceba34

Browse files
committed
😉 add tests and readme, do release
1 parent 5e5a53c commit 0ceba34

File tree

8 files changed

+159
-12
lines changed

8 files changed

+159
-12
lines changed

README.md

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,62 @@
11
# php-dreamkas
2-
Фискализация чека для Дримкас-Ф на php
2+
Фискализация чека для Дримкас-Ф для PHP 7.2
3+
4+
Для более старых версий PHP придётся править код на предмет типов у функций.
5+
6+
## Установка
7+
8+
```
9+
composer require devgroup/php-dreamkas
10+
```
11+
12+
## Пример кода
13+
14+
```php
15+
<?php
16+
use DevGroup\Dreamkas\Api;
17+
use DevGroup\Dreamkas\CustomerAttributes;
18+
use DevGroup\Dreamkas\exceptions\ValidationException;
19+
use DevGroup\Dreamkas\Payment;
20+
use DevGroup\Dreamkas\Position;
21+
use DevGroup\Dreamkas\Receipt;
22+
use DevGroup\Dreamkas\TaxMode;
23+
use GuzzleHttp\Exception\ClientException;
24+
25+
/***
26+
* 123 - ID кассы
27+
* MODE_MOCK - режим, может быть MODE_MOCK, MODE_PRODUCTION, MODE_MODE_DEBUG
28+
*/
29+
$api = new Api('ACCESS_TOKEN из профиля', 123, Api::MODE_MOCK);
30+
31+
$receipt = new Receipt();
32+
$receipt->taxMode = TaxMode::MODE_SIMPLE;
33+
$receipt->positions[] = new Position([
34+
'name' => 'Билет - тест',
35+
'quantity' => 2,
36+
'price' => 210000, // цена в копейках за 1 шт. или 1 грамм
37+
]);
38+
$receipt->payments[] = new Payment([
39+
'sum' => 420000, // стоимость оплаты по чеку
40+
]);
41+
$receipt->attributes = new CustomerAttributes([
42+
'email' => 'info@devgroup.ru', // почта покупателя
43+
'phone' => '74996776566', // телефон покупателя
44+
]);
45+
46+
$receipt->calculateSum();
47+
48+
49+
$response = [];
50+
try {
51+
$response = $api->postReceipt($receipt);
52+
} catch (ValidationException $e) {
53+
// Это исключение кидается, когда информация в чеке не правильная
54+
} catch (ClientException $e) {
55+
echo $e->getResponse()->getBody();
56+
// Это исключение кидается, когда при передачи чека в Дрикас произошла ошибка. Лучше отправить чек ещё раз
57+
// Если будут дубли - потом отменяйте через $receipt->type = Receipt::TYPE_REFUND;
58+
}
59+
60+
```
61+
62+
Made by DevGroup.ru - [Создание интернет-магазинов](https://devgroup.ru/services/internet-magazin).

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"description": "Фискализация чека для Дримкас-Ф на php",
44
"type": "library",
55
"require": {
6+
"php": "~7.2.0",
67
"guzzlehttp/guzzle": "~6.3.0"
78
},
89
"require-dev": {

src/Api.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
namespace DevGroup\Dreamkas;
44

55
use GuzzleHttp\Client;
6-
use GuzzleHttp\Middleware;
7-
use GuzzleHttp\Psr7\Request;
8-
96

107
/**
118
* Class Api
@@ -23,7 +20,7 @@ class Api
2320
/** @var Client */
2421
protected $client;
2522

26-
protected $baseUri = [
23+
protected static $baseUri = [
2724
self::MODE_PRODUCTION => 'https://kabinet.dreamkas.ru/api/',
2825
self::MODE_MOCK => 'https://private-anon-2a1e26f7f7-kabinet.apiary-mock.com/api/',
2926
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
4340
$this->createClient();
4441
}
4542

46-
protected function createClient()
43+
protected function createClient(): void
4744
{
48-
$baseUri = $this->baseUri[$this->mode] ?? null;
45+
$baseUri = static::$baseUri[$this->mode] ?? null;
4946
if ($baseUri === null) {
5047
throw new \RuntimeException('Unknown Dreamkas Api mode');
5148
}

src/Configurable.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,18 @@ public function toArray(): array
3030
if ($value === null) {
3131
continue;
3232
}
33-
if (is_array($value)) {
33+
if (\is_array($value)) {
3434
$newValue = [];
3535
foreach ($value as $valueKey => $valueItem) {
36-
if ($valueItem instanceof Configurable) {
36+
if ($valueItem instanceof self) {
3737
$newValue[$valueKey] = $valueItem->toArray();
3838
} else {
3939
$newValue[$valueKey] = $valueItem;
4040
}
4141
}
4242
$value = $newValue;
4343
}
44-
if ($value instanceof Configurable) {
44+
if ($value instanceof self) {
4545
$value = $value->toArray();
4646
}
4747
$result[$key] = $value;

src/CustomerAttributes.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
use DevGroup\Dreamkas\exceptions\ValidationException;
77

8+
/**
9+
* Class CustomerAttributes описывает информацию о покупателе, куда ему высылать чек
10+
*/
811
class CustomerAttributes extends Configurable
912
{
1013
/** @var string|null */

src/Receipt.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ class Receipt extends Configurable
1414
public const TYPE_OUTFLOW = 'OUTFLOW';
1515
public const TYPE_OUTFLOW_REFUND = 'OUTFLOW_REFUND';
1616

17+
// Тип чека
1718
public $type = self::TYPE_SALE;
1819

1920
public $timeout = 300;
2021

22+
// Налоговый режим
2123
public $taxMode;
2224

2325
/** @var Position[] */
@@ -29,6 +31,7 @@ class Receipt extends Configurable
2931
/** @var CustomerAttributes|array */
3032
public $attributes = [];
3133

34+
// Общая сумма чека
3235
public $total = [
3336
'priceSum' => 0,
3437
];
@@ -40,7 +43,7 @@ class Receipt extends Configurable
4043
public function toArray(): array
4144
{
4245
$this->calculateSum();
43-
return parent::toArray(); // TODO: Change the autogenerated stub
46+
return parent::toArray();
4447
}
4548

4649
/**
@@ -75,7 +78,7 @@ public function validate(): bool
7578
throw new ValidationException('No taxMode specified');
7679
}
7780

78-
if (is_array($this->attributes) && \count($this->attributes) === 0) {
81+
if (\is_array($this->attributes) && \count($this->attributes) === 0) {
7982
throw new ValidationException('No customer attributes specified');
8083
}
8184
if ($this->attributes instanceof CustomerAttributes) {

tests/ApiTest.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace DevGroup\Dreamkas\tests;
4+
5+
use DevGroup\Dreamkas\Api;
6+
use DevGroup\Dreamkas\CustomerAttributes;
7+
use DevGroup\Dreamkas\exceptions\ValidationException;
8+
use DevGroup\Dreamkas\Payment;
9+
use DevGroup\Dreamkas\Position;
10+
use DevGroup\Dreamkas\Receipt;
11+
use DevGroup\Dreamkas\TaxMode;
12+
use GuzzleHttp\Exception\ClientException;
13+
14+
15+
/**
16+
* Class ApiTest
17+
*/
18+
class ApiTest extends \PHPUnit_Framework_TestCase
19+
{
20+
21+
public function testJson()
22+
{
23+
$api = new Api('FAKE', 123, Api::MODE_MOCK);
24+
$result = $api->json('GET', 'products');
25+
26+
$this->assertSame([[]], $result);
27+
}
28+
29+
public function testPostReceipt()
30+
{
31+
32+
$api = new Api('FAKE', 123, Api::MODE_MOCK);
33+
34+
$receipt = new Receipt();
35+
$receipt->taxMode = TaxMode::MODE_SIMPLE;
36+
$receipt->positions[] = new Position([
37+
'name' => 'Билет - тест',
38+
'quantity' => 2,
39+
'price' => 210000,
40+
]);
41+
$receipt->payments[] = new Payment([
42+
'sum' => 420000,
43+
]);
44+
$receipt->attributes = new CustomerAttributes([
45+
'email' => 'info@devgroup.ru',
46+
]);
47+
48+
$receipt->calculateSum();
49+
50+
51+
$response = [];
52+
try {
53+
$response = $api->postReceipt($receipt);
54+
} catch (ValidationException $e) {
55+
$this->assertFalse(true, 'Validation exception: ' . $e->getMessage());
56+
} catch (ClientException $e) {
57+
echo $e->getResponse()->getBody();
58+
$this->assertFalse(true, 'Client exception');
59+
}
60+
$this->assertArrayHasKey('status', $response);
61+
$this->assertArrayHasKey('id', $response);
62+
$this->assertArrayHasKey('createdAt', $response);
63+
64+
//
65+
$receipt->type = Receipt::TYPE_REFUND;
66+
$response = [];
67+
try {
68+
$response = $api->postReceipt($receipt);
69+
} catch (ValidationException $e) {
70+
$this->assertFalse(true, 'Validation exception: ' . $e->getMessage());
71+
} catch (ClientException $e) {
72+
echo $e->getResponse()->getBody();
73+
$this->assertFalse(true, 'Client exception');
74+
}
75+
$this->assertArrayHasKey('status', $response);
76+
$this->assertArrayHasKey('id', $response);
77+
$this->assertArrayHasKey('createdAt', $response);
78+
79+
}
80+
81+
82+
}

tests/bootstrap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php

0 commit comments

Comments
 (0)