nebijokit/omniva

Library to communicate with Omniva API

v0.2.1 2018-07-31 08:11 UTC

This package is not auto-updated.

Last update: 2024-11-18 18:28:42 UTC


README

PHP wrapper for courier Omniva integration.

Build Status Maintainability Total Downloads

SensioLabsInsight Scrutinizer Code Quality

Examples

Get label

Returns Label response (stdClass object) with encoded PDF & barcode. For Response structure view getLabel phpdoc.

$client = new Client($username, $password);
$client->getLabel($parcel);

Get pickup points list

This endpoint returns list of pickup points. Pickup point can be Terminal or Post office. Field Type determines whether field is Terminal (Type: 0) or Post office (Type: 1).

// username & password is not necessary for pickup points
$client = new Client($username, $password);

$points = $client->getPickupPoints();

Create Shipment & get Label

use Omniva\Parcel;
use Omniva\Client;
use Omniva\Address;
use Omniva\Service;

$client = new Client($username, $password);

$shipment = $parcel->getShipment();
$id = $parcel->getId();

$omnivaParcel = new Parcel();
$omnivaParcel
    ->setWeight($parcel->getWeightInKg())
    ->setPartnerId($id)
    ->setComment($shipment->getRemark())
;

if ($shipment->getCodAmount()) {
    $omnivaParcel->setCodAmount($shipment->getCodAmount());
    $omnivaParcel->addService(Service::COD());
}

$sender = new Address();
$sender
    ->setName($shipment->getSender()->getName())
    ->setPhone($shipment->getSender()->getPhone())
    ->setCountryCode($shipment->getSender()->getCountry()->getCode())
    ->setCity($shipment->getSender()->getCity())
    ->setStreet($shipment->getSender()->getStreet())
    ->setPostCode($shipment->getSender()->getPostalCode())
;

if ($shipment->getSender()->getEmail()) {
    $sender->setEmail($shipment->getSender()->getEmail());
}

$omnivaParcel->setSender($sender);

$returnee = clone $sender;
$omnivaParcel->setReturnee($returnee);

$receiver = new Address();
$receiver
    ->setName($shipment->getReceiver()->getName())
    ->setCountryCode($shipment->getReceiver()->getCountry()->getCode())
;

if ($shipment->getReceiver()->getEmail()) {
    $receiver->setEmail($shipment->getReceiver()->getEmail());
}

if ($shipment->getReceiver()->getPhone()) {
    $receiver->setPhone($shipment->getReceiver()->getPhone());
}

if ($shipment->getReceiver()->isTypeTerminal()) {
    $terminal = $shipment->getReceiver()->getTerminal();

    $pickupPoint = new PickupPoint($terminal->getIdentifier());
    $pickupPoint->setType($terminal->isPostOffice() ? PickupPoint::TYPE_POST_OFFICE : PickupPoint::TYPE_TERMINAL);

    $receiver->setPickupPoint($pickupPoint);

    if ($shipment->getReceiver()->getPhone()) {
        $omnivaParcel->addService(Service::SMS());
    }

    if ($shipment->getReceiver()->getEmail()) {
        $omnivaParcel->addService(Service::EMAIL());
    }
} else {
    $receiver
        ->setCity($shipment->getReceiver()->getCity())
        ->setStreet($shipment->getReceiver()->getStreet())
        ->setPostCode($shipment->getReceiver()->getPostalCode())
    ;
}
$omnivaParcel->setReceiver($receiver);

$response = $client->createShipment($omnivaParcel);

$trackingNumber = $response->savedPacketInfo->barcodeInfo->barcode;
$parcel->setTrackingNumber($trackingNumber);
$omnivaParcel->setTrackingNumber($trackingNumber);

$response = $client->getLabel($omnivaParcel);

// PDF content
$content = $response->successAddressCards->addressCardData->fileData;

Ideas for further development

  • implement Client tracking
  • add Symfony\Constraint for data validation