GeeSuthSoft.KSA.ZATCA

An Unofficial Repo, This Repo will be have a tools to help programmers to new re

This is a fork of GeeSuth/GeeSuthSoft.KSA.ZATCA, kept for reference and contributions. It is not an original project.

GeeSuthSoft.KSA.ZATCA

Overview

GeeSuthSoft.KSA.ZATCA is a .NET library that helps developers integrate with ZATCA (Zakat, Tax and Customs Authority) requirements in Saudi Arabia. This library provides tools and utilities for e-invoicing compliance.

Features

QR images: QR images are generated with managed code (PNG) and work on Linux/Docker;
center-image (QrCodeOption.CenterImage) and Qr.GetImage remain Windows-only.

Installation

Install via NuGet Package Manager:

dotnet add package GeeSuthSoft.KSA.ZATCA

Configuration

Add the ZATCA service to your dependency injection container in Program.cs or Startup.cs:

using GeeSuthSoft.KSA.ZATCA.Extensions;


services.AddZatca(options =>
            {
                options.Environment = EnvironmentType.NonProduction;
                options.LogRequestAndResponse = true;
            });

Usage

OnBoarding Service

using GeeSuthSoft.KSA.ZATCA.Services;
using GeeSuthSoft.KSA.ZATCA.Dto;

// Example: Generate CSR (Certificate Signing Request)
var onboardingService = serviceProvider.GetRequiredService<IZatcaOnboardingService>();
var csrDto = new CsrGenerationDto {
    CommonName = "TST-886431145-399999999900003",
    SerialNumber = "1-TST|2-TST|3-ed22f1d8-e6a2-1118-9b58-d9a8f11e445f",
    OrganizationIdentifier = "399999999900003",
    OrganizationUnitName = "Riyadh Branch",
    OrganizationName = "Maximum Speed Tech Supply LTD",
    CountryName = "SA",
    InvoiceType = "1100",
    LocationAddress = "RRRD2929",
    IndustryBusinessCategory = "Supply activities"
};
var csrResult = onboardingService.GenerateCsr(csrDto);

// Example: Get CSID (Compliance certificate and secret)
var csidResult = await onboardingService.GetCSIDAsync(csrResult.Csr, "12345");

// Example: Get PCSID (Production certificate and secret)
var pcsidRequest = new PCSIDRequestDto {
    CsidComplianceRequestId = csidResult.RequestID,
    CsidBinarySecurityToken = csidResult.BinarySecurityToken,
    CsidSecret = csidResult.Secret
};
var pcsidResult = await onboardingService.GetPCSIDAsync(pcsidRequest);

Signing Service

using GeeSuthSoft.KSA.ZATCA.Services;
using GeeSuthSoft.KSA.ZATCA.Dto;

var signService = serviceProvider.GetRequiredService<IZatcaSignInvoiceService>();
var invoice = /* construct Invoice object (see below) */;
var signRequest = new SignedInvoiceRequestDto {
    Invoice = invoice,
    BinaryToken = pcsidResult.BinarySecurityToken, // from onboarding
    Secret = csrResult.PrivateKey // from CSR generation
};
var signedInvoice = signService.GetSignedInvoice(signRequest);

Compliance Check

using GeeSuthSoft.KSA.ZATCA.Services;
using GeeSuthSoft.KSA.ZATCA.Dto;

var invoiceService = serviceProvider.GetRequiredService<IZatcaInvoiceService>();
var complianceResult = await invoiceService.ComplianceCheck(
    ccsidBinaryToken: csidResult.BinarySecurityToken,
    ccsidSecret: csidResult.Secret,
    requestApi: signedInvoice.RequestApi // from signing
);

Sharing Service

using GeeSuthSoft.KSA.ZATCA.Services;
using GeeSuthSoft.KSA.ZATCA.Dto;

var shareService = serviceProvider.GetRequiredService<IZatcaShareService>();
var shareRequest = new ShareInvoiceRequestDto {
    invoiceObject = invoice, // your Invoice object
    tokens = new PCSIDInfoDto {
        BinaryToken = pcsidResult.BinarySecurityToken,
        PCSIDSecret = pcsidResult.Secret,
        privateKey = csrResult.PrivateKey
    },
    IsClearance = true // or false for reporting
};
var shareResponse = await shareService.ShareInvoiceWithZatcaAsync(shareRequest);

