Skip to content

QR Codes

Extracting and generating QR payloads from signed invoices.

Example

pub fn main() {
    use fatoora_core::invoice::xml::parse::parse_signed_invoice_xml;

    // Doc example: extract QR payload from signed XML.
    let signed_xml_path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("tests/fixtures/invoices/sample-simplified-invoice.xml");
    let signed_xml = std::fs::read_to_string(&signed_xml_path).expect("read signed invoice xml");
    let signed = parse_signed_invoice_xml(&signed_xml).expect("parse signed invoice");
    let qr = signed.qr_code();
    assert!(!qr.is_empty());
}
from fatoora.invoice import parse_signed_invoice_xml

# signed_xml_path = "path/to/signed_invoice.xml"
signed_xml = signed_xml_path.read_text(encoding="utf-8")
signed = parse_signed_invoice_xml(signed_xml)
qr = signed.qr()
assert qr
#include "fatoora/invoice.h"

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifndef FATOORA_DOC_SIGNED_XML
#define FATOORA_DOC_SIGNED_XML "path/to/signed_invoice.xml"
#endif

static char *read_file(const char *path);

int main(void) {
    const char *signed_xml_path = FATOORA_DOC_SIGNED_XML;
    char *xml_cstr = read_file(signed_xml_path);
    if (!xml_cstr) {
        /* handle error */
        return 1;
    }

    /* signed_xml_path = "path/to/signed_invoice.xml" */
    struct FfiResult_FfiSignedInvoice signed_invoice = fatoora_parse_signed_invoice_xml(xml_cstr);
    if (!signed_invoice.ok) {
        /* handle error */
        return 1;
    }

    struct FfiResult_FfiString qr = fatoora_signed_invoice_qr(&signed_invoice.value);
    assert(qr.value.ptr && strlen(qr.value.ptr) > 0);
    /* use qr.value.ptr, then free */
    fatoora_string_free(qr.value);
    free(xml_cstr);
    fatoora_signed_invoice_free(&signed_invoice.value);
    return 0;
}

Notes

  • The QR payload is a base64-encoded TLV string. Tag ordering follows ZATCA (seller, VAT, timestamp, totals) and includes signing tags when available.

See also: QR Reference