PHP code example of loyaltylt / sdk

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

    

loyaltylt / sdk example snippets




oyaltyLt\SDK\LoyaltySDK;

$sdk = new LoyaltySDK([
    'apiKey' => 'lty_your_api_key',
    'apiSecret' => 'your_api_secret',
    'environment' => 'production', // or 'staging'
    'locale' => 'lt',
]);

// Get shops
$shops = $sdk->getShops();
print_r($shops['data']);

// Generate QR login session
$session = $sdk->generateQrLogin('POS Terminal #1', $shopId);

echo $session['session_id'];
echo $session['qr_code']; // Deep link for QR code
echo $session['expires_at'];

// Poll for status (or subscribe over Reverb)
$status = $sdk->pollQrLogin($session['session_id']);

if ($status['status'] === 'authenticated') {
    $user = $status['user'];
    echo "Welcome, " . $user['name'];
}

// Generate QR card scan session
$session = $sdk->generateQrCardSession('POS Terminal', $shopId);

// Display QR code to customer
$qrImageUrl = "https://api.qrserver.com/v1/create-qr-code/?size=250x250&data=" 
    . urlencode($session['qr_code']);

// Poll for customer identification
$result = $sdk->pollQrCardStatus($session['session_id']);

if ($result['status'] === 'completed') {
    $card = $result['card_data'];
    echo "Customer: " . $card['user']['name'];
    echo "Points: " . $card['points_balance'];
}

// Reverb connection details — no per-session token is needed, the QR channels
// are public and the unguessable session id is the secret.
$config = $sdk->getRealtimeConfig();

echo $config['key'];                                       // Reverb app key (public)
echo "wss://{$config['host']}:{$config['port']}";          // wss://ws.loyalty.lt:443
echo "qr-login.{$session['session_id']}";                  // channel to subscribe to
// Bind the `status_update` event with any Pusher-protocol client.

// Get all shops
$shops = $sdk->getShops();

// Filter shops
$shops = $sdk->getShops([
    'is_active' => true,
    'is_virtual' => false,
]);

// Get cards
$cards = $sdk->getLoyaltyCards();

// Get single card
$card = $sdk->getLoyaltyCard(123);

// Get card by number
$cardInfo = $sdk->getLoyaltyCardInfo([
    'card_number' => '123-456-789'
]);

// Get points balance
$balance = $sdk->getPointsBalance([
    'card_id' => 123
]);

// Award points
$transaction = $sdk->createTransaction([
    'card_id' => 123,
    'amount' => 50.00,
    'points' => 50,
    'type' => 'earn',
    'description' => 'Purchase reward',
    'reference' => 'ORDER-12345',
]);

// Get transactions
$transactions = $sdk->getTransactions([
    'card_id' => 123,
    'type' => 'earn',
]);

// Get offers
$offers = $sdk->getOffers(['is_active' => true]);

// Create offer
$offer = $sdk->createOffer([
    'title' => 'Summer Sale',
    'description' => '20% off all items',
    'discount_type' => 'percentage',
    'discount_value' => 20,
    'start_date' => '2024-06-01',
    'end_date' => '2024-08-31',
]);

// Get categories
$categories = $sdk->getCategories();

// Import offers from XML
$result = $sdk->importFromUrl('https://example.com/offers.xml', [
    'auto_publish' => true,
]);

// Validate XML
$validation = $sdk->validateXml('https://example.com/offers.xml');

// Get import stats
$stats = $sdk->getImportStats();

use LoyaltyLt\SDK\Exceptions\LoyaltySDKException;

try {
    $result = $sdk->getLoyaltyCardInfo(['card_number' => 'INVALID']);
} catch (LoyaltySDKException $e) {
    echo "Error: " . $e->getMessage();
    echo "Code: " . $e->getErrorCode();
    echo "HTTP Status: " . $e->getHttpStatus();
}

// config/services.php
'loyalty' => [
    'api_key' => env('LOYALTY_API_KEY'),
    'api_secret' => env('LOYALTY_API_SECRET'),
    'environment' => env('LOYALTY_ENVIRONMENT', 'production'),
],

// app/Providers/AppServiceProvider.php
use LoyaltyLt\SDK\LoyaltySDK;

public function register()
{
    $this->app->singleton(LoyaltySDK::class, function ($app) {
        return new LoyaltySDK([
            'apiKey' => config('services.loyalty.api_key'),
            'apiSecret' => config('services.loyalty.api_secret'),
            'environment' => config('services.loyalty.environment'),
        ]);
    });
}

// Usage in controller
public function __construct(private LoyaltySDK $loyalty) {}

public function processTransaction(Request $request)
{
    return $this->loyalty->createTransaction([
        'card_id' => $request->card_id,
        'amount' => $request->amount,
        'shop_id' => $request->shop_id,
    ]);
}