Sample code for 30+ languages & platforms
Swift

palena.sii.cl getToken SOAP Request

See more SII Chile Examples

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

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

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

    // Create the XML to be signed...
    let xmlToSign = CkoXml()!
    xmlToSign.tag = "getToken"
    // This is the seed obtained from palena.sii.cl getSeed
    xmlToSign.updateChildContent(tagPath: "item|Semilla", value: "033878794660")

    let gen = CkoXmlDSigGen()!

    gen.sigLocation = "getToken"
    gen.sigLocationMod = 0
    gen.sigNamespacePrefix = ""
    gen.sigNamespaceUri = "http://www.w3.org/2000/09/xmldsig#"
    gen.signedInfoCanonAlg = "EXCL_C14N"
    gen.signedInfoDigestMethod = "sha1"

    gen.addSameDocRef(id: "", digestMethod: "sha1", canonMethod: "", prefixList: "", refType: "")

    // Provide a certificate + private key. (PFX password is test123)
    let cert = CkoCert()!
    success = cert.loadPfxFile(path: "qa_data/pfx/cert_test123.pfx", password: "test123")
    if success == false {
        print("\(cert.lastErrorText!)")
        return
    }

    gen.setX509Cert(cert: cert, usePrivateKey: true)

    gen.keyInfoType = "X509Data"
    gen.x509Type = "Certificate"

    // Load XML to be signed...
    let sbXml = CkoStringBuilder()!
    xmlToSign.emitXmlDecl = false
    xmlToSign.getSb(sb: sbXml)

    gen.behaviors = "IndentedSignature"

    // Sign the XML...
    success = gen.createXmlDSigSb(sbXml: sbXml)
    if success == false {
        print("\(gen.lastErrorText!)")
        return
    }

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

    let http = CkoHttp()!

    var responseStatusCode: Int

    http.uncommonOptions = "AllowEmptyHeaders"
    http.setRequestHeader(name: "SOAPAction", value: "")

    // The endpoint for this soap service is:
    var endPoint: String? = "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>

    let xml = CkoXml()!
    xml.tag = "soapenv:Envelope"
    xml.addAttribute(name: "xmlns:xsi", value: "http://www.w3.org/2001/XMLSchema-instance")
    xml.addAttribute(name: "xmlns:xsd", value: "http://www.w3.org/2001/XMLSchema")
    xml.addAttribute(name: "xmlns:soapenv", value: "http://schemas.xmlsoap.org/soap/envelope/")
    xml.addAttribute(name: "xmlns:def", value: "http://DefaultNamespace")
    xml.updateChildContent(tagPath: "soapenv:Header", value: "")
    xml.updateAttrAt(tagPath: "soapenv:Body|def:getToken", autoCreate: true, attrName: "soapenv:encodingStyle", attrValue: "http://schemas.xmlsoap.org/soap/encoding/")
    xml.updateChildContent(tagPath: "soapenv:Body|def:getToken|pszXml", value: "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.
    let sbSoapXml = CkoStringBuilder()!
    sbSoapXml.append(value: xml.getXml())
    var numReplaced: Int = sbXml.replace(value: "&", replacement: "&amp;").intValue
    numReplaced = sbXml.replace(value: ">", replacement: "&gt;").intValue
    numReplaced = sbXml.replace(value: "<", replacement: "&lt;").intValue
    numReplaced = sbXml.replace(value: "\"", replacement: "&quot;").intValue
    numReplaced = sbSoapXml.replace(value: "SIGNED_XML_GOES_HERE", replacement: sbXml.getAsString()).intValue

    var xmlStr: String? = sbSoapXml.getAsString()
    let resp = CkoHttpResponse()!
    success = http.httpStr(verb: "POST", url: endPoint, bodyStr: xmlStr, charset: "utf-8", contentType: "text/xml", response: resp)
    if success == false {
        print("\(http.lastErrorText!)")
        return
    }

    responseStatusCode = resp.statusCode.intValue

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

    // You may examine the exact HTTP header sent with the POST like this:
    print("LastHeader:")
    print("\(http.lastHeader!)")

    // Examine the XML returned by the web service:
    print("XML Response:")
    let xmlResp = CkoXml()!
    xmlResp.load(xmlData: resp.bodyStr)
    print("\(xmlResp.getXml()!)")

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

}