Hello guys i am facing with the same issue about zatca dll, when i am using cmd its okay, but when i am using ValidateEInvoice method its getting some error which one of them its validate EN Schematrons, validate KSA Schematrons and validate EInvoice PIH
also my .net code source its in hereâŚ
cant validate at all, could you please help me?
using Zatca.EInvoice.SDK.Contracts.Models;
using System.Xml;
using System.Text;
using Zatca.EInvoice.SDK;
using ZatcaWebAPI.Models;
using System.Xml.Linq;
using System.ComponentModel.DataAnnotations;
namespace ZatcaWebApp.Services
{
public class InvoiceService
{
private const string FilePath = @âC:\Users\tr32040\Downloads\zatca-einvoicing-sdk-238-R3.3.6\Data\PIH\pih.txtâ;
public (RequestResult requestResult, string InvoiceHash, string Base64QrCode) GenerateInvoiceRequest(InvoiceRequestModel requestModel)
{
try
{
UpdatePihFile(requestModel.PIH);
string correctedBase64 = requestModel.XmlContent;
int mod4 = correctedBase64.Length % 4;
if (mod4 > 0)
{
correctedBase64 += new string('=', 4 - mod4);
}
byte[] xmlBytes = Convert.FromBase64String(correctedBase64);
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(Encoding.UTF8.GetString(xmlBytes));
xmlDocument = PrettyXml(xmlDocument);
var (requestResult, invoiceHash, base64QrCode) = GenerateRequestApi(xmlDocument, requestModel.X509CertificateContent, requestModel.PrivateKeyContent, requestModel.PIH);
return (requestResult, invoiceHash, base64QrCode);
}
catch (Exception ex)
{
throw new Exception($"An error occurred: {ex.Message}");
}
}
private void UpdatePihFile(string newContent)
{
try
{
System.IO.File.WriteAllText(FilePath, newContent);
}
catch (Exception ex)
{
throw new Exception($"An error occurred: {ex.Message}");
}
}
private static (RequestResult requestResult, string InvoiceHash, string Base64QrCode) GenerateRequestApi(XmlDocument document, string x509CertificateContent, string privateKey, string PIH)
{
SignResult signedInvoiceResult = new EInvoiceSigner().SignDocument(document, x509CertificateContent, privateKey);
Zatca.EInvoice.SDK.EInvoiceValidator validator = new Zatca.EInvoice.SDK.EInvoiceValidator();
var validationResult = validator.ValidateEInvoice(signedInvoiceResult.SignedEInvoice, x509CertificateContent, PIH);
if (validationResult != null)
{
foreach (var e in validationResult.ValidationSteps)
{
Console.WriteLine(e.ValidationStepName + " : " + e.IsValid);
if (!e.IsValid)
{
foreach (var x in e.ErrorMessages)
{
Console.WriteLine(x);
}
}
else
{
foreach (var x in e.WarningMessages)
{
Console.WriteLine(x);
}
}
}
Console.WriteLine($"\nOverall Signed Invoice Validation : {validationResult.IsValid}!");
if (validationResult.IsValid)
{
var (invoiceHash, base64QrCode) = ExtractInvoiceHashAndBase64QrCode(signedInvoiceResult.SignedEInvoice);
var requestResult = new RequestGenerator().GenerateRequest(signedInvoiceResult.SignedEInvoice);
return (requestResult, invoiceHash, base64QrCode);
}
else
{
return (null, null, null);
}
}
return (null, null, null);
}
private static (string invoiceHash, string base64QRCode) ExtractInvoiceHashAndBase64QrCode(XmlDocument doc)
{
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("ext", "urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2");
nsmgr.AddNamespace("ds", "http://www.w3.org/2000/09/xmldsig#");
nsmgr.AddNamespace("cbc", "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2");
nsmgr.AddNamespace("cac", "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2");
string invoiceHash = doc.SelectSingleNode("//ds:Reference[@Id='invoiceSignedData']/ds:DigestValue", nsmgr)?.InnerText;
string base64QRCode = doc.SelectSingleNode("//cac:AdditionalDocumentReference[cbc:ID='QR']/cac:Attachment/cbc:EmbeddedDocumentBinaryObject", nsmgr)?.InnerText;
return (invoiceHash, base64QRCode);
}
private static XmlDocument PrettyXml(XmlDocument inputXml)
{
XmlDocument formattedXml = new XmlDocument() { PreserveWhitespace = true };
using (MemoryStream memoryStream = new MemoryStream())
{
using (StreamWriter streamWriter = new StreamWriter(memoryStream, new UTF8Encoding(false)))
{
XmlWriterSettings settings = new XmlWriterSettings()
{
Indent = true,
IndentChars = " ",
OmitXmlDeclaration = false,
Encoding = Encoding.UTF8,
};
using (XmlWriter xmlWriter = XmlWriter.Create(streamWriter, settings))
{
inputXml.Save(xmlWriter);
}
}
string utf8Xml = Encoding.UTF8.GetString(memoryStream.ToArray()).Trim();
formattedXml.LoadXml(utf8Xml);
}
return formattedXml;
}
}
}