PHP code example of gosimple / tezga
1. Go to this page and download the library: Download gosimple/tezga 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/ */
gosimple / tezga example snippets
use GoSimple\Tezga\TezgaClient;
$tezga = new TezgaClient(getenv('TEZGA_API_KEY'));
// A production key issues a REAL receipt. Check first.
echo $tezga->getEnvironment(); // "production" | "sandbox" | "simulation"
$doc = $tezga->issueReceipt([
'externalId' => 'ORDER-1042', // idempotency key: never issues twice
'nacinPlacanja' => 'Kartica',
'tok' => 'fiskalni',
'stavke' => [
['naziv' => 'Espresso', 'kolicina' => 2, 'cenaSaPdv' => 180, 'poreskaStopa' => '20'],
['naziv' => 'Croissant', 'kolicina' => 1, 'cenaSaPdv' => 220, 'poreskaStopa' => '10'],
],
'kupac' => ['naziv' => 'ACME d.o.o.', 'pib' => '123456789'], // pib -> B2B receipt
'posaljiMejl' => true,
]);
echo $doc['fiskalni']['pfrBroj'], ' ', $doc['javniPdfUrl'];
$tezga = new TezgaClient($apiKey, TezgaClient::DEFAULT_BASE_URL, 'my-shop');
use GoSimple\Tezga\Webhooks;
$raw = file_get_contents('php://input');
$payload = Webhooks::parse(
$raw,
$_SERVER['HTTP_X_TEZGA_SIGNATURE'] ?? null,
getenv('TEZGA_WEBHOOK_SECRET'),
);
// $payload['dogadjaj'] : racun.fiskalizovan | racun.greska
// | refundacija.izvrsena | storno.izvrsen
// $payload['podaci'] : the document, plus your externalId and provider when known
http_response_code(200);
use GoSimple\Tezga\TezgaException;
try {
$tezga->issueReceipt(['externalId' => 'X', 'stavke' => []]);
} catch (TezgaException $e) {
error_log($e->getStatus() . ' ' . $e->getMessage()); // message is the API's `error`
foreach ($e->getFields() as $f) {
error_log(($f['path'] ?? '') . ': ' . ($f['message'] ?? ''));
}
if ($e->isRetryable()) { /* 429 rate limit, or 409 still in flight */ }
}