PHP code example of texhub / laklak-b2b

1. Go to this page and download the library: Download texhub/laklak-b2b 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/ */

    

texhub / laklak-b2b example snippets


use TexHub\LaklakB2b\LakLak;
use TexHub\LaklakB2b\Enums\Environment;

$laklak = LakLak::make('YOUR_API_KEY', Environment::Test); // Environment::Production when live

// Addresses
$cities = $laklak->addresses()->cities('Dush');
$terminals = $laklak->addresses()->parcelTerminals(cityId: 39720);
$pickups = $laklak->addresses()->pickupPoints(cityId: 39720);

foreach ($cities->data() as $city) {
    echo $city['id'] . ' ' . $city['name'] . PHP_EOL;
}

use TexHub\LaklakB2b\Requests\ShipmentRequest;
use TexHub\LaklakB2b\Enums\DeliveryType;
use TexHub\LaklakB2b\Enums\PaymentStatus;

$shipment = $laklak->shipments()->create(
    ShipmentRequest::make(
        externalOrderId: 'ORD-100032',
        customerPhone: '+992900123456',
        deliveryType: DeliveryType::ParcelTerminal,
        dropOffLocationId: 2,
        paymentStatus: PaymentStatus::Unpaid,
    )->customerName('Anu Lily')
);

echo $shipment->get('tracking_number');   // B2B-CODE-100001
echo $shipment->get('label_print_url');   // QR label to print & attach

// Update payment status (+ weight when paid):
$updated = $laklak->shipments()->updatePaymentStatus('ORD-100032', PaymentStatus::Paid, '2.5 KG');
echo $updated->get('parcel_terminal_password');

// Print labels for several shipments:
$laklak->shipments()->labelPrintUrl(['B2B-CODE-100001', 'B2B-CODE-100002']);

use TexHub\LaklakB2b\Enums\DeliveryStatus;

// Verify, then parse:
$laklak->webhooks()->assertValid(
    $_SERVER['HTTP_X_WEBHOOK_KEY'] ?? null,
    $_SERVER['HTTP_X_WEBHOOK_TIMESTAMP'] ?? null,
);

$event = $laklak->webhooks()->parse(file_get_contents('php://input'));

if ($event->isDeliveryStatusChanged()) {
    $event->trackingNumber();
    $event->externalOrderId();
    $event->deliveryStatus();            // DeliveryStatus enum: pending|picked_up|on_the_way|ready_to_pick|delivered|expired
    $event->deliveryStatus()->isFinal(); // true for delivered/expired
    $event->parcelTerminalPassword();
}

if ($event->isDeliveryAddressChanged()) {
    $event->deliveryType();              // DeliveryType enum
    $event->dropOffLocationId();
    $event->dropOffLocation();           // full location array: id, visible_id, type, cluster, …
}

http_response_code(200); // acknowledge

use TexHub\LaklakB2b\Exceptions\ApiException;

try {
    $laklak->shipments()->create($request);
} catch (ApiException $e) {
    $e->httpStatus;
    $e->isValidationError();   // 422
    $e->errors;                // ['field' => ['message', ...]]
    $e->isUnauthorized();      // 401/403
}

use TexHub\LaklakB2b\Laravel\LakLak;

$cities = LakLak::addresses()->cities();
LakLak::shipments()->create($request);

use TexHub\LaklakB2b\LakLak;
use TexHub\LaklakB2b\Config;
use TexHub\LaklakB2b\Tests\Support\FakeTransport;

$t = (new FakeTransport())->push(['success' => true, 'data' => []]);
$laklak = new LakLak(new Config('KEY'), $t);
$laklak->addresses()->cities(); // assert on $t->last()
bash
php artisan vendor:publish --tag=laklak-b2b-config

src/
├── LakLak.php               # entry — addresses()/shipments()/webhooks()
├── Config.php               # immutable configuration
├── Enums/                   # Environment, DeliveryType, PaymentStatus, DeliveryStatus
├── Http/                    # Transport, CurlTransport, HttpClient, RawResponse
├── Requests/ShipmentRequest # fluent shipment builder
├── Resources/               # Addresses, Shipments
├── Webhook/                 # WebhookHandler (verify + parse), WebhookEvent
├── Responses/               # Response (ArrayAccess), ListResponse (paginated)
├── Exceptions/              # ApiException, TransportException, …
└── Laravel/                 # ServiceProvider + Facade