i am using SDK (.NET) in my project winforms. i am facing this error on simulation.
I have gone through all the documentation, but I couldn’t find any help on this.
Is there anyone here who has resolved this error/issue for me?
{
“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”: “invalid-invoice-hash”,
“category”: “INVOICE_HASHING_ERRORS”,
“message”: “The invoice hash API body does not match the (calculated) Hash of the XML”,
“status”: “ERROR”
}
],
“status”: “ERROR”
},
“reportingStatus”: “NOT_REPORTED”,
“clearanceStatus”: null,
“qrSellertStatus”: null,
“qrBuyertStatus”: null
}
Dear @devabdullahbukhari ,
Thank you for reaching out.
Regarding this raised error, please ensure the following:
1- For simplified tax documents, ensure that the private key & x.509 certificate that have been used in the signing process are correlated, if you used a private key that doesn’t belong to the used x.509 certificate or vice versa, then you might receive such errors.
2- Is the hash calculation are being done on the final invoice? making any adjustment after hash generation to the XML will return errors.
3- Is the hash generation steps followed are correct? for the same infected invoice API request, please use our SDK using CMD (CLI) to generate the invoiceHash, without making and changes to the XML, if the calculated invocieHash is different than the submitted invoiceHash in the API body, then you need to review the steps of the applied Hash calculation from your side.
Regards,
Dear @Aturkistani,
Thank you for your previous response and guidance.
I have carefully followed all recommended steps and used both ZATCA’s SDK and .NET DLL for generating and submitting the simplified invoices. However, I am still facing issues with the invoice hash validation during the compliance phase, despite the signed invoice being successfully generated.
To give you a better understanding, I am sharing a relevant code snippet (below) which outlines my end-to-end flow — including CSR generation, CSID acquisition, invoice signing, QR code/hash generation, validation, and submission.
• The invoice is signed successfully with the correct CSID and private key pair.
• The validation step fails, reporting hash mismatch or similar errors.
• I’m working in developer mode, and I’d like to confirm if that could affect hash validation in any way.
• The QR Code and HashCode are generated after signing the invoice, as shown in the code — please confirm if this order is correct and expected.
Sample Code (C#):
public void GenerateCSIDVIACmd()
{
//Generate CSR
GenerateCSRKeys();
CreateCSRFile();
if (!this.IsValid)
{
throw new Exception(“CSR Generation Failed: " + string.Join(”, ", errors));
}
//Post CSR to ZATCA to Get CSID
var response = PostComplianceCSID().ConfigureAwait(false).GetAwaiter().GetResult();
string csid = DecodeBase64ToCSID(response.BinarySecurityToken);
string secret = response.Secret;
_csrData.CSRKeysDetails.BinerySecurityToken = response.BinarySecurityToken;
_csrData.CSRKeysDetails.Secret = secret;
_csrData.CSRKeysDetails.CSID = csid;
_csrData.CSRKeysDetails.RequestID = response.RequestID.ToString();
_csrData.CSRKeysDetails.OTP = _csrData.OTPCode;
// insert to Files
_csrData.CSRKeysDetails.CSID.ToSaveFile(Path.Combine(ZatcaSDKHelper.BasePath, zatcaSDKHelper.configSettings.CertPath));
_csrData.CSRKeysDetails.PrivateKey.ToSaveFile(Path.Combine(ZatcaSDKHelper.BasePath, zatcaSDKHelper.configSettings.PrivKeyPath));
_csrData.CSRKeysDetails.CSRKey.ToSaveFile(Path.Combine(ZatcaSDKHelper.BasePath, zatcaSDKHelper.configSettings.CsrPath));
// Invoice Generation
var inv = ZatcaInvoice();
var res = new InvoiceGenerator(inv, _csrData, ZatcaInvoiceType.SimplifiedInvoice);
var XML_result = res.LoadXmlString();
var signedInv = new EInvoiceSigner().SignDocument(XML_result, _csrData.CSRKeysDetails.CSID, _csrData.CSRKeysDetails.PrivateKey);
var QRCode = GenerateQRCode(signedInv.SignedEInvoice);
XML_result = signedInv.SignedEInvoice;
XML_result.OuterXml.ToSaveFile(Path.Combine(ZatcaSDKHelper.BasePath, ZatcaSDKHelper.InvoiceFile));
//Signed Invoiced
zatcaSDKHelper.SignInvoiceAsync();
zatcaSDKHelper.ValidateInvoiceAsync();
zatcaSDKHelper.GenerateJsonFormatAsync();
var jsonRequest = System.IO.File.ReadAllText(Path.Combine(ZatcaSDKHelper.BasePath, zatcaSDKHelper.configSettings.JsonPath));
var InvoiceRequest = JsonConvert.DeserializeObject<InvoiceRequest>(jsonRequest);
string credentials = $"{_csrData.CSRKeysDetails.BinerySecurityToken}:{_csrData.CSRKeysDetails.Secret}";
byte[] bytes = Encoding.UTF8.GetBytes(credentials);
var Token = Convert.ToBase64String(bytes);
HttpClientHandler clientHandler = new HttpClientHandler();
clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };
var client = new HttpClient(clientHandler);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Add("Authorization", $"Basic {Token}");
client.DefaultRequestHeaders.Add("Clearance-Status", "0");
client.DefaultRequestHeaders.Add("Accept-Version", "V2");
client.DefaultRequestHeaders.Add("Accept-Language", "en");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.Timeout = TimeSpan.FromMilliseconds(60000);
StringContent jcontent = new StringContent(JsonConvert.SerializeObject(InvoiceRequest), Encoding.UTF8, "application/json");
HttpResponseMessage response1 = client.PostAsync(aPIHelper.PostComplianceInvoice, jcontent).Result;
string Jsonstring = response1.Content.ReadAsStringAsync().Result; ;
}
I would greatly appreciate it if you could review the above flow and help identify where the potential issue might be. If required, I am open to sharing the generated signed XML as well for further analysis. I also attached the screenshot of response from SDK (CMD).
Looking forward to your support in resolving this.
Best regards,