PHP code example of nikba / laravel-bussystem-api

1. Go to this page and download the library: Download nikba/laravel-bussystem-api 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/ */

    

nikba / laravel-bussystem-api example snippets


use Nikba\LaravelBussystemApi\Data\SearchCriteria;
use Nikba\LaravelBussystemApi\Enums\SortType;
use Nikba\LaravelBussystemApi\Facades\BusSystem;

$criteria = SearchCriteria::create()
    ->date('2026-09-15')
    ->from(3)
    ->to(7)
    ->bus()
    ->directOnly()
    ->sortBy(SortType::Price)
    ->currency('EUR')
    ->language('en');

$routes = BusSystem::getRoutes($criteria);

$points = BusSystem::getPoints(['lang' => 'en']);

$intervalId = (string) $selectedRoute['interval_id'];

$seats = BusSystem::getFreeSeats($intervalId);
$discounts = BusSystem::getDiscounts($intervalId);
$baggage = BusSystem::getBaggage($intervalId);

use Nikba\LaravelBussystemApi\Data\BookingData;
use Nikba\LaravelBussystemApi\Exceptions\BusSystemValidationException;

$booking = BookingData::create('EUR', 'en')
    ->addRoute('2026-09-15', $intervalId)
    ->addPassenger(
        firstName: 'John',
        lastName: 'Doe',
        birthDate: '1990-01-15',
        docType: 1,
        docNumber: 'AB123456',
        gender: 'M',
    )
    ->addSeat(routeIndex: 0, seat: '12')
    ->setContactInfo('+37360000000', '[email protected]');

$validationErrors = $booking->validate();

if ($validationErrors !== []) {
    throw new BusSystemValidationException(
        implode('; ', $validationErrors),
        context: ['errors' => $validationErrors],
    );
}

$order = BusSystem::createOrder($booking);

$orderId = (int) $order['order_id'];

$tickets = BusSystem::buyTickets($orderId, 'en');

$reservation = BusSystem::reserveTickets($orderId, [
    'phone' => '+37360000000',
    'email' => '[email protected]',
    'lang' => 'en',
]);

use Nikba\LaravelBussystemApi\Contracts\BusSystemClientInterface;
use Nikba\LaravelBussystemApi\Data\SearchCriteria;

final class SearchRoutes
{
    public function __construct(
        private readonly BusSystemClientInterface $busSystem,
    ) {}

    public function handle(int $fromCityId, int $toCityId, string $date): array
    {
        return $this->busSystem->getRoutes(
            SearchCriteria::create()
                ->date($date)
                ->from($fromCityId)
                ->to($toCityId),
        );
    }
}

$flights = BusSystem::getRoutes(
    SearchCriteria::create()
        ->date('2026-10-10')
        ->airportFrom('KIV')
        ->airportTo('VIE')
        ->air()
        ->airPassengers(adults: 2, children: 1)
        ->airServiceClass('E')
        ->airDirect(false),
);

$booking = BookingData::create()
    ->addRoute('2026-09-15', 'outbound-interval')
    ->addRoute('2026-09-22', 'return-interval')
    ->addPassenger('John', 'Doe', '1990-01-15', 1, 'AB123456')
    ->addSeats(0, ['12'])
    ->addSeats(1, ['8'])
    ->setContactInfo('+37360000000', '[email protected]');

$order = BusSystem::getOrder($orderId, $securityCode, 'en');

$ticket = BusSystem::getTicket([
    'ticket_id' => $ticketId,
    'security' => $ticketSecurityCode,
    'lang' => 'en',
]);

$cancellation = BusSystem::cancelTickets([
    'order_id' => $orderId,
    'security' => $securityCode,
    'lang' => 'en',
]);

use Nikba\LaravelBussystemApi\Enums\SortType;
use Nikba\LaravelBussystemApi\Enums\TransportType;

$criteria
    ->transport(TransportType::Train)
    ->sortBy(SortType::Time);

use Illuminate\Support\Facades\Log;
use Nikba\LaravelBussystemApi\Exceptions\BusSystemApiException;
use Nikba\LaravelBussystemApi\Exceptions\BusSystemAuthenticationException;
use Nikba\LaravelBussystemApi\Exceptions\BusSystemException;
use Nikba\LaravelBussystemApi\Exceptions\BusSystemValidationException;

try {
    $routes = BusSystem::getRoutes($criteria);
} catch (BusSystemAuthenticationException $exception) {
    report($exception);
    abort(502, 'BusSystem authentication failed.');
} catch (BusSystemValidationException $exception) {
    return response()->json([
        'message' => $exception->getMessage(),
        'context' => $exception->getContext(),
    ], 422);
} catch (BusSystemApiException $exception) {
    Log::warning('BusSystem request failed', [
        'message' => $exception->getMessage(),
    ]);
    abort(503, 'The transport service is temporarily unavailable.');
} catch (BusSystemException $exception) {
    report($exception);
    abort(500);
}

use Nikba\LaravelBussystemApi\Enums\OrderStatus;
use Nikba\LaravelBussystemApi\Models\Order;

$order = Order::create([
    'order_id' => $response['order_id'],
    'security_code' => $response['security'],
    'status' => $response['status'],
    'currency' => 'EUR',
    'api_response' => $response,
]);

$activeOrders = Order::query()->active()->get();
$paidOrders = Order::query()->paid()->get();

if ($order->status() === OrderStatus::Buy) {
    // The order is paid.
}

use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use Nikba\LaravelBussystemApi\Services\BusSystemClient;

$handler = new MockHandler([
    new Response(200, [], json_encode(['status' => 'ok'], JSON_THROW_ON_ERROR)),
]);

$client = new BusSystemClient(
    apiUrl: 'https://example.test',
    login: 'test-login',
    password: 'test-password',
    httpClient: new Client(['handler' => HandlerStack::create($handler)]),
);

$response = $client->ping();
bash
php artisan vendor:publish --tag=bussystem-config
bash
php artisan vendor:publish --tag=bussystem-migrations
php artisan migrate