PHP code example of yr-group / laravel-zatca
1. Go to this page and download the library: Download yr-group/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/ */
yr-group / laravel-zatca example snippets
use YrGroup\LaravelZatca\Facades\Zatca;
// Generate QR code
$qrCode = Zatca::phaseOne()
->sellerName('My Company')
->vatNumber('123456789123456')
->timestamp(now())
->totalWithVat('115.00')
->vatTotal('15.00')
->generate();
// Generate as base64 PNG
$qrCodeBase64 = Zatca::phaseOne()
->sellerName('My Company')
->vatNumber('123456789123456')
->timestamp(now())
->totalWithVat('115.00')
->vatTotal('15.00')
->generateBase64();
use YrGroup\LaravelZatca\Facades\Zatca;
$merchantData = [
'vat_number' => '310461435700003',
'solution_name' => config('zatca.solution_name'),
'version' => config('zatca.version'),
'serial_number' => '1-TST|2-TST|3-' . uniqid(),
'common_name' => 'TST-886431145-399999999900003',
'organization_name' => 'My Test Company',
'organizational_unit' => 'Riyad Branch',
'registered_address' => 'Riyadh',
'business_category' => 'Retail',
];
$result = Zatca::csr()->generateCSR($merchantData);
// Returns:
// [
// 'csr_content' => '...',
// 'private_key_path' => '/path/to/private_key.pem',
// 'csr_path' => '/path/to/csr.pem'
// ]
$csrContent = file_get_contents(storage_path('app/zatca/csr.pem'));
$otp = '123456'; // From ZATCA portal
$result = Zatca::api()->getComplianceCSID($csrContent, $otp);
// Save the certificate and secret
file_put_contents(storage_path('app/zatca/certificate.txt'), $result['binary_security_token']);
file_put_contents(storage_path('app/zatca/secret.txt'), $result['secret']);
// Load your XML invoice (must conform to ZATCA UBL 2.1 format)
$xmlInvoice = file_get_contents(storage_path('app/zatca/invoices/invoice.xml'));
// Load certificate and secret
$certificate = file_get_contents(storage_path('app/zatca/certificate.txt'));
$secret = file_get_contents(storage_path('app/zatca/secret.txt'));
$privateKeyPath = storage_path('app/zatca/private_key.pem');
// Sign the invoice
$signedData = Zatca::invoice()->signInvoice(
$xmlInvoice,
$certificate,
$privateKeyPath,
$secret
);
// Extract UUID from XML
$doc = new \DOMDocument();
$doc->loadXML($xmlInvoice);
$xpath = new \DOMXPath($doc);
$uuid = $xpath->query('//cbc:UUID')->item(0)->nodeValue;
// Report invoice (B2B - Standard invoices)
$result = Zatca::api()->reportInvoice(
$signedData['signed_invoice'],
$signedData['hash'],
$uuid,
$certificate,
$secret
);
// Or clear invoice (B2C - Simplified invoices)
$result = Zatca::api()->clearInvoice(
$signedData['signed_invoice'],
$signedData['hash'],
$uuid,
$certificate,
$secret
);
$complianceRequestId = 'request-id-from-compliance';
$certificate = file_get_contents(storage_path('app/zatca/certificate.txt'));
$secret = file_get_contents(storage_path('app/zatca/secret.txt'));
$result = Zatca::api()->getProductionCSID(
$complianceRequestId,
$certificate,
$secret
);
// Save production certificate and secret
file_put_contents(storage_path('app/zatca/prod_certificate.txt'), $result['binary_security_token']);
file_put_contents(storage_path('app/zatca/prod_secret.txt'), $result['secret']);
Zatca::phaseOne()
->sellerName(string $name)
->vatNumber(string $vatNumber)
->timestamp($timestamp)
->totalWithVat(string $total)
->vatTotal(string $vat)
->generate(?QrCodeOptions $options = null): string
->generateBase64(): string
Zatca::csr()->generateCSR(array $merchantData): array
Zatca::api()->getComplianceCSID(string $csrContent, string $otp): array
Zatca::api()->checkInvoiceCompliance(string $signedInvoice, string $invoiceHash, string $uuid, string $certificate, string $secret): array
Zatca::api()->getProductionCSID(string $complianceRequestId, string $certificate, string $secret): array
Zatca::api()->reportInvoice(string $signedInvoice, string $invoiceHash, string $uuid, string $certificate, string $secret): array
Zatca::api()->clearInvoice(string $signedInvoice, string $invoiceHash, string $uuid, string $certificate, string $secret): array
Zatca::invoice()->signInvoice(string $xmlInvoice, string $certificateContent, string $privateKeyPath, string $secret): array
bash
php artisan vendor:publish --tag=zatca-config