Sample code for 30+ languages & platforms
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 C Downloads

C
#include <C_CkXml.h>
#include <C_CkXmlDSigGen.h>
#include <C_CkCert.h>
#include <C_CkStringBuilder.h>
#include <C_CkHttp.h>
#include <C_CkHttpResponse.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkXml xmlToSign;
    HCkXmlDSigGen gen;
    HCkCert cert;
    HCkStringBuilder sbXml;
    const char *strXml;
    int responseStatusCode;
    HCkHttp http;
    const char *endPoint;
    HCkHttpResponse resp;
    HCkXml 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 = CkXml_Create();
    CkXml_putTag(xmlToSign,"SOAP-ENV:Envelope");
    CkXml_AddAttribute(xmlToSign,"xmlns:SOAP-ENV","http://schemas.xmlsoap.org/soap/envelope/");
    CkXml_UpdateChildContent(xmlToSign,"SOAP-ENV:Header","");
    CkXml_UpdateAttrAt(xmlToSign,"SOAP-ENV:Body|RegisterWTNRequest",TRUE,"xmlns","https://eFiskalizimi.tatime.gov.al/FiscalizationService/schema");
    CkXml_UpdateAttrAt(xmlToSign,"SOAP-ENV:Body|RegisterWTNRequest",TRUE,"xmlns:ns2","http://www.w3.org/2000/09/xmldsig#");
    CkXml_UpdateAttrAt(xmlToSign,"SOAP-ENV:Body|RegisterWTNRequest",TRUE,"Id","Request");
    CkXml_UpdateAttrAt(xmlToSign,"SOAP-ENV:Body|RegisterWTNRequest|Header",TRUE,"SendDateTime","2019-09-03T14:19:01+02:00");
    CkXml_UpdateAttrAt(xmlToSign,"SOAP-ENV:Body|RegisterWTNRequest|Header",TRUE,"UUID","42db3af5-0d9f-4dea-95b4-4b947ab8d8e7");
    CkXml_UpdateAttrAt(xmlToSign,"SOAP-ENV:Body|RegisterWTNRequest|WTN",TRUE,"BusinUnit","bb123bb123");
    CkXml_UpdateAttrAt(xmlToSign,"SOAP-ENV:Body|RegisterWTNRequest|WTN",TRUE,"DateTimeCreated","2019-09-03T14:19:01+02:00");
    CkXml_UpdateAttrAt(xmlToSign,"SOAP-ENV:Body|RegisterWTNRequest|WTN",TRUE,"DestinAddr","Destination address");
    CkXml_UpdateAttrAt(xmlToSign,"SOAP-ENV:Body|RegisterWTNRequest|WTN",TRUE,"DestinCity","Destination city");
    CkXml_UpdateAttrAt(xmlToSign,"SOAP-ENV:Body|RegisterWTNRequest|WTN",TRUE,"IsAfterDel","false");
    CkXml_UpdateAttrAt(xmlToSign,"SOAP-ENV:Body|RegisterWTNRequest|WTN",TRUE,"OperatorCode","oo123oo123");
    CkXml_UpdateAttrAt(xmlToSign,"SOAP-ENV:Body|RegisterWTNRequest|WTN",TRUE,"SoftNum","ss123ss123");
    CkXml_UpdateAttrAt(xmlToSign,"SOAP-ENV:Body|RegisterWTNRequest|WTN",TRUE,"StartAddr","Start address");
    CkXml_UpdateAttrAt(xmlToSign,"SOAP-ENV:Body|RegisterWTNRequest|WTN",TRUE,"StartCity","Start city");
    CkXml_UpdateAttrAt(xmlToSign,"SOAP-ENV:Body|RegisterWTNRequest|WTN",TRUE,"TransDate","2019-09-03T14:19:01+02:00");
    CkXml_UpdateAttrAt(xmlToSign,"SOAP-ENV:Body|RegisterWTNRequest|WTN",TRUE,"VehPlates","AA0000AA");
    CkXml_UpdateAttrAt(xmlToSign,"SOAP-ENV:Body|RegisterWTNRequest|WTN",TRUE,"WTNIC","5C5E58580D0A24E1F7A5E5E011929511");
    CkXml_UpdateAttrAt(xmlToSign,"SOAP-ENV:Body|RegisterWTNRequest|WTN",TRUE,"WTNICSignature","82D69C38206D ... F5FCA48");
    CkXml_UpdateAttrAt(xmlToSign,"SOAP-ENV:Body|RegisterWTNRequest|WTN",TRUE,"WTNNum","12345678901");
    CkXml_UpdateAttrAt(xmlToSign,"SOAP-ENV:Body|RegisterWTNRequest|WTN|Issuer",TRUE,"NUIS","I12345678I");
    CkXml_UpdateAttrAt(xmlToSign,"SOAP-ENV:Body|RegisterWTNRequest|WTN|Issuer",TRUE,"Name","Issuer name");
    CkXml_UpdateAttrAt(xmlToSign,"SOAP-ENV:Body|RegisterWTNRequest|WTN|Items|I",TRUE,"C","501234567890");
    CkXml_UpdateAttrAt(xmlToSign,"SOAP-ENV:Body|RegisterWTNRequest|WTN|Items|I",TRUE,"N","Item name");
    CkXml_UpdateAttrAt(xmlToSign,"SOAP-ENV:Body|RegisterWTNRequest|WTN|Items|I",TRUE,"Q","1.0");
    CkXml_UpdateAttrAt(xmlToSign,"SOAP-ENV:Body|RegisterWTNRequest|WTN|Items|I",TRUE,"U","piece");

    gen = CkXmlDSigGen_Create();

    CkXmlDSigGen_putSigLocation(gen,"SOAP-ENV:Envelope|SOAP-ENV:Body|RegisterWTNRequest");
    CkXmlDSigGen_putSigLocationMod(gen,0);
    CkXmlDSigGen_putSigNamespacePrefix(gen,"");
    CkXmlDSigGen_putSigNamespaceUri(gen,"http://www.w3.org/2000/09/xmldsig#");
    CkXmlDSigGen_putSignedInfoCanonAlg(gen,"EXCL_C14N");
    CkXmlDSigGen_putSignedInfoDigestMethod(gen,"sha256");

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

    // Provide a certificate + private key. (PFX password is test123)
    cert = CkCert_Create();
    success = CkCert_LoadPfxFile(cert,"qa_data/pfx/cert_test123.pfx","test123");
    if (success == FALSE) {
        printf("%s\n",CkCert_lastErrorText(cert));
        CkXml_Dispose(xmlToSign);
        CkXmlDSigGen_Dispose(gen);
        CkCert_Dispose(cert);
        return;
    }

    CkXmlDSigGen_SetX509Cert(gen,cert,TRUE);

    CkXmlDSigGen_putKeyInfoType(gen,"X509Data");
    CkXmlDSigGen_putX509Type(gen,"Certificate");

    // Load XML to be signed...
    sbXml = CkStringBuilder_Create();
    CkXml_GetXmlSb(xmlToSign,sbXml);

    CkXmlDSigGen_putBehaviors(gen,"CompactSignedXml,ForceAddEnvelopedSignatureTransform");

    // Sign the XML...
    success = CkXmlDSigGen_CreateXmlDSigSb(gen,sbXml);
    if (success == FALSE) {
        printf("%s\n",CkXmlDSigGen_lastErrorText(gen));
        CkXml_Dispose(xmlToSign);
        CkXmlDSigGen_Dispose(gen);
        CkCert_Dispose(cert);
        CkStringBuilder_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 = CkStringBuilder_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 = CkHttp_Create();
    CkHttp_SetRequestHeader(http,"SOAPAction","https://eFiskalizimi.tatime.gov.al/FiscalizationService/RegisterWTN");

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

    resp = CkHttpResponse_Create();
    success = CkHttp_HttpStr(http,"POST",endPoint,strXml,"utf-8","text/xml",resp);
    if (success == FALSE) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkXml_Dispose(xmlToSign);
        CkXmlDSigGen_Dispose(gen);
        CkCert_Dispose(cert);
        CkStringBuilder_Dispose(sbXml);
        CkHttp_Dispose(http);
        CkHttpResponse_Dispose(resp);
        return;
    }

    responseStatusCode = CkHttpResponse_getStatusCode(resp);

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

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

    // Examine the XML returned by the web service:
    printf("XML Response:\n");
    xmlResp = CkXml_Create();
    CkXml_LoadXml(xmlResp,CkHttpResponse_bodyStr(resp));
    printf("%s\n",CkXml_getXml(xmlResp));

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


    CkXml_Dispose(xmlToSign);
    CkXmlDSigGen_Dispose(gen);
    CkCert_Dispose(cert);
    CkStringBuilder_Dispose(sbXml);
    CkHttp_Dispose(http);
    CkHttpResponse_Dispose(resp);
    CkXml_Dispose(xmlResp);

    }