Sample code for 30+ languages & platforms
PowerBuilder

Create sii.cl Factura Electrónica (Chile Servicio de Impuestos Internos)

See more XAdES Examples

Demonstrates how to sign an XML invoice according to Chilean Internal Revenue Service regulations.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Gen
oleobject loo_Cert
oleobject loo_SbXml
oleobject loo_Verifier
integer li_NumSigs
integer li_VerifyIdx
integer li_Verified

li_Success = 0

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

// See: Apply 2nd Signature to sii.cl Factura Electr�nica (Chile Servicio de Impuestos Internos)
// for an example showing how to apply a 2nd signature.

// We want to sign XML that looks like the following:

// <?xml version="1.0" encoding="iso-8859-1"?>
// <DTE version="1.0" xmlns="http://www.sii.cl/SiiDte">
//     <Documento ID="F13T34">
//         <Encabezado>
//             <IdDoc>
//                 <TipoDTE>34</TipoDTE>
//                 <Folio>13</Folio>
//                 <FchEmis>2020-07-17</FchEmis>
//                 <FmaPago>1</FmaPago>
//                 <FchVenc>2020-07-17</FchVenc>
//             </IdDoc>
//             <Emisor>
//                 <RUTEmisor>...</RUTEmisor>
//                 <RznSoc>...</RznSoc>
//                 <GiroEmis>...</GiroEmis>
//                 <Acteco>...</Acteco>
//                 <DirOrigen>...</DirOrigen>
//                 <CmnaOrigen>...</CmnaOrigen>
//                 <CiudadOrigen>...</CiudadOrigen>
//             </Emisor>
//             <Receptor>
//                 <RUTRecep>...</RUTRecep>
//                 <RznSocRecep>...</RznSocRecep>
//                 <GiroRecep>...</GiroRecep>
//                 <Contacto/>
//                 <DirRecep>...</DirRecep>
//                 <CmnaRecep>...</CmnaRecep>
//                 <CiudadRecep>...</CiudadRecep>
//             </Receptor>
//             <Totales>
//                 <MntExe>14999</MntExe>
//                 <MntTotal>14999</MntTotal>
//             </Totales>
//         </Encabezado>
//         <Detalle>
//             <NroLinDet>1</NroLinDet>
//             <CdgItem>
//                 <TpoCodigo>INT</TpoCodigo>
//                 <VlrCodigo>1</VlrCodigo>
//             </CdgItem>
//             <NmbItem>Atenci�n profesional mes de Junio 2020</NmbItem>
//             <QtyItem>1</QtyItem>
//             <UnmdItem>UNI</UnmdItem>
//             <PrcItem>14999</PrcItem>
//             <MontoItem>14999</MontoItem>
//         </Detalle>
//         <TED version="1.0">
//             <DD>
//                 <RE>99972220-K</RE>
//                 <TD>34</TD>
//                 <F>13</F>
//                 <FE>2020-07-17</FE>
//                 <RR>99942999-2</RR>
//                 <RSR>...</RSR>
//                 <MNT>14999</MNT>
//                 <IT1>Atencion profesional mes de Junio 2020</IT1>
//                 <CAF version="1.0">
//                     <DA>
//                         <RE>99972220-K</RE>
//                         <RS>...</RS>
//                         <TD>34</TD>
//                         <RNG>
//                             <D>3</D>
//                             <H>12</H>
//                         </RNG>
//                         <FA>2019-10-10</FA>
//                         <RSAPK>
//                             <M>2zHVYpcVNQRvS2yFuqdrh...TEQZx/m0t9HVTgWKZvlc6LSQ==</M>
//                             <E>Aw==</E>
//                         </RSAPK>
//                         <IDK>300</IDK>
//                     </DA>
//                     <FRMA algoritmo="SHA1withRSA">LaVkjISGu...sBtsQL1jR9lw==</FRMA>
//                 </CAF>
//                 <TSTED>2020-07-17T13:19:10</TSTED>
//             </DD>
//             <FRMT algoritmo="SHA1withRSA">LxZr6zmXRZIfTz7...IXS6sp4vfz2fIsA==</FRMT>
//         </TED>
//         <TmstFirma>2020-07-17T13:19:10</TmstFirma>
//     </Documento>
// </DTE>

li_Success = 1

loo_Gen = create oleobject
li_rc = loo_Gen.ConnectToNewObject("Chilkat.XmlDSigGen")
if li_rc < 0 then
    destroy loo_Gen
    MessageBox("Error","Connecting to COM object failed")
    return
end if

loo_Gen.SigLocation = "DTE"
loo_Gen.SigLocationMod = 0
loo_Gen.SigNamespacePrefix = ""
loo_Gen.SigNamespaceUri = "http://www.w3.org/2000/09/xmldsig#"
loo_Gen.SignedInfoCanonAlg = "C14N"
loo_Gen.SignedInfoDigestMethod = "sha1"

// -------- Reference 1 --------
loo_Gen.AddSameDocRef("F13T34","sha1","","","")

// Provide a certificate + private key. (PFX password is test123)
loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")

li_Success = loo_Cert.LoadPfxFile("qa_data/pfx/cert_test123.pfx","test123")
if li_Success <> 1 then
    Write-Debug loo_Cert.LastErrorText
    destroy loo_Gen
    destroy loo_Cert
    return
end if

loo_Gen.SetX509Cert(loo_Cert,1)

loo_Gen.KeyInfoType = "X509Data+KeyValue"
loo_Gen.X509Type = "Certificate"

// Load XML to be signed...
loo_SbXml = create oleobject
li_rc = loo_SbXml.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_SbXml.LoadFile("qa_data/xml_dsig/sii_cl/xmlToSign.xml","iso-8859-1")
if li_Success = 0 then
    Write-Debug "Failed to load XML file."
    destroy loo_Gen
    destroy loo_Cert
    destroy loo_SbXml
    return
end if

loo_Gen.Behaviors = "IndentedSignature"

// Sign the XML...
li_Success = loo_Gen.CreateXmlDSigSb(loo_SbXml)
if li_Success <> 1 then
    Write-Debug loo_Gen.LastErrorText
    destroy loo_Gen
    destroy loo_Cert
    destroy loo_SbXml
    return
end if

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

// Save the signed XML to a file.
li_Success = loo_SbXml.WriteFile("qa_data/xml_dsig/sii_cl/signed1.xml","iso-8859-1",0)

Write-Debug loo_SbXml.GetAsString()

// ----------------------------------------
// Verify the signatures we just produced...
loo_Verifier = create oleobject
li_rc = loo_Verifier.ConnectToNewObject("Chilkat.XmlDSig")

li_Success = loo_Verifier.LoadSignatureSb(loo_SbXml)
if li_Success <> 1 then
    Write-Debug loo_Verifier.LastErrorText
    destroy loo_Gen
    destroy loo_Cert
    destroy loo_SbXml
    destroy loo_Verifier
    return
end if

li_NumSigs = loo_Verifier.NumSignatures
li_VerifyIdx = 0
do while li_VerifyIdx < li_NumSigs
    loo_Verifier.Selector = li_VerifyIdx
    li_Verified = loo_Verifier.VerifySignature(1)
    if li_Verified <> 1 then
        Write-Debug loo_Verifier.LastErrorText
        destroy loo_Gen
        destroy loo_Cert
        destroy loo_SbXml
        destroy loo_Verifier
        return
    end if

    li_VerifyIdx = li_VerifyIdx + 1
loop
Write-Debug "All signatures were successfully verified."


destroy loo_Gen
destroy loo_Cert
destroy loo_SbXml
destroy loo_Verifier