Sample code for 30+ languages & platforms
PureBasic

palena.sii.cl getToken SOAP Request

See more SII Chile Examples

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

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkXml.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkXmlDSigGen.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkCert.pb"

Procedure ChilkatExample()

    success.i = 0

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

    ; Create the XML to be signed...
    xmlToSign.i = CkXml::ckCreate()
    If xmlToSign.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkXml::setCkTag(xmlToSign, "getToken")
    ; This is the seed obtained from palena.sii.cl getSeed
    CkXml::ckUpdateChildContent(xmlToSign,"item|Semilla","033878794660")

    gen.i = CkXmlDSigGen::ckCreate()
    If gen.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkXmlDSigGen::setCkSigLocation(gen, "getToken")
    CkXmlDSigGen::setCkSigLocationMod(gen, 0)
    CkXmlDSigGen::setCkSigNamespacePrefix(gen, "")
    CkXmlDSigGen::setCkSigNamespaceUri(gen, "http://www.w3.org/2000/09/xmldsig#")
    CkXmlDSigGen::setCkSignedInfoCanonAlg(gen, "EXCL_C14N")
    CkXmlDSigGen::setCkSignedInfoDigestMethod(gen, "sha1")

    CkXmlDSigGen::ckAddSameDocRef(gen,"","sha1","","","")

    ; Provide a certificate + private key. (PFX password is test123)
    cert.i = CkCert::ckCreate()
    If cert.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkCert::ckLoadPfxFile(cert,"qa_data/pfx/cert_test123.pfx","test123")
    If success = 0
        Debug CkCert::ckLastErrorText(cert)
        CkXml::ckDispose(xmlToSign)
        CkXmlDSigGen::ckDispose(gen)
        CkCert::ckDispose(cert)
        ProcedureReturn
    EndIf

    CkXmlDSigGen::ckSetX509Cert(gen,cert,1)

    CkXmlDSigGen::setCkKeyInfoType(gen, "X509Data")
    CkXmlDSigGen::setCkX509Type(gen, "Certificate")

    ; Load XML to be signed...
    sbXml.i = CkStringBuilder::ckCreate()
    If sbXml.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkXml::setCkEmitXmlDecl(xmlToSign, 0)
    CkXml::ckGetXmlSb(xmlToSign,sbXml)

    CkXmlDSigGen::setCkBehaviors(gen, "IndentedSignature")

    ; Sign the XML...
    success = CkXmlDSigGen::ckCreateXmlDSigSb(gen,sbXml)
    If success = 0
        Debug CkXmlDSigGen::ckLastErrorText(gen)
        CkXml::ckDispose(xmlToSign)
        CkXmlDSigGen::ckDispose(gen)
        CkCert::ckDispose(cert)
        CkStringBuilder::ckDispose(sbXml)
        ProcedureReturn
    EndIf

    ; -----------------------------------------------

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    responseStatusCode.i

    CkHttp::setCkUncommonOptions(http, "AllowEmptyHeaders")
    CkHttp::ckSetRequestHeader(http,"SOAPAction","")

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

    xml.i = CkXml::ckCreate()
    If xml.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkXml::setCkTag(xml, "soapenv:Envelope")
    CkXml::ckAddAttribute(xml,"xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance")
    CkXml::ckAddAttribute(xml,"xmlns:xsd","http://www.w3.org/2001/XMLSchema")
    CkXml::ckAddAttribute(xml,"xmlns:soapenv","http://schemas.xmlsoap.org/soap/envelope/")
    CkXml::ckAddAttribute(xml,"xmlns:def","http://DefaultNamespace")
    CkXml::ckUpdateChildContent(xml,"soapenv:Header","")
    CkXml::ckUpdateAttrAt(xml,"soapenv:Body|def:getToken",1,"soapenv:encodingStyle","http://schemas.xmlsoap.org/soap/encoding/")
    CkXml::ckUpdateChildContent(xml,"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.
    sbSoapXml.i = CkStringBuilder::ckCreate()
    If sbSoapXml.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckAppend(sbSoapXml,CkXml::ckGetXml(xml))
    numReplaced.i = CkStringBuilder::ckReplace(sbXml,"&","&amp;")
    numReplaced = CkStringBuilder::ckReplace(sbXml,">","&gt;")
    numReplaced = CkStringBuilder::ckReplace(sbXml,"<","&lt;")
    numReplaced = CkStringBuilder::ckReplace(sbXml,Chr(34),"&quot;")
    numReplaced = CkStringBuilder::ckReplace(sbSoapXml,"SIGNED_XML_GOES_HERE",CkStringBuilder::ckGetAsString(sbXml))

    xmlStr.s = CkStringBuilder::ckGetAsString(sbSoapXml)
    resp.i = CkHttpResponse::ckCreate()
    If resp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkHttp::ckHttpStr(http,"POST",endPoint,xmlStr,"utf-8","text/xml",resp)
    If success = 0
        Debug CkHttp::ckLastErrorText(http)
        CkXml::ckDispose(xmlToSign)
        CkXmlDSigGen::ckDispose(gen)
        CkCert::ckDispose(cert)
        CkStringBuilder::ckDispose(sbXml)
        CkHttp::ckDispose(http)
        CkXml::ckDispose(xml)
        CkStringBuilder::ckDispose(sbSoapXml)
        CkHttpResponse::ckDispose(resp)
        ProcedureReturn
    EndIf

    responseStatusCode = CkHttpResponse::ckStatusCode(resp)

    Debug "Response Status Code: " + Str(responseStatusCode)

    ; You may examine the exact HTTP header sent with the POST like this:
    Debug "LastHeader:"
    Debug CkHttp::ckLastHeader(http)

    ; Examine the XML returned by the web service:
    Debug "XML Response:"
    xmlResp.i = CkXml::ckCreate()
    If xmlResp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkXml::ckLoadXml(xmlResp,CkHttpResponse::ckBodyStr(resp))
    Debug CkXml::ckGetXml(xmlResp)

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


    CkXml::ckDispose(xmlToSign)
    CkXmlDSigGen::ckDispose(gen)
    CkCert::ckDispose(cert)
    CkStringBuilder::ckDispose(sbXml)
    CkHttp::ckDispose(http)
    CkXml::ckDispose(xml)
    CkStringBuilder::ckDispose(sbSoapXml)
    CkHttpResponse::ckDispose(resp)
    CkXml::ckDispose(xmlResp)


    ProcedureReturn
EndProcedure