PHP code example of tcgunel / omniship-mng

1. Go to this page and download the library: Download tcgunel/omniship-mng 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/ */

    

tcgunel / omniship-mng example snippets


use Omniship\Omniship;
use Omniship\Common\Address;
use Omniship\Common\Package;
use Psr\SimpleCache\CacheInterface;

/** @var CacheInterface $cache */ // any PSR-16 cache works (Laravel's
                                  // Cache::store(), Symfony Psr16Cache, etc.)

$mng = Omniship::create('MNG');
$mng->initialize([
    // platform-wide IBM keys (from your apizone app)
    'clientId'       => env('MNG_CLIENT_ID'),
    'clientSecret'   => env('MNG_CLIENT_SECRET'),
    // per-merchant MNG account
    'customerNumber' => '1234567890',     // merchant's MNG müşteri numarası
    'password'       => 'permanent-panel-pwd',
    'identityType'   => 1,                // always 1
    'testMode'       => true,             // sandbox vs production
    'tokenCache'     => $cache,           // optional, see "JWT caching"
]);

$response = $mng->createShipment([
    'referenceId' => 'OMN-12345',         // unique, uppercased, ≤30 chars
    'shipTo' => new Address(
        name:    'Ad Soyad',
        street1: 'Örnek mah. 123. sok. No:5 Daire:7',
        city:    'Adana',
        district:'Seyhan',
        phone:   '+905555555555',
        email:   '[email protected]',
    ),
    'recipientCityCode'     => 1,         // from CBS Info — see below
    'recipientDistrictCode' => 100,
    'recipientTaxNumber'    => '11111111110', // TC Kimlik (or placeholder)
    'packages' => [
        new Package(weight: 1.0, desi: 1.0, quantity: 1, description: 'Test'),
    ],
])->send();

if ($response->isSuccessful()) {
    echo $response->getShipmentId();   // "614118757013" — MNG's shipment id
    echo $response->getTrackingNumber(); // same as shipmentId
    echo $response->getBarcode();      // "C@6B@H21FMLRPNAAA6J" — scannable
    $label = $response->getLabel();    // ZPL string for Zebra printer
    file_put_contents('label.zpl', $label->content);
} else {
    echo $response->getMessage(); // human-readable MNG error description
    echo $response->getCode();    // HTTP status
}

// in your order lifecycle hook
RegisterMngRecipientJob::dispatch($order);

// the job
public function handle(): void
{
    $mng = $this->buildCarrier($this->order->shop);
    $mng->createRecipient([
        'shipTo'                => $this->buildAddress($this->order),
        'recipientCityCode'     => $this->lookupCityCode(...),
        'recipientDistrictCode' => $this->lookupDistrictCode(...),
        'recipientTaxNumber'    => $this->order->tc_no ?: '11111111110',
    ])->send();

    $this->order->forceFill(['mng_recipient_registered_at' => now()])->save();
}

$mng->createRecipient([
    'shipTo'                => $address,    // Omniship\Common\Address
    'recipientCityCode'     => 34,
    'recipientDistrictCode' => 956,
    'recipientTaxNumber'    => '12345678901', // 11-digit TC or 10-digit Vergi
])->send();

$mng->createShipment([
    // ...usual fields...
    'createBarcode' => false,
])->send();

$response->getShipmentId();    // MNG shipment ID (12-digit numeric)
$response->getTrackingNumber(); // alias for getShipmentId()
$response->getBarcode();       // first piece's scannable barcode value
$response->getBarcodes();      // all pieces' barcode values
$response->getInvoiceId();     // MNG invoice number ("FM378349")
$response->getLabel();         // Omniship\Common\Label with ZPL content

$mng->createReturnShipment([
    'referenceId'           => 'RTN-' . $orderId,
    'shipFrom'              => $consumerAddress,
    'recipientCityCode'     => $consumerCityCode,
    'recipientDistrictCode' => $consumerDistrictCode,
    'packages'              => [...],
])->send();

echo $response->getReturnLabelUrl(); // https://mn.tc?rtnid=...

$mng->cancelShipment([
    'referenceId' => 'OMN-12345',     // your reference
    'shipmentId'  => '614118757013',  // MNG's shipment id from createShipment
])->send();

$mng->cancelOrder([
    'referenceId' => 'OMN-12345',
])->send();

// by your reference
$mng->getTrackingStatus(['referenceId' => 'OMN-12345'])->send();

// by MNG shipment id (12-digit numeric)
$mng->getTrackingStatus(['trackingNumber' => '614118757013'])->send();

$cities = $mng->getCities()->send()->getCities();
// [['code' => 1, 'name' => 'Adana'], ['code' => 34, 'name' => 'İstanbul'], ...]

$districts = $mng->getDistricts(['cityCode' => 1])->send()->getDistricts();
// [['cityCode' => 1, 'cityName' => 'Adana', 'code' => 85, 'name' => 'Çukurova'], ...]

$mng->initialize([
    // ...
    'tokenCache' => Cache::store(),  // Laravel
    // 'tokenCache' => new Symfony\Component\Cache\Psr16Cache($adapter), // Symfony
]);
bash
# All tests
docker compose run --rm php bash -c "cd omniship-mng && vendor/bin/pest"

# Specific file
docker compose run --rm php bash -c "cd omniship-mng && vendor/bin/pest tests/Message/CreateShipmentRequestTest.php"