PHP code example of webpos / caspos-client

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

    

webpos / caspos-client example snippets

 
use WebServis\CasposClient;

$client = new CasposClient(
    ipAddress: '1192.168.1.150', // The static local IP displayed on the SUNMI device screen
    username: 'WebPOS_User',
    password: 'secure_integration_password',
    port: 5544 // Default integration port
);

use WebServis\CasposItem;
use WebServis\CasposSaleRequest;
use RuntimeException;
use LogicException;

try {
    // 1. Setup the receipt wrapper with a fixed unique UUID bound to your database order ID
    $saleRequest = new CasposSaleRequest(
        documentUuid: 'c8b4f17a-8f5b-4cde-a112-823cdde39104',
        cardPayment: 17.80, // Total paid via terminal card
        cashierName: 'Waiter: Elvin M.',
        note: 'Service Type: Delivery | Order #10943'
    );

    // 2. Append individual item lines
    $saleRequest->addItem(new CasposItem(
        name: 'Pizza "Margarita" (Large)', // Quotes will be sanitized automatically
        code: '400123456789',
        quantity: 1.000,
        salePrice: 15.00,
        discountAmount: 1.52
    ));

    $saleRequest->addItem(new CasposItem(
        name: 'Coca-Cola 330ml',
        code: '5449000000996',
        quantity: 1.000,
        salePrice: 3.00,
        discountAmount: 0.30
    ));

    // 3. Service fees must be appended as a standard line item
    $saleRequest->addItem(new CasposItem(
        name: 'Service Charge (10%)',
        code: 'SRV10',
        quantity: 1.000,
        salePrice: 1.80,
        discountAmount: 0.18
    ));

    // 4. Send the request to the physical terminal
    // Note: mapping array conversion implicitly fires the internal balance validator
    $response = $client->sendSale($saleRequest);
    
    echo "Success! Fiscal Document ID: " . $response['fiscal_id'] ?? 'Printed';

} catch (LogicException $e) {
    // Fired if payment math does not match item totals locally (0.01 margin protection)
    echo "Validation Error: " . $e->getMessage();
} catch (RuntimeException $e) {
    // Fired on network dropouts, API timeouts, or hardware communication errors
    echo "Terminal Connection Error: " . $e->getMessage();
}
 
// Check if the current work day is active
$status = $client->getShiftStatus();

if ($status['data']['isShiftOpen'] === false) {
    // Open a new work day shift
    $client->openShift(cashierName: 'Manager Name', initialDeposit: 50.00);
}

// Close shift at midnight (Prints rəsmi Z-Report)
$client->closeShift(
    cashierName: 'Manager Name', 
    documentUuid: bin2hex(random_bytes(16))
);
 php 
composer update webservis/caspos-client