Create a parcel - webapix/mygls-sdk GitHub Wiki

Required parameters:

use Webapix\GLS\Models\Parcel;

$parcel = new Parcel();

Set the client number. The client number is your account's id.

$parcel->setClientNumber(...);

Pickup address must to implements \Webapix\GLS\Contracts\Address interface.

$parcel->setPickupAddress(...);

Delivery info must to implements \Webapix\GLS\Contracts\Contact interface.

use Webapix\GLS\Models\Address;

$deliveryAddress = new Address('John Doe', 'countryCode', 'zip', 'City', 'Address', 'house number info');
$deliveryAddress->setContactName('John Doe');
$deliveryAddress->setContactEmail('[email protected]');
$deliveryAddress->setContactPhone('+3630123456789');

$parcel->setDeliveryInfo($deliveryAddress);

If you use the PSD (Parcel Shop Delivery) service, delivery address contains just the contact information, you should use the \Webapix\GLS\Models\Contact model.

use Webapix\GLS\Models\Contact;

$parcel->setDeliveryInfo(
    new Contact('John Doe', '[email protected]', '+3630123456789')
);

:warning: Considering the API updates pushed to production by GLS in March 2021, unfortunately, for PSD services the address data has also become mandatory. With the update the country code has become compulsory, while the rest of the data can be sent blank. Until further API changes are made in this regard, please keep using the provided Webapix\GLS\Models\Address model.

use Webapix\GLS\Models\Address;

$deliveryAddress = new Address('John Doe', 'HU', '', '', '', '');
$deliveryAddress->setContactName('John Doe');
$deliveryAddress->setContactEmail('[email protected]');
$deliveryAddress->setContactPhone('+3630123456789');

Optional parameters:

Set the cash on delivery amount:

$parcel->setCodAmount(1000);

Set the pickup date. Its optional, default value is the current date.

$parcel->setPickupDate(new \DateTime());

Add comment to the label:

$parcel->setContent('Comment');

Add client reference:

$parcel->setClientReference('order-1');

Set count of parcels sent in one shipment. Default value is: 1

$parcel->setCount(3);

Cash on delivery client reference number used for payment pairing.

$parcel->setCodReference('order-1');

You can add services to the parcel. Available services

use Webapix\GLS\Services\Exchange;

$parcel->addService(new Exchange);

Fluent syntax

You can create parcel with fluent syntax:

use Webapix\GLS\Models\Parcel;

(new Parcel)
    ->setClientNumber(1234567)
    ->setPickupAddress(<class that extends \Webapix\GLS\Contracts\Address>)
    ->setContent('My comment')
    ->setDeliveryInfo(<class that extends \Webapix\GLS\Contracts\Contact>);

You can add parameters conditionally:

use Webapix\GLS\Models\Parcel;

(new Parcel)
    ->when($order->isCodPayment(), function (Parcel $parcel) use ($order) {
        return $parcel->setCodAmount($order->totalPrice());
    });

ParcelFormattable interface

use Webapix\GLS\Models\Parcel;

class Order implements \Webapix\GLS\Contracts\ParcelFormattable
{
    public function toParcel(): Parcel
    {
        return (new Parcel)
            ->setClientNumber($this->GLSAccount()->clientNumber())
            ->setDeliveryInfo($this->deliveryInfo)
            ->setPickupAddress($this->pickupAddress)
            ->setContent($this->comment);
    }

    //...
}