Just use xml from Sample SDK to test. Its compliance with UBL2.1 and you can focus to get How to Signing Invoice.
If you just need to get Invoicehash
, Signing Invoice
, and Create Request Api
for SignedDocument, you can simply use .Net Zatca.EInvoice.SDK.
Let me share the sample code.
- Add Zatca.EInvoice.SDK and Zatca.EInvoice.SDK.Contracts as Assembly Reference
- Add Rules files to your project folder.
...repos\ZatcaWithSDK\ZatcaWithSDK\Data\Rules\schematrons\20210819_ZATCA_E-invoice_Validation_Rules.xsl
...repos\ZatcaWithSDK\ZatcaWithSDK\Data\Rules\schematrons\CEN-EN16931-UBL.xsl
- Then you need
cert.pem
,ec-secp256k1-priv-key.pem
andSimplified_Invoice.xml
to test the SDK (i use this folderC:\Tmp\SdkTest\
for all myFiles)
using System.Xml;
using Zatca.EInvoice.SDK;
using Zatca.EInvoice.SDK.Contracts.Models;
namespace ZatcaWithSDK
{
internal class Program
{
static void Main(string[] args)
{
Console.OutputEncoding = System.Text.Encoding.UTF8;
var eInvoicePath = @"C:\tmp\SdkTest\Simplified_Invoice.xml";
var certificateFilePath = @"C:\tmp\SdkTest\cert.pem";
var privateKeyFilePath = @"C:\tmp\SdkTest\ec-secp256k1-priv-key.pem";
var signedPath = @"C:\tmp\SdkTest\Signed_Simplified_Invoice.xml";
var jsonPath = @"C:\tmp\SdkTest\ApiRequestPayload.json";
XmlDocument document = new XmlDocument() { PreserveWhitespace = true};
document.Load(eInvoicePath);
SignResult result = new EInvoiceSigner().SignDocument(document, File.ReadAllText(certificateFilePath), File.ReadAllText(privateKeyFilePath));
ShowSignResult(result);
if (result.IsValid)
{
result.SaveSignedEInvoice(signedPath);
Console.WriteLine($"Invoice has been saved successfully\n");
//Get Invoice Hash for Next PIH
var SignedInvoiceHash = GetInvoiceHash(result);
Console.WriteLine($"Invoice Hash : {SignedInvoiceHash}\n");
//GetRequestApi Payload
RequestGenerator RequestGenerator = new RequestGenerator();
RequestResult RequestResult = RequestGenerator.GenerateRequest(result.SignedEInvoice);
if (RequestResult.IsValid)
{
RequestResult.SaveRequestToFile(jsonPath);
Console.WriteLine($"Request Api Payload : \n{ RequestResult.InvoiceRequest.Serialize()}");
}
}
}
public static string GetInvoiceHash(SignResult signResult)
{
var step = signResult.Steps
.FirstOrDefault(s => s.StepName == "Generate EInvoice Hash");
return step?.ResultedValue ?? "Invoice Hash not found";
}
public static void ShowSignResult(SignResult signResult)
{
foreach (var step in signResult.Steps)
{
Console.WriteLine($"Step: {step.StepName}");
Console.WriteLine($" Status: {(step.IsValid ? "Valid" : "Invalid")}");
//Console.WriteLine($" ResultValue: {step.ResultedValue}");
if (step.ErrorMessages.Any())
{
Console.WriteLine(" Errors:");
foreach (var error in step.ErrorMessages)
{
Console.WriteLine($" - {error}");
}
}
if (step.WarningMessages.Any())
{
Console.WriteLine(" Warnings:");
foreach (var warning in step.WarningMessages)
{
Console.WriteLine($" - {warning}");
}
}
}
Console.WriteLine();
Console.WriteLine($"Overall Sign Result: {(signResult.IsValid ? "Valid" : "Invalid")}");
Console.WriteLine();
}
}
}
This work good.
I am waiting for someone to provide a code example, how to validate Invoice using .Net Zatca.EInvoice.SDK Library.
So far I have not managed to do it.