تخطي إلى المحتوى الرئيسي

Developer Docs · Schema v1.0.0

توثيق مطوري GulfInvoice Check

دليل تقني شامل لتشغيل محرك التحقق محلياً، دمج واجهة FastAPI أو حزمة Python، واستخدام Apify Actors.

1. البدء السريع محلياً ($0)

يعتمد المحرك على Python 3.12 و uv. شغّل الاختبارات وخادم FastAPI المحلي بالأوامر التالية:

Install & Start Local APIpowershell
uv sync --extra dev
uv run pytest --cov=packages/gic_core
uv run uvicorn gic_api.main:app --host 127.0.0.1 --port 8000 --app-dir apps/local-api/src

2. صيغ المدخلات وحدود الأمان

  • ZATCA XML POST /v1/validate/zatca: UTF-8 UBL 2.1 XML, max 1 MiB, max depth 64, max elements 25,000. DOCTYPE and ENTITY declarations are strictly blocked.
  • ZATCA QR POST /v1/decode/qr: Base64 TLV string (max 4,096 chars accepted; warning above ZATCA's 700-char guideline) + optional comparison XML.
  • JoFotara Validate & Build POST /v1/validate/jofotara / POST /v1/build/jofotara: UBL 2.1 XML or strictly typed JSON request with Decimal numeric fields.
  • Batch Limit POST /v1/validate/batch: Bounded to 50 items per request by default; remote URL fetching is disabled for SSRF protection.

3. مخطط المخرجات الموحّد (Schema v1.0.0)

ValidationResult Envelopejson
{
  "schema_version": "1.0.0",
  "standard": "zatca",
  "status": "invalid",
  "is_valid_preflight": false,
  "summary": {
    "total_rules_evaluated": 40,
    "passed_count": 34,
    "error_count": 6,
    "warning_count": 0,
    "unsupported_count": 2,
    "duration_ms": 1.82
  },
  "findings": [
    {
      "rule_id": "BR-KSA-65",
      "category": "business_rule",
      "severity": "error",
      "xpath": "/Invoice/cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cbc:PlotIdentification",
      "actual_value": "88",
      "expected_value": "4 numeric digits (e.g. 2223)",
      "evidence_status": "under_review"
    }
  ]
}

4. أمثلة Python Client و FastAPI

clients/python/gic_clientpython
from pathlib import Path
from gic_client import GulfInvoiceClient

xml = Path("fixtures/zatca/invalid_br_ksa_65_and_vat.xml").read_text(encoding="utf-8")
with GulfInvoiceClient("http://127.0.0.1:8000") as client:
    result = client.validate_zatca(xml)
    print(result.status, [f.rule_id for f in result.findings])

5. تشغيل Apify Actors والمعالجة الجماعية

تضم المنصة ثلاثة Actors مستقلة: zatca-invoice-validator و zatca-qr-checker و jofotara-invoice-toolkit. الفوترة معطّلة افتراضياً وتستخدم توقيع Apify Python SDK 3.4 الموثق.

Actor Input (zatca-invoice-validator)json
{
  "invoices": [
    { "item_id": "inv-001", "xml": "<Invoice>...</Invoice>" },
    { "item_id": "inv-002", "xml": "<Invoice>...</Invoice>" }
  ],
  "maxItems": 50
}

6. الربط مع n8n و GitHub Actions CI/CD

في n8n، استخدم عقدة HTTP Request نحو الخادم المحلي أثناء التطوير أو عقدة Apify الرسمية بعد نشر الـ Actors. وفي CI/CD أضف فحص الفواتير الاصطناعية ضمن pytest:

.github/workflows/erp-invoice-gate.ymlyaml
- name: Preflight Validate ERP Synthetic Fixtures
  run: |
    uv run python -c "
    from pathlib import Path
    from gic_core.validation import validate_zatca_xml
    res = validate_zatca_xml(Path('fixtures/zatca/valid_standard_b2b.xml').read_text())
    assert res.is_valid_preflight, [f.rule_id for f in res.findings]
    "

7. استكشاف الأخطاء الشائعة

  • SEC-XML-DTD: Strip any <!DOCTYPE> header injected by legacy XML serializers.
  • QR-03 (Arabic UTF-8 length mismatch): Always measure TLV lengths in UTF-8 bytes (len(s.encode("utf-8"))), never character length.
  • Browser Quick Check vs Local API: Start Uvicorn on 127.0.0.1:8000. On a locally served site (localhost ports 3000 and 4173, pre-configured in CORS) the workspace detects it automatically. On a public site origin the page never contacts localhost on its own: click Connect local API in the workspace or status page (your browser may ask for local-network permission), and start Uvicorn with that origin allowed, e.g. GIC_API_CORS_ORIGINS=https://your-site.example (comma-separated; setting it replaces the localhost defaults).