Sample code for 30+ languages & platforms
DataFlex

SII POST boleta.electronica.token

See more SII Chile Examples

Obtener un token de at autenticación para el envío y consultas automatizadas de boletas electrónicas.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
ToSign    Handle hoXmlToSign
    Handle hoGen
    Variant vCert
    Handle hoCert
    Variant vSbXml
    Handle hoSbXml
    Handle hoHttp
    String sUrl
    Variant vResp
    Handle hoResp
    Handle hoXml
    Integer iESTADO
    String sGLOSA
    String sTOKEN
    String sTemp1
    Integer iTemp1

    Move False To iSuccess

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

    // Create the XML to be signed...
    // The following XML is created:
    // 
    //   <?xml version="1.0" encoding="UTF-8"?>
    //   <getToken><item><Semilla>030530912644</Semilla></item></getToken>
    // 
    Get Create (RefClass(cComChilkatXml)) To hoXmlToSign
    If (Not(IsComObjectCreated(hoXmlToSign))) Begin
        Send CreateComObject of hoXmlToSign
    End
    Set ComTag Of hoXmlToSign To "getToken"
    Send ComUpdateChildContent To hoXmlToSign "item|Semilla" "030530912644"

    // Sign the XML.
    // 
    // We wish to create the following signed XML:

    //   <?xml version="1.0" encoding="UTF-8"?>
    //   <getToken>
    //   	<item>
    //   		<Semilla>030530912644</Semilla>
    //   	</item>
    //   	<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
    //   		<SignedInfo>
    //   			<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315">
    //   			</CanonicalizationMethod>
    //   			<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1">
    //   			</SignatureMethod>
    //   			<Reference URI="">
    //   				<Transforms>
    //   					<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature">
    //   					</Transform>
    //   				</Transforms>
    //   				<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1">
    //   				</DigestMethod>
    //   				<DigestValue>l2s9BqLppHaWo+w1Al1J5SsYScs=</DigestValue>
    //   			</Reference>
    //   		</SignedInfo>
    //   		<SignatureValue>jlbzatIIBLW8AjH++5uVTTrGIMVwGButuoAR88y/hvSc1+6/eW1K864fK3cKi76oArqk7lAM4pP okoXme0JT/hRXXGo6ecuKzO18z2WfPWwgnN0f3ac03TDu7PwfqiDG9mhQpYfIkNp6GNJIiqlg9PG2w1fOJ1QoypsrQmKq6 YU=</SignatureValue>
    //   		<KeyInfo>
    //   			<KeyValue>
    //   				<RSAKeyValue>
    //   					<Modulus>2Pb4kEB19m7NmOUYew9f36325yrTLTPMU7qzYG2A0/BsubxDdgQw2Op0x6zXvOVX sYI9KkPXtD5orKJMjwxYRv9wUWdyiE776Rv4ljfJO7EQhIK1fDQDnPt0HefBS06Xzg2QLBvLR+pe1vc6C02Dr99v+lnLA8 mnZiJlRHndhNU=</Modulus>
    //   					<Exponent>AQAB</Exponent>
    //   				</RSAKeyValue>
    //   			</KeyValue>
    //   			<X509Data>
    //   				<X509Certificate>MIIF1DCCBLygAwIBAgIDAQNtMA0GCSqGSIb3DQEBBQUAMIHGMQswCQYDVQQG
    //   EwJDTDEYMBYGA1UEChMPQWNlcHRhLmNvbSBTLkEuMTgwNgYDVQQLEy9BdXRv
    //   cmlkYWQgY2VydGlmaWNhZG9yYSBDbGFzZSAzIHBlcnNvbmEgbmF0dXJhbDFD
    //   MEEGA1UEAxM6QWNlcHRhLmNvbSBBdXRvcmlkYWQgY2VydGlmaWNhZG9yYSBD
    //   bGFzZSAzIHBlcnNvbmEgbmF0dXJhbDEeMBwGCSqGSIb3DQEJARYPaW5mb0Bh
    //   Y2VwdGEuY29tMB4XDTAxMDkyNTIxMDgxMloXDTAyMDkyNTIxMDgxMlowgZ8x
    //   CzAJBgNVBAYTAkNMMRgwFgYDVQQKEw9BY2VwdGEuY29tIFMuQS4xLDAqBgNV
    //   BAsTI0NlcnRpZmljYWRvIENsYXNlIDMgUGVyc29uYSBOYXR1cmFsMRwwGgYJ
    //   KoZIhvcNAQkBFg1uY2hlbGVAc2lpLmNsMSowKAYDVQQDEyFOSUNPTEFTIFpB
    //   UFJJQU4gQ0hFTEVCSUZTS0kgQkFFWkEwgZ8wDQYJKoZIhvcNAQEBBQADgY0A
    //   MIGJAoGBANj2+JBAdfZuzZjlGHsPX9+t9ucq0y0zzFO6s2BtgNPwbLm8Q3YE
    //   MNjqdMes17zlV7GCPSpD17Q+aKyiTI8MWEb/cFFncohO++kb+JY3yTuxEISC
    //   tXw0A5z7dB3nwUtOl84NkCwby0fqXtb3OgtNg6/fb/pZywPJp2YiZUR53YTV
    //   AgMBAAGjggJyMIICbjAdBggrBgEEAbVrDwQRFg9BY2VwdGEuY29tIFMuQS4w
    //   JQYDVR0RBB4wHKAaBggrBgEEAcEBAaAOFgwxMC40MTEuODcxLTIwDwYIKwYB
    //   Jh0z1DR3Pl3xOiaFIjSXsQO2PSzcA3wZXYF+KDrMul8e5lAF2NNiLmMVtXEx
    //   ZykMaTGGWS0ZETDhJmBwEZGpP4+lt/JhgwF1Sb6wdrXp7MFCJUc1Tj+/5JqH
    //   1kP0E63/hVElrcP0g8Zn8Z+vr/PMGW1kKgE0IyS4iJ8eIhNSK5phFyKJUn0l
    //   BmIZX7u89d5u7X8=
    //   </X509Certificate>
    //   			</X509Data>
    //   		</KeyInfo>
    //   	</Signature>
    //   </getToken>

    Get Create (RefClass(cComChilkatXmlDSigGen)) To hoGen
    If (Not(IsComObjectCreated(hoGen))) Begin
        Send CreateComObject of hoGen
    End

    Set ComSigLocation Of hoGen To "getToken"
    Set ComSigLocationMod Of hoGen To 0
    Set ComSigNamespacePrefix Of hoGen To ""
    Set ComSigNamespaceUri Of hoGen To "http://www.w3.org/2000/09/xmldsig#"
    Set ComSignedInfoCanonAlg Of hoGen To "C14N"
    Set ComSignedInfoDigestMethod Of hoGen To "sha1"

    // -------- Reference 1 --------
    Get ComAddSameDocRef Of hoGen "" "sha1" "" "" "" To iSuccess

    // Provide a certificate + private key. (PFX password is test123)
    Get Create (RefClass(cComChilkatCert)) To hoCert
    If (Not(IsComObjectCreated(hoCert))) Begin
        Send CreateComObject of hoCert
    End
    Get ComLoadPfxFile Of hoCert "qa_data/pfx/cert_test123.pfx" "test123" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCert To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get pvComObject of hoCert to vCert
    Get ComSetX509Cert Of hoGen vCert True To iSuccess

    Set ComKeyInfoType Of hoGen To "X509Data+KeyValue"
    Set ComX509Type Of hoGen To "Certificate"

    // Load XML to be signed...
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbXml
    If (Not(IsComObjectCreated(hoSbXml))) Begin
        Send CreateComObject of hoSbXml
    End
    Get pvComObject of hoSbXml to vSbXml
    Get ComGetXmlSb Of hoXmlToSign vSbXml To iSuccess

    Set ComBehaviors Of hoGen To "IndentedSignature"

    // Sign the XML...
    Get pvComObject of hoSbXml to vSbXml
    Get ComCreateXmlDSigSb Of hoGen vSbXml To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoGen To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComGetAsString Of hoSbXml To sTemp1
    Showln sTemp1

    // Send the POST with signed XML in the request body
    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End

    Send ComSetRequestHeader To hoHttp "accept" "application/xml"

    // Use one of the following domains, depending on the environment:
    // apicert.sii.cl  - Certification Environment
    // api.sii.cl      - Production Environment
    Move "https://apicert.sii.cl/recursos/v1/boleta.electronica.token" To sUrl
    Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
    If (Not(IsComObjectCreated(hoResp))) Begin
        Send CreateComObject of hoResp
    End
    Get pvComObject of hoSbXml to vSbXml
    Get pvComObject of hoResp to vResp
    Get ComHttpSb Of hoHttp "POST" sUrl vSbXml "utf-8" "application/xml" vResp To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Examine the response status code:
    Get ComStatusCode Of hoResp To iTemp1
    Showln "response status code = " iTemp1

    // Examine the response body:
    Get ComBodyStr Of hoResp To sTemp1
    Showln "response body: " sTemp1

    // We expect a response like this:
    //   <?xml version="1.0" encoding="UTF-8"?>
    //   <SII:RESPUESTA xmlns:SII="http://www.sii.cl/XMLSchema">
    //   	<SII:RESP_HDR>
    //   		<ESTADO>00</ESTADO>
    //   		<GLOSA>Token Creado</GLOSA>
    //   	</SII:RESP_HDR>
    //   	<SII:RESP_BODY>
    //   		<TOKEN>XAuSbYXiNh9Ik</TOKEN>
    //   	</SII:RESP_BODY>
    //   </SII:RESPUESTA>

    // Parse the XML response.
    Get Create (RefClass(cComChilkatXml)) To hoXml
    If (Not(IsComObjectCreated(hoXml))) Begin
        Send CreateComObject of hoXml
    End
    Get ComBodyStr Of hoResp To sTemp1
    Get ComLoadXml Of hoXml sTemp1 To iSuccess
    Get ComGetChildIntValue Of hoXml "SII:RESP_HDR|ESTADO" To iESTADO
    Get ComGetChildContent Of hoXml "SII:RESP_HDR|GLOSA" To sGLOSA
    Get ComGetChildContent Of hoXml "SII:RESP_BODY|TOKEN" To sTOKEN


End_Procedure