PHP code example of maksekeskus / php-shipping-sdk

1. Go to this page and download the library: Download maksekeskus/php-shipping-sdk 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/ */

    

maksekeskus / php-shipping-sdk example snippets




akeCommerceShipping\SDK\Environment;
use MakeCommerceShipping\SDK\Http\MakeCommerceClient;

$appInfo = [
    'module'           => 'MyShippingPlugin',
    'module_version'   => '1.0.0',
    'platform'         => 'WooCommerce', // Name of the platform you are using
    'platform_version' => '8.0.0',
];

$mcs = new MakeCommerceClient(
    Environment::TEST,    // Environment::TEST or Environment::LIVE
    'YOUR_SHOP_ID',       // From merchant portal
    'YOUR_SECRET_KEY',    // From merchant portal
    'your-persistent-uuid',// Generated once per installation; stored and reused on every request
    $appInfo               // Optional
);

$mcs->setLocale('et'); // Default is 'en'

$response = $mcs->connectShop(
    'https://myshop.com',                    // REMOTE_ADDR — the site that will host the iframe
    'https://myshop.com/order/{id}/view',    // optional orderUrl
    'https://myshop.com/webhook'             // optional webhookUrl
);

$iframeUrl = $mcs->getIframeUrl($response->body->jwt);

echo '<iframe src="' . $iframeUrl . '" width="100%" height="720px"></iframe>';

$iframeUrl = $mcs->getIframeUrl($response->body->jwt);

echo '<iframe src="' . $iframeUrl . '" width="100%" height="720px"></iframe>';

$rates = $mcs->getRates([
    'weight'      => 2500,  // grams — rounded to int automatically
    'destination' => 'EE',  // 2-letter ISO country code
]);

$pickupPoints = $mcs->listCarrierDestinations('omniva', 'EE');

// Single shipment
$shipment = $mcs->createShipment(
    'omniva',
    MakeCommerceClient::TYPE_PICKUPPOINT,
    [
        [
            'order' => [
                'id'        => '1',
                'reference' => 'ORDER-REF-001',
            ],
            'destination' => [
                'id'      => '11701',  // pickup point ID from listCarrierDestinations()
                'country' => 'EE',
            ],
            'recipient' => [
                'name'  => 'John Smith',
                'phone' => '+37256123123',
                'email' => '[email protected]',
            ],
        ]
    ]
);

// Multiple shipments — each shipment is one element in the array
$shipments = $mcs->createShipment('omniva', MakeCommerceClient::TYPE_PICKUPPOINT, [
    ['order' => [...], 'destination' => [...], 'recipient' => [...]],
    ['order' => [...], 'destination' => [...], 'recipient' => [...]],
]);

$shipment = $mcs->createShipment(
    'omniva',
    MakeCommerceClient::TYPE_COURIER,
    [
        [
            'order'       => ['id' => '1', 'reference' => 'ORDER-REF-001'],
            'destination' => [
                'zip'     => '10134',
                'country' => 'EE',
                'county'  => 'Harjumaa',
                'city'    => 'Tallinn',
                'street'  => 'Vana Lõuna 12',
            ],
            'recipient'   => [
                'name'  => 'John Smith',
                'phone' => '+37256123123',
                'email' => '[email protected]',
            ],
        ]
    ]
);

$result = $mcs->getShipments();                             // server default page size
$result = $mcs->getShipments(20);                           // first page of 20
$result = $mcs->getShipments(20, $result->nextPageToken);   // next page

$shipment = $mcs->getShipment('204138EE568');

$updated = $mcs->updateShipment(
    '204138EE568', // trackingId from createShipment()
    'omniva',
    MakeCommerceClient::TYPE_PICKUPPOINT,
    [
        [
            'order'       => ['id' => '1', 'reference' => 'ORDER-REF-001'],
            'destination' => ['id' => '9002', 'country' => 'EE'],
            'recipient'   => ['name' => 'John Smith', 'phone' => '+37256123123', 'email' => '[email protected]'],
        ]
    ]
);

$shipment = $mcs->createShipment(...);

header('Content-Type: application/pdf');
echo $mcs->getLabel($shipment->trackingId); // '204138EE568', not the UUID

$shipment = $mcs->getShipment('204138EE568');

