<?php
use LoyaltyLt\SDK\LoyaltySDK;
use LoyaltyLt\SDK\Exceptions\LoyaltySDKException;
class POSController
{
private LoyaltySDK $sdk;
private ?array $currentCustomer = null;
private ?array $qrSession = null;
public function __construct()
{
$this->sdk = new LoyaltySDK([
'apiKey' => env('LOYALTY_API_KEY'),
'apiSecret' => env('LOYALTY_API_SECRET'),
]);
}
public function startCustomerIdentification(): array
{
$this->qrSession = $this->sdk->generateQrCardSession('POS');
return [
'qr_url' => "https://api.loyalty.lt/qr?data="
. urlencode($this->qrSession['qr_code']) . "&size=300",
'session_id' => $this->qrSession['session_id'],
'expires_at' => $this->qrSession['expires_at'],
];
}
public function checkCustomerStatus(): ?array
{
if (!$this->qrSession) return null;
$result = $this->sdk->pollQrCardStatus($this->qrSession['session_id']);
if ($result['status'] === 'completed') {
$this->currentCustomer = $result['card_data'];
return $this->currentCustomer;
}
return null;
}
public function processCheckout(float $total): array
{
if (!$this->currentCustomer) {
throw new \Exception('No customer identified');
}
$pointsToAward = (int) ($total * 10); // 10 points per €1
$transaction = $this->sdk->createTransaction([
'card_id' => $this->currentCustomer['id'],
'amount' => $total,
'points' => $pointsToAward,
'type' => 'earn',
'description' => 'Purchase',
'reference' => 'POS-' . time(),
]);
return [
'transaction_id' => $transaction['id'],
'points_awarded' => $pointsToAward,
'customer' => $this->currentCustomer['user']['name'],
];
}
}