Hello,
Need help with the “[Error] Invalid Certificate” error at the ‘Generate QR Code’ step during coding for the compliance.
Please help.
Below are the image of the error and the code used
Sub Main()
Try
’ ---------------- CONFIG ----------------
Dim templateFilename As String = “F:\Kapil\JUNK\ZATCA Testing\OrginalTemplate\Simplified_Invoice.xml”
' ✅ Use PEM files directly (already extracted from your CSID .pfx)
Dim certPemPath As String = "F:\Kapil\JUNK\ZATCA Testing\Csrconfig\CSID From Comlience\cert.pem"
''Dim certPemPath As String = "F:\Kapil\JUNK\ZATCA Testing\Csrconfig\CSID From Comlience\fullchain.pem"
''Dim keyPemPath As String = "F:\Kapil\JUNK\ZATCA Testing\Csrconfig\CSID From Comlience\private_key_pkcs8.pem"
Dim keyPemPath As String = "F:\Kapil\JUNK\ZATCA Testing\Csrconfig\CSID From Comlience\private_ec.pem"
' ✅ Compliance creds (Basic Auth: token:secret)
Dim csidBinarySecurityTokenBase64 As String = "yyyyyy"
Dim csidSecretBase64 As String = "abcd"
' Invoice header values
Dim sellerName As String = "ABC LTD"
Dim sellerVat As String = "300000000000123"
Dim invoiceId As String = "SME00010-REPLACED"
Dim invoiceUuid As String = Guid.NewGuid().ToString()
Dim issueDate As DateTime = DateTime.UtcNow
' Items
Dim items As New List(Of InvoiceLine) From {
New InvoiceLine("1", "كتاب", 33D, 3D),
New InvoiceLine("2", "قلم", 3D, 34D)
}
' ---------------- Load template ----------------
If Not File.Exists(templateFilename) Then Throw New FileNotFoundException("Template not found: " & templateFilename)
Dim xmlDoc As New XmlDocument()
xmlDoc.PreserveWhitespace = True
xmlDoc.Load(templateFilename)
' ---------------- Replace header values ----------------
Dim nsmgr = BuildNsMgr(xmlDoc)
xmlDoc.SelectSingleNode("/def:Invoice/cbc:ID", nsmgr).InnerText = invoiceId
xmlDoc.SelectSingleNode("/def:Invoice/cbc:UUID", nsmgr).InnerText = invoiceUuid
xmlDoc.SelectSingleNode("/def:Invoice/cbc:IssueDate", nsmgr).InnerText = issueDate.ToString("yyyy-MM-dd")
xmlDoc.SelectSingleNode("/def:Invoice/cbc:IssueTime", nsmgr).InnerText = issueDate.ToString("HH:mm:ss\Z")
xmlDoc.SelectSingleNode("//cac:PartyLegalEntity/cbc:RegistrationName", nsmgr).InnerText = sellerName
xmlDoc.SelectSingleNode("//cac:PartyTaxScheme/cbc:CompanyID", nsmgr).InnerText = sellerVat
' Buyer, lines, totals
EnsureB2CMinimalBuyer(xmlDoc)
ClearExistingInvoiceLines(xmlDoc, nsmgr)
AppendInvoiceLines(xmlDoc, nsmgr, items)
Dim totalEx = items.Sum(Function(i) i.UnitPrice * i.Quantity)
Dim totalVat = Math.Round(totalEx * 0.15D, 2)
Dim totalInc = totalEx + totalVat
SetTotals(xmlDoc, nsmgr, totalEx, totalInc)
EnsureQrContainer(xmlDoc, nsmgr)
' ---------------- Load PEMs (NO PFX EXPORT IN CODE) ----------------
''Dim certificateContent As String = PemUtils.LoadCertificatePem(certPemPath)
''Dim privateKeyContent As String = PemUtils.LoadPrivateKeyPem(keyPemPath, requirePkcs8:=True) ' SDK usually expects PKCS#8
Dim certificateContent As String = File.ReadAllText(certPemPath, Encoding.ASCII).Replace(vbCr, "").Trim()
Dim privateKeyContent As String = File.ReadAllText(keyPemPath, Encoding.ASCII).Replace(vbCr, "").Trim()
' ---------------- SIGN + QR VIA SDK ----------------
Dim signer As New EInvoiceSigner()
Dim result As SignResult = signer.SignDocument(xmlDoc, certificateContent, privateKeyContent)
If result Is Nothing OrElse Not result.IsValid Then
Dim errMsg As String = If(result IsNot Nothing AndAlso Not String.IsNullOrEmpty(result.ErrorMessage), result.ErrorMessage, "Unknown error")
Throw New Exception("SDK signing failed: " & errMsg)
End If
Dim signedDoc As XmlDocument = result.SignedEInvoice
' ---------------- Save signed XML ----------------
Dim outXml As String = "F:\Kapil\JUNK\ZATCA Testing\Sample invoice\invoice_signed_sdk.xml"
result.SaveSignedEInvoice(outXml)
' ---------------- Compliance API ----------------
ValidateWithComplianceAPI(signedDoc, outXml, csidBinarySecurityTokenBase64, csidSecretBase64)
Catch ex As Exception
txtresp.Text = "ERROR: " & ex.Message
End Try
End Sub
