I successfully built an ASP.NET application which uses Zatca .NET SDK (Saudi Arabia E-Invoicing) to create and sign xml files. Everything is working fine in ASP.NET with the SDK.
Now I am trying to make a .NET API to do the same functions. But when executing the API, it gets stuck in a function. Where the function is using Zatca .NET SDK to execute.
Code of the function is:
public static string[] SignDocument(XmlDocument doc, string signedPath, string certificate, string privateKey)
{
string[] retresult = new string[3];
try
{
SignResult result = new EInvoiceSigner().SignDocument(doc, certificate, privateKey);
if (result != null && result.IsValid)
{
result.SaveSignedEInvoice(signedPath);
string hash = (from s in result.Steps
where s.StepName.Contains("Generate EInvoice Hash")
select s).First<StepResult>().ResultedValue;
string[] RequestResult = new string[2];
RequestResult = GenerateRequest(signedPath);
if (RequestResult[0] == "success")
{
retresult[0] = "success";
retresult[1] = hash;
retresult[2] = RequestResult[1];
return retresult;
}
else
{
retresult[0] = "invreq";
retresult[1] = "";
retresult[2] = "";
return retresult;
}
}
else
{
retresult[0] = "sign";
return retresult;
}
}
catch (Exception ex)
{
retresult[0] = "failed";
return retresult;
}
}
Here on executing the below line it shows QR code error.
SignResult result = new EInvoiceSigner().SignDocument(doc, certificate, privateKey);
The error is passed from the SDK. Problem is that the same function is working fine in the ASP.NET application. Everything is the same. Using same SDK dll, same values are passed to the function.
I tried different checking and couldn’t find any success. I also tried to extract code from the SDK dll and it’s as below:
namespace Zatca.EInvoice.SDK
{
public class EInvoiceSigner : IEInvoiceSigner
{
private readonly CoRExecutor signDocumentExecutor;
public EInvoiceSigner()
{
this.signDocumentExecutor = new CoRExecutor().AddMiddleware<ValidateSignInputsMiddleware>().AddMiddleware<GenerateHashMiddleware>().AddMiddleware<GenerateSignatureMiddleware>().AddMiddleware<ParseCertificateMiddleware>().AddMiddleware<HashCertificatesMiddleware>().AddMiddleware<TransformXMLResultMiddleware>().AddMiddleware<PopulateSignedSignaturePropertiesMiddleware>().AddMiddleware<PopulateUBLExtensionsMiddleware>().AddMiddleware<GenerateQRMiddleware>().AddMiddleware<FinalizeSignDocumentMiddleware>();
}
public SignResult SignDocument(
XmlDocument eInvoice,
string certificateContent,
string privateKeyContent)
{
EInvoiceData invoice = new EInvoiceData(eInvoice)
{
CertificateContent = certificateContent,
PrivateKeyContent = privateKeyContent
};
this.signDocumentExecutor.Execute(invoice);
return new SignResult()
{
SignedEInvoice = invoice.EInvoice,
IsValid = invoice.IsValid,
ErrorMessage = invoice.Errors,
Steps = invoice.Steps
};
}
}
}
Error shown is:
[Error] Generating EInvoice Signature[Error] Generating EInvoice QR Code
I can not make any changes to the SDK. I can not understand the problem. Please help