inspirum/balikobot

PHP library for Balikobot API

v7.11.0 2024-09-05 11:45 UTC

README

Latest Stable Version Build Status Coverage Status Quality Score PHPStan Total Downloads Software License

Offers implementation of Balikobot API v2 described in the official documentation until v2.004 (2024-08-06).

If you want to use older API v1, please use ^4.0 version.

More details are available in changelog.

Usage example

See more available methods' documentation in Usage section.

All the code snippets shown here are modified for clarity, so they may not be executable.

Create packages and order shipment

use Inspirum\Balikobot\Definitions\Carrier;
use Inspirum\Balikobot\Definitions\Country;
use Inspirum\Balikobot\Definitions\Currency;
use Inspirum\Balikobot\Definitions\Service;
use Inspirum\Balikobot\Model\PackageData\DefaultPackageData;
use Inspirum\Balikobot\Model\PackageData\DefaultPackageDataCollection;
use Inspirum\Balikobot\Service\PackageService;

/** @var Inspirum\Balikobot\Service\PackageService $packageService */

// create new package collection for specific carrier
$packagesData = new DefaultPackageDataCollection(Carrier::CP);

// create new package
$packageData = new DefaultPackageData();
$packageData->setServiceType(Service::CP_NP);
$packageData->setRecName('Josef Novák');
$packageData->setRecZip('11000');
$packageData->setRecCountry(Country::CZECH_REPUBLIC);
$packageData->setRecPhone('776555888');
$packageData->setCodPrice(1399.00);
$packageData->setCodCurrency(Currency::CZK);

// add package to collection
$packagesData->add($packageData);

// upload packages to balikobot
$packages = $packageService->addPackages($packagesData);

// save package IDs
$data = [];
$data['packages'] = $packages->getPackageIds();

// save track URL for each package
foreach($packages as $package) {
  $data['trackUrl'][] = $package->getTrackUrl();
}

// order shipment for packages
$orderedShipment = $packageService->orderShipment($orderedPackages);

// save order ID and labels URL
$data['orderId'] = $orderedShipment->getOrderId();
$data['labelsUrl'] = $orderedShipment->getLabelsUrl();
$data['handoverUrl'] = $orderedShipment->getHandoverUrl();

/**
var_dump($data);
[
  'packages' => [
    0 => 42719
    1 => 42720
  ]
  'trackUrl' => [
    0 => 'https://www.postaonline.cz/trackandtrace/-/zasilka/cislo?parcelNumbers=DR00112233M'
    1 => 'https://www.postaonline.cz/trackandtrace/-/zasilka/cislo?parcelNumbers=DR00112234M' 
  ]
  'orderId' => 2757
  'labelsUrl' => 'https://pdf.balikobot.cz/cp/eNorMTIwt9A1NbYwM76cMBAXAn4.'
  'handoverUrl' => 'https://pdf.balikobot.cz/cp/eNorMTIwt9A1NbawtARcMBAhAoU.'
]
*/

Test packages data / delete packages

use Inspirum\Balikobot\Exception\Exception;

/** @var Inspirum\Balikobot\Service\PackageService $packageService */

// check if packages data is valid
try {
    $packageService->checkPackages($packagesData);
} catch (Exception $exception) {
    return $exception->getErrors();
}

// drop packages if shipment is not ordered yet
$packageService->dropPackages($packages);

Track packages

use Inspirum\Balikobot\Definitions\Status;

/** @var Inspirum\Balikobot\Service\TrackService $trackService */

// track last package status
$status = $trackService->trackPackageLastStatus($packages[0]);
/**
var_dump($status);
Inspirum\Balikobot\Model\Status\DefaultStatus {
  private $carrier => 'cp'
  private $carrierId => '1234'
  private $id => 2.2
  private $name => 'Zásilka byla doručena příjemci.'
  private $description => 'Dodání zásilky. (77072 - Depo Olomouc 72)'
  private $type => 'event'
  private $date => DateTimeImmutable { '2018-07-02 09:15:01.000000' }
}
*/
        