if ($shipment->status === 'FAILED') {
    // getLabel() will fail for this shipment.
}

// Verify first, decode second.
if (!$mcs->validateWebhook($_POST)) {
    http_response_code(400);
    return; // MAC mismatch — discard the notification
}

$update = json_decode($_POST['json']);

$update->order_id;         // the order ID you passed when creating the shipment
$update->tracking_id;      // e.g. '204219EE566'
$update->shipment_status;  // numeric status code, e.g. '242'

$response = $mcs->connectShop('https://myshop.com');

$response->code;        // 200
$response->body->jwt;   // JWT string — pass to getIframeUrl()
$response->rawBody;     // Raw JSON string
$response->headers;     // Array of response headers

use MakeCommerceShipping\SDK\Exception\MCException;
use GuzzleHttp\Exception\GuzzleException;

try {
    $shipment = $mcs->createShipment('omniva', MakeCommerceClient::TYPE_PICKUPPOINT, $shipmentData);
} catch (MCException $e) {
    $status  = $e->getCode();     // e.g. 401
    $message = $e->getMessage();  // e.g. "Invalid credentials" — the API's own message

    // The full response, when the failure came from the API
    if ($response = $e->getResponse()) {
        $response->rawBody;  // the raw error body
        $response->body;     // decoded, e.g. ->message
        $response->headers;
    }
} catch (GuzzleException $e) {
    // Network failure — the request never got an HTTP response
    error_log('Network error: ' . $e->getMessage());
}

try {
    $shipment = $mcs->getShipment($trackingId);
} catch (MCException $e) {
    if ($e->getCode() === 404) {
        // No such shipment
    } else {
        throw $e;
    }
}



akeCommerceShipping\SDK\Environment;
use MakeCommerceShipping\SDK\Http\MakeCommerceClient;
use MakeCommerceShipping\SDK\Exception\MCException;
use GuzzleHttp\Exception\GuzzleException;

$appInfo = [
    'module'           => 'MyShippingPlugin',
    'module_version'   => '1.0.0',
    'platform'         => 'WooCommerce',
    'platform_version' => '8.0.0',
];

$mcs = new MakeCommerceClient(
    Environment::TEST,
    'YOUR_SHOP_ID',
    'YOUR_SECRET_KEY',
    'my-woocommerce-plugin',
    $appInfo
);

try {
    // --- REQUIRED FIRST-TIME SETUP (steps 1–3 must be done before anything else) ---

    // Step 1: Connect the shop.
    $response = $mcs->connectShop(
        'https://myshop.com',
        'https://myshop.com/order/{id}/view',  // optional orderUrl
        'https://myshop.com/webhook'           // optional webhookUrl
    );

    // Step 2: Embed the shipping manager iframe on your settings/onboarding page
    $iframeUrl = $mcs->getIframeUrl($response->body->jwt);
    echo '<iframe src="' . $iframeUrl . '" width="100%" height="720px"></iframe>';

    // Step 3: The merchant fills in their sender address
    // inside the iframe. Nothing to call here — the merchant does this in the browser.
    // The features below are only available after the merchant completes that step.

    // --- NORMAL SDK USAGE (only after setup is complete) ---

    $rates = $mcs->getRates(['weight' => 1500, 'destination' => 'EE']); // weight in grams; price in response is in cents

    $pickupPoints = $mcs->listCarrierDestinations('omniva', 'EE');

    // Create a pickup point shipment
    $shipment = $mcs->createShipment(
        'omniva',
        MakeCommerceClient::TYPE_PICKUPPOINT,
        [
            [
                'order' => [
                    'id'        => '42',
                    'reference' => 'ORDER-42',
                ],
                'destination' => [
                    'id'      => '9002',
                    'country' => 'EE',
                ],
                'recipient' => [
                    'name'  => 'Jane Doe',
                    'phone' => '+37256000000',
                    'email' => '[email protected]',
                ],
            ]
        ]
    );

    header('Content-Type: application/pdf');
    echo $mcs->getLabel($shipment->trackingId);

} catch (MCException $e) {
    error_log('MakeCommerce API error [' . $e->getCode() . ']: ' . $e->getMessage());
} catch (GuzzleException $e) {
    error_log('Network error: ' . $e->getMessage());
}
bash
composer