How to convert Tag8 and Tag9 from base64

hey there,

i write this code for building QR Code:

            // build QR (using TVL) for 5 things below
            // (Seller’s name "it is name of branch", VAT registration number of the seller "BranchVatID", Time stamp of the invoice (date and time) in accordance with ISO 8601 (example 2022 - 02 - 21T12: 13:57Z), Invoice total (with VAT), VAT total)
            var tag1 = $"{(char)1}{(char)Encoding.UTF8.GetByteCount("test")}{"test"}";
            var tag2 = $"{(char)2}{(char)Encoding.UTF8.GetByteCount("300000000000003")}{"300000000000003"}";
            var tag3 = $"{(char)3}{(char)Encoding.UTF8.GetByteCount(new DateTime(2022, 8, 17, 17, 41, 08).ToString("yyyy-MM-ddTHH:mm:ss"))}{new DateTime(2022, 8, 17, 17, 41, 08).ToString("yyyy-MM-ddTHH:mm:ss")}";
            var tag4 = $"{(char)4}{(char)Encoding.UTF8.GetByteCount("231.15")}{"231.15"}";
            var tag5 = $"{(char)5}{(char)Encoding.UTF8.GetByteCount("30.15")}{"30.15"}";
            var tag6 = $"{(char)6}{(char)Encoding.UTF8.GetByteCount(invoiceHash1)}{invoiceHash1}";
            var tag7 = $"{(char)7}{(char)Encoding.UTF8.GetByteCount(digitalSignature)}{digitalSignature}";

            // The hex string
            string PublicKey1 = "BFz+SYnoXtzpHzlD436/oCTo/CzuBXJcItxmjV4qROWxnRUY0jy2qZIVp86kWF+irTEceJ1yc0QlOx61prXUZFA=";


            var tag8 = $"{(char)8}{(char)Encoding.UTF8.GetByteCount(PublicKey1)}{PublicKey1}";

            // The hex string
            string hexSignature = "MEQCIFZ3j/8M0PYE4FVoalmWpMLCsy5GJe+Qe7jq9BOC1BE8AiBV4gp04LJkgBSFLjLThY0B7PAoU6OFiWPjvm+dEGG4hA==";


            var tag9 = $"{(char)9}{(char)Encoding.UTF8.GetByteCount(hexSignature)}{hexSignature}";



            var TLV1 = tag1 + tag2 + tag3 + tag4 + tag5 + tag6 + tag7 + tag8 + tag9;

            // convert TLV object to string base 64
            var fQR = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(TLV1));

the response from api is:
{
“validationResults”: {
“infoMessages”: [
{
“type”: “INFO”,
“code”: “XSD_ZATCA_VALID”,
“category”: “XSD validation”,
“message”: “Complied with UBL 2.1 standards in line with ZATCA specifications”,
“status”: “PASS”
}
],
“warningMessages”: ,
“errorMessages”: [
{
“type”: “ERROR”,
“code”: “QRCODE_INVALID”,
“category”: “QRCODE_VALIDATION”,
“message”: “Invalid QR code format, Please follow the ZATCA QR code specifications”,
“status”: “ERROR”
}
],
“status”: “ERROR”
},
“reportingStatus”: “NOT_REPORTED”,
“clearanceStatus”: null,
“qrSellertStatus”: null,
“qrBuyertStatus”: null
}

Dear @aymenpro71

Please note that tags 8 and 9 are in hex bytes by default, you shouldn’t encode them to base64 before composing the TLV tags.

Follow these steps:

  1. Convert all tags to hex bytes.
  2. Tags already in hex (like the public key) remain unchanged.
  3. Compose the TLV structure (Tag + Length + Value) for all tags (after conversion to hex)
  4. Encode the entire TLV structure to base64—do not encode each tag individually, but rather encode the full composition of all tags at once.

This should clarify your doubts insha’Allah.

Dear @Aturkistani

I applied the instructions you mentioned and sent that and the result was the same problem with tag8 and tag9

response after update:
{
“validationResults”: {
“infoMessages”: [
{
“type”: “INFO”,
“code”: “XSD_ZATCA_VALID”,
“category”: “XSD validation”,
“message”: “Complied with UBL 2.1 standards in line with ZATCA specifications”,
“status”: “PASS”
}
],
“warningMessages”: ,
“errorMessages”: [
{
“type”: “ERROR”,
“code”: “publicKey_QRCODE_INVALID”,
“category”: “QRCODE_VALIDATION”,
“message”: “ECDSA Public Key does not match with qr code ECDSA public key”,
“status”: “ERROR”
},
{
“type”: “ERROR”,
“code”: “CERTIFICATE_SIGNATURE_QRCODE_INVALID”,
“category”: “QRCODE_VALIDATION”,
“message”: "certificate signature does not match with qr certificate signature value ",
“status”: “ERROR”
}
],
“status”: “ERROR”
},
“reportingStatus”: “NOT_REPORTED”,
“clearanceStatus”: null,
“qrSellertStatus”: null,
“qrBuyertStatus”: null
}

Dear @aymenpro71,

I highly recommend to refer to the QR code section in the E-invoicing detailed technical guide for further information.

And as an alternative approach, you can use ZATCA’s official SDK to generate the QR code from.

Thank you,