This commit is contained in:
2025-07-20 21:16:01 +02:00
parent f206b96009
commit d079aff6cc
18 changed files with 415 additions and 98 deletions

View File

@@ -0,0 +1,108 @@

Imports System.IO
Imports System.Net
Imports System.Security.Cryptography
Imports System.Text
Imports Newtonsoft.Json
Public Class cBZST_UID_REST
Shared ReadOnly ApiHost As String = "api.evatr.vies.bzst.de"
Shared ReadOnly ApiPort As Integer = 443
Shared ReadOnly ApiBasePath As String = "/v1"
' === 1. MAC Authentication Header Builder ===
Shared Function BuildMacHeader(httpMethod As String, path As String, body As String, id As String, base64Key As String) As String
Dim ts As String = CLng((DateTime.UtcNow - #1/1/1970#).TotalSeconds).ToString()
Dim nonce As String = Guid.NewGuid().ToString("N").Substring(0, 12)
Dim bodyHashPart As String = ""
If httpMethod = "POST" AndAlso body IsNot Nothing Then
Using sha = SHA256.Create()
bodyHashPart = Convert.ToBase64String(sha.ComputeHash(Encoding.UTF8.GetBytes(body)))
End Using
End If
Dim macInput As String = $"{ts}{vbLf}{nonce}{vbLf}{httpMethod}{vbLf}{path}{vbLf}{ApiHost}{vbLf}{ApiPort}{vbLf}{bodyHashPart}{vbLf}"
Dim keyBytes As Byte() = Convert.FromBase64String(base64Key)
Dim hmac As New HMACSHA256(keyBytes)
Dim macSig As String = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(macInput)))
Return $"MAC id=""{id}"", ts=""{ts}"", nonce=""{nonce}"", mac=""{macSig}"""
End Function
' === 2. HTTP Call Helper ===
Shared Function DoRequest(httpMethod As String, path As String, body As String, authHeader As String) As String
Dim url As String = $"https://{ApiHost}{path}"
Dim req = CType(WebRequest.Create(url), HttpWebRequest)
req.Method = httpMethod
req.Headers.Add("Authorization", authHeader)
If httpMethod = "POST" Then
req.ContentType = "application/json"
Dim bodyBytes = Encoding.UTF8.GetBytes(body)
req.ContentLength = bodyBytes.Length
Using s = req.GetRequestStream()
s.Write(bodyBytes, 0, bodyBytes.Length)
End Using
Else
req.Accept = "application/json"
End If
Using resp = CType(req.GetResponse(), HttpWebResponse)
Using sr = New StreamReader(resp.GetResponseStream())
Return sr.ReadToEnd()
End Using
End Using
End Function
' === 3. CheckVat (einfach) ===
Public Shared Function CheckVat(countryCode As String, vatNumber As String, apiId As String, apiKeyBase64 As String) As String
Dim path = $"{ApiBasePath}/euvat/{countryCode}{vatNumber}"
Dim auth = BuildMacHeader("GET", path, Nothing, apiId, apiKeyBase64)
Return DoRequest("GET", path, Nothing, auth)
End Function
' === 4. CheckVatQualified (mit Zusatzdaten) ===
Public Shared Function CheckVatQualified(countryCode As String, vatNumber As String,
street As String, postalCode As String, city As String,
apiId As String, apiKeyBase64 As String) As String
Dim path = $"{ApiBasePath}/euvat/qualified"
Dim payload = New With {
.countryCode = countryCode,
.vatNumber = vatNumber,
.street = street,
.postalCode = postalCode,
.city = city
}
Dim jsonBody As String = JsonConvert.SerializeObject(payload)
Dim auth = BuildMacHeader("POST", path, jsonBody, apiId, apiKeyBase64)
Return DoRequest("POST", path, jsonBody, auth)
End Function
' === 5. Statusmeldungen abrufen ===
Public Function GetStatusMessages(apiId As String, apiKeyBase64 As String) As String
Dim path = $"{ApiBasePath}/info/statusmeldungen"
Dim auth = BuildMacHeader("GET", path, Nothing, apiId, apiKeyBase64)
Return DoRequest("GET", path, Nothing, auth)
End Function
Sub EXAMPLE()
Dim apiId As String = "DEINE_ID"
Dim apiKey As String = "DEIN_BASE64_KEY"
' 1) Einfache Prüfung
Dim simple = CheckVat("AT", "U18522105", apiId, apiKey)
Console.WriteLine("Simple VAT Response: " & simple)
' 2) Qualifizierte Prüfung
Dim qual = CheckVatQualified("DE", "123456789", "Musterstr. 1", "12345", "Musterstadt", apiId, apiKey)
Console.WriteLine("Qualified VAT Response: " & qual)
' 3) Statusmeldungen
Dim status = GetStatusMessages(apiId, apiKey)
Console.WriteLine("Status Messages: " & status)
End Sub
End Class

