PHP code example of codesbytvt / token-management-sdk

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

    

codesbytvt / token-management-sdk example snippets




okenManagement\SDK\TokenClient;

// Initialize the client
// Option 1: User Authentication (JWT)
// For cloud deployment
$client = new TokenClient([
    'base_url' => 'https://api.yourdomain.com'
]);

// For local subdirectory deployment (e.g., MAMP, XAMPP)
$client = new TokenClient([
    'base_url' => 'http://localhost/api.token-management'
]);

// Option 2: Client Authentication (API Key/Secret)
// Best for Kiosks, Displays, and Machine-to-Machine
$client = new TokenClient([
    'base_url' => 'https://api.yourdomain.com',  // or http://localhost/api.token-management
    'client_id' => 'YOUR_CLIENT_ID',
    'client_secret' => 'YOUR_CLIENT_SECRET'
]);


// Authenticate
$auth = $client->auth()->signIn('[email protected]', 'password');
$client->setAccessToken($auth->getAccessToken());

// Issue a token
$token = $client->tokens()->issue([
    'mlocation_id' => 1,
    'mservicepoint_id' => 2,
    'mtokencategory_id' => 1,
    'customer_name' => 'John Doe'
]);

echo "Token Number: " . $token->getTokenNumber();

try {
    $health = $client->health();
    echo "API Status: " . $health['status'];
    // Output: API Status: ok
} catch (\Exception $e) {
    echo "API is down: " . $e->getMessage();
}

$auth = $client->auth()->signUp(
    'John',                    // First name
    '[email protected]',        // Email/username
    'SecurePassword123'        // Password
);

$accessToken = $auth->getAccessToken();
$refreshToken = $auth->getRefreshToken();

$auth = $client->auth()->signIn('[email protected]', 'password');
$client->setAccessToken($auth->getAccessToken());

$auth = $client->auth()->signInWithGoogle($googleAuthCode);
$client->setAccessToken($auth->getAccessToken());

$auth = $client->auth()->refreshToken($refreshToken);
$client->setAccessToken($auth->getAccessToken());

$token = $client->tokens()->issue([
    'mlocation_id' => 1,
    'mservicepoint_id' => 2,
    'mtokencategory_id' => 1,
    'customer_name' => 'John Doe',
    'customer_phone' => '+1234567890'
]);

echo $token->getTokenNumber(); // "G042"
echo $token->getStatus();      // 1 (WAITING)

$token = $client->tokens()->callNext(
    1,  // Location ID
    2   // Service Point ID
);

echo "Now serving: " . $token->getTokenNumber();

$token = $client->tokens()->callById(
    123,  // Token ID
    1,    // Location ID
    2     // Service Point ID
);

$token = $client->tokens()->skip(123, 1, 2);

$token = $client->tokens()->complete(123, 1, 2);

$serving = $client->tokens()->currentlyServing(1); // Location ID
foreach ($serving as $token) {
    echo $token['token_number'] . " at " . $token['servicepoint_name'] . "\n";
}

$tokens = $client->tokens()->list([
    'mlocation_id' => 1,
    'status' => 1,  // WAITING
    'page' => 1,
    'pageLength' => 20
]);

// Create location
$location = $client->locations()->create([
    'name' => 'Main Branch',
    'address' => '123 Main St',
    'city' => 'New York',
    'status' => 1
]);

// List locations
$locations = $client->locations()->list();

// Get specific location
$location = $client->locations()->get(1);

// Update location
$location = $client->locations()->update(1, [
    'name' => 'Updated Name'
]);

// Delete location
$client->locations()->delete(1);

// Create service point
$servicePoint = $client->servicePoints()->create([
    'mlocation_id' => 1,
    'name' => 'Counter 1',
    'display_label' => 'C1',
    'status' => 1
]);

// List service points
$servicePoints = $client->servicePoints()->list([
    'mlocation_id' => 1
]);

// Create category
$category = $client->tokenCategories()->create([
    'name' => 'General',
    'prefix' => 'G',
    'status' => 1
]);

// List categories
$categories = $client->tokenCategories()->list();

// Create display
$display = $client->displays()->create([
    'mlocation_id' => 1,
    'name' => 'Main Display',
    'status' => 1
]);

// Get display data (public endpoint, no auth 

$client = new TokenClient([
    'base_url' => 'https://api.yourdomain.com',  // Required
    'timeout' => 30,                              // Optional, default: 30
    'verify_ssl' => true                          // Optional, default: true
]);

use TokenManagement\SDK\Exceptions\ApiException;
use TokenManagement\SDK\Exceptions\AuthenticationException;
use TokenManagement\SDK\Exceptions\ValidationException;

try {
    $auth = $client->auth()->signIn('[email protected]', 'wrong-password');
} catch (AuthenticationException $e) {
    echo "Authentication failed: " . $e->getMessage();
} catch (ValidationException $e) {
    echo "Validation errors: ";
    print_r($e->getValidationErrors());
} catch (ApiException $e) {
    echo "API error: " . $e->getMessage();
}

1 - WAITING
2 - CALLED
3 - SERVING
4 - COMPLETED
5 - SKIPPED



okenManagement\SDK\TokenClient;
use TokenManagement\SDK\Exceptions\ApiException;

try {
    // Initialize
    $client = new TokenClient([
        'base_url' => 'https://api.yourdomain.com'
    ]);

    // Authenticate
    $auth = $client->auth()->signIn('[email protected]', 'password');
    $client->setAccessToken($auth->getAccessToken());

    // Create a location
    $location = $client->locations()->create([
        'name' => 'Downtown Branch',
        'city' => 'New York'
    ]);

    // Create a service point
    $servicePoint = $client->servicePoints()->create([
        'mlocation_id' => $location['id'],
        'name' => 'Counter 1',
        'display_label' => 'C1'
    ]);

    // Create a token category
    $category = $client->tokenCategories()->create([
        'name' => 'General',
        'prefix' => 'G'
    ]);

    // Issue a token
    $token = $client->tokens()->issue([
        'mlocation_id' => $location['id'],
        'mservicepoint_id' => $servicePoint['id'],
        'mtokencategory_id' => $category['id'],
        'customer_name' => 'John Doe'
    ]);

    echo "Token issued: " . $token->getTokenNumber() . "\n";

    // Call the token
    $calledToken = $client->tokens()->callNext(
        $location['id'],
        $servicePoint['id']
    );

    echo "Now serving: " . $calledToken->getTokenNumber() . "\n";

    // Complete the token
    $completedToken = $client->tokens()->complete(
        $calledToken->getId(),
        $location['id'],
        $servicePoint['id']
    );

    echo "Token completed!\n";

} catch (ApiException $e) {
    echo "Error: " . $e->getMessage() . "\n";
}