A Django or FastAPI billing system can produce PINT AE documents with lxml templates or a UBL object layer, validate them against UBL XSDs with lxml and against the PINT AE schematron by executing the compiled XSLT with Saxon-HE (via saxonche) or by calling a validation service, submit through an Accredited Service Provider's API from a Celery task with retries, and store acknowledgements in a status model. Celery with a durable broker gives the queueing behaviour the regulations assume.
Key facts at a glance
- XML: lxml for generation and XSD validation.
- Schematron: compile to XSLT and run with saxonche, or use a small validation microservice.
- Queue: Celery with RabbitMQ or Redis;
acks_lateand retries with back-off. - Provider adapter: a class per ASP behind a common interface; credentials from environment.
- Status: webhook view + periodic beat task for polling.
Generating the document
from lxml import etree
NS = {'cbc': 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2',
'cac': 'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2'}
def build_invoice(inv):
root = etree.Element('{urn:oasis:names:specification:ubl:schema:xsd:Invoice-2}Invoice', nsmap={None: 'urn:oasis:names:specification:ubl:schema:xsd:Invoice-2', **NS})
sub(root, 'cbc:CustomizationID', PINT_AE_CUSTOMIZATION_ID)
sub(root, 'cbc:ID', inv.number)
sub(root, 'cbc:IssueDate', inv.issued_on.isoformat())
sub(root, 'cbc:InvoiceTypeCode', '380')
sub(root, 'cbc:DocumentCurrencyCode', inv.currency)
root.append(party('AccountingSupplierParty', inv.company)) # EndpointID schemeID 0235
root.append(party('AccountingCustomerParty', inv.customer))
...
return etree.tostring(root, xml_declaration=True, encoding='UTF-8')Keep element order exactly as the UBL schema requires; lxml will not reorder for you, and schema validation will fail on misplaced elements.
Validating
schema = etree.XMLSchema(etree.parse('UBL-Invoice-2.1.xsd'))
schema.assertValid(etree.fromstring(xml)) # raises on schema errors
from saxonche import PySaxonProcessor
with PySaxonProcessor(license=False) as proc:
xslt = proc.new_xslt30_processor().compile_stylesheet(stylesheet_file='pint-ae.xsl')
svrl = xslt.transform_to_string(xdm_node=proc.parse_xml(xml_text=xml.decode()))
fatal = parse_svrl_fatal(svrl)Compile the stylesheet once per worker process. The compiled PINT AE XSLT is produced from the published schematron with the standard ISO schematron skeleton.
Submitting from Celery
@shared_task(bind=True, acks_late=True, autoretry_for=(TransientAspError,),
retry_backoff=60, retry_backoff_max=3600, max_retries=10)
def submit_einvoice(self, invoice_id):
inv = Invoice.objects.select_for_update().get(pk=invoice_id)
asp = get_asp_adapter(inv.company)
res = asp.submit(inv.xml, idempotency_key=inv.number)
Ack.objects.create(invoice=inv, kind='provider_accepted', payload=res.raw)
Operating it
Where Zaini Developers fits in
We have delivered this stack for SaaS platforms that invoice on behalf of many businesses: multi-tenant, per-tenant provider credentials, and a shared validation service so every tenant's documents are checked before they leave.
Book a free consultation →Frequently asked questions
Is there a Python library for PINT AE?
No widely adopted one yet. lxml plus the public schemas and schematron covers generation and validation; our team can supply a reference implementation.
Can I validate without Saxon?
Options include executing the schematron XSLT with another XSLT 2/3 processor or calling a validation microservice (for example our PHP engine over HTTP).
Does FastAPI change anything?
No. Replace Celery with your preferred task runner and the Django ORM with your persistence layer; the pipeline is the same.
How do SaaS platforms invoice for their customers?
Each customer is a separate seller with its own TIN and provider appointment; the platform acts as their system, not as the seller.
