Sample code for 30+ languages & platforms
Visual FoxPro

Sign Bolivia Invoice XML

See more XML Digital Signatures Examples

Demonstrates how to create an XML digital signature (XmlDSig) for a Bolivia invoice (factura).

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loXml
LOCAL loGen
LOCAL loCert
LOCAL loSbXml
LOCAL loVerifier
LOCAL lnNumSigs
LOCAL lnVerifyIdx
LOCAL lnVerified

lnSuccess = 0

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

lnSuccess = 1

* Create the following XML to be signed:

* <?xml version="1.0" encoding="UTF-8"?>
* <facturaElectronicaCompraVenta xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="facturaElectronicaCompraVenta.xsd">
*     <cabecera>
*         <nitEmisor>99999999</nitEmisor>
*         <razonSocialEmisor>Abc Xyz</razonSocialEmisor>
*         <municipio>Cochabamba - Cochabamba</municipio>
*         <telefono>4444444</telefono>
*         <numeroFactura>417</numeroFactura>
*         <cuf>ABABABABABABABABABABABABABABABABABABABABABABABABABABABABA</cuf>
*         <cufd>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</cufd>
*         <codigoSucursal>0</codigoSucursal>
*         <direccion>aaa aaa aaa</direccion>
*         <codigoPuntoVenta>1</codigoPuntoVenta>
*         <fechaEmision>2023-04-13T22:43:22.417</fechaEmision>
*         <nombreRazonSocial>xyz</nombreRazonSocial>
*         <codigoTipoDocumentoIdentidad>1</codigoTipoDocumentoIdentidad>
*         <numeroDocumento>1234567</numeroDocumento>
*            <complemento xsi:nil="true"/>
*         <codigoCliente>1234567</codigoCliente>
*         <codigoMetodoPago>1</codigoMetodoPago>
*            <numeroTarjeta xsi:nil="true"/>
*         <montoTotal>22.00</montoTotal>
*         <montoTotalSujetoIva>22.00</montoTotalSujetoIva>
*         <codigoMoneda>1</codigoMoneda>
*         <tipoCambio>1.00</tipoCambio>
*         <montoTotalMoneda>22.00</montoTotalMoneda>
*            <montoGiftCard xsi:nil="true"/>
*         <descuentoAdicional>0.00</descuentoAdicional>
*            <codigoExcepcion>1</codigoExcepcion>
*            <cafc xsi:nil="true"/>
*         <leyenda>Ley N� 453: El proveedor de servicios debe habilitar medios e instrumentos para efectuar consultas y reclamaciones.</leyenda>
*         <usuario>nromero</usuario>
*         <codigoDocumentoSector>1</codigoDocumentoSector>
*     </cabecera>
*     <detalle>
*         <actividadEconomica>620100</actividadEconomica>
*         <codigoProductoSin>123456</codigoProductoSin>
*         <codigoProducto>1</codigoProducto>
*         <descripcion>Economicos</descripcion>
*         <cantidad>1.00</cantidad>
*         <unidadMedida>58</unidadMedida>
*         <precioUnitario>22.00</precioUnitario>
*         <montoDescuento>0.00</montoDescuento>
*         <subTotal>22.00</subTotal>
*            <numeroSerie xsi:nil="true"/>
*            <numeroImei xsi:nil="true"/>
*     </detalle>
* </facturaElectronicaCompraVenta>

* Use this online tool to generate code from sample XML: 
* Generate Code to Create XML

