1. Go to this page and download the library: Download meita/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/ */
meita / zatca example snippets
use Zatca\ZatcaClient;
use Zatca\Invoice;
$client = new ZatcaClient([
// عنوان واجهة البرمجة؛ استخدم sandbox للتجارب أو production للإرسال الفعلي
'base_url' => 'https://sandbox.zatca.gov.sa/e-invoicing/core/v2',
// معرف الختم الصادر (CSID)
'client_id' => 'YOUR_CSID_HERE',
// السر المرتبط بالـ CSID
'secret' => 'YOUR_SECRET_HERE',
// المسار إلى المفتاح الخاص لتوقيع الفاتورة
'private_key_path' => __DIR__ . '/keys/private_key.pem',
// المسار إلى الشهادة العامة (اختياري في هذه النسخة)
'certificate_path' => __DIR__ . '/keys/certificate.crt',
]);
// البيانات المطلوبة للفاتورة، ويمكنك تخصيصها حسب احتياجاتك
$data = [
'uuid' => Invoice::generateUuid(),
'issue_date' => date('c'), // ISO 8601 timestamp
'seller_name' => 'شركة المثال للتجارة',
'seller_vat' => '123456789012345',
'invoice_total' => 115.00,
'vat_total' => 15.00,
// في حال كانت هذه ليست أول فاتورة، أدخل Hash الفاتورة السابقة
'previous_hash' => null,
// عناصر أخرى يمكن إضافتها فى ملف XML
'items' => [
[
'name' => 'منتج 1',
'quantity' => 1,
'price' => 100.00,
],
],
];
$invoice = new Invoice($data);
// يمكن تعديل العناصر بعد الإنشاء
$invoice->setPreviousInvoiceHash('previousHashValue');
// هذه الطريقة تقوم تلقائياً بإنشاء XML، احتساب hash وتوقيعه، بناء رمز QR، ثم إرسالها عبر واجهة التصفية
$response = $client->sendStandardInvoice($invoice);
if ($response['status'] === 'success') {
// احفظ الفاتورة المختومة (response['invoice']) كما هو موضح فى مواصفات ZATCA
file_put_contents('cleared_invoice.xml', base64_decode($response['invoice']));
} else {
// عالج الأخطاء بناءً على رسالة الرفض أو التحذيرات
var_dump($response['errors']);
}
$response = $client->sendSimplifiedInvoice($invoice);
// فى هذا النموذج، يجب أن تحتوى الفاتورة على الختم والرمز قبل الإرسال
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.