1. Go to this page and download the library: Download corecave/laravel-zatca 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/ */
corecave / laravel-zatca example snippets
use Corecave\Zatca\Facades\Zatca;
use Corecave\Zatca\Invoice\InvoiceBuilder;
use Corecave\Zatca\Enums\VatCategory;
// Build the invoice
$invoice = InvoiceBuilder::simplified()
->setInvoiceNumber('INV-2024-001')
->setIssueDate(now())
->addLineItem([
'name' => 'Product Name',
'quantity' => 2,
'unit_price' => 100.00,
'vat_category' => VatCategory::STANDARD,
])
->addLineItem([
'name' => 'Service',
'quantity' => 1,
'unit_price' => 250.00,
'vat_category' => VatCategory::STANDARD,
])
->build();
// Report to ZATCA
$result = Zatca::report($invoice);
if ($result->isSuccess()) {
$qrCode = $result->getQrCode();
$signedXml = $result->getSignedXml();
// Store or display the QR code
}
use Corecave\Zatca\Facades\Zatca;
use Corecave\Zatca\Invoice\InvoiceBuilder;
$invoice = InvoiceBuilder::standard()
->setInvoiceNumber('INV-2024-002')
->setIssueDate(now())
->setBuyer([
'name' => 'Buyer Company LLC',
'vat_number' => '300000000000003', // Used for TIN scheme if no registration_number
'registration_number' => '1234567890', // CRN - Commercial Registration Number
'registration_scheme' => 'CRN', // Optional: CRN, MOM, MLS, SAG, 700, OTH
'address' => [
'street' => 'King Fahd Road',
'building' => '1234', // Required for SA buyers (KSA-18)
'additional_number' => '5678', // Optional (KSA-23)
'city' => 'Riyadh',
'district' => 'Al Olaya', // Required for SA buyers (KSA-4)
'postal_code' => '12345',
'country' => 'SA',
],
])
->addLineItem([
'name' => 'Consulting Services',
'quantity' => 10,
'unit_price' => 1000.00,
'vat_category' => VatCategory::STANDARD,
])
->build();
// Clear with ZATCA (must be done BEFORE issuing to customer)
$result = Zatca::clear($invoice);
if ($result->isSuccess()) {
// ZATCA adds cryptographic stamp and QR code
$clearedXml = $result->getClearedXml();
$qrCode = $result->getQrCode();
}
use Corecave\Zatca\Invoice\InvoiceBuilder;
// Credit note for a standard invoice
$creditNote = InvoiceBuilder::creditNote(simplified: false)
->setInvoiceNumber('CN-2024-001')
->setOriginalInvoice('INV-2024-002') // Reference original invoice
->setReason('Returned goods')
->setBuyer([/* buyer details */])
->addLineItem([
'name' => 'Returned Product',
'quantity' => 1,
'unit_price' => 100.00,
'vat_category' => VatCategory::STANDARD,
])
->build();
// Process (automatically clears for standard, reports for simplified)
$result = Zatca::process($creditNote);
// Automatically uses report() for simplified invoices
// and clear() for standard invoices
$result = Zatca::process($invoice);
if ($result->wasReported()) {
// B2C invoice was reported
}
if ($result->wasCleared()) {
// B2B invoice was cleared
}
use Corecave\Zatca\Facades\Zatca;
// Check if production certificate exists
if (Zatca::certificate()->hasActiveProductionCertificate()) {
// Ready to issue invoices
}
// Get certificate details
$cert = Zatca::certificate()->getActive('production');
$expiresAt = $cert->getExpiresAt();
$isExpiringSoon = $cert->isExpiringSoon(30);
// Get certificates expiring soon
$expiring = Zatca::certificate()->getExpiringSoon(30);
// Get certificate issuer (for debugging)
$issuer = $cert->getFormattedIssuer();
// Should be: CN=TSZEINVOICE-SubCA-1, DC=extgazt, DC=gov, DC=local (simulation)
// NOT: CN=eInvoicing (mock/sandbox)
use Corecave\Zatca\Facades\Zatca;
// Generate XML without submitting
$xml = Zatca::generateXml($invoice);
// Validate XML
$isValid = Zatca::validateXml($xml);
// Sign XML
$signedXml = Zatca::signXml($xml);
use Corecave\Zatca\Hash\HashChainManager;
$hashManager = app(HashChainManager::class);
// Get next ICV
$icv = $hashManager->getNextIcv();
// Get previous invoice hash
$pih = $hashManager->getPreviousInvoiceHash();
// Verify chain integrity
$result = $hashManager->verifyChain();
if (!$result['valid']) {
// Chain has issues
}
// Get statistics
$stats = $hashManager->getStatistics();
use Corecave\Zatca\Exceptions\ApiException;
use Corecave\Zatca\Exceptions\ValidationException;
use Corecave\Zatca\Exceptions\CertificateException;
try {
$result = Zatca::report($invoice);
} catch (ValidationException $e) {
// Invoice validation failed
$errors = $e->getErrors();
} catch (ApiException $e) {
// ZATCA API error
$zatcaErrors = $e->getZatcaErrors();
$zatcaWarnings = $e->getZatcaWarnings();
} catch (CertificateException $e) {
// Certificate issue: renew the CSID with `php artisan zatca:renew-csid`, then retry
$certificateError = $e->getMessage();
}