PHP code example of rarus / bonus-server-php-sdk

1. Go to this page and download the library: Download rarus/bonus-server-php-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rarus / bonus-server-php-sdk example snippets


$client = RarusLMS::client('API_URL','API_TOKEN');

$card = $client->cards()->getById(1); // Возвращает Cards\DTO\CardDto
$user = $client->users()->getById(1); // Возвращает Users\DTO\UserDto

// Сериализация в массив
$payload = $card->toArray();

use GuzzleHttp\HandlerStack;
use GuzzleHttp\MessageFormatter;
use GuzzleHttp\Middleware;
use Money\Currency;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Rarus\LMS\SDK\RarusLMS;
use Rarus\LMS\SDK\Users\DTO\Fabric;
use Rarus\LMS\SDK\Users\DTO\Factory;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Psr16Cache;

$logger = new Logger('rarus-bonus-service');
$logger->pushHandler(
    new StreamHandler(
        getenv('LOG_FILE'), 
        getenv('LOG_LEVEL'), 
    )
);

$guzzleHandlerStack = HandlerStack::create();
$guzzleHandlerStack->push(
    Middleware::log(
        $logger,
        new MessageFormatter(MessageFormatter::DEBUG)
    )
);

$httpClient = new GuzzleHttp\Client([
    'base_uri' => getenv('API_URL'),
    'connect_timeout' => 10,
    'headers' => [
        'Accept' => 'application/json',
    ],
    'handler' => $guzzleHandlerStack,  
]);

$psr6Cache = new FilesystemAdapter(); 
$psr16Cache = new Psr16Cache($psr6Cache);

$client = RarusLMS::factory()
    ->setApiKey(getenv('API_TOKEN'))
    ->setHttpClient($httpClient)
    ->setLogger($logger)
    ->setCurrency(new Currency('RUB'))
    ->setDateTimeZone(new DateTimeZone('Europe/Moscow'))
    ->setCache($psr16Cache)
    ->build();

$userDto = Factory::create()
    ->withName('Иванов Иван')
    ->withPhone('+7(900)000-00-00')
    ->withShopId(1)
    ->withEmail('[email protected]')
    ->build();
    
$newUser = $client->users()->createUser($userDto); // Возвращает Users\DTO\UserDto

$user = $client->users(300)->getUserById(1); // кеширует ответ на 5 мин
$card = $client->cards()->getCardById(1);

$card = $client->cards()->getById(1); // Возвращает Cards\DTO\CardDto
$user = $client->users()->getById(1); // Возвращает Users\DTO\UserDto

// Сериализация в массив
$payload = $card->toArray();

use Money\Money;
use Money\Currency;

// В fromArray:
$balance = MoneyParser::parse($data['balance'] ?? 0.0, $currency);

// В toArray (для JSON):
'balance'  => MoneyParser::toString($this->balance),

use Money\Currency;
$card = $client->cards()->getById(1);
$balanceMoney = $card->balance->getAmount();
echo MoneyParser::convertMoneyToString($balanceMoney, new Currency('RUB')); // 509,7050

$cached = $card->toArray();
$again = Cards\DTO\CardDto::fromArray($cached);

try {
    $user = $this->client->users()->getUserById($userId);
} catch (NetworkException $e) {
    // ошибка сетевого уровня
} catch (ApiClientException $e) {
    // ошибка, возвращённая сервером API
} catch (UnknownException $e) {
    // непредусмотренная ошибка
}
bash
composer