Skip to content

Commit 3fa8d05

Browse files
authored
Merge pull request #7 from b24io/issue#6
issue#6 fix error on add contact
2 parents e8ac788 + 89ea566 commit 3fa8d05

File tree

17 files changed

+494
-347
lines changed

17 files changed

+494
-347
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/.idea*
22
vendor
3+
tests/logs/
34
composer.phar
45
composer.lock

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ before_script:
1818
- composer install --no-interaction --prefer-source --dev
1919

2020
script:
21-
composer test
21+
composer unit-tests

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
# loyalty-php-sdk
2+
## 2.0.0 (1.02.2020)
3+
* add unit tests for `\SDK\Bitrix24\Contacts\DTO\Fabric::initContactFromArray`
4+
* add example `admin-add-contact-with-card-number.php`
5+
* add integration tests for `\SDK\Bitrix24\Contacts\Transport\Admin\Transport`
6+
* change constructor args for `\SDK\Bitrix24\Contacts\DTO\Contact`
7+
* remove setters for `\SDK\Bitrix24\Contacts\DTO\Contact`
8+
* remove setters for `\SDK\Bitrix24\Contacts\DTO\Address`
9+
210
## 1.1.0 (26.01.2020)
311
* add method `getReportByMetricCode` in `\Metrics\Transport\Admin` transport, return `MetricReportResponse` with `Report` DTO
412
* add interface `DefaultRequestArgumentsInterface` with default request argument fields

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# loyalty-php-sdk
2-
[![License](https://poser.pugx.org/b24io/loyalty-php-sdk/license.svg)](https://packagist.org/packages/b24io/loyalty-php-sdk) [![Total Downloads](https://poser.pugx.org/b24io/loyalty-php-sdk/downloads.svg)](https://packagist.org/packages/b24io/loyalty-php-sdk)
1+
# loyalty-php-sdk
2+
[![License](https://poser.pugx.org/b24io/loyalty-php-sdk/license.svg)](https://packagist.org/packages/b24io/loyalty-php-sdk) [![Total Downloads](https://poser.pugx.org/b24io/loyalty-php-sdk/downloads.svg)](https://packagist.org/packages/b24io/loyalty-php-sdk) [![Build Status](https://travis-ci.org/b24io/loyalty-php-sdk.svg?branch=master)](https://travis-ci.org/b24io/loyalty-php-sdk)
33

44
Loyalty PHP SDK is a tool for work with REST-API Bitrix24 Application [Loyalty Program and bonus cards for Bitrix24 CRM](https://www.bitrix24.ru/apps/?app=b24io.loyalty)
55

composer.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"ext-curl": "*",
2626
"psr/log": "^1.0",
2727
"monolog/monolog": "1.*",
28-
"guzzlehttp/guzzle": "6.x",
28+
"guzzlehttp/guzzle": "6.5",
2929
"moneyphp/money": "^3.0",
3030
"fig/http-message-util": "1.*",
3131
"eloquent/enumeration": "^5.1",
@@ -43,8 +43,11 @@
4343
}
4444
},
4545
"scripts": {
46-
"test": [
47-
"phpunit --colors=always --verbose"
46+
"unit-tests": [
47+
"phpunit --testsuite unit-tests --colors=always --verbose"
48+
],
49+
"integration-tests": [
50+
"phpunit --testsuite integration-tests --colors=always --verbose"
4851
]
4952
}
5053
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
require_once 'vendor/autoload.php';
5+
6+
use \Monolog\Logger;
7+
use \B24io\Loyalty\SDK;
8+
use \B24io\Loyalty\SDK\OperationsJournal\DTO\OperationType;
9+
use Ramsey\Uuid\Uuid;
10+
11+
use GuzzleHttp\HandlerStack;
12+
use GuzzleHttp\Middleware;
13+
use GuzzleHttp\MessageFormatter;
14+
15+
$argv = getopt('', ['clientApiKey::', 'authApiKey::', 'apiEndpoint::']);
16+
$fileName = basename(__FILE__);
17+
18+
$clientApiKey = $argv['clientApiKey'];
19+
if ($clientApiKey === null) {
20+
throw new \InvalidArgumentException(sprintf('error: argument «clientApiKey» not found') . PHP_EOL);
21+
}
22+
$authApiKey = $argv['authApiKey'];
23+
if ($authApiKey === null) {
24+
throw new \InvalidArgumentException(sprintf('error: argument «authApiKey» not found') . PHP_EOL);
25+
}
26+
$apiEndpoint = $argv['apiEndpoint'];
27+
if ($apiEndpoint === null) {
28+
throw new \InvalidArgumentException(sprintf('error: argument «apiEndpoint» not found') . PHP_EOL);
29+
}
30+
31+
// check connection to API
32+
$log = new Logger('loyalty-php-sdk');
33+
$log->pushHandler(new \Monolog\Handler\StreamHandler('loyalty-php-sdk-example.log', Logger::DEBUG));
34+
$guzzleHandlerStack = HandlerStack::create();
35+
$guzzleHandlerStack->push(
36+
Middleware::log(
37+
$log,
38+
new MessageFormatter(MessageFormatter::SHORT)
39+
)
40+
);
41+
$httpClient = new \GuzzleHttp\Client();
42+
43+
$log->info('loyalty.apiClient.start');
44+
$token = new SDK\Auth\DTO\Token(
45+
SDK\Transport\DTO\Role::initializeByCode('admin'),
46+
Uuid::fromString($clientApiKey),
47+
Uuid::fromString($authApiKey)
48+
);
49+
$apiClient = new SDK\ApiClient($apiEndpoint, $token, $httpClient, $log);
50+
$apiClient->setGuzzleHandlerStack($guzzleHandlerStack);
51+
52+
$bitrix24Transport = SDK\Bitrix24\Contacts\Transport\Admin\Fabric::getBitrix24ContactsTransport($apiClient, $log);
53+
54+
try {
55+
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
56+
$randomMobileNumber = $phoneUtil->parse(
57+
sprintf(
58+
'+7%s',
59+
substr(str_replace('.', '', (string)microtime(true)), 0, 10)
60+
)
61+
);
62+
$randomCardNumber = (int)str_replace('.', '', (string)microtime(true));
63+
64+
$contactDto = new SDK\Bitrix24\Contacts\DTO\Contact(
65+
new \DateTime(),
66+
new \DateTime(),
67+
'John',
68+
'Doe',
69+
null,
70+
$randomMobileNumber
71+
);
72+
73+
$result = $bitrix24Transport->addWithCardNumber($contactDto, $randomCardNumber, new SDK\Transport\DTO\Reason('examples'));
74+
print(sprintf('query result:') . PHP_EOL);
75+
print(sprintf(' - message operation: %s', $result->getMeta()->getMessage()) . PHP_EOL);
76+
print(sprintf(' - role: %s', $result->getMeta()->getRole()->key()) . PHP_EOL);
77+
print(sprintf(' - duration: %s', $result->getMeta()->getDuration()) . PHP_EOL);
78+
79+
$phoneNumberFormatter = \libphonenumber\PhoneNumberUtil::getInstance();
80+
print(sprintf('- contact:') . PHP_EOL);
81+
print(sprintf(' id: %s', $result->getContact()->getContactId()->getId()) . PHP_EOL);
82+
print(sprintf(' email: %s', $result->getContact()->getEmail()) . PHP_EOL);
83+
print(sprintf(
84+
' phone: %s',
85+
$result->getContact()->getMobilePhone() !== null ?
86+
$phoneNumberFormatter->format(
87+
$result->getContact()->getMobilePhone(),
88+
\libphonenumber\PhoneNumberFormat::INTERNATIONAL
89+
) : null
90+
) . PHP_EOL);
91+
92+
$decimalMoneyFormatter = new \Money\Formatter\DecimalMoneyFormatter(new \Money\Currencies\ISOCurrencies());
93+
print(sprintf('- card:') . PHP_EOL);
94+
print(sprintf(' number: %s', $result->getCard()->getNumber()) . PHP_EOL);
95+
print(sprintf(' uuid: %s', $result->getCard()->getUuid()->toString()) . PHP_EOL);
96+
print(sprintf(' status: %s', $result->getCard()->getStatus()->getCode()) . PHP_EOL);
97+
print(sprintf(
98+
' balance: %s %s',
99+
$decimalMoneyFormatter->format($result->getCard()->getBalance()),
100+
$result->getCard()->getBalance()->getCurrency()->getCode() . PHP_EOL
101+
));
102+
print(sprintf(' percentage: %s', $result->getCard()->getPercentage()->format()) . PHP_EOL);
103+
} catch (SDK\Exceptions\ApiClientException $exception) {
104+
var_dump($exception->getApiProblem()->asArray());
105+
} catch (\Throwable $exception) {
106+
var_dump($exception->getMessage());
107+
}

phpunit.xml.dist

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,22 @@
44
bootstrap="tests/bootstrap.php"
55
colors="true">
66
<testsuites>
7-
<testsuite name="loyalty-php-sdk test suite">
8-
<directory>tests/src/</directory>
7+
<testsuite name="unit-tests">
8+
<directory>tests/unit/src/</directory>
9+
</testsuite>
10+
<testsuite name="integration-tests">
11+
<directory>tests/integration/src/</directory>
912
</testsuite>
1013
</testsuites>
1114
<filter>
1215
<whitelist>
1316
<directory suffix=".php">src/</directory>
1417
</whitelist>
1518
</filter>
19+
<php>
20+
<env name="LOYALTY_API_CLIENT_LOG_FILE" value="loyalty-api-client-integration-tests.log" force="true"/>
21+
<env name="LOYALTY_PRODUCTION_TEST_CLIENT_API_KEY" value="6b921f12-0a9c-41a9-9fec-31359f1a193e" force="true"/>
22+
<env name="LOYALTY_PRODUCTION_TEST_CLIENT_ADMIN_KEY" value="cc954f3c-99c1-4b1b-ab6d-2a7af59da7c7" force="true"/>
23+
<env name="LOYALTY_PRODUCTION_TEST_API_ENDPOINT" value="https://loyalty.b24.cloud/api/" force="true"/>
24+
</php>
1625
</phpunit>

src/ApiClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class ApiClient
2525
/**
2626
* @var string SDK version
2727
*/
28-
protected const SDK_VERSION = '1.1.0';
28+
protected const SDK_VERSION = '2.0.0';
2929
/**
3030
* @var string user agent
3131
*/

0 commit comments

Comments
 (0)