inspirum/balikobot

PHP library for Balikobot API

v6.3.0 2021-11-16 11:59 UTC

README

Created as part of inspishop e-commerce platform by inspirum team.

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

Offers implementation of Balikobot API v2 described in the documentation

  • Support for all API requests
  • Simple add/track/drop package methods
  • All package options are accessible via setter and getter methods
  • The entire code is covered by unit and integration tests
  • Customizable Requester for easy functionality expandability (caching, etc.)

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.

Setup service

// get credentials
$apiUser = getenv('BALIKOBOT_API_USER');
$apiKey  = getenv('BALIKOBOT_API_KEY');

// init balikobot class
$requester = new Requester($apiUser, $apiKey, sslVerify: true);
$balikobot = new Balikobot($requester);

Create packages and order shipment

// create new package collection for specific shipper
$packages = new PackageCollection(Shipper::CP);

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

// add package to collection
$packages->add($package);

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

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

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

// order shipment for packages
$orderedShipment = $balikobot->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

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

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

Track packages

// track last package status
$status = $balikobot->trackPackageLastStatus($orderedPackages[0]);
/*
var_dump($status);
Inspirum\Balikobot\Model\Values\PackageStatus {
  'date'        => DateTime { '2018-07-02 09:15:01.000000' }
  'id'          => 2.2
  'name'        => 'Zásilka byla doručena příjemci.'
  'description' => 'Dodání zásilky. (77072 - Depo Olomouc 72)'
  'type'        => 'event'
}
*/

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

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

foreach($branches as $branch) {
  /*
  var_dump($branch);
  Inspirum\Balikobot\Model\Values\Branch {
    private $shipper  => '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": "^6.0"

Version

Support all options for Balikobot API v2 described in the official documentation until v1.941 (2021-11-09).

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

More details are available in changelog.

Usage

Definitons

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

Client

Support all options for Balikobot API described in given documentation.

Balikobot

Extension over Client that uses custom DTO classes as an input and output for its methods.

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.