PHP code example of laraditz / shopee

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


$returns = Shopee::returns()->getReturnList(page_no: 0, page_size: 50, update_time_from: now()->subDays(7)->timestamp, update_time_to: now()->timestamp);

$return = Shopee::returns()->getReturnDetail(return_sn: '2209010001');

// Synced records
$order->returns;   // ShopeeOrder hasMany ShopeeReturn
$shop->returns;    // ShopeeShop hasMany ShopeeReturn

use Laraditz\Shopee\Facades\Shopee;

// Get order details
$orderDetails = Shopee::order()->getOrderDetail(
    order_sn_list: '211020BNFYMXXX,211020BNFYXXX2'
);

// Get shop information
$shopInfo = Shopee::shop()->getShopInfo();

// Search products
$products = Shopee::product()->searchItem(
    item_name: 'smartphone',
    page_size: 20,
    offset: 0
);

// Add a new product
$newProduct = Shopee::product()->addItem(
    item_name: 'My Product',
    description: 'Product description',
    original_price: 29.90,
    normal_stock: 100,
    weight: 0.5,
    item_sku: 'SKU-001',
    category_id: 100001,
    // ... refer to Shopee API Reference for full parameter list
);

// Update an existing product
Shopee::product()->updateItem(
    item_id: 123456789,
    item_name: 'Updated Product Name',
    original_price: 24.90,
);

// Delete a product
Shopee::product()->deleteItem(item_id: 123456789);

// Alternative: using service container
$orders = app('shopee')->order()->getOrderList(
    time_range_field: 'create_time',
    time_from: strtotime('-30 days'),
    time_to: time(),
    page_size: 50
);

use Laraditz\Shopee\Facades\Shopee;

// Method 1: Using make() with shop_id
$products = Shopee::make(shop_id: '2257XXXXX')
    ->product()
    ->getItemList(
        offset: 0,
        page_size: 10,
        item_status: 'NORMAL'
    );

// Method 2: Using shopId() method
$orders = Shopee::shopId('2257XXXXX')
    ->order()
    ->getOrderList(
        time_range_field: 'create_time',
        time_from: strtotime('-7 days'),
        time_to: time()
    );

use Laraditz\Shopee\Facades\Shopee;
use Illuminate\Http\Client\RequestException;

try {
    $result = Shopee::product()->updateStock(
        item_id: 123456789,
        stock_list: [
            [
                'model_id' => 123123123,
                'seller_stock' => [
                    [
                        'location_id' => 'MYZ',
                        'stock' => 100,
                    ]
                ],
            ]
        ]
    );
} catch (RequestException $e) {
    // Handle HTTP/network errors
    logger()->error('Request failed: ' . $e->getMessage());
}



namespace App\Listeners;

use Laraditz\Shopee\Events\WebhookReceived;

class YourWebhookListener
{
    public function handle(WebhookReceived $event)
    {
        $webhookData = $event->data;

        // Process order updates, product changes, etc.
        if ($webhookData['event'] === 'order_status_update') {
            // Handle order status changes
        }
    }
}

use Laraditz\Shopee\Events\WebhookReceived;

protected $listen = [
    WebhookReceived::class => [
        YourWebhookListener::class,
    ],
];

Shopee::make()->shop()->generateAuthorizationURL(?string $redirect = null): string

Shopee::make()->verifyPushSignature(string $url, string $rawBody, string $authorization): bool

// config/shopee.php
'encrypt_tokens' => env('SHOPEE_ENCRYPT_TOKENS', false),

// config/shopee.php
'logging' => [
    'requests' => env('SHOPEE_LOG_REQUESTS', true),
    'redact_secrets' => env('SHOPEE_LOG_REDACT_SECRETS', false),
    'prune_after_days' => env('SHOPEE_LOG_PRUNE_AFTER_DAYS'),
],

Schedule::command('model:prune', [
    '--model' => [
        \Laraditz\Shopee\Models\ShopeeRequest::class,
        \Laraditz\Shopee\Models\ShopeeWebhook::class,
    ],
])->daily();

protected function schedule(Schedule $schedule)
{
    // Refresh tokens every 3 hours to prevent expiration
    $schedule->command('shopee:refresh-token')
             ->everyThreeHours()
             ->withoutOverlapping();

    // Clean up expired tokens daily
    $schedule->command('shopee:flush-expired-token')
             ->daily();
}
bash
php artisan vendor:publish --provider="Laraditz\Shopee\ShopeeServiceProvider" --tag="config"
bash
php artisan vendor:publish --provider="Laraditz\Shopee\ShopeeServiceProvider" --tag="migrations"
php artisan migrate
bash
php artisan shopee:encrypt-tokens