PHP code example of laraditz / lazada

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

    

laraditz / lazada example snippets


$url = Lazada::auth()->authorizationUrl();
// Redirect the seller to this URL

use Laraditz\Lazada\Facades\Lazada;

// Using facade (recommended)
Lazada::order()->list(created_after: '2023-11-17T00:00:00+08:00');

// Using service container
app('lazada')->order()->list(created_after: '2023-11-17T00:00:00+08:00');

use Laraditz\Lazada\Facades\Lazada;
use Laraditz\Lazada\Exceptions\LazadaAPIError;

// Step 1: Get the authorization URL and redirect the seller to it
$url = Lazada::auth()->authorizationUrl();

// Step 2: After the seller authorizes, exchange the code for a token
// (The package handles this automatically via the built-in callback route)
// If using a custom callback, call this manually:
try {
    $token = Lazada::auth()->accessToken(code: '0_123456_XxxXXXXxxXXxxXXXXxxxxxxXXXXxx');
} catch (LazadaAPIError $e) {
    logger()->error('Lazada token error', [
        'message' => $e->getMessage(),
        'code'    => $e->getMessageCode(),
        'request' => $e->getRequestId(),
    ]);
}

// Get orders created after a date
$orders = Lazada::order()->list(created_after: '2023-11-17T00:00:00+08:00');

// Get a specific order
$order = Lazada::order()->get(order_id: '16090');

// Get items in an order
$items = Lazada::order()->items(order_id: '16090');

// Get order documents (invoice, shipping label)
$document = Lazada::order()->document(
    order_item_ids: [1234567, 1234568],
    doc_type: 'invoice',
);

// Get product list
$products = Lazada::product()->get(
    filter: 'live',
    offset: 0,
    limit: 50,
);

// Get a specific product item
$item = Lazada::product()->item(item_id: 123456789);

// Update sellable stock quantity for a SKU
Lazada::product()->updateSellableQuantity(
    payload: json_encode([
        'skuId' => 'your-sku-id',
        'sellableQuantity' => 100,
    ])
);

// Get payout status
$payout = Lazada::finance()->payoutStatus(
    created_after: '2023-01-01T00:00:00+08:00',
);

// Query account transactions
$transactions = Lazada::finance()->accountTransactions(
    start_time: '2023-11-01T00:00:00+08:00',
    end_time: '2023-11-30T23:59:59+08:00',
);

// Query transaction details
$details = Lazada::finance()->transactionDetail(
    trans_type: 3,
    start_time: '2023-11-01T00:00:00+08:00',
    end_time: '2023-11-30T23:59:59+08:00',
);

// Sync seller info from Lazada API to DB
Lazada::seller()->get();

// Get the resolved seller model (no API call)
$seller = Lazada::seller()->info();
echo $seller->name;
echo $seller->short_code;

// Get pick-up store list
$stores = Lazada::seller()->pickUpStoreList();

// Method 1: Using make() — reuse the same instance for multiple calls
$lazada = Lazada::make(seller_id: 'MYXXXXXXXX');
$orders   = $lazada->order()->list(created_after: '2023-11-17T00:00:00+08:00');
$products = $lazada->product()->get(filter: 'live', offset: 0, limit: 50);

// Method 2: Pass seller_id on any service call to switch sellers inline
Lazada::order(seller_id: 'MYXXXXXXXX')->list(created_after: '2023-11-17T00:00:00+08:00');

use Laraditz\Lazada\Facades\Lazada;
use Laraditz\Lazada\Exceptions\LazadaAPIError;

try {
    $orders = Lazada::order()->list(created_after: '2023-11-17T00:00:00+08:00');
} catch (LazadaAPIError $e) {
    // Handle Lazada API errors
    logger()->error('Lazada API Error', [
        'message'    => $e->getMessage(),
        'code'       => $e->getMessageCode(),
        'request_id' => $e->getRequestId(),
        'result'     => $e->getResult(), // raw response
    ]);
} catch (\Throwable $th) {
    throw $th;
}

// app/Listeners/LazadaWebPushListener.php

namespace App\Listeners;

use Laraditz\Lazada\Events\WebPushReceived;

class LazadaWebPushListener
{
    public function handle(WebPushReceived $event): void
    {
        $pushType = $event->pushType;
        $data     = $event->data;

        // Handle the push content based on type
    }
}

// app/Providers/EventServiceProvider.php

protected $listen = [
    \Laraditz\Lazada\Events\WebPushReceived::class => [
        \App\Listeners\LazadaWebPushListener::class,
    ],
];

protected function schedule(Schedule $schedule)
{
    // Refresh tokens daily (live access tokens expire in 30 days)
    $schedule->command('lazada:refresh-token')
             ->daily()
             ->withoutOverlapping();

    // Clean up expired tokens weekly
    $schedule->command('lazada:flush-expired-token')
             ->weekly();
}
bash
php artisan vendor:publish --provider="Laraditz\Lazada\LazadaServiceProvider" --tag="migrations"
bash
php artisan migrate
bash
php artisan vendor:publish --provider="Laraditz\Lazada\LazadaServiceProvider" --tag="config"