Sample code for 30+ languages & platforms
Lua

palena.sii.cl getToken SOAP Request

See more SII Chile Examples

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

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

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

-- Create the XML to be signed...
local xmlToSign = chilkat.Xml.new()
xmlToSign.tag = "getToken"
-- This is the seed obtained from palena.sii.cl getSeed
xmlToSign:update_child_content("item|Semilla", "033878794660")

local gen = chilkat.XmlDSigGen.new()

gen.sig_location = "getToken"
gen.sig_location_mod = 0
gen.sig_namespace_prefix = ""
gen.sig_namespace_uri = "http://www.w3.org/2000/09/xmldsig#"
gen.signed_info_canon_alg = "EXCL_C14N"
gen.signed_info_digest_method = "sha1"

gen:add_same_doc_ref("", "sha1", "", "", "")

-- Provide a certificate + private key. (PFX password is test123)
local cert = chilkat.Cert.new()
local success = cert:load_pfx_file("qa_data/pfx/cert_test123.pfx", "test123")
if not success then
    print(cert.last_error_text)
    return
end

gen:set_x509_cert(cert, true)

gen.key_info_type = "X509Data"
gen.x509_type = "Certificate"

-- Load XML to be signed...
local sbXml = chilkat.StringBuilder.new()
xmlToSign.emit_xml_decl = false
xmlToSign:get_xml_sb(sbXml)

gen.behaviors = "IndentedSignature"

-- Sign the XML...
success = gen:create_xml_dsig_sb(sbXml)
if not success then
    print(gen.last_error_text)
    return
end

-- -----------------------------------------------

local http = chilkat.Http.new()

http.uncommon_options = "AllowEmptyHeaders"
http:set_request_header("SOAPAction", "")

-- The endpoint for this soap service is:
local endPoint = "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>

local xml = chilkat.Xml.new()
xml.tag = "soapenv:Envelope"
xml:add_attribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
xml:add_attribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema")
xml:add_attribute("xmlns:soapenv", "http://schemas.xmlsoap.org/soap/envelope/")
xml:add_attribute("xmlns:def", "http://DefaultNamespace")
xml:update_child_content("soapenv:Header", "")
xml:update_attr_at("soapenv:Body|def:getToken", true, "soapenv:encodingStyle", "http://schemas.xmlsoap.org/soap/encoding/")
xml:update_child_content("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.
local sbSoapXml = chilkat.StringBuilder.new()
sbSoapXml:append(xml:get_xml())
local numReplaced = sbXml:replace("&", "&amp;")
numReplaced = sbXml:replace(">", "&gt;")
numReplaced = sbXml:replace("<", "&lt;")
numReplaced = sbXml:replace("\"", "&quot;")
numReplaced = sbSoapXml:replace("SIGNED_XML_GOES_HERE", sbXml:get_as_string())

local xmlStr = sbSoapXml:get_as_string()
local resp = chilkat.HttpResponse.new()
success = http:http_str("POST", endPoint, xmlStr, "utf-8", "text/xml", resp)
if not success then
    print(http.last_error_text)
    return
end

local responseStatusCode = resp.status_code

print("Response Status Code: " .. responseStatusCode)

-- You may examine the exact HTTP header sent with the POST like this:
print("LastHeader:")
print(http.last_header)

-- Examine the XML returned by the web service:
print("XML Response:")
local xmlResp = chilkat.Xml.new()
xmlResp:load_xml(resp.body_str)
print(xmlResp:get_xml())

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