View File

@@ -0,0 +1,75 @@
'VERALTET!!!!!!!!!!!!!!!!!!!!!!!!!!
'Imports CookComputing.XmlRpc
'Public Interface IEvatR
' <XmlRpcMethod("evatrRPC.checkVatSimple")>
' Function CheckVatSimple(tnID As String, ländercode As String, ustid As String) As XmlRpcStruct
' <XmlRpcMethod("evatrRPC.checkVatQualified")>
' Function CheckVatQualified(tnID As String, ländercode As String, ustid As String,
' firmenname As String, ort As String, plz As String, strasse As String) As XmlRpcStruct
'End Interface
'Public Class cBZST_UID_XML
' Shared tnid As String = "DE813570890" ' BZSt Teilnehmer-ID
' Public Sub TEST()
' ' Zum manuellen Testen
' Dim gültig1 = checkVATSimple("AT", "U12345678")
' Console.WriteLine("checkVATSimple returned: " & gültig1)
' Dim gültig2 = checkVATQualified("DE", "123456789", "Beispielfirma GmbH", "Musterstadt", "12345", "Musterstraße 1")
' Console.WriteLine("checkVATQualified returned: " & gültig2)
' End Sub
' Public Shared Function checkVATSimple(ByVal countryCode As String, ByVal vatNumber As String) As Boolean
' Try
' Console.WriteLine("==== Einfache UID-Abfrage ====")
' Dim proxy = XmlRpcProxyGen.Create(Of IEvatR)()
' Dim client = CType(proxy, XmlRpcClientProtocol)
' client.Url = "https://evatr.bzst.de/eVatR/xmlrpc"
' client.XmlEncoding = System.Text.Encoding.UTF8
' Dim res As XmlRpcStruct = proxy.CheckVatSimple(tnid, countryCode, vatNumber)
' Console.WriteLine("Land: " & countryCode)
' Console.WriteLine("USt-IdNr.: " & vatNumber)
' Console.WriteLine("Ergebniscode: " & res("erg_code"))
' Console.WriteLine("Ergebnistext: " & res("erg_text"))
' Console.WriteLine("Gültig: " & res("gueltig"))
' Return Convert.ToBoolean(res("gueltig"))
' Catch ex As Exception
' Console.WriteLine("Fehler bei einfacher Prüfung: " & ex.Message)
' Return False
' End Try
' End Function
' Public Shared Function checkVATQualified(ByVal countryCode As String, ByVal vatNumber As String,
' ByVal companyName As String, ByVal city As String,
' ByVal postalCode As String, ByVal street As String) As Boolean
' Try
' Dim proxy = XmlRpcProxyGen.Create(Of IEvatR)()
' Dim client = CType(proxy, XmlRpcClientProtocol)
' client.Url = "https://evatr.bzst.de/eVatR/xmlrpc"
' client.XmlEncoding = System.Text.Encoding.UTF8
' Dim res As XmlRpcStruct = proxy.CheckVatQualified(tnid, countryCode, vatNumber, companyName, city, postalCode, street)
' Console.WriteLine("==== Qualifizierte UID-Abfrage ====")
' Console.WriteLine("Land: " & countryCode)
' Console.WriteLine("USt-IdNr.: " & vatNumber)
' Console.WriteLine("Firma: " & companyName)
' Console.WriteLine("Adresse: " & street & ", " & postalCode & " " & city)
' Console.WriteLine("Ergebniscode: " & res("erg_code"))
' Console.WriteLine("Ergebnistext: " & res("erg_text"))
' Console.WriteLine("Gültig: " & res("gueltig"))
' Return Convert.ToBoolean(res("gueltig"))
' Catch ex As Exception
' Console.WriteLine("Fehler bei qualifizierter Prüfung: " & ex.Message)
' Return False
' End Try
' End Function
'End Class

View File

@@ -165,6 +165,11 @@ Public Class cTelotecAPI
Function GetTAMessages(Company) As List(Of TAMessage)
Console.WriteLine("-------------LOS-------------")
VERAG_PROG_ALLGEMEIN.cChilkat_Helper.UnlockCilkat()
Dim messages As New List(Of TAMessage)()
Dim http As New Http()