Sample code for 30+ languages & platforms
Zig

palena.sii.cl getToken SOAP Request

See more SII Chile Examples

Demonstrates how to call getToken SOAP request at palena.sii.cl

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();

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

    // Create the XML to be signed...
    const xml_to_sign = try chilkat.Xml.init();
    defer xml_to_sign.deinit();
    xml_to_sign.setTag("getToken");
    // This is the seed obtained from palena.sii.cl getSeed
    xml_to_sign.updateChildContent("item|Semilla", "033878794660");

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

    gen.setSigLocation("getToken");
    gen.setSigLocationMod(0);
    gen.setSigNamespacePrefix("");
    gen.setSigNamespaceUri("http://www.w3.org/2000/09/xmldsig#");
    gen.setSignedInfoCanonAlg("EXCL_C14N");
    gen.setSignedInfoDigestMethod("sha1");

    gen.addSameDocRef("", "sha1", "", "", "") 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.setEmitXmlDecl(false);
    xml_to_sign.getXmlSb(sb_xml) catch {};

    gen.setBehaviors("IndentedSignature");

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

    // -----------------------------------------------

    const http = try chilkat.Http.init();
    defer http.deinit();

    http.setUncommonOptions("AllowEmptyHeaders");
    http.setRequestHeader("SOAPAction", "");

    // The endpoint for this soap service is:
    const end_point = "https://palena.sii.cl/DTEWS/GetTokenFromSeed.jws";

    // Send the following SOAP XML

    // <soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:def="http://DefaultNamespace">
    //    <soapenv:Header/>
    //    <soapenv:Body>
    //       <def:getToken soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    //          <pszXml>SIGNED_XML_GOES_HERE</pszXml>
    //       </def:getToken>
    //    </soapenv:Body>
    // </soapenv:Envelope>

    const xml = try chilkat.Xml.init();
    defer xml.deinit();
    xml.setTag("soapenv:Envelope");
    xml.addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance") catch {};
    xml.addAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema") catch {};
    xml.addAttribute("xmlns:soapenv", "http://schemas.xmlsoap.org/soap/envelope/") catch {};
    xml.addAttribute("xmlns:def", "http://DefaultNamespace") catch {};
    xml.updateChildContent("soapenv:Header", "");
    xml.updateAttrAt("soapenv:Body|def:getToken", true, "soapenv:encodingStyle", "http://schemas.xmlsoap.org/soap/encoding/") catch {};
    xml.updateChildContent("soapenv:Body|def:getToken|pszXml", "SIGNED_XML_GOES_HERE");

    // We must replace the "SIGNED_XML_GOES_HERE" with the exact contents of the signed XML to ensure the signed XML is not modified in any way.
    const sb_soap_xml = try chilkat.StringBuilder.init();
    defer sb_soap_xml.deinit();
    sb_soap_xml.append(try xml.getXml(alloc)) catch {};
    var num_replaced: i32 = sb_xml.replace("&", "&amp;");
    num_replaced = sb_xml.replace(">", "&gt;");
    num_replaced = sb_xml.replace("<", "&lt;");
    num_replaced = sb_xml.replace("\"", "&quot;");
    num_replaced = sb_soap_xml.replace("SIGNED_XML_GOES_HERE", try sb_xml.getAsString(alloc));

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

    const response_status_code = resp.getStatusCode();

    std.debug.print("Response Status Code: {d}\n", .{response_status_code});

    // You may 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
}