QR Code Images (Linux / Docker friendly)

QR images are generated with QRCoder's managed PngByteQRCode writer, so they work on
Linux/Docker without libgdiplus or System.Drawing.EnableUnixSupport.

// base64 encoded PNG
var base64Png = Qr.GetBase64(sellerName, vatNumber, receiptDate, vatAmount, totalWithVat);

// directly usable in an <img src="...">
var dataUrl = Qr.GetBase64InUrl(sellerName, vatNumber, receiptDate, vatAmount, totalWithVat);

// raw bytes / stream (cross-platform)
byte[] pngBytes = Qr.GetPngBytes(sellerName, vatNumber, receiptDate, vatAmount, totalWithVat);
using Stream pngStream = Qr.GetPngStream(sellerName, vatNumber, receiptDate, vatAmount, totalWithVat);

// or from a signed invoice
var qrDataUrl = signedInvoice.Base64QrCodeContent.GenerateImageDirectOpenInBrowser();

QrCodeOption.CenterImage and Qr.GetImage (which returns System.Drawing.Bitmap)
remain Windows-only. On other platforms they throw a clear PlatformNotSupportedException;
use GetPngBytes / GetPngStream instead.

Constructing an Invoice

You can use the test helper as a template:

using GeeSuthSoft.KSA.ZATCA.Xml.RootPaths;

var invoice = InvoicesTemplateTest.GetSimpleInvoice();
// Or construct manually:
// var invoice = new Invoice { ... };

Result of ZATCA app :

image


Test & Web API Example Projects

1. Unit & Integration Tests (GeeSuthSoft.KSA.ZATCA.XunitTest)

This project contains comprehensive unit and integration tests for the ZATCA library using xUnit. It demonstrates how to use the library’s services in real scenarios, including onboarding, signing, compliance, and sharing invoices.

How to Run the Tests

What’s Covered

Example Test (Compliance)

[Fact]
public async Task ComplianceCheckTest()
{
    var csrDto = CompanyTemplateTest.CrsCompanyInfo();
    var csrResult = _zatcaOnboardingService.GenerateCsr(csrDto);
    var csidResult = await _zatcaOnboardingService.GetCSIDAsync(csrResult.Csr);
    var pcsidRequest = new PCSIDRequestDto {
        CsidComplianceRequestId = csidResult.RequestID,
        CsidBinarySecurityToken = csidResult.BinarySecurityToken,
        CsidSecret = csidResult.Secret
    };
    var pcsidResult = await _zatcaOnboardingService.GetPCSIDAsync(pcsidRequest);
    var invoice = InvoicesTemplateTest.GetSimpleInvoice();
    var signRequest = new SignedInvoiceRequestDto {
        Invoice = invoice,
        Secret = csrResult.PrivateKey,
        BinaryToken = pcsidResult.BinarySecurityToken
    };
    var signed = _zatcaSignInvoiceService.GetSignedInvoice(signRequest);
    var result = await _zatcaInvoiceService.ComplianceCheck(
        csidResult.BinarySecurityToken,
        csidResult.Secret,
        signed.RequestApi
    );
    Assert.NotNull(result);
}

How to Add Your Own Tests


2. Web API Test Project (GeeSuthSoft.KSA.ZATCA.WebApiTest)

This is a minimal ASP.NET Core Web API project for manual and integration testing of the ZATCA library via HTTP endpoints. It demonstrates how to expose the library’s services as RESTful APIs.

How to Run

Endpoints

Example: Sharing an Invoice

POST /zatca-simple/share-json
Content-Type: application/json

{
  "invoiceObject": { /* ...Invoice fields... */ },
  "tokens": {
    "BinaryToken": "...",
    "PCSIDSecret": "...",
    "privateKey": "..."
  },
  "IsClearance": true
}

Configuration

Customization


Tip: Use the test project as a reference for writing your own tests, and the Web API project as a template for integrating ZATCA features into your own web applications.