PHP code example of smart-dato / dhl-parcel-returns-sdk

1. Go to this page and download the library: Download smart-dato/dhl-parcel-returns-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/ */

    

smart-dato / dhl-parcel-returns-sdk example snippets


use SmartDato\DhlParcelReturns\Data\Orders\ContactAddressData;
use SmartDato\DhlParcelReturns\Data\Orders\ReturnOrderData;
use SmartDato\DhlParcelReturns\Data\Orders\ValueData;
use SmartDato\DhlParcelReturns\Data\Orders\WeightData;
use SmartDato\DhlParcelReturns\Enums\Currency;
use SmartDato\DhlParcelReturns\Enums\LabelType;
use SmartDato\DhlParcelReturns\Enums\WeightUom;
use SmartDato\DhlParcelReturns\Facades\DhlParcelReturns;

$confirmation = DhlParcelReturns::orders()->create(
    data: new ReturnOrderData(
        receiverId: 'deu',
        shipper: new ContactAddressData(
            name1: 'Max Mustermann',
            addressStreet: 'Charles-de-Gaulle Str.',
            addressHouse: '20',
            postalCode: '53113',
            city: 'Bonn',
            email: '[email protected]',
        ),
        customerReference: 'Order #12345',
        itemWeight: new WeightData(WeightUom::Kilograms, 1),
        itemValue: new ValueData(value: 10, currency: Currency::Eur),
    ),
    labelType: LabelType::Both,
);

$confirmation->shipmentNo;        // "999991587211"
$confirmation->label->b64;        // base64-encoded shipment label
$confirmation->qrLabel?->b64;     // base64-encoded QR label (when requested)
$confirmation->qrLink;            // deep link to import the QR code into the Post & DHL app
$confirmation->routingCode;       // "40327653113+99000933090010"

use SmartDato\DhlParcelReturns\Data\Orders\CommodityData;
use SmartDato\DhlParcelReturns\Data\Orders\CustomsDetailsData;
use SmartDato\DhlParcelReturns\Enums\CountryOfOrigin;

$confirmation = DhlParcelReturns::orders()->create(
    data: new ReturnOrderData(
        receiverId: 'che',
        shipper: $shipper,
        itemWeight: new WeightData(WeightUom::Grams, 1200),
        itemValue: new ValueData(value: 1012.99),
        customsDetails: new CustomsDetailsData(
            items: [
                new CommodityData(
                    itemDescription: 'T-Shirt',
                    packagedQuantity: 1,
                    itemWeight: new WeightData(WeightUom::Grams, 500),
                    itemValue: new ValueData(value: 1000),
                    countryOfOrigin: CountryOfOrigin::FRA,
                    hsCode: '61099090',
                ),
            ],
        ),
    ),
);

use SmartDato\DhlParcelReturns\Enums\Country;
use SmartDato\DhlParcelReturns\Facades\DhlParcelReturns;

$receivers = DhlParcelReturns::locations()->get(
    country: Country::Deu,
    postalCode: '53113',
    maxResult: 10,
);

foreach ($receivers as $receiver) {
    $receiver->receiverId;
    $receiver->billingNumber;
    $receiver->receiverAddress->city;
}

$version = DhlParcelReturns::general()->version();

$version->version; // "v1.0.0"
$version->env;     // "production"

use SmartDato\DhlParcelReturns\DhlParcelReturns;

// With OAuth2 (recommended)
$dhl = DhlParcelReturns::make([
    'api_key' => 'your-client-id',
    'client_secret' => 'your-client-secret',
    'username' => 'your-business-customer-username',
    'password' => 'your-business-customer-password',
    'sandbox' => true,
]);

// Or with legacy API key
$dhl = DhlParcelReturns::make([
    'api_key' => 'your-api-key',
    'sandbox' => true,
]);

$confirmation = $dhl->orders()->create($data);

$dhl = DhlParcelReturns::make([
    'api_key' => 'your-api-key',
    'base_url' => 'https://api-eu.dhl.com/parcel/de/shipping/returns/v1',
]);

use SmartDato\DhlParcelReturns\Exceptions\DhlParcelReturnsApiException;

try {
    DhlParcelReturns::orders()->create($data);
} catch (DhlParcelReturnsApiException $e) {
    $e->getMessage();  // "Unprocessable: ..."
    $e->getCode();     // 422
    $e->detail;        // RFC 7807 detail
    $e->instance;      // RFC 7807 instance URI
}