Skip to main content

django-zatca

ZATCA (Fatoora) e-invoicing integration for Django — Saudi Arabia VAT e-invoicing

PyPI version Python versions Django versions License

معالجة الفوترة الإلكترونية وفق متطلبات هيئة الزكاة والضريبة والجمارك (زاتكا) - نظام فاتورة


Features

  • Phase 1 — Simplified e-invoicing (QR code generation with TLV encoding)
  • Phase 2 — Full e-invoicing (EC key pair & CSR generation, XML signing, compliance/certificate issuance, clearance & reporting)
  • Pure Python SVG QR — No external QR library required; built-in pure-Python QR generator
  • Django models — ZatcaCertificate and ZatcaInvoiceLog for audit trail
  • Management commands — zatca_onboard, zatca_sync, zatca_check
  • REST API endpoints — /zatca/status, /zatca/onboard, /zatca/invoice/sync
  • Signals — invoice_cleared, invoice_reported, invoice_failed for extensibility

Requirements

  • Python 3.7+
  • Django 2.2+

Quick Start

1. Install

pip install django-zatca

2. Add to INSTALLED_APPS

INSTALLED_APPS = [
    ...
    "django_zatca",
]

3. Configure

Add ZATCA to your Django settings:

ZATCA = {
    "PHASE": "both",                  # "phase1", "phase2", or "both"
    "ENVIRONMENT": "sandbox",         # "sandbox" or "production"
    "EGS_UUID": "your-egs-uuid",
    "VAT_NUMBER": "311111111100003",
    "VAT_NAME": "Your Company Name",
    "CITY": "Riyadh",
    "CITY_SUBDIVISION": "Al Olaya",
    "STREET": "King Fahd Road",
    "BUILDING": "1234",
    "PLOT_IDENTIFICATION": "5678",
    "POSTAL_ZONE": "12345",
    "BRANCH_NAME": "Main Branch",
    "BRANCH_INDUSTRY": "Retail",
    "CRN_NUMBER": "CRN12345",
    "CERTIFICATE": "",                # base64-encoded compliance certificate
    "PRIVATE_KEY": "",                # base64-encoded EC private key
    "SECRET": "",                     # secret from ZATCA
    "QUEUE_CONNECTION": "sync",
    "QUEUE_NAME": "zatca",
    "LOGGING_ENABLED": True,
    "LOG_CHANNEL": "zatca",
    "API_SANDBOX_BASE": "https://gw-fatoora.zatca.gov.sa/e-invoicing/developer-portal",
    "API_PRODUCTION_BASE": "https://gw-fatoora.zatca.gov.sa/e-invoicing/core",
}

Or use environment variables with prefix ZATCA_:

export ZATCA_PHASE=both
export ZATCA_ENVIRONMENT=sandbox
export ZATCA_VAT_NUMBER=311111111100003

4. Run migrations

python manage.py migrate django_zatca

5. Generate QR code

from django_zatca.services.qr_code import generate_phase1_qr
from django_zatca.services.svg_qr import SvgQrGenerator

tlv = generate_phase1_qr("Company Name", "311111111100003", "2024-01-15T14:30:00Z", "115.00", "15.00")
svg = SvgQrGenerator().encode(tlv, size=200)
# save svg bytes to file or return as HTTP response

6. Onboard (Phase 2)

python manage.py zatca_onboard --otp=123456 --save

7. Submit invoices

from django_zatca.tasks import sync_invoice_to_zatca

sync_invoice_to_zatca(
    invoice_data={
        "invoice_serial_number": "SER001",
        "invoice_counter_number": 1,
        "issue_date": "2024-01-15",
        "issue_time": "14:30:00",
        "currency": "SAR",
        "previous_invoice_hash": "",
        "invoice_type": "INVOICE",
        "line_items": [
            {"id": "1", "name": "Item", "quantity": "1", "tax_exclusive_price": "100.00", "vat_percent": "0.15"},
        ],
    },
    egs_unit=egs_config,
    certificate=certificate,
    private_key=private_key,
    secret=secret,
)

API Endpoints

Include the URLs in your project:

from django.urls import include, path

urlpatterns = [
    path("api/", include("django_zatca.urls")),
]
Endpoint Method Description
/api/zatca/status GET Returns phase & environment status
/api/zatca/onboard POST Onboard EGS unit (requires otp in JSON body)
/api/zatca/invoice/sync POST Submit an invoice for clearance/reporting

Management Commands

Command Description
python manage.py zatca_onboard Interactive onboarding wizard
python manage.py zatca_sync --invoice=SERIAL Sync a single invoice
python manage.py zatca_check Check package readiness

Signals

from django_zatca.signals import invoice_cleared, invoice_reported, invoice_failed

@receiver(invoice_cleared)
def on_invoice_cleared(sender, invoice_data, result, **kwargs):
    # update your local db status
    pass

Development

git clone https://github.com/aghfatehi/django-zatca.git
cd django-zatca
pip install -e .
pip install -r requirements-dev.txt
pytest

License

MIT

Metadata

Release files for django-zatca 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-zatca 1.0.0
File Size Uploaded Provenance Actions
django_zatca-1.0.0.tar.gz 45.8 kB Without provenance Details for django_zatca-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-zatca 1.0.0
File Interpreter ABI Platform Actions
django_zatca-1.0.0-py3-none-any.whl Python 3 none any Details for django_zatca-1.0.0-py3-none-any.whl

Total release size: 108.3 kB

Release files / django_zatca-1.0.0.tar.gz

Download URL django_zatca-1.0.0.tar.gz
Size 45.8 kB
Tags Source
SHA-256 checksum
How to use checksums
2a73090cb740887b8c704cbddaeb21844f9f697e53053d58adb346613f7f30a9
BLAKE2b-256 checksum
How to use checksums
60159b283afbb05670f90d39f03f8cfdce3e698e2201a0dc68c5f1f92e4f906b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.6

Release files / django_zatca-1.0.0-py3-none-any.whl

Download URL django_zatca-1.0.0-py3-none-any.whl
Size 62.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e5ec7706b40b4a75bd3cf42853132728ad7e4d8bc504efb63af61a2c9a225bfa
BLAKE2b-256 checksum
How to use checksums
efdd14ba710f594ad2fdaba1b7e1516ec3d35f99af15cc7bada6e4af8cabe25d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.6

Security for django-zatca 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page