HMRC
This commit is contained in:
176
Aviso/CHMRC.vb
176
Aviso/CHMRC.vb
@@ -11,6 +11,7 @@ Imports Microsoft.Extensions.Hosting
|
|||||||
Imports System.Collections.Specialized
|
Imports System.Collections.Specialized
|
||||||
Imports System.Text
|
Imports System.Text
|
||||||
Imports System.IO
|
Imports System.IO
|
||||||
|
Imports VERAG_PROG_ALLGEMEIN
|
||||||
|
|
||||||
|
|
||||||
Public Class cHMRC
|
Public Class cHMRC
|
||||||
@@ -436,6 +437,77 @@ Public Class cHMRC
|
|||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
|
|
||||||
|
Shared Sub HMRC_RefreshToken(ByVal TOKEN As cHMRCToken)
|
||||||
|
|
||||||
|
If DateDiff(DateInterval.Minute, TOKEN.token_refresh_datetime, Now) < 230 Then 'bis 240 MIN / 4hr
|
||||||
|
'Token ist noch frisch
|
||||||
|
Exit Sub
|
||||||
|
End If
|
||||||
|
|
||||||
|
' Chilkat Lizenz aktivieren (falls benötigt)
|
||||||
|
VERAG_PROG_ALLGEMEIN.cChilkat_Helper.UnlockCilkat()
|
||||||
|
|
||||||
|
' HTTP-Objekt für API-Request erstellen
|
||||||
|
Dim http As New Chilkat.Http()
|
||||||
|
|
||||||
|
' URL für HMRC Token Refresh
|
||||||
|
Dim url As String = "https://api.service.hmrc.gov.uk/oauth/token"
|
||||||
|
|
||||||
|
' HTTP-Request-Objekt für POST-Anfrage
|
||||||
|
Dim req As New Chilkat.HttpRequest()
|
||||||
|
req.HttpVerb = "POST"
|
||||||
|
req.Path = "/oauth/token"
|
||||||
|
req.ContentType = "application/x-www-form-urlencoded"
|
||||||
|
|
||||||
|
' OAuth2 Credentials einfügen
|
||||||
|
|
||||||
|
Dim TOKEN_NEW = ""
|
||||||
|
req.AddParam("client_secret", CLIENT_SECRET)
|
||||||
|
req.AddParam("client_id", CLIENT_ID)
|
||||||
|
req.AddParam("grant_type", "client_credentials")
|
||||||
|
req.AddParam("refresh_token", TOKEN_NEW)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
' rest.AddQueryParam("scope", scope)
|
||||||
|
|
||||||
|
|
||||||
|
' Anfrage senden
|
||||||
|
Dim resp As Chilkat.HttpResponse = http.PostUrlEncoded(url, req)
|
||||||
|
|
||||||
|
' Fehlerprüfung
|
||||||
|
If resp Is Nothing Then
|
||||||
|
Console.WriteLine("Fehler: " & http.LastErrorText)
|
||||||
|
Exit Sub
|
||||||
|
End If
|
||||||
|
|
||||||
|
' API-Antwort ausgeben
|
||||||
|
Dim json As New Chilkat.JsonObject()
|
||||||
|
json.Load(resp.BodyStr)
|
||||||
|
|
||||||
|
' Neuer Access Token
|
||||||
|
Dim accessToken As String = json.StringOf("access_token")
|
||||||
|
Dim newRefreshToken As String = json.StringOf("refresh_token")
|
||||||
|
Dim expiresIn As String = json.StringOf("expires_in")
|
||||||
|
|
||||||
|
Console.WriteLine("Neuer Access Token: " & accessToken)
|
||||||
|
Console.WriteLine("Neuer Refresh Token: " & newRefreshToken)
|
||||||
|
Console.WriteLine("Gültigkeit (Sekunden): " & expiresIn)
|
||||||
|
|
||||||
|
' Optional: Tokens in einer Datei speichern
|
||||||
|
'Dim tokenFile As String = "token.json"
|
||||||
|
'json.EmitCompact = False
|
||||||
|
'json.WriteFile(tokenFile)
|
||||||
|
|
||||||
|
TOKEN.token_BEARER_TOKEN = newRefreshToken
|
||||||
|
TOKEN.token_refresh_datetime = Now
|
||||||
|
TOKEN.SAVE()
|
||||||
|
|
||||||
|
Console.WriteLine("Tokens wurden in '" & tokenFile & "' gespeichert.")
|
||||||
|
|
||||||
|
End Sub
|
||||||
|
|
||||||
|
|
||||||
Shared Function grantAccessApplication(ByRef AccessToken, scope) As String
|
Shared Function grantAccessApplication(ByRef AccessToken, scope) As String
|
||||||
Try
|
Try
|
||||||
VERAG_PROG_ALLGEMEIN.cChilkat_Helper.UnlockCilkat()
|
VERAG_PROG_ALLGEMEIN.cChilkat_Helper.UnlockCilkat()
|
||||||
@@ -1273,7 +1345,7 @@ Public Class cHMRC
|
|||||||
outgoingQueryString.Add("grant_type", "client_credentials")
|
outgoingQueryString.Add("grant_type", "client_credentials")
|
||||||
' outgoingQueryString.Add("refresh_token", refreshToken)
|
' outgoingQueryString.Add("refresh_token", refreshToken)
|
||||||
outgoingQueryString.Add("client_id", CLIENT_ID)
|
outgoingQueryString.Add("client_id", CLIENT_ID)
|
||||||
outgoingQueryString.Add("client_secret", CLIENT_Secret)
|
outgoingQueryString.Add("client_secret", CLIENT_SECRET)
|
||||||
outgoingQueryString.Add("scope", scope) '"write:goods-movement-system")
|
outgoingQueryString.Add("scope", scope) '"write:goods-movement-system")
|
||||||
Dim postBytes As Byte() = New ASCIIEncoding().GetBytes(outgoingQueryString.ToString())
|
Dim postBytes As Byte() = New ASCIIEncoding().GetBytes(outgoingQueryString.ToString())
|
||||||
|
|
||||||
@@ -1284,15 +1356,15 @@ Public Class cHMRC
|
|||||||
Try
|
Try
|
||||||
|
|
||||||
Using response As System.Net.WebResponse = request.GetResponse()
|
Using response As System.Net.WebResponse = request.GetResponse()
|
||||||
Using streamReader As System.IO.StreamReader = New System.IO.StreamReader(response.GetResponseStream())
|
Using streamReader As System.IO.StreamReader = New System.IO.StreamReader(response.GetResponseStream())
|
||||||
' Parse the JSON the way you prefer
|
' Parse the JSON the way you prefer
|
||||||
Dim jsonResult As RefreshTokenResultJSON = New System.Web.Script.Serialization.JavaScriptSerializer().Deserialize(streamReader.ReadToEnd(), GetType(RefreshTokenResultJSON))
|
Dim jsonResult As RefreshTokenResultJSON = New System.Web.Script.Serialization.JavaScriptSerializer().Deserialize(streamReader.ReadToEnd(), GetType(RefreshTokenResultJSON))
|
||||||
AccessToken = jsonResult.access_token
|
AccessToken = jsonResult.access_token
|
||||||
|
|
||||||
|
|
||||||
' For more information, refer to the documentation
|
' For more information, refer to the documentation
|
||||||
End Using
|
End Using
|
||||||
End Using
|
End Using
|
||||||
|
|
||||||
Catch ex As Exception
|
Catch ex As Exception
|
||||||
MsgBox(ex.Message & ex.StackTrace)
|
MsgBox(ex.Message & ex.StackTrace)
|
||||||
@@ -1361,8 +1433,100 @@ Public Class cHMRC
|
|||||||
' ' For more information, refer to the documentation
|
' ' For more information, refer to the documentation
|
||||||
' End Using
|
' End Using
|
||||||
'End Using
|
'End Using
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Shared Function getTOKEN(APPLICATION) As cHMRCToken
|
||||||
|
Dim TOKEN = New cHMRCToken(VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA, APPLICATION)
|
||||||
|
If TOKEN.hasEntry = False OrElse TOKEN.token_BEARER_TOKEN = "" Then
|
||||||
|
Dim AccessToken = ""
|
||||||
|
Dim appl = ""
|
||||||
|
Select Case APPLICATION
|
||||||
|
Case "GVMS"
|
||||||
|
appl = "write:goods-movement-system+read:pull-notifications"
|
||||||
|
Case "VAT_CHECK" '"EORI_CHECK"
|
||||||
|
appl = "read:vat write:vat"
|
||||||
|
End Select
|
||||||
|
grantAccess(AccessToken, appl)
|
||||||
|
TOKEN.token_BEARER_TOKEN = AccessToken
|
||||||
|
TOKEN.token_refresh_datetime = Now
|
||||||
|
TOKEN.SAVE()
|
||||||
|
Else
|
||||||
|
HMRC_RefreshToken(TOKEN)
|
||||||
|
End If
|
||||||
|
Return TOKEN
|
||||||
|
End Function
|
||||||
|
|
||||||
|
|
||||||
|
Shared Function checkEORI_UK(eoriNumber As String, ByVal response As String) As Boolean
|
||||||
|
' Chilkat-Objekt initialisieren
|
||||||
|
Dim http As New Chilkat.Http
|
||||||
|
|
||||||
|
http.SetRequestHeader("Accept", "application/x-www-form-urlencoded")
|
||||||
|
|
||||||
|
' EORI-Nummer für die Überprüfung
|
||||||
|
'Dim eoriNumber As String = "GB123456789000"
|
||||||
|
Dim url As String = "https://api.service.hmrc.gov.uk/customs/eori/lookup/check-multiple-eori"
|
||||||
|
|
||||||
|
|
||||||
|
Dim requestBody As New Chilkat.JsonObject
|
||||||
|
requestBody.AppendArray("eoris").AddStringAt(-1, eoriNumber)
|
||||||
|
requestBody.AppendArray("eoris").AddStringAt(-1, "GB8392848394939")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
' HTTP GET-Anfrage senden
|
||||||
|
Dim responseHttp = http.PostJson2(url, "application/json", requestBody.Emit())
|
||||||
|
|
||||||
|
' Fehlerprüfung
|
||||||
|
If http.LastMethodSuccess <> True Then
|
||||||
|
Console.WriteLine("Fehler: " & http.LastErrorText)
|
||||||
|
Return False
|
||||||
|
End If
|
||||||
|
response = responseHttp.BodyStr
|
||||||
|
' Antwort ausgeben
|
||||||
|
Console.WriteLine("API Antwort: " & responseHttp.BodyStr)
|
||||||
|
Return True
|
||||||
|
End Function
|
||||||
|
|
||||||
|
Shared Function checkVAT_UK(VATNumber As String, ByVal response As String) As Boolean
|
||||||
|
' Chilkat-Objekt initialisieren
|
||||||
|
Dim http As New Chilkat.Http
|
||||||
|
|
||||||
|
' Falls API-Schlüssel oder Authentifizierung erforderlich ist, hinzufügen
|
||||||
|
' Beispiel: http.SetRequestHeader("Authorization", "Bearer YOUR_ACCESS_TOKEN")
|
||||||
|
|
||||||
|
VERAG_PROG_ALLGEMEIN.cChilkat_Helper.UnlockCilkat()
|
||||||
|
|
||||||
|
Dim TOKEN = getTOKEN("VAT_CHECK")
|
||||||
|
|
||||||
|
If TOKEN.hasEntry Then
|
||||||
|
http.SetRequestHeader("Authorization", "Bearer " & accessToken)
|
||||||
|
http.SetRequestHeader("Accept", "application/x-www-form-urlencoded")
|
||||||
|
|
||||||
|
' EORI-Nummer für die Überprüfung
|
||||||
|
Dim url As String = "https://api.service.hmrc.gov.uk/organisations/vat/check-vat-number/lookup/" & VATNumber
|
||||||
|
|
||||||
|
response = http.QuickGetStr(url)
|
||||||
|
|
||||||
|
If http.LastMethodSuccess <> True Then
|
||||||
|
Console.WriteLine("Fehler: " & http.LastErrorText)
|
||||||
|
Return False
|
||||||
|
End If
|
||||||
|
|
||||||
|
' Antwort ausgeben
|
||||||
|
Console.WriteLine("API Antwort: " & response)
|
||||||
|
Return True
|
||||||
|
End If
|
||||||
|
Return False
|
||||||
|
End Function
|
||||||
|
|
||||||
Public Class RefreshTokenResultJSON
|
Public Class RefreshTokenResultJSON
|
||||||
Public Property access_token As String
|
Public Property access_token As String
|
||||||
End Class
|
End Class
|
||||||
|
|||||||
@@ -5065,7 +5065,27 @@ ELSE_ATILLA:
|
|||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
|
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
|
||||||
VERAG_PROG_ALLGEMEIN.cOpenAI.doFirstAI()
|
|
||||||
|
|
||||||
|
MsgBox(VERAG_PROG_ALLGEMEIN.cOpenAI.askAI_TruckPlate("AW: Kennzeichen: CB4178EB | Grenze SUB DE007701 Suben | Absender CEMOBSAN;TEKIRDAG | Gewicht: 7.200,0"))
|
||||||
|
MsgBox(VERAG_PROG_ALLGEMEIN.cOpenAI.askAI_TruckPlate("*00*Fwd: Abgabenbescheid 5003/25001269 T31M492"))
|
||||||
|
MsgBox(VERAG_PROG_ALLGEMEIN.cOpenAI.askAI_TruckPlate("AW: Warenbeschreibung in T1-Dokumenten - 5003/25000389"))
|
||||||
|
MsgBox(VERAG_PROG_ALLGEMEIN.cOpenAI.askAI_TruckPlate("Kennzeichen: 06CLS811 | Grenze WAI DE008904 Waidhaus | Absender VAMET | Gewicht: 1.689,0"))
|
||||||
|
MsgBox(VERAG_PROG_ALLGEMEIN.cOpenAI.askAI_TruckPlate("*00*RE: 16 ALF 807 AVIS/ GERMANY"))
|
||||||
|
|
||||||
|
|
||||||
|
Exit Sub
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Dim resp = ""
|
||||||
|
cHMRC.checkEORI_UK("GB078068385000", resp)
|
||||||
|
MsgBox(resp)
|
||||||
|
|
||||||
|
'resp = ""
|
||||||
|
'cHMRC.checkVAT_UK("389356931", resp)
|
||||||
|
'MsgBox(resp)
|
||||||
|
'VERAG_PROG_ALLGEMEIN.cOpenAI.doFirstAI()
|
||||||
'saveLukowaPDFsFromTherefore()
|
'saveLukowaPDFsFromTherefore()
|
||||||
' MsgBox(VERAG_PROG_ALLGEMEIN.cATEZ_Tariff.GetTariffInfo_SingleOrEmpty("6207220000"))
|
' MsgBox(VERAG_PROG_ALLGEMEIN.cATEZ_Tariff.GetTariffInfo_SingleOrEmpty("6207220000"))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user