PHP code example of sahlowle / laravel-zatca

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

    

sahlowle / laravel-zatca example snippets


use Hyderkamran\LaravelZatca\Traits\HasZatca;
use Hyderkamran\LaravelZatca\Enums\InvoiceType;

class Invoice extends Model
{
    use HasZatca;

    public function getZatcaInvoiceType(): InvoiceType
    {
        return $this->is_b2b ? InvoiceType::STANDARD : InvoiceType::SIMPLIFIED;
    }

    public function toZatcaSeller(): array
    {
        return [
            'name'       => 'Acme Corp',
            'vat_number' => '300000000000003',
            // ...
        ];
    }

    public function toZatcaBuyer(): array
    {
        return [
            'name'       => $this->customer->name,
            'vat_number' => $this->customer->vat_number,
        ];
    }

    public function toZatcaItems(): array
    {
        return $this->items->map(fn($item) => [
            'name'     => $item->name,
            'quantity' => $item->qty,
            'price'    => $item->price,
        ])->toArray();
    }
}

$invoice = Invoice::find(1);

// Phase 1: Generate XML locally
$xml = $invoice->generateZatcaXml();

// Phase 2: Submit to ZATCA directly (auto-signs and routes to Clearance or Reporting)
$response = $invoice->submitToZatca();

if ($response->isSuccessful()) {
    echo "Invoice submitted successfully!";
} else {
    print_r($response->errors);
}

use Hyderkamran\LaravelZatca\Facades\Zatca;

$invoiceService = Zatca::invoice()
    ->simplified()
    ->seller(['name' => 'Acme', 'vat_number' => '300000000000003'])
    ->buyer(['name' => 'John Doe'])
    ->items([
        ['name' => 'Service A', 'quantity' => 1, 'price' => 100],
    ])
    ->vat(15);

// Get Base64 QR Code Image
$qrBase64 = $invoiceService->generateQrCode();

// Get valid XML
$xml = $invoiceService->generate();

// Submit
$response = Zatca::submit($invoiceService);
bash
php artisan vendor:publish --tag="zatca-config"
php artisan vendor:publish --tag="zatca-migrations"
bash
php artisan migrate
bash
php artisan zatca:generate-csr --env=sandbox --save-db