Swift
Swift
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 Swift Downloads
func chilkatTest() {
var success: Bool = false
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
success = true
// 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
let xml = CkoXml()!
xml.tag = "facturaElectronicaCompraVenta"
xml.addAttribute(name: "xmlns:xsi", value: "http://www.w3.org/2001/XMLSchema-instance")
xml.addAttribute(name: "xsi:noNamespaceSchemaLocation", value: "facturaElectronicaCompraVenta.xsd")
xml.updateChildContent(tagPath: "cabecera|nitEmisor", value: "99999999")
xml.updateChildContent(tagPath: "cabecera|razonSocialEmisor", value: "Abc Xyz")
xml.updateChildContent(tagPath: "cabecera|municipio", value: "Cochabamba - Cochabamba")
xml.updateChildContent(tagPath: "cabecera|telefono", value: "4444444")
xml.updateChildContent(tagPath: "cabecera|numeroFactura", value: "417")
xml.updateChildContent(tagPath: "cabecera|cuf", value: "ABABABABABABABABABABABABABABABABABABABABABABABABABABABABA")
xml.updateChildContent(tagPath: "cabecera|cufd", value: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
xml.updateChildContent(tagPath: "cabecera|codigoSucursal", value: "0")
xml.updateChildContent(tagPath: "cabecera|direccion", value: "aaa aaa aaa")
xml.updateChildContent(tagPath: "cabecera|codigoPuntoVenta", value: "1")
xml.updateChildContent(tagPath: "cabecera|fechaEmision", value: "2023-04-13T22:43:22.417")
xml.updateChildContent(tagPath: "cabecera|nombreRazonSocial", value: "xyz")
xml.updateChildContent(tagPath: "cabecera|codigoTipoDocumentoIdentidad", value: "1")
xml.updateChildContent(tagPath: "cabecera|numeroDocumento", value: "1234567")
xml.updateAttrAt(tagPath: "cabecera|complemento", autoCreate: true, attrName: "xsi:nil", attrValue: "true")
xml.updateChildContent(tagPath: "cabecera|codigoCliente", value: "1234567")
xml.updateChildContent(tagPath: "cabecera|codigoMetodoPago", value: "1")
xml.updateAttrAt(tagPath: "cabecera|numeroTarjeta", autoCreate: true, attrName: "xsi:nil", attrValue: "true")
xml.updateChildContent(tagPath: "cabecera|montoTotal", value: "22.00")
xml.updateChildContent(tagPath: "cabecera|montoTotalSujetoIva", value: "22.00")
xml.updateChildContent(tagPath: "cabecera|codigoMoneda", value: "1")
xml.updateChildContent(tagPath: "cabecera|tipoCambio", value: "1.00")
xml.updateChildContent(tagPath: "cabecera|montoTotalMoneda", value: "22.00")
xml.updateAttrAt(tagPath: "cabecera|montoGiftCard", autoCreate: true, attrName: "xsi:nil", attrValue: "true")
xml.updateChildContent(tagPath: "cabecera|descuentoAdicional", value: "0.00")
xml.updateChildContent(tagPath: "cabecera|codigoExcepcion", value: "1")
xml.updateAttrAt(tagPath: "cabecera|cafc", autoCreate: true, attrName: "xsi:nil", attrValue: "true")
xml.updateChildContent(tagPath: "cabecera|leyenda", value: "Ley N� 453: El proveedor de servicios debe habilitar medios e instrumentos para efectuar consultas y reclamaciones.")
xml.updateChildContent(tagPath: "cabecera|usuario", value: "nromero")
xml.updateChildContent(tagPath: "cabecera|codigoDocumentoSector", value: "1")
xml.updateChildContent(tagPath: "detalle|actividadEconomica", value: "620100")
xml.updateChildContent(tagPath: "detalle|codigoProductoSin", value: "123456")
xml.updateChildContent(tagPath: "detalle|codigoProducto", value: "1")
xml.updateChildContent(tagPath: "detalle|descripcion", value: "Economicos")
xml.updateChildContent(tagPath: "detalle|cantidad", value: "1.00")
xml.updateChildContent(tagPath: "detalle|unidadMedida", value: "58")
xml.updateChildContent(tagPath: "detalle|precioUnitario", value: "22.00")
xml.updateChildContent(tagPath: "detalle|montoDescuento", value: "0.00")
xml.updateChildContent(tagPath: "detalle|subTotal", value: "22.00")
xml.updateAttrAt(tagPath: "detalle|numeroSerie", autoCreate: true, attrName: "xsi:nil", attrValue: "true")
xml.updateAttrAt(tagPath: "detalle|numeroImei", autoCreate: true, attrName: "xsi:nil", attrValue: "true")
let gen = CkoXmlDSigGen()!
gen.sigLocation = "facturaElectronicaCompraVenta"
gen.sigLocationMod = 0
gen.sigNamespacePrefix = ""
gen.sigNamespaceUri = "http://www.w3.org/2000/09/xmldsig#"
gen.signedInfoCanonAlg = "C14N"
gen.signedInfoDigestMethod = "sha256"
gen.addSameDocRef(id: "", digestMethod: "sha256", canonMethod: "C14N_WithComments", prefixList: "", refType: "")
// Provide your certificate + private key. (PFX password is test123)
let cert = CkoCert()!
success = cert.loadPfxFile(path: "qa_data/pfx/cert_test123.pfx", password: "test123")
if success != true {
print("\(cert.lastErrorText!)")
return
}
gen.setX509Cert(cert: cert, usePrivateKey: true)
gen.keyInfoType = "X509Data"
gen.x509Type = "Certificate"
gen.behaviors = "EnvelopedTransformFirst"
// Load XML to be signed...
let sbXml = CkoStringBuilder()!
xml.emitCompact = true
xml.getSb(sb: sbXml)
// Sign the XML...
success = gen.createXmlDSigSb(sbXml: sbXml)
if success != true {
print("\(gen.lastErrorText!)")
return
}
// -----------------------------------------------
// Save the signed XML to a file.
success = sbXml.writeFile(path: "qa_output/signedXml.xml", charset: "utf-8", emitBom: true)
print("\(sbXml.getAsString()!)")
// ----------------------------------------
// Verify the signatures we just produced...
let verifier = CkoXmlDSig()!
success = verifier.loadSignatureSb(sbXmlSig: sbXml)
if success != true {
print("\(verifier.lastErrorText!)")
return
}
var numSigs: Int = verifier.numSignatures.intValue
var verifyIdx: Int = 0
while verifyIdx < numSigs {
verifier.selector = verifyIdx
var verified: Bool = verifier.verifySignature(verifyReferenceDigests: true)
if verified != true {
print("\(verifier.lastErrorText!)")
return
}
verifyIdx = verifyIdx + 1
}
print("All signatures were successfully verified.")
}