PIH (Previous Invoice Hash)
PIH is the hash of the device's previous invoice, written into the next one so a ZATCA device's invoices form a chain. BR-KSA-61 requires it.
PIH stands for Previous Invoice Hash. Every document a device issues carries the invoice hash of the document before it from the same device, which, with the ICV counter, chains the device's invoices together.
In the XML (field KSA-13) it is a cac:AdditionalDocumentReference whose cbc:ID is PIH, with the hash in cac:Attachment/cbc:EmbeddedDocumentBinaryObject and mimeCode="text/plain". A device's first invoice has no predecessor, so the official text fixes a value for it: NWZlY2Vi…OQ==, the base64 of the hex SHA-256 of the character "0".
What goes wrong: BR-KSA-61 fires when the element is missing, empty or not text/plain. BR-KSA-26 describes how the hash is computed, but when we tested SDK 3.4.8 the check only needs a non-empty value: a made-up base64 string passes. A clean validator run does not prove your chain is right. Typical mistakes are storing the hash before ZATCA accepts the invoice, and two tills sharing one chain.
On Matheel, each response returns invoice_hash; send it as previous_invoice_hash on the next invoice. With a device_token Matheel keeps the PIH inside the token and fills it in for you, including on the first invoice.
Example
<cac:AdditionalDocumentReference>
<cbc:ID>PIH</cbc:ID>
<cac:Attachment>
<cbc:EmbeddedDocumentBinaryObject mimeCode="text/plain">NWZlY2ViNjZmZmM4NmYzOGQ5NTI3ODZjNmQ2OTZjNzljMmRiYzIzOWRkNGU5MWI0NjcyOWQ3M2EyN2ZiNTdlOQ==</cbc:EmbeddedDocumentBinaryObject>
</cac:Attachment>
</cac:AdditionalDocumentReference>
FAQ
What is the PIH of the first invoice?
BR-KSA-26's official text gives it: NWZlY2ViNjZmZmM4NmYzOGQ5NTI3ODZjNmQ2OTZjNzljMmRiYzIzOWRkNGU5MWI0NjcyOWQ3M2EyN2ZiNTdlOQ==, the base64 of the hex SHA-256 of the character "0".
The validator accepted my PIH. Is it correct?
Not necessarily. The SDK 3.4.8 rules only check that a value is present, so any base64 passes. Take the hash from the last invoice ZATCA actually accepted.
Is there one PIH chain per company?
No, one per device (EGS): each device has its own counter and its own last accepted hash.
Related ZATCA rules
Related terms
Let Matheel handle this for you
Matheel signs the invoice, files it with ZATCA and keeps the counter and hash in one request, and returns the PDF with the QR.
Try Matheel, no signup Matheel API docs