Sample code for 30+ languages & platforms
Unicode C

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 Unicode C Downloads

Unicode C
#include <C_CkXmlW.h>
#include <C_CkXmlDSigGenW.h>
#include <C_CkCertW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkHttpW.h>
#include <C_CkHttpResponseW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkXmlW xmlToSign;
    HCkXmlDSigGenW gen;
    HCkCertW cert;
    HCkStringBuilderW sbXml;
    const wchar_t *strXml;
    int responseStatusCode;
    HCkHttpW http;
    const wchar_t *endPoint;
    HCkHttpResponseW resp;
    HCkXmlW xmlResp;

    success = 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...
    xmlToSign = CkXmlW_Create();
    CkXmlW_putTag(xmlToSign,L"SOAP-ENV:Envelope");
    CkXmlW_AddAttribute(xmlToSign,L"xmlns:SOAP-ENV",L"http://schemas.xmlsoap.org/soap/envelope/");
    CkXmlW_UpdateChildContent(xmlToSign,L"SOAP-ENV:Header",L"");
    CkXmlW_UpdateAttrAt(xmlToSign,L"SOAP-ENV:Body|RegisterWTNRequest",TRUE,L"xmlns",L"https://eFiskalizimi.tatime.gov.al/FiscalizationService/schema");
    CkXmlW_UpdateAttrAt(xmlToSign,L"SOAP-ENV:Body|RegisterWTNRequest",TRUE,L"xmlns:ns2",L"http://www.w3.org/2000/09/xmldsig#");
    CkXmlW_UpdateAttrAt(xmlToSign,L"SOAP-ENV:Body|RegisterWTNRequest",TRUE,L"Id",L"Request");
    CkXmlW_UpdateAttrAt(xmlToSign,L"SOAP-ENV:Body|RegisterWTNRequest|Header",TRUE,L"SendDateTime",L"2019-09-03T14:19:01+02:00");
    CkXmlW_UpdateAttrAt(xmlToSign,L"SOAP-ENV:Body|RegisterWTNRequest|Header",TRUE,L"UUID",L"42db3af5-0d9f-4dea-95b4-4b947ab8d8e7");
    CkXmlW_UpdateAttrAt(xmlToSign,L"SOAP-ENV:Body|RegisterWTNRequest|WTN",TRUE,L"BusinUnit",L"bb123bb123");
    CkXmlW_UpdateAttrAt(xmlToSign,L"SOAP-ENV:Body|RegisterWTNRequest|WTN",TRUE,L"DateTimeCreated",L"2019-09-03T14:19:01+02:00");
    CkXmlW_UpdateAttrAt(xmlToSign,L"SOAP-ENV:Body|RegisterWTNRequest|WTN",TRUE,L"DestinAddr",L"Destination address");
    CkXmlW_UpdateAttrAt(xmlToSign,L"SOAP-ENV:Body|RegisterWTNRequest|WTN",TRUE,L"DestinCity",L"Destination city");
    CkXmlW_UpdateAttrAt(xmlToSign,L"SOAP-ENV:Body|RegisterWTNRequest|WTN",TRUE,L"IsAfterDel",L"false");
    CkXmlW_UpdateAttrAt(xmlToSign,L"SOAP-ENV:Body|RegisterWTNRequest|WTN",TRUE,L"OperatorCode",L"oo123oo123");
    CkXmlW_UpdateAttrAt(xmlToSign,L"SOAP-ENV:Body|RegisterWTNRequest|WTN",TRUE,L"SoftNum",L"ss123ss123");
    CkXmlW_UpdateAttrAt(xmlToSign,L"SOAP-ENV:Body|RegisterWTNRequest|WTN",TRUE,L"StartAddr",L"Start address");
    CkXmlW_UpdateAttrAt(xmlToSign,L"SOAP-ENV:Body|RegisterWTNRequest|WTN",TRUE,L"StartCity",L"Start city");
    CkXmlW_UpdateAttrAt(xmlToSign,L"SOAP-ENV:Body|RegisterWTNRequest|WTN",TRUE,L"TransDate",L"2019-09-03T14:19:01+02:00");
    CkXmlW_UpdateAttrAt(xmlToSign,L"SOAP-ENV:Body|RegisterWTNRequest|WTN",TRUE,L"VehPlates",L"AA0000AA");
    CkXmlW_UpdateAttrAt(xmlToSign,L"SOAP-ENV:Body|RegisterWTNRequest|WTN",TRUE,L"WTNIC",L"5C5E58580D0A24E1F7A5E5E011929511");
    CkXmlW_UpdateAttrAt(xmlToSign,L"SOAP-ENV:Body|RegisterWTNRequest|WTN",TRUE,L"WTNICSignature",L"82D69C38206D ... F5FCA48");
    CkXmlW_UpdateAttrAt(xmlToSign,L"SOAP-ENV:Body|RegisterWTNRequest|WTN",TRUE,L"WTNNum",L"12345678901");
    CkXmlW_UpdateAttrAt(xmlToSign,L"SOAP-ENV:Body|RegisterWTNRequest|WTN|Issuer",TRUE,L"NUIS",L"I12345678I");
    CkXmlW_UpdateAttrAt(xmlToSign,L"SOAP-ENV:Body|RegisterWTNRequest|WTN|Issuer",TRUE,L"Name",L"Issuer name");
    CkXmlW_UpdateAttrAt(xmlToSign,L"SOAP-ENV:Body|RegisterWTNRequest|WTN|Items|I",TRUE,L"C",L"501234567890");
    CkXmlW_UpdateAttrAt(xmlToSign,L"SOAP-ENV:Body|RegisterWTNRequest|WTN|Items|I",TRUE,L"N",L"Item name");
    CkXmlW_UpdateAttrAt(xmlToSign,L"SOAP-ENV:Body|RegisterWTNRequest|WTN|Items|I",TRUE,L"Q",L"1.0");
    CkXmlW_UpdateAttrAt(xmlToSign,L"SOAP-ENV:Body|RegisterWTNRequest|WTN|Items|I",TRUE,L"U",L"piece");

    gen = CkXmlDSigGenW_Create();

    CkXmlDSigGenW_putSigLocation(gen,L"SOAP-ENV:Envelope|SOAP-ENV:Body|RegisterWTNRequest");
    CkXmlDSigGenW_putSigLocationMod(gen,0);
    CkXmlDSigGenW_putSigNamespacePrefix(gen,L"");
    CkXmlDSigGenW_putSigNamespaceUri(gen,L"http://www.w3.org/2000/09/xmldsig#");
    CkXmlDSigGenW_putSignedInfoCanonAlg(gen,L"EXCL_C14N");
    CkXmlDSigGenW_putSignedInfoDigestMethod(gen,L"sha256");

    // -------- Reference 1 --------
    CkXmlDSigGenW_AddSameDocRef(gen,L"Request",L"sha256",L"EXCL_C14N",L"",L"");

    // Provide a certificate + private key. (PFX password is test123)
    cert = CkCertW_Create();
    success = CkCertW_LoadPfxFile(cert,L"qa_data/pfx/cert_test123.pfx",L"test123");
    if (success == FALSE) {
        wprintf(L"%s\n",CkCertW_lastErrorText(cert));
        CkXmlW_Dispose(xmlToSign);
        CkXmlDSigGenW_Dispose(gen);
        CkCertW_Dispose(cert);
        return;
    }

    CkXmlDSigGenW_SetX509Cert(gen,cert,TRUE);

    CkXmlDSigGenW_putKeyInfoType(gen,L"X509Data");
    CkXmlDSigGenW_putX509Type(gen,L"Certificate");

    // Load XML to be signed...
    sbXml = CkStringBuilderW_Create();
    CkXmlW_GetXmlSb(xmlToSign,sbXml);

    CkXmlDSigGenW_putBehaviors(gen,L"CompactSignedXml,ForceAddEnvelopedSignatureTransform");

    // Sign the XML...
    success = CkXmlDSigGenW_CreateXmlDSigSb(gen,sbXml);
    if (success == FALSE) {
        wprintf(L"%s\n",CkXmlDSigGenW_lastErrorText(gen));
        CkXmlW_Dispose(xmlToSign);
        CkXmlDSigGenW_Dispose(gen);
        CkCertW_Dispose(cert);
        CkStringBuilderW_Dispose(sbXml);
        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...

    strXml = CkStringBuilderW_getAsString(sbXml);

    // 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..
    http = CkHttpW_Create();
    CkHttpW_SetRequestHeader(http,L"SOAPAction",L"https://eFiskalizimi.tatime.gov.al/FiscalizationService/RegisterWTN");

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

    resp = CkHttpResponseW_Create();
    success = CkHttpW_HttpStr(http,L"POST",endPoint,strXml,L"utf-8",L"text/xml",resp);
    if (success == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkXmlW_Dispose(xmlToSign);
        CkXmlDSigGenW_Dispose(gen);
        CkCertW_Dispose(cert);
        CkStringBuilderW_Dispose(sbXml);
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        return;
    }

    responseStatusCode = CkHttpResponseW_getStatusCode(resp);

    // Check that the responseStatusCode equals 200...
    wprintf(L"Response Status Code: %d\n",responseStatusCode);

    // Examine the exact HTTP header sent with the POST like this:
    wprintf(L"LastHeader:\n");
    wprintf(L"%s\n",CkHttpW_lastHeader(http));

    // Examine the XML returned by the web service:
    wprintf(L"XML Response:\n");
    xmlResp = CkXmlW_Create();
    CkXmlW_LoadXml(xmlResp,CkHttpResponseW_bodyStr(resp));
    wprintf(L"%s\n",CkXmlW_getXml(xmlResp));

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


    CkXmlW_Dispose(xmlToSign);
    CkXmlDSigGenW_Dispose(gen);
    CkCertW_Dispose(cert);
    CkStringBuilderW_Dispose(sbXml);
    CkHttpW_Dispose(http);
    CkHttpResponseW_Dispose(resp);
    CkXmlW_Dispose(xmlResp);

    }