Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.0.0+

Create Signed SOAP XML for Albanian Fiscalization Service

See more XAdES Examples

This example is created for a customer that needs to send signed billing/invoicing SOAP XML requests to http://efiskalizimi-test.tatime.gov.al:80/FiscalizationService

The example demonstrates how to create and signed the SOAP XML message that is to be sent.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    var success: bool = false;

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // --------------------------------------------------------------------------------
    // Also see Chilkat's Online WSDL Code Generator
    // to generate code and SOAP Request and Response XML for each operation in a WSDL.
    // --------------------------------------------------------------------------------

    // We want to build and send a signed SOAP XML that looks like the following:

    // <?xml version="1.0" encoding="utf-8"?>
    // <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    //     <SOAP-ENV:Header/>
    //     <SOAP-ENV:Body>
    //         <RegisterWTNRequest xmlns="https://eFiskalizimi.tatime.gov.al/FiscalizationService/schema" xmlns:ns2="http://www.w3.org/2000/09/xmldsig#" Id="Request">
    //             <Header SendDateTime="2019-09-03T14:19:01+02:00" UUID="42db3af5-0d9f-4dea-95b4-4b947ab8d8e7"/>
    //             <WTN BusinUnit="bb123bb123" DateTimeCreated="2019-09-03T14:19:01+02:00" DestinAddr="Destination address"
    //             DestinCity="Destination city" IsAfterDel="false" OperatorCode="oo123oo123"
    //             SoftNum="ss123ss123" StartAddr="Start address" StartCity="Start city" TransDate="2019-09-03T14:19:01+02:00"
    //             VehPlates="AA0000AA" WTNIC="5C5E58580D0A24E1F7A5E5E011929511" WTNICSignature="82D69C38206D ... F5FCA48" WTNNum="12345678901">
    //                 <Issuer NUIS="I12345678I" Name="Issuer name"/>
    //                 <Items>
    //                     <I C="501234567890" N="Item name" Q="1.0" U="piece"/>
    //                 </Items>
    //             </WTN>
    //             <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
    //                 <SignedInfo>
    //                     <CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
    //                     <SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
    //                     <Reference URI="#Request">
    //                         <Transforms>
    //                             <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
    //                             <Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
    //                         </Transforms>
    //                         <DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
    //                         <DigestValue>aNa0r4RW04BlsAciTUrWHGW9PADskl2op315BAQ0hTg=</DigestValue>
    //                     </Reference>
    //                 </SignedInfo>
    //                 <SignatureValue>4/j/d4j/5xCJ2YUP+XTC6li0B94...........................KESd38NT5+puArBcNgLYIjLx/dh6Q==</SignatureValue>
    //                 <KeyInfo>
    //                     <X509Data>
    //                         <X509Certificate>MIIE6TCCAtGgAwIBAgICEA8wDQYJKoZIhvcNA......................uoqWsSuLmA==</X509Certificate>
    //                     </X509Data>
    //                 </KeyInfo>
    //             </Signature>
    //         </RegisterWTNRequest>
    //     </SOAP-ENV:Body>
    // </SOAP-ENV:Envelope>

    success = true;
    // Create the XML to be signed...
    const xml_to_sign = try chilkat.Xml.init();
    defer xml_to_sign.deinit();
    xml_to_sign.setTag("SOAP-ENV:Envelope");
    xml_to_sign.addAttribute("xmlns:SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/") catch {};
    xml_to_sign.updateChildContent("SOAP-ENV:Header", "");
    xml_to_sign.updateAttrAt("SOAP-ENV:Body|RegisterWTNRequest", true, "xmlns", "https://eFiskalizimi.tatime.gov.al/FiscalizationService/schema") catch {};
    xml_to_sign.updateAttrAt("SOAP-ENV:Body|RegisterWTNRequest", true, "xmlns:ns2", "http://www.w3.org/2000/09/xmldsig#") catch {};
    xml_to_sign.updateAttrAt("SOAP-ENV:Body|RegisterWTNRequest", true, "Id", "Request") catch {};
    xml_to_sign.updateAttrAt("SOAP-ENV:Body|RegisterWTNRequest|Header", true, "SendDateTime", "2019-09-03T14:19:01+02:00") catch {};
    xml_to_sign.updateAttrAt("SOAP-ENV:Body|RegisterWTNRequest|Header", true, "UUID", "42db3af5-0d9f-4dea-95b4-4b947ab8d8e7") catch {};
    xml_to_sign.updateAttrAt("SOAP-ENV:Body|RegisterWTNRequest|WTN", true, "BusinUnit", "bb123bb123") catch {};
    xml_to_sign.updateAttrAt("SOAP-ENV:Body|RegisterWTNRequest|WTN", true, "DateTimeCreated", "2019-09-03T14:19:01+02:00") catch {};
    xml_to_sign.updateAttrAt("SOAP-ENV:Body|RegisterWTNRequest|WTN", true, "DestinAddr", "Destination address") catch {};
    xml_to_sign.updateAttrAt("SOAP-ENV:Body|RegisterWTNRequest|WTN", true, "DestinCity", "Destination city") catch {};
    xml_to_sign.updateAttrAt("SOAP-ENV:Body|RegisterWTNRequest|WTN", true, "IsAfterDel", "false") catch {};
    xml_to_sign.updateAttrAt("SOAP-ENV:Body|RegisterWTNRequest|WTN", true, "OperatorCode", "oo123oo123") catch {};
    xml_to_sign.updateAttrAt("SOAP-ENV:Body|RegisterWTNRequest|WTN", true, "SoftNum", "ss123ss123") catch {};
    xml_to_sign.updateAttrAt("SOAP-ENV:Body|RegisterWTNRequest|WTN", true, "StartAddr", "Start address") catch {};
    xml_to_sign.updateAttrAt("SOAP-ENV:Body|RegisterWTNRequest|WTN", true, "StartCity", "Start city") catch {};
    xml_to_sign.updateAttrAt("SOAP-ENV:Body|RegisterWTNRequest|WTN", true, "TransDate", "2019-09-03T14:19:01+02:00") catch {};
    xml_to_sign.updateAttrAt("SOAP-ENV:Body|RegisterWTNRequest|WTN", true, "VehPlates", "AA0000AA") catch {};
    xml_to_sign.updateAttrAt("SOAP-ENV:Body|RegisterWTNRequest|WTN", true, "WTNIC", "5C5E58580D0A24E1F7A5E5E011929511") catch {};
    xml_to_sign.updateAttrAt("SOAP-ENV:Body|RegisterWTNRequest|WTN", true, "WTNICSignature", "82D69C38206D ... F5FCA48") catch {};
    xml_to_sign.updateAttrAt("SOAP-ENV:Body|RegisterWTNRequest|WTN", true, "WTNNum", "12345678901") catch {};
    xml_to_sign.updateAttrAt("SOAP-ENV:Body|RegisterWTNRequest|WTN|Issuer", true, "NUIS", "I12345678I") catch {};
    xml_to_sign.updateAttrAt("SOAP-ENV:Body|RegisterWTNRequest|WTN|Issuer", true, "Name", "Issuer name") catch {};
    xml_to_sign.updateAttrAt("SOAP-ENV:Body|RegisterWTNRequest|WTN|Items|I", true, "C", "501234567890") catch {};
    xml_to_sign.updateAttrAt("SOAP-ENV:Body|RegisterWTNRequest|WTN|Items|I", true, "N", "Item name") catch {};
    xml_to_sign.updateAttrAt("SOAP-ENV:Body|RegisterWTNRequest|WTN|Items|I", true, "Q", "1.0") catch {};
    xml_to_sign.updateAttrAt("SOAP-ENV:Body|RegisterWTNRequest|WTN|Items|I", true, "U", "piece") catch {};

    const gen = try chilkat.XmlDSigGen.init();
    defer gen.deinit();

    gen.setSigLocation("SOAP-ENV:Envelope|SOAP-ENV:Body|RegisterWTNRequest");
    gen.setSigLocationMod(0);
    gen.setSigNamespacePrefix("");
    gen.setSigNamespaceUri("http://www.w3.org/2000/09/xmldsig#");
    gen.setSignedInfoCanonAlg("EXCL_C14N");
    gen.setSignedInfoDigestMethod("sha256");

    // -------- Reference 1 --------
    gen.addSameDocRef("Request", "sha256", "EXCL_C14N", "", "") catch {};

    // Provide a certificate + private key. (PFX password is test123)
    const cert = try chilkat.Cert.init();
    defer cert.deinit();
    cert.loadPfxFile("qa_data/pfx/cert_test123.pfx", "test123") catch {
        std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
        return;
    };

    gen.setX509Cert(cert, true) catch {};

    gen.setKeyInfoType("X509Data");
    gen.setX509Type("Certificate");

    // Load XML to be signed...
    const sb_xml = try chilkat.StringBuilder.init();
    defer sb_xml.deinit();
    xml_to_sign.getXmlSb(sb_xml) catch {};

    gen.setBehaviors("CompactSignedXml,ForceAddEnvelopedSignatureTransform");

    // Sign the XML...
    gen.createXmlDSigSb(sb_xml) catch {
        std.debug.print("{s}\n", .{try gen.getLastErrorText(alloc)});
        return;
    };

    // -----------------------------------------------
    // Now to send the SOAP request....

    // Note: Chilkat did not actually test this by sending the request to the server.
    // The example is a best-guess for how to do it...

    const str_xml = try sb_xml.getAsString(alloc);

    // We'll need to add this in the HTTP header:
    // SOAPAction: "https://eFiskalizimi.tatime.gov.al/FiscalizationService/RegisterWTN"

    // Note: This is for the RegisterWTN request.

    // You may wish to load the WSDL at https://efiskalizimi-test.tatime.gov.al/FiscalizationService-v1/FiscalizationService.wsdl into the SoapUI application to see
    // the SOAP requests..
    const http = try chilkat.Http.init();
    defer http.deinit();
    http.setRequestHeader("SOAPAction", "https://eFiskalizimi.tatime.gov.al/FiscalizationService/RegisterWTN");

    // The testing endpoint for this soap service is likely:
    const end_point = "https://efiskalizimi-test.tatime.gov.al/FiscalizationService-v1";

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpStr("POST", end_point, str_xml, "utf-8", "text/xml", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    const response_status_code = resp.getStatusCode();

    // Check that the responseStatusCode equals 200...
    std.debug.print("Response Status Code: {d}\n", .{response_status_code});

    // Examine the exact HTTP header sent with the POST like this:
    std.debug.print("LastHeader:\n", .{});
    std.debug.print("{s}\n", .{try http.getLastHeader(alloc)});

    // Examine the XML returned by the web service:
    std.debug.print("XML Response:\n", .{});
    const xml_resp = try chilkat.Xml.init();
    defer xml_resp.deinit();
    xml_resp.loadXml(try resp.getBodyStr(alloc)) catch {};
    std.debug.print("{s}\n", .{try xml_resp.getXml(alloc)});

    // Use this online tool to generate parsing code from response XML:
    // Generate Parsing Code from XML
}