PHP code example of alexanderpoellmann / laravel-post-plc

1. Go to this page and download the library: Download alexanderpoellmann/laravel-post-plc 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/ */

    

alexanderpoellmann / laravel-post-plc example snippets


use AlexanderPoellmann\LaravelPostPlc\Enums\PostProductCodes;
use AlexanderPoellmann\LaravelPostPlc\Shipping\PostPlcShippingAdapter;
use AlexanderPoellmann\Shipping\Data\Address;
use AlexanderPoellmann\Shipping\Data\Parcel;
use AlexanderPoellmann\Shipping\Data\Shipment;

$adapter = app(PostPlcShippingAdapter::class)
    ->forProduct(PostProductCodes::PaketOesterreich);

$result = $adapter->createShipment(new Shipment(
    sender: new Address('Sender GmbH', 'Rochusmarkt', '1030', 'Wien', 'AT', houseNumber: '5'),
    recipient: new Address('Recipient GmbH', 'Hauptplatz', '4020', 'Linz', 'AT', houseNumber: '1'),
    parcels: [new Parcel(weightInGrams: 1200, lengthInMillimeters: 300, widthInMillimeters: 200, heightInMillimeters: 100)],
    reference: 'ORDER-42',
));

foreach ($result->trackingNumbers as $trackingNumber) {
    $trackingNumber->value; // String, preserving leading zeros.
}

foreach ($result->labels as $label) {
    $label->contents; // Decoded PDF bytes or raw ZPL text.
    $label->mimeType;
    $label->trackingNumber; // Null for a document covering multiple parcels.
}

use AlexanderPoellmann\LaravelPostPlc\Enums\LabelSizes;
use AlexanderPoellmann\LaravelPostPlc\Enums\PaperLayouts;
use AlexanderPoellmann\LaravelPostPlc\Enums\PrinterLanguages;

$thermal = $adapter->withPrinter(
    language: PrinterLanguages::ZPL2,
    labelSize: LabelSizes::SHORT,
    paperLayout: PaperLayouts::SHORT,
);

foreach (app()->tagged('shipping.adapters') as $carrier) {
    $carrier->carrier();
}

use AlexanderPoellmann\LaravelPostPlc\Classes\Address;
use AlexanderPoellmann\LaravelPostPlc\Classes\Collo;
use AlexanderPoellmann\LaravelPostPlc\Classes\Shipment;
use AlexanderPoellmann\LaravelPostPlc\DataTransferObjects\FeatureRow;
use AlexanderPoellmann\LaravelPostPlc\Enums\PostProductCodes;
use AlexanderPoellmann\LaravelPostPlc\Enums\ServiceMethods;
use AlexanderPoellmann\LaravelPostPlc\Facades\LaravelPostPlc;

$from = (new Address)
    ->name('Absender GmbH')
    ->street('Rochusmarkt 5')
    ->postCode('1030')
    ->city('Wien')
    ->countryCode('AT')
    ->phone('+431234567')
    ->email('[email protected]')
    ->get();

$to = (new Address)
    ->name('Musterfirma GmbH', 'c/o Maria Muster')
    ->street('Landesgerichtsstraße 1')
    ->postCode('1010')
    ->city('Wien')
    ->countryCode('AT')
    ->email('[email protected]')
    ->get();

$shipment = (new Shipment)
    ->withPrinter()
    ->withNumber('ORDER-12345')
    ->using(PostProductCodes::PaketPremiumOesterreichB2B)
    ->from($from)
    ->to($to)
    ->withFeatures([
        FeatureRow::cashOnDelivery(
            amount: 199.99,
            currency: 'EUR',
            iban: 'AT000000000000000000',
            bic: 'BICCODE',
            accountHolder: 'Muster GmbH',
            paymentReference: 'ORDER-12345',
        ),
    ])
    ->parcels([
        (new Collo)->weight(0.4)->get(),
    ])
    ->get();

LaravelPostPlc::request(ServiceMethods::ImportShipment, $shipment, asRow: true);

$result = LaravelPostPlc::toObject();

use AlexanderPoellmann\LaravelPostPlc\Validation\ShipmentValidator;