loXml = CreateObject('Chilkat.Xml')
loXml.Tag = "facturaElectronicaCompraVenta"
loXml.AddAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance")
loXml.AddAttribute("xsi:noNamespaceSchemaLocation","facturaElectronicaCompraVenta.xsd")
loXml.UpdateChildContent("cabecera|nitEmisor","99999999")
loXml.UpdateChildContent("cabecera|razonSocialEmisor","Abc Xyz")
loXml.UpdateChildContent("cabecera|municipio","Cochabamba - Cochabamba")
loXml.UpdateChildContent("cabecera|telefono","4444444")
loXml.UpdateChildContent("cabecera|numeroFactura","417")
loXml.UpdateChildContent("cabecera|cuf","ABABABABABABABABABABABABABABABABABABABABABABABABABABABABA")
loXml.UpdateChildContent("cabecera|cufd","xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
loXml.UpdateChildContent("cabecera|codigoSucursal","0")
loXml.UpdateChildContent("cabecera|direccion","aaa aaa aaa")
loXml.UpdateChildContent("cabecera|codigoPuntoVenta","1")
loXml.UpdateChildContent("cabecera|fechaEmision","2023-04-13T22:43:22.417")
loXml.UpdateChildContent("cabecera|nombreRazonSocial","xyz")
loXml.UpdateChildContent("cabecera|codigoTipoDocumentoIdentidad","1")
loXml.UpdateChildContent("cabecera|numeroDocumento","1234567")
loXml.UpdateAttrAt("cabecera|complemento",1,"xsi:nil","true")
loXml.UpdateChildContent("cabecera|codigoCliente","1234567")
loXml.UpdateChildContent("cabecera|codigoMetodoPago","1")
loXml.UpdateAttrAt("cabecera|numeroTarjeta",1,"xsi:nil","true")
loXml.UpdateChildContent("cabecera|montoTotal","22.00")
loXml.UpdateChildContent("cabecera|montoTotalSujetoIva","22.00")
loXml.UpdateChildContent("cabecera|codigoMoneda","1")
loXml.UpdateChildContent("cabecera|tipoCambio","1.00")
loXml.UpdateChildContent("cabecera|montoTotalMoneda","22.00")
loXml.UpdateAttrAt("cabecera|montoGiftCard",1,"xsi:nil","true")
loXml.UpdateChildContent("cabecera|descuentoAdicional","0.00")
loXml.UpdateChildContent("cabecera|codigoExcepcion","1")
loXml.UpdateAttrAt("cabecera|cafc",1,"xsi:nil","true")
loXml.UpdateChildContent("cabecera|leyenda","Ley N� 453: El proveedor de servicios debe habilitar medios e instrumentos para efectuar consultas y reclamaciones.")
loXml.UpdateChildContent("cabecera|usuario","nromero")
loXml.UpdateChildContent("cabecera|codigoDocumentoSector","1")
loXml.UpdateChildContent("detalle|actividadEconomica","620100")
loXml.UpdateChildContent("detalle|codigoProductoSin","123456")
loXml.UpdateChildContent("detalle|codigoProducto","1")
loXml.UpdateChildContent("detalle|descripcion","Economicos")
loXml.UpdateChildContent("detalle|cantidad","1.00")
loXml.UpdateChildContent("detalle|unidadMedida","58")
loXml.UpdateChildContent("detalle|precioUnitario","22.00")
loXml.UpdateChildContent("detalle|montoDescuento","0.00")
loXml.UpdateChildContent("detalle|subTotal","22.00")
loXml.UpdateAttrAt("detalle|numeroSerie",1,"xsi:nil","true")
loXml.UpdateAttrAt("detalle|numeroImei",1,"xsi:nil","true")

loGen = CreateObject('Chilkat.XmlDSigGen')

loGen.SigLocation = "facturaElectronicaCompraVenta"
loGen.SigLocationMod = 0
loGen.SigNamespacePrefix = ""
loGen.SigNamespaceUri = "http://www.w3.org/2000/09/xmldsig#"
loGen.SignedInfoCanonAlg = "C14N"
loGen.SignedInfoDigestMethod = "sha256"

loGen.AddSameDocRef("","sha256","C14N_WithComments","","")

* Provide your certificate + private key. (PFX password is test123)
loCert = CreateObject('Chilkat.Cert')
lnSuccess = loCert.LoadPfxFile("qa_data/pfx/cert_test123.pfx","test123")
IF (lnSuccess <> 1) THEN
    ? loCert.LastErrorText
    RELEASE loXml
    RELEASE loGen
    RELEASE loCert
    CANCEL
ENDIF

loGen.SetX509Cert(loCert,1)

loGen.KeyInfoType = "X509Data"
loGen.X509Type = "Certificate"

loGen.Behaviors = "EnvelopedTransformFirst"

* Load XML to be signed...
loSbXml = CreateObject('Chilkat.StringBuilder')
loXml.EmitCompact = 1
loXml.GetXmlSb(loSbXml)

* Sign the XML...
lnSuccess = loGen.CreateXmlDSigSb(loSbXml)
IF (lnSuccess <> 1) THEN
    ? loGen.LastErrorText
    RELEASE loXml
    RELEASE loGen
    RELEASE loCert
    RELEASE loSbXml
    CANCEL
ENDIF

* -----------------------------------------------

* Save the signed XML to a file.
lnSuccess = loSbXml.WriteFile("qa_output/signedXml.xml","utf-8",1)

? loSbXml.GetAsString()

* ----------------------------------------
* Verify the signatures we just produced...
loVerifier = CreateObject('Chilkat.XmlDSig')
lnSuccess = loVerifier.LoadSignatureSb(loSbXml)
IF (lnSuccess <> 1) THEN
    ? loVerifier.LastErrorText
    RELEASE loXml
    RELEASE loGen
    RELEASE loCert
    RELEASE loSbXml
    RELEASE loVerifier
    CANCEL
ENDIF

lnNumSigs = loVerifier.NumSignatures
lnVerifyIdx = 0
DO WHILE lnVerifyIdx < lnNumSigs
    loVerifier.Selector = lnVerifyIdx
    lnVerified = loVerifier.VerifySignature(1)
    IF (lnVerified <> 1) THEN
        ? loVerifier.LastErrorText
        RELEASE loXml
        RELEASE loGen
        RELEASE loCert
        RELEASE loSbXml
        RELEASE loVerifier
        CANCEL
    ENDIF

    lnVerifyIdx = lnVerifyIdx + 1
ENDDO
? "All signatures were successfully verified."

RELEASE loXml
RELEASE loGen
RELEASE loCert
RELEASE loSbXml
RELEASE loVerifier