PHP code example of saudizatca / laravel-zatca

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

    

saudizatca / laravel-zatca example snippets


use SaudiZATCA\Facades\Zatca;

$result = Zatca::certificate()->generateCSR([
    'organization_identifier' => '300000000000003',
    'organization' => 'Your Company',
    'common_name' => 'Your Company',
    'street' => 'King Fahd Road',
    'city' => 'Riyadh',
]);

// $result['csr'] - CSR content
// $result['private_key'] - Private key content

use SaudiZATCA\Facades\Zatca;
use SaudiZATCA\Data\InvoiceData;
use SaudiZATCA\Data\InvoiceLineData;
use SaudiZATCA\Data\SellerData;
use SaudiZATCA\Data\BuyerData;

// Create seller
$seller = SellerData::fromConfig(config('zatca.seller'));

// Create buyer (for B2B/standard invoices)
$buyer = new BuyerData(
    name: 'Buyer Company',
    vatNumber: '300000000000004',
    city: 'Jeddah'
);

// Create invoice
$invoice = new InvoiceData(
    invoiceNumber: 'INV-001',
    issueDate: new DateTime(),
    lines: [
        new InvoiceLineData('Product A', 2, 50.0, 15.0),
        new InvoiceLineData('Product B', 1, 100.0, 15.0),
    ],
    type: InvoiceData::TYPE_STANDARD // or TYPE_SIMPLIFIED
);

// Process (generate XML, sign, QR, submit)
$result = Zatca::processInvoice($invoice, $seller, $buyer);

// $result['uuid']
// $result['invoice_hash']
// $result['signed_xml']
// $result['qr_code']
// $result['submission']

use SaudiZATCA\Facades\Zatca;
use SaudiZATCA\Data\SellerData;

$seller = new SellerData('Your Company', '300000000000003');

$qrCode = Zatca::generatePhase1QR($seller, 115.0, 15.0);
// Returns Base64-encoded TLV QR data

use SaudiZATCA\Facades\Zatca;

// Generate XML only
$xml = Zatca::xml()->generate($invoice, $seller, $buyer);

// Sign existing XML
$signedResult = Zatca::invoice()->signInvoice($xml, $invoice);

// Generate QR for signed invoice
$qrCode = Zatca::invoice()->generateQRCode($seller, $invoice, $signedResult['invoice_hash'], $signedResult);

// Submit to ZATCA
$submission = Zatca::invoice()->submitToZatca($signedResult['signed_xml'], $signedResult['invoice_hash'], $invoice);

use SaudiZATCA\Models\ZatcaCertificate;

// Get active production certificate
$cert = ZatcaCertificate::active()
    ->environment('production')
    ->first();

// Check validity
if ($cert && $cert->isValid()) {
    // Use certificate
}

use SaudiZATCA\Models\ZatcaInvoice;

// Get pending invoices
$pending = ZatcaInvoice::pending()->get();

// Get today's invoices
$today = ZatcaInvoice::today()->get();

// Get by status
$submitted = ZatcaInvoice::status('submitted')->get();

use SaudiZATCA\Models\ZatcaLog;

// Get recent errors
$errors = ZatcaLog::errors()->recent(24)->get();

// Get API logs
$apiLogs = ZatcaLog::category('api')->recent()->get();

use SaudiZATCA\Exceptions\ZatcaException;
use SaudiZATCA\Exceptions\CertificateException;
use SaudiZATCA\Exceptions\APIException;
use SaudiZATCA\Exceptions\InvoiceException;

try {
    $result = Zatca::processInvoice($invoice, $seller);
} catch (CertificateException $e) {
    // Handle certificate errors
    Log::error('Certificate: ' . $e->getMessage());
} catch (APIException $e) {
    // Handle API errors
    Log::error('API Error (' . $e->getStatusCode() . '): ' . $e->getMessage());
    if ($e->getDetails()) {
        Log::error('Details: ' . json_encode($e->getDetails()));
    }
} catch (InvoiceException $e) {
    // Handle invoice errors
    Log::error('Invoice: ' . $e->getMessage());
} catch (ZatcaException $e) {
    // Handle general ZATCA errors
    Log::error('ZATCA: ' . $e->getMessage());
}

'logging' => [
    'enabled' => true,
    'channel' => 'zatca', // Create this channel in logging.php
    'level' => 'debug',
    'log_api_requests' => true,
    'log_api_responses' => true,
    'log_sensitive_data' => false, // Set false in production
],

'channels' => [
    'zatca' => [
        'driver' => 'single',
        'path' => storage_path('logs/zatca.log'),
        'level' => 'debug',
    ],
],
bash
php artisan vendor:publish --tag=zatca-config
bash
php artisan migrate
bash
php artisan zatca:status
bash
php artisan zatca:csr --vat=300000000000003 --org="Your Company"
bash
php artisan zatca:compliance-csid --otp=123456
bash
php artisan zatca:production-csid
bash
# Simplified invoice (B2C)
php artisan zatca:report --number=INV-001 --total=115 --vat=15

# Standard invoice (B2B) - 

tests/
├── Unit/
│   ├── CertificateServiceTest.php
│   ├── InvoiceServiceTest.php
│   └── QRCodeServiceTest.php
└── Feature/
    ├── APIIntegrationTest.php
    └── InvoiceSubmissionTest.php
bash
# Check OpenSSL EC support
php -r "var_dump(openssl_get_curve_names());"

# Verify CSR content
openssl req -in storage/zatca/certificates/csr.pem -text -noout