$validation = app(ShipmentValidator::class)->validate($shipment);
$validation->throwIfInvalid();

use AlexanderPoellmann\LaravelPostPlc\Resolvers\AllowedServicesResolver;

$capabilities = app(AllowedServicesResolver::class)->forCountries('DE');

foreach ($capabilities->products() as $product) {
    $product->code->value;
    $product->name;
    $product->contractProduct; // PLC's "Vertragsprodukt Ja/Nein" flag
    $product->order;
    $product->featureCodes();
}

$capabilities->allowsProduct('45');
$capabilities->allowsFeature('45', '054');

// config/post-plc.php
'capabilities' => [
    'policy' => [
        'enabled_products' => ['10', '45'],
        'disabled_products' => [],
        'disabled_features' => [
            '*' => ['006'],      // disable COD globally
            '45' => ['054'],     // disable sender info on this product
        ],
        'preferred_products' => [
            'AT' => '10',
            'DE' => '45',
            '*' => '70',
        ],
        'fallback_product' => null,
    ],
],

use AlexanderPoellmann\LaravelPostPlc\Policies\ServicePolicy;

$policy = app(ServicePolicy::class);

$policy->allowsProduct($capabilities, '45');
$policy->allowsFeature($capabilities, '45', '054');
$preferred = $policy->preferredProduct($capabilities, 'DE');

use AlexanderPoellmann\LaravelPostPlc\ValueObjects\ProductCode;

$shipment = (new Shipment)
    ->using(ProductCode::from('NEW-PRODUCT'))
    ->to($to)
    ->get();

FeatureRow::make('999', 'value');

use AlexanderPoellmann\LaravelPostPlc\Returns\ReturnLabelService;
use AlexanderPoellmann\LaravelPostPlc\Returns\ReturnShipmentFactory;

$returnShipment = app(ReturnShipmentFactory::class)->fromAddresses(
    sender: $customerAddress,
    recipient: $warehouseAddress,
);

$result = app(ReturnLabelService::class)->createLabel($returnShipment);

$pdf = $result->pdfData;

$result = app(ReturnLabelService::class)->createQr($returnShipment);

$qrPngBase64 = $result->qrCode;
$code128Base64 = $result->code128;

$returnShipment = app(ReturnShipmentFactory::class)->fromOutbound(
    outbound: $originalShipment,
    returnRecipient: $warehouseAddress,
);

use AlexanderPoellmann\LaravelPostPlc\Enums\PostProductCodes;

$returnShipment = app(ReturnShipmentFactory::class)->fromOutbound(
    $originalShipment,
    $warehouseAddress,
    PostProductCodes::RetourpaketInternationalStandard,
    parcels: [(new \AlexanderPoellmann\LaravelPostPlc\Classes\Collo)->weight(1.2)->get()],
);

// config/post-plc.php
'profiles' => [
    'store-b' => [
        'client_id' => env('PLC_STORE_B_CLIENT_ID'),
        'org_unit_id' => env('PLC_STORE_B_ORG_UNIT_ID'),
        'org_unit_guid' => env('PLC_STORE_B_ORG_UNIT_GUID'),
        'identifier' => 'Store B',
        'sandbox' => false,
    ],
],

use AlexanderPoellmann\LaravelPostPlc\LaravelPostPlc;

$client = app(LaravelPostPlc::class)->forProfile('store-b');

$factory = new ReturnShipmentFactory($client);
$labels = new ReturnLabelService($client, app(ShipmentValidator::class));
$capabilities = new AllowedServicesResolver($client, app(\Illuminate\Contracts\Cache\Repository::class));

$importer = (new Address)
    ->name('Importer GmbH')
    ->street('Importweg 1')
    ->postCode('1010')
    ->city('Wien')
    ->countryCode('AT')
    ->customsDutyAccountNumber('...')
    ->customsTaxAccountNumber('...')
    ->provinceCode('AT-9')
    ->get();

$shipment = (new Shipment)
    // ...
    ->importer($importer)
    ->referenceBarcodeType('C')
    ->get();
bash
php artisan vendor:publish --tag="laravel-post-plc-config"