mqqz/fatoora-rs
Open-source toolkit for everything you'd need for ZATCA (Zakat, Tax and Customs Authority of Saudi Arabia) Phase 1 and 2 compliant e-invoicing
πΈπ¦ fatoora-rs π§Ύ
π Homepage Β β’Β β‘ Features Β β’Β π₯ Installation Β β’Β π‘ Examples Β β’Β ποΈ Documentation
An unofficial open-source toolkit for everything you'd need for ZATCA (Zakat, Tax and Customs Authority of Saudi Arabia) Phase 1 and 2 compliant e-invoicing.
With bindings and support for many programming languages:
fatoora-rsis in active early development. While the core functionality is usable, the public API is still evolving and may change as the project matures.
We strive to maintain good test coverage and stability, but users should be aware that some rough edges may remain.
Feedback and contributions are especially welcome at this stage.
Disclaimer:
fatoora-rs is not affiliated, associated, authorized, endorsed by, or in any way officially connected with ZATCA (Zakat, Tax and Customs Authority), or any of its subsidiaries or its affiliates. The official ZATCA website can be found at https://zatca.gov.sa.
ποΈ Documentation
Checkout the homepage, also the Rust API at docs.rs may prove useful.
β‘ Features
Everything done by the official ZATCA SDK
- π© CSR Generation
- βοΈ Invoice Signing (All invoice types)
- β Validation (UBL only for now)
- π§Ύ QR Generation
- π¨ API Requests
π But we do it faster and better e.g. ~190x faster invoice hashing than ZATCA's SDK (see bench/)
π§© Dependencies
XML parsing/manipulation is done internally with libxml2, so you might need to install it if you haven't already. See here for relevant instructions.
π₯ Installation
Rust
cargo add fatoora-core
Python
pip install fatoora-rs
C/C++
Download the precompiled shared library and headers for your platform on the repo's releases (fatoora-ffi-*).
Alternatively, if you'd like to build from source. Clone the repo and run:
cargo build -p fatoora-ffi --release
The compiled library will be in target/release/ for your platform (e.g. libfatoora_ffi.so, libfatoora_ffi.dylib, or fatoora_ffi.dll) and the headers will be (re)generated and written to fatoora-ffi/include/
CLI
The cli tool can also be installed with cargo:
cargo install fatoora-rs-cli
Alternatively, you can grab the precompiled binary for your platform on the repo's releases (fatoora-rs-cli-*).
π‘ Examples
CSR Generation
Rust
use fatoora_core::config::EnvironmentType;
use fatoora_core::csr::{CsrProperties, SigningKey};
let props_text = std::fs::read_to_string("csr.properties")?;
let props = CsrProperties::from_properties_str(&props_text)?;
let key = SigningKey::generate();
let csr = props.build(&key, EnvironmentType::NonProduction)?;
let csr_pem = csr.to_pem()?;
let key_pem = key.to_pem()?;Python
from fatoora.config import Environment
from fatoora.csr import CsrProperties, SigningKey
props = CsrProperties.parse_csr_config_file("csr.properties")
key = SigningKey.generate()
csr = props.build(key, Environment.NON_PRODUCTION)
csr_pem = csr.to_pem_base64()
key_pem = key.to_pem()CLI
fatoora-rs-cli csr --csr-config csr.properties --generated-csr csr.pem --private-key key.pem --pemInvoice Signing
Rust
use fatoora_core::invoice::sign::InvoiceSigner;
let cert_pem = std::fs::read_to_string("cert.pem")?;
let key_pem = std::fs::read_to_string("key.pem")?;
let signer = InvoiceSigner::from_pem(cert_pem.trim(), key_pem.trim())?;
let xml = std::fs::read_to_string("invoice.xml")?;
let signed_xml = signer.sign_xml(&xml)?;CLI
fatoora-rs-cli sign --invoice invoice.xml --cert cert.pem --key key.pem --signed-invoice signed.xmlValidation
Rust
use fatoora_core::config::Config;
use fatoora_core::invoice::validation::validate_xml_invoice_from_str;
let config = Config::new(fatoora_core::config::EnvironmentType::NonProduction);
let xml = std::fs::read_to_string("invoice.xml")?;
validate_xml_invoice_from_str(&xml, &config)?;CLI
fatoora-rs-cli validate --invoice invoice.xml --xsd-path assets/schemas/UBL2.1/xsd/maindoc/UBL-Invoice-2.1.xsdQR Generation & Reading
Rust
use fatoora_core::invoice::xml::parse::parse_signed_invoice_xml;
let xml = std::fs::read_to_string("signed.xml")?;
let signed = parse_signed_invoice_xml(&xml)?;
let qr = signed.qr_code();CLI
fatoora-rs-cli qr --invoice invoice.xml
fatoora-rs-cli qr --invoice signed.xml
fatoora-rs-cli qr --invoice signed.xml --fail-on-signed
fatoora-rs-cli qr-read --invoice signed.xmlInvoice Hash
Rust
use fatoora_core::invoice::xml::parse::parse_finalized_invoice_xml;
let xml = std::fs::read_to_string("invoice.xml")?;
let invoice = parse_finalized_invoice_xml(&xml)?;
let hash = invoice.hash_base64()?;CLI
fatoora-rs-cli generate-hash --invoice invoice.xmlInvoice Request Payload
Rust
use fatoora_core::invoice::xml::parse::parse_signed_invoice_xml;
let xml = std::fs::read_to_string("signed.xml")?;
let signed = parse_signed_invoice_xml(&xml)?;
let payload = serde_json::json!({
"invoiceHash": signed.invoice_hash(),
"uuid": signed.uuid(),
"invoice": signed.to_xml_base64(),
});CLI
fatoora-rs-cli invoice-request --invoice signed.xml --api-request request.jsonAnd much more... see the guides in the homepage for more info.
π€ Contributing
Contributions are always welcome! See CONTRIBUTING.md for more details.
πͺ§ Roadmap
- Increase test coverage to 100% (Inshallah)
- Add the full validation suite (not only UBL schema)
- Expand bindings to other languages (subject to demand)
- PDF invoice generation
Author π€
This project began as a personal undertaking, and
continues to be developed and maintained by me @mqqz.
While this means development happens alongside other commitments, I do my
best to keep the project maintained and responsive to issues. Contributions,
feedback, and suggestions are always welcome.
If you or your team are working on fintech or ZATCA integrations and need help building
reliable scalable infrastructure, from someone who knows his way around this,
feel free to get in touch ;)