PHP code example of puleeno / nhanh-vn-sdk

1. Go to this page and download the library: Download puleeno/nhanh-vn-sdk 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/ */

    

puleeno / nhanh-vn-sdk example snippets


use Puleeno\NhanhVn\Client\NhanhClientBuilder;

// Tạo client với Builder Pattern
$client = NhanhClientBuilder::create()
    ->withAppId('your_app_id_here')           // ID ứng dụng từ Nhanh.vn
    ->withBusinessId('your_business_id_here')  // ID doanh nghiệp
    ->withAccessToken('your_access_token_here') // Token truy cập
    ->withEnvironment('production')             // Môi trường: 'production' hoặc 'sandbox'
    ->withTimeout(30)                          // Timeout cho API calls (giây)
    ->build();

// Hoặc sử dụng convenience methods
$client = NhanhClientBuilder::createBasic(
    'your_app_id_here',
    'your_business_id_here',
    'your_access_token_here'
);

// Cho development
$client = NhanhClientBuilder::createDevelopment(
    'your_app_id_here',
    'your_business_id_here',
    'your_access_token_here'
);

use Puleeno\NhanhVn\Client\NhanhVnClient;
use Puleeno\NhanhVn\Config\ClientConfig;

// Tạo cấu hình client
$config = new ClientConfig([
    'appId' => 'your_app_id_here',           // ID ứng dụng từ Nhanh.vn
    'businessId' => 'your_business_id_here',  // ID doanh nghiệp
    'accessToken' => 'your_access_token_here', // Token truy cập
    'environment' => 'production',             // Môi trường: 'production' hoặc 'sandbox'
    'timeout' => 30                           // Timeout cho API calls (giây)
]);

// Khởi tạo client singleton
$client = NhanhVnClient::getInstance($config);

// Lấy access token từ Nhanh.vn
$oauthModule = $client->oauth();

// Thực hiện OAuth flow
$authUrl = $oauthModule->getAuthorizationUrl([
    'scope' => 'read write',
    'state' => 'random_state_string'
]);

// Xử lý callback và lấy access token
$token = $oauthModule->handleCallback($code);

// Tìm kiếm sản phẩm
$products = $client->products()->search([
    'keyword' => 'iPhone',
    'categoryId' => 1,
    'minPrice' => 1000000,
    'maxPrice' => 50000000,
    'page' => 1,
    'perPage' => 20
]);

// Thêm sản phẩm mới
$addResponse = $client->products()->add([
    'id' => 'PROD001',
    'name' => 'iPhone 15 Pro',
    'price' => 25000000,
    'categoryId' => 1,
    'description' => 'Điện thoại thông minh cao cấp'
]);

// Thêm ảnh sản phẩm
$imageResponse = $client->products()->addExternalImage([
    'productId' => 12345,
    'externalImages' => [
        'https://example.com/image1.jpg',
        'https://example.com/image2.jpg'
    ]
]);

// Tìm kiếm tất cả khách hàng
$customers = $client->customers()->getAll(1, 20);

// Tìm kiếm khách hàng theo ID
$customer = $client->customers()->searchById(12345);

// Tìm kiếm khách hàng theo số điện thoại
$customer = $client->customers()->searchByMobile('0987654321');

// Lấy khách hàng theo loại (lẻ, sỉ, đại lý)
$retailCustomers = $client->customers()->getRetailCustomers(1, 10);
$wholesaleCustomers = $client->customers()->getWholesaleCustomers(1, 10);

// Lấy khách hàng theo khoảng thời gian cập nhật
$customers = $client->customers()->getByDateRange(
    '2024-01-01 00:00:00',
    '2024-12-31 23:59:59',
    1,
    20
);