if (Status::isError($status->getId())) {
  // handle package delivery error
}

if ($status->getId() === Status::COD_PAID) {
  // CoD has been credited to the sender's account
}

if (Status::isDelivered($status->getId())) {
  // handle delivered package 
}

Import branches

use Inspirum\Balikobot\Definitions\Carrier;
use Inspirum\Balikobot\Definitions\Country;

/** @var Inspirum\Balikobot\Service\BranchService $branchService */

// get only branches for Zasilkovna in CZ/SK
$branches = $branchService->getBranchesForCarrierAndCountries(
  Carrier::ZASILKOVNA, 
  [Country::CZECH_REPUBLIC, Country::SLOVAKIA]
); 

foreach($branches as $branch) {
  /**
  var_dump($branch);
  Inspirum\Balikobot\Model\Branch\DefaultBranch {
    private $carrier => 'zasilkovna'
    private $service => null
    private $branchId => '10000'
    private $uid => 'VMCZ-zasilkovna-branch-10000'
    private $id => '10000'
    private $type => 'branch'
    private $name => 'Hradec Králové, Dukelská tř. 1713/7 (OC Atrium - Traficon) Tabák Traficon'
    private $city => 'Hradec Králové'
    private $street => 'Dukelská tř. 1713/7'
    private $zip => '50002'
    private $cityPart => null
    private $district => 'okres Hradec Králové'
    private $region => 'Královéhradecký kraj'
    private $country => 'CZ'
    ...
  }
  */
}

System requirements

If you are still using older PHP version, you can use this package in ^5.0 version (for PHP 7.1+).

Installation

Run composer require command:

composer require inspirum/balikobot

or add a requirement to your composer.json:

"inspirum/balikobot": "^7.0"

Setup service

Available framework integrations:

But you can also use it without any framework implementation:

use Inspirum\Balikobot\Client\DefaultClient;
use Inspirum\Balikobot\Client\DefaultCurlRequester;
use Inspirum\Balikobot\Client\Response\Validator;
use Inspirum\Balikobot\Model\Label\DefaultLabelFactory;
use Inspirum\Balikobot\Model\OrderedShipment\DefaultOrderedShipmentFactory;
use Inspirum\Balikobot\Model\Package\DefaultPackageFactory;
use Inspirum\Balikobot\Model\PackageData\DefaultPackageDataFactory;
use Inspirum\Balikobot\Model\ProofOfDelivery\DefaultProofOfDeliveryFactory;
use Inspirum\Balikobot\Model\Status\DefaultStatusFactory;
use Inspirum\Balikobot\Model\TransportCost\DefaultTransportCostFactory;
use Inspirum\Balikobot\Service\DefaultPackageService;
use Inspirum\Balikobot\Service\DefaultTrackService;

$apiUser = getenv('BALIKOBOT_API_USER');
$apiKey = getenv('BALIKOBOT_API_KEY');

$requester = new DefaultCurlRequester($apiUser, $apiKey, sslVerify: true);
$validator = new Validator();
$client = new DefaultClient($requester, $validator);

$packageService = new DefaultPackageService(
    $client,
    new DefaultPackageDataFactory(),
    new DefaultPackageFactory($validator),
    new DefaultOrderedShipmentFactory(),
    new DefaultLabelFactory(),
    new DefaultProofOfDeliveryFactory($validator),
    new DefaultTransportCostFactory($validator),
);

$trackService = new DefaultTrackService(
    $client,
    new DefaultStatusFactory($validator),
);

// ...

Usage

The module contains several helper classes that contain most of the constants needed to work with the Balikobot API.

Testing

To run unit tests, run:

composer test:test

You can also run only Unit or Integration test suites, run:

composer test:unit
composer test:integration

To show coverage, run:

composer test:coverage

To run all test (phpcs, phpstan, phpunit, etc.), run:

composer test

For testing purposes, you can use these credentials:

  • API username: balikobot_test2cztest
  • API key: #lS1tBVo

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email tomas.novotny@inspirum.cz instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.