PHP code example of ernadoo / mondial-relay-bundle

1. Go to this page and download the library: Download ernadoo/mondial-relay-bundle 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/ */

    

ernadoo / mondial-relay-bundle example snippets


return [
    // ...
    Ernadoo\MondialRelayBundle\ErnadooMondialRelayBundle::class => ['all' => true],
];

use Ernadoo\MondialRelay\Contract\MondialRelayClientInterface;
use Ernadoo\MondialRelay\Shipment\Address;
use Ernadoo\MondialRelay\Shipment\DeliveryMode;
use Ernadoo\MondialRelay\Shipment\Parcel;
use Ernadoo\MondialRelay\Shipment\ShipmentRequest;

final class ShippingService
{
    public function __construct(
        private readonly MondialRelayClientInterface $mondialRelay,
    ) {
    }

    public function createLabel(): string
    {
        $request = new ShipmentRequest(
            sender: new Address(
                countryCode: 'FR', postCode: '59510', city: 'Hem',
                streetName: '4 Av. Antoine Pinay', firstName: 'Erwan', lastName: 'Nader',
                mobileNo: '+33600000000',
            ),
            recipient: new Address(
                countryCode: 'FR', postCode: '75001', city: 'Paris',
                streetName: '1 Rue de la Paix', firstName: 'Jane', lastName: 'Doe',
                mobileNo: '+33600000001',
            ),
            parcels: [new Parcel(weightGrams: 500, content: 'Clothes')],
            deliveryMode: DeliveryMode::RELAY,
            deliveryLocation: 'FR-066974', // relay point ID, e.g. from the picker below
            // Empty deliveryLocation: Mondial Relay lets the recipient choose (notified by SMS/email)
        );

        $response = $this->mondialRelay->createShipment($request);

        // $response->trackingUrl is the public tracking link
        return $response->labelOutput; // PDF label URL
    }
}

use Ernadoo\MondialRelay\Exception\MondialRelayException;
use Ernadoo\MondialRelayBundle\Translation\MondialRelayErrorMessage;

try {
    $label = $shipping->createLabel();
} catch (MondialRelayException $e) {
    $this->logger->error('Label not created: {message}', ['message' => $e->getMessage(), 'exception' => $e]);
    $this->addFlash('danger', MondialRelayErrorMessage::fromException($e)); // {{ message|trans }} in Twig
    // or, as a string: MondialRelayErrorMessage::fromException($e)->trans($translator)
}

use Ernadoo\MondialRelay\ParcelShop\ParcelShopSearchRequest;

$shops = $this->mondialRelay->searchParcelShops(
    new ParcelShopSearchRequest(countryCode: 'FR', postCode: '75001'),
);

foreach ($shops as $shop) {
    // $shop->id, $shop->name, $shop->locationCode() …
}