846 lines
35 KiB
VB.net
846 lines
35 KiB
VB.net
Imports System.Net.Http
|
||
Imports System.Web
|
||
Imports System
|
||
Imports Microsoft.AspNetCore.Authentication.Cookies
|
||
Imports Microsoft.AspNetCore.Builder
|
||
Imports Microsoft.AspNetCore.Hosting
|
||
Imports Microsoft.AspNetCore.Http
|
||
Imports Microsoft.Extensions.Configuration
|
||
Imports Microsoft.Extensions.DependencyInjection
|
||
Imports Microsoft.Extensions.Hosting
|
||
Imports System.Collections.Specialized
|
||
Imports System.Text
|
||
Imports System.IO
|
||
|
||
'Public Class Startup
|
||
' Public Sub New(ByVal configuration As IConfiguration)
|
||
' configuration = configuration
|
||
' End Sub
|
||
|
||
' Public ReadOnly Property Configuration As IConfiguration
|
||
|
||
' Public Sub ConfigureServices(ByVal services As IServiceCollection)
|
||
' services.Configure(Configuration)
|
||
|
||
' services.Configure(Of CookiePolicyOptions)(Function(options)
|
||
' options.CheckConsentNeeded = Function(context) True
|
||
' options.MinimumSameSitePolicy = SameSiteMode.None
|
||
' End Function)
|
||
' services.AddSession(Function(options)
|
||
' options.IdleTimeout = TimeSpan.FromSeconds(10)
|
||
' options.Cookie.HttpOnly = True
|
||
' options.Cookie.IsEssential = True
|
||
' End Function)
|
||
' services.AddAuthentication(Function(options)
|
||
' options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme
|
||
' options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme
|
||
' options.DefaultChallengeScheme = "HMRC"
|
||
' End Function).AddCookie().AddOAuth("HMRC", Function(options)
|
||
' options.ClientId = Configuration("clientId")
|
||
' options.ClientSecret = Configuration("clientSecret")
|
||
' options.CallbackPath = New PathString(Configuration("oauthCallbackUri"))
|
||
' options.Scope.Add("hello")
|
||
' options.SaveTokens = True
|
||
' options.AuthorizationEndpoint = Configuration("uri") & "/oauth/authorize"
|
||
' options.TokenEndpoint = Configuration("uri") & "/oauth/token"
|
||
' End Function)
|
||
' services.AddRazorPages(Function(options)
|
||
' options.Conventions.AddPageRoute("/HelloWorld/Index", "")
|
||
' End Function)
|
||
' End Sub
|
||
|
||
' Public Sub Configure(ByVal app As IApplicationBuilder, ByVal env As IWebHostEnvironment)
|
||
' If env.IsDevelopment() Then
|
||
' app.UseDeveloperExceptionPage()
|
||
' Else
|
||
' app.UseExceptionHandler("/Error")
|
||
' app.UseHsts()
|
||
' End If
|
||
|
||
' app.UseHttpsRedirection()
|
||
' app.UseStaticFiles()
|
||
' app.UseRouting()
|
||
' app.UseAuthorization()
|
||
' app.UseAuthentication()
|
||
' app.UseSession()
|
||
' app.UseCookiePolicy()
|
||
' app.UseEndpoints(Function(endpoints)
|
||
' endpoints.MapRazorPages()
|
||
' endpoints.MapControllerRoute(name:="default", pattern:="{controller=HelloWorld}/{action=Index}")
|
||
' End Function)
|
||
' End Sub
|
||
'End Class
|
||
|
||
|
||
|
||
Public Class VATResponse
|
||
Public Class Address
|
||
Public Property line1 As String
|
||
Public Property postcode As String
|
||
Public Property countryCode As String
|
||
End Class
|
||
|
||
Public Class Target
|
||
Public Property name As String
|
||
Public Property vatNumber As String
|
||
Public Property address As Address
|
||
End Class
|
||
|
||
Public Class Root
|
||
Public Property target As Target
|
||
Public Property processingDate As DateTime
|
||
End Class
|
||
|
||
End Class
|
||
|
||
Public Class cHMRC
|
||
|
||
Shared API_STRING = "https://test-api.service.hmrc.gov.uk"
|
||
'Shared API_STRING As String = "https://api.service.hmrc.gov.uk"
|
||
|
||
Shared Sub hmrcTEST2()
|
||
'Dim rClient As RestClient = New RestClient()
|
||
'rClient.endPoint = "https://test-api.service.hmrc.gov.uk/hello/user"
|
||
'Dim strResponse As String = String.Empty
|
||
'strResponse = rClient.makeRequest(_accessToken)
|
||
|
||
Dim rest As New Chilkat.Rest
|
||
|
||
' Connect to the REST server.
|
||
Dim bTls As Boolean = True
|
||
Dim port As Integer = 443
|
||
Dim bAutoReconnect As Boolean = True
|
||
Dim success As Boolean = rest.Connect("https://test-api.service.hmrc.gov.uk", port, bTls, bAutoReconnect)
|
||
'389356931 -> VERAG-UNISPED
|
||
'Dim responseJson As String = rest.FullRequestNoBody("GET", "/organisations/vat/check-vat-number/lookup/553557881")
|
||
'Dim responseJson As String = rest.FullRequestNoBody("GET", "/organisations/vat/check-vat-number/lookup/553552881") 'falsch
|
||
Dim responseJson As String = rest.FullRequestNoBody("GET", "/organisations/vat/check-vat-number/lookup/5535532881") 'falsch
|
||
If (rest.LastMethodSuccess <> True) Then
|
||
Console.WriteLine(rest.LastErrorText)
|
||
Exit Sub
|
||
End If
|
||
|
||
Try
|
||
|
||
Dim read = Newtonsoft.Json.Linq.JObject.Parse(responseJson)
|
||
|
||
If read.Item("code") IsNot Nothing Then
|
||
Dim VAT_Message = read.Item("message").ToString
|
||
Dim VAT_code = read.Item("code").ToString
|
||
MsgBox("ERROR: " & VAT_Message, MsgBoxStyle.OkOnly, VAT_code)
|
||
Else
|
||
' Dim json As String = "{""name"":""Rap God"",""statistics"":{""likeCount"":""122255"",""dislikeCount"":""4472""}}"
|
||
Dim VAT_Name = read.Item("target")("name").ToString
|
||
Dim VAT_processingDate = read.Item("processingDate").ToString
|
||
Dim VAT_Strasse = read.Item("target")("address")("line1").ToString
|
||
Dim VAT_PLZ = read.Item("target")("address")("postcode").ToString
|
||
Dim VAT_countryCode = read.Item("target")("address")("countryCode").ToString
|
||
|
||
MsgBox(VAT_Name)
|
||
MsgBox(VAT_processingDate)
|
||
End If
|
||
Catch ex As Exception
|
||
MsgBox(ex.Message & ex.StackTrace)
|
||
End Try
|
||
|
||
|
||
'Console.WriteLine(responseJson)
|
||
'Console.WriteLine("----")
|
||
|
||
'' We can alternatively do this:
|
||
'rest.ClearAllQueryParams()
|
||
'' rest.AddQueryParam("consumer_key", "YOUR_CONSUMER_KEY")
|
||
'' rest.AddQueryParam("consumer_secret", "YOUR_CONSUMER_SECRET")
|
||
'responseJson = rest.FullRequestNoBody("GET", "/wp-json/wc/v1/products")
|
||
'If (rest.LastMethodSuccess <> True) Then
|
||
' Console.WriteLine(rest.LastErrorText)
|
||
' Exit Sub
|
||
'End If
|
||
|
||
|
||
Console.WriteLine(responseJson)
|
||
|
||
End Sub
|
||
|
||
|
||
Shared Sub hmrcTEST()
|
||
UnlockCilkat()
|
||
Dim AccessToken = ""
|
||
grantAccess(AccessToken, "write:goods-movement-system")
|
||
'getHMRC_Token(AccessToken, "write:goods-movement-system")
|
||
MsgBox(AccessToken)
|
||
If AccessToken <> "" Then
|
||
|
||
Dim Notification_Box_Id = ""
|
||
Dim Notification_Message_Id = ""
|
||
If genGVMS_GMR(AccessToken, Notification_Box_Id, Notification_Message_Id) Then
|
||
|
||
|
||
Dim AccessTokenNotTEST = ""
|
||
' getHMRC_Token(AccessTokenNotTEST, "read:pull-notifications")
|
||
grantAccess(AccessTokenNotTEST, "read:pull-notifications")
|
||
If AccessTokenNotTEST <> "" Then
|
||
getGVMS_Notifications(AccessTokenNotTEST, Notification_Box_Id)
|
||
End If
|
||
End If
|
||
End If
|
||
|
||
Exit Sub
|
||
'UnlockCilkat()
|
||
|
||
'Dim AccessTokenNotTEST = ""
|
||
'grantAccess(AccessTokenNotTEST, "read:pull-notifications")
|
||
'If AccessTokenNotTEST <> "" Then
|
||
' getGVMS_Notifications(AccessTokenNotTEST, "877b619c-33c1-4ab6-a34f-8bd7542e0596")
|
||
|
||
'End If
|
||
Exit Sub
|
||
|
||
'Dim AccessToken = ""
|
||
'grantAccess(AccessToken, "write:goods-movement-system")
|
||
'If AccessToken <> "" Then
|
||
|
||
' Dim Notification_Box_Id = ""
|
||
' Dim Notification_Message_Id = ""
|
||
' genGVMS_GMR(AccessToken, Notification_Box_Id, Notification_Message_Id)
|
||
|
||
' Dim AccessTokenNot = ""
|
||
' grantAccess(AccessTokenNot, "read:pull-notifications")
|
||
' If AccessTokenNot <> "" Then
|
||
' getGVMS_Notifications(AccessTokenNot, Notification_Box_Id)
|
||
|
||
' End If
|
||
'End If
|
||
|
||
|
||
|
||
Exit Sub
|
||
getGVMS_RefData()
|
||
|
||
Exit Sub
|
||
|
||
|
||
|
||
Exit Sub
|
||
'genGVMS_GMR("TEST")
|
||
|
||
'Exit Sub
|
||
'Dim AccessToken = ""
|
||
'grantAccess(AccessToken)
|
||
'If AccessToken <> "" Then
|
||
' genGVMS_GMR(AccessToken)
|
||
'End If
|
||
End Sub
|
||
Shared Sub grantAccess(ByRef AccessToken, scope)
|
||
|
||
UnlockCilkat()
|
||
|
||
Debug.WriteLine("HMRC Start")
|
||
Dim oauth2 As Chilkat.OAuth2 = New Chilkat.OAuth2()
|
||
Dim success As Boolean
|
||
|
||
oauth2.ListenPort = 3017
|
||
'oauth2.AuthorizationEndpoint = "https://test-api.service.hmrc.gov.uk/oauth/authorize"
|
||
'oauth2.TokenEndpoint = "https://test-api.service.hmrc.gov.uk/oauth/token"
|
||
oauth2.AuthorizationEndpoint = API_STRING & "/oauth/authorize"
|
||
oauth2.TokenEndpoint = API_STRING & "/oauth/token"
|
||
oauth2.ClientId = "xybTOMaQWcuifeW5xnGggojlACWC"
|
||
oauth2.ClientSecret = "bf1cfb6e-1bcb-4282-b7a0-3d3ccb2b1dc1"
|
||
'oauth2.Scope = "read:vat write:vat" !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||
oauth2.Scope = scope '"write:goods-movement-system"
|
||
Dim url As String = oauth2.StartAuth()
|
||
|
||
If oauth2.LastMethodSuccess <> True Then
|
||
Debug.WriteLine(oauth2.LastErrorText)
|
||
Exit Sub
|
||
End If
|
||
|
||
Dim http As Chilkat.Http = New Chilkat.Http()
|
||
|
||
' System.Diagnostics.Process.Start(url)
|
||
|
||
Dim f As New Form
|
||
f.Size = New Size(800, 1400)
|
||
Dim wb As New WebBrowser
|
||
f.Controls.Add(wb)
|
||
f.StartPosition = FormStartPosition.CenterScreen
|
||
wb.Dock = DockStyle.Fill
|
||
wb.Navigate(url)
|
||
Dim done = False
|
||
|
||
|
||
'AddHandler wb.DocumentCompleted, Sub()
|
||
' If Not done Then
|
||
|
||
' If wb.Url.AbsolutePath = "/api-test-login/sign-in" Then
|
||
' Try
|
||
' Dim txtInput As HtmlElement = wb.Document.Forms(0).GetElementsByTagName("input").GetElementsByName("userId")(0)
|
||
' txtInput.InnerText = "224015321897"
|
||
' Dim txtInputPwd As HtmlElement = wb.Document.Forms(0).GetElementsByTagName("input").GetElementsByName("password")(0)
|
||
' txtInputPwd.InnerText = "ry1cspz0nsvj"
|
||
' done = True
|
||
' Catch
|
||
' End Try
|
||
' End If
|
||
' End If
|
||
' MsgBox(wb.Url.AbsolutePath)
|
||
' End Sub
|
||
|
||
If f.ShowDialog() Then
|
||
|
||
|
||
|
||
Dim numMsWaited As Integer = 0
|
||
|
||
|
||
'While (numMsWaited < 120000) AndAlso (oauth2.AuthFlowState < 3)
|
||
' oauth2.SleepMs(100)
|
||
' numMsWaited = numMsWaited + 100
|
||
'End While
|
||
' If there was no response from the browser within 30 seconds, then
|
||
' the AuthFlowState will be equal to 1 Or 2.
|
||
' 1: Waiting for Redirect. The OAuth2 background thread Is waiting to receive the redirect HTTP request from the browser.
|
||
' 2: Waiting for Final Response. The OAuth2 background thread Is waiting for the final access token response.
|
||
' In that case, cancel the background task started in the call to StartAuth.
|
||
|
||
If oauth2.AuthFlowState < 3 Then
|
||
oauth2.Cancel()
|
||
Debug.WriteLine("No response from the browser!")
|
||
Exit Sub
|
||
End If
|
||
|
||
Debug.WriteLine("HMRC here")
|
||
|
||
|
||
' Check the AuthFlowState to see if authorization was granted, denied, Or if some error occurred
|
||
' The possible AuthFlowState values are:
|
||
' 3 Completed with Success. The OAuth2 flow has completed, the background thread exited, And the successful JSON response Is available in AccessTokenResponse property.
|
||
' 4: Completed with Access Denied. The OAuth2 flow has completed, the background thread exited, And the error JSON Is available in AccessTokenResponse property.
|
||
' 5: Failed Prior To Completion. The OAuth2 flow failed To complete, the background thread exited, And the error information Is available in the FailureInfo property.
|
||
|
||
If oauth2.AuthFlowState = 5 Then
|
||
Debug.WriteLine("OAuth2 failed to complete.")
|
||
Debug.WriteLine(oauth2.FailureInfo)
|
||
Exit Sub
|
||
End If
|
||
|
||
If oauth2.AuthFlowState = 4 Then
|
||
Debug.WriteLine("OAuth2 authorization was denied.")
|
||
Debug.WriteLine(oauth2.AccessTokenResponse)
|
||
Exit Sub
|
||
End If
|
||
|
||
If oauth2.AuthFlowState <> 3 Then
|
||
Debug.WriteLine("Unexpected AuthFlowState:" & Convert.ToString(oauth2.AuthFlowState))
|
||
Exit Sub
|
||
End If
|
||
MsgBox("OAuth2 authorization granted!")
|
||
Debug.WriteLine("OAuth2 authorization granted!")
|
||
Debug.WriteLine("Access Token = " & oauth2.AccessToken)
|
||
Dim json As Chilkat.JsonObject = New Chilkat.JsonObject()
|
||
json.Load(oauth2.AccessTokenResponse)
|
||
json.EmitCompact = False
|
||
Debug.WriteLine(json.Emit())
|
||
MsgBox(json.Emit())
|
||
|
||
' The JSON response looks Like this
|
||
|
||
' {
|
||
' "token_type": "Bearer",
|
||
' "scope": "user_impersonation",
|
||
' "expires_in": "3599",
|
||
' "ext_expires_in": "0",
|
||
' "expires_on": "1524783438",
|
||
' "not_before": "1524779538",
|
||
' "resource": "https://mydomain.api.crm.dynamics.com",
|
||
' "access_token": "...",
|
||
' "refresh_token": "...",
|
||
' "id_token": "..."
|
||
' }
|
||
|
||
' If an "expires_on" member does Not exist, then add the JSON member by
|
||
' getting the current system date/time And adding the "expires_in" seconds.
|
||
' This way we'll know when the token expires.
|
||
|
||
If json.HasMember("expires_on") <> True Then
|
||
Dim dtExpire As Chilkat.CkDateTime = New Chilkat.CkDateTime()
|
||
dtExpire.SetFromCurrentSystemTime()
|
||
dtExpire.AddSeconds(json.IntOf("expires_in"))
|
||
json.AppendString("expires_on", dtExpire.GetAsUnixTimeStr(False))
|
||
End If
|
||
|
||
Debug.WriteLine(json.Emit())
|
||
Dim fac As Chilkat.FileAccess = New Chilkat.FileAccess()
|
||
'fac.WriteEntireTextFile("qa_data/tokens/hmrc.json", json.Emit(), "utf-8", False)
|
||
fac.WriteEntireTextFile("C:\Users\DEVELOPER1\Desktop\hmrc.json", json.Emit(), "utf-8", False)
|
||
AccessToken = oauth2.AccessToken
|
||
End If
|
||
End Sub
|
||
Private Shared Sub UnlockCilkat()
|
||
Dim glob As Chilkat.[Global] = New Chilkat.[Global]()
|
||
Dim success As Boolean = glob.UnlockBundle("VERAGS.CB1122022_JJ6kgS2zoR0g")
|
||
|
||
If success <> True Then
|
||
Debug.WriteLine(glob.LastErrorText)
|
||
Return
|
||
End If
|
||
|
||
Dim status As Integer = glob.UnlockStatus
|
||
|
||
If status = 2 Then
|
||
Debug.WriteLine("Unlocked using purchased unlock code.")
|
||
Else
|
||
Debug.WriteLine("Unlocked in trial mode.")
|
||
End If
|
||
|
||
Debug.WriteLine(glob.LastErrorText)
|
||
|
||
|
||
End Sub
|
||
Shared Sub VATTEST(hmrc_app_server_token)
|
||
|
||
Dim rest As New Chilkat.Rest
|
||
Dim success As Boolean
|
||
|
||
|
||
Dim bTls As Boolean = True
|
||
Dim port As Integer = 443
|
||
Dim bAutoReconnect As Boolean = True
|
||
success = rest.Connect(API_STRING, port, bTls, bAutoReconnect)
|
||
If (success <> True) Then
|
||
Debug.WriteLine("ConnectFailReason: " & rest.ConnectFailReason)
|
||
Debug.WriteLine(rest.LastErrorText)
|
||
Exit Sub
|
||
End If
|
||
|
||
|
||
' See the Online Tool for Generating JSON Creation Code
|
||
Dim json As New Chilkat.JsonObject
|
||
json.UpdateString("periodKey", "#001")
|
||
json.UpdateNumber("vatDueSales", "100.00")
|
||
json.UpdateNumber("vatDueAcquisitions", "100.00")
|
||
json.UpdateNumber("totalVatDue", "200")
|
||
json.UpdateNumber("vatReclaimedCurrPeriod", "100.00")
|
||
json.UpdateNumber("netVatDue", "100")
|
||
json.UpdateNumber("totalValueSalesExVAT", "500")
|
||
json.UpdateNumber("totalValuePurchasesExVAT", "500")
|
||
json.UpdateNumber("totalValueGoodsSuppliedExVAT", "500")
|
||
json.UpdateNumber("totalAcquisitionsExVAT", "500")
|
||
json.UpdateBool("finalised", True)
|
||
|
||
|
||
rest.AddHeader("Content-Type", "application/json")
|
||
rest.AddHeader("Authorization", "Bearer " & hmrc_app_server_token)
|
||
rest.AddHeader("Accept", "application/vnd.hmrc.1.0+json")
|
||
|
||
Dim sbRequestBody As New Chilkat.StringBuilder
|
||
json.EmitSb(sbRequestBody)
|
||
Dim sbResponseBody As New Chilkat.StringBuilder
|
||
success = rest.FullRequestSb("POST", "/organisations/vat/123456789/returns", sbRequestBody, sbResponseBody)
|
||
If (success <> True) Then
|
||
Debug.WriteLine(rest.LastErrorText)
|
||
Exit Sub
|
||
End If
|
||
|
||
Dim respStatusCode As Integer = rest.ResponseStatusCode
|
||
If (respStatusCode >= 400) Then
|
||
Debug.WriteLine("Response Status Code = " & respStatusCode)
|
||
Debug.WriteLine("Response Header:")
|
||
Debug.WriteLine(rest.ResponseHeader)
|
||
Debug.WriteLine("Response Body:")
|
||
Debug.WriteLine(sbResponseBody.GetAsString())
|
||
Exit Sub
|
||
End If
|
||
|
||
|
||
End Sub
|
||
|
||
Shared Sub getGVMS_RefData()
|
||
|
||
Dim rest As New Chilkat.Rest
|
||
Dim success As Boolean
|
||
|
||
Dim bTls As Boolean = True
|
||
Dim port As Integer = 443
|
||
Dim bAutoReconnect As Boolean = True
|
||
|
||
success = rest.Connect(API_STRING, port, bTls, bAutoReconnect)
|
||
If (success <> True) Then
|
||
Debug.WriteLine("ConnectFailReason: " & rest.ConnectFailReason)
|
||
Debug.WriteLine(rest.LastErrorText)
|
||
|
||
Exit Sub
|
||
End If
|
||
|
||
Dim json As New Chilkat.JsonObject
|
||
|
||
'rest.AddHeader("Content-Type", "application/json")
|
||
'rest.AddHeader("Authorization", "Bearer " & hmrc_app_server_token)
|
||
rest.AddHeader("Accept", "application/vnd.hmrc.1.0+json")
|
||
|
||
Dim sbRequestBody As New Chilkat.StringBuilder
|
||
json.EmitSb(sbRequestBody)
|
||
|
||
MsgBox("BODY: " & sbRequestBody.ToString)
|
||
|
||
Dim sbResponseBody As New Chilkat.StringBuilder
|
||
success = rest.FullRequestSb("GET", "/customs/goods-movement-system/reference-data", sbRequestBody, sbResponseBody)
|
||
MsgBox("1")
|
||
If (success <> True) Then
|
||
Debug.WriteLine(rest.LastErrorText)
|
||
MsgBox(rest.LastErrorText)
|
||
Exit Sub
|
||
End If
|
||
Dim respStatusCode As Integer = rest.ResponseStatusCode
|
||
MsgBox("NO:" & respStatusCode)
|
||
If (respStatusCode >= 200) Then
|
||
MsgBox(sbResponseBody.GetAsString())
|
||
|
||
IO.File.WriteAllText("C:\Users\DEVELOPER1\Desktop\hmrc4.json", sbResponseBody.GetAsString())
|
||
|
||
|
||
|
||
Debug.WriteLine("Response Status Code = " & respStatusCode)
|
||
Debug.WriteLine("Response Header:")
|
||
Debug.WriteLine(rest.ResponseHeader)
|
||
Debug.WriteLine("Response Body:")
|
||
Debug.WriteLine(sbResponseBody.GetAsString())
|
||
MsgBox("Response Body:")
|
||
MsgBox(sbResponseBody.GetAsString())
|
||
Exit Sub
|
||
End If
|
||
|
||
|
||
End Sub
|
||
Shared Function genGVMS_GMR(hmrc_app_server_token As String, Notification_Box_Id As String, Notification_Message_Id As String) As Boolean
|
||
|
||
Dim rest As New Chilkat.Rest
|
||
Dim success As Boolean
|
||
|
||
|
||
Dim bTls As Boolean = True
|
||
Dim port As Integer = 443
|
||
Dim bAutoReconnect As Boolean = True
|
||
success = rest.Connect(API_STRING, port, bTls, bAutoReconnect)
|
||
If (success <> True) Then
|
||
Debug.WriteLine("ConnectFailReason: " & rest.ConnectFailReason)
|
||
Debug.WriteLine(rest.LastErrorText)
|
||
MsgBox(rest.LastErrorText)
|
||
Return False
|
||
End If
|
||
|
||
'"UK_INBOUND",
|
||
' "UK_OUTBOUND",
|
||
' "GB_TO_NI",
|
||
' "NI_TO_GB"
|
||
|
||
' See the Online Tool for Generating JSON Creation Code
|
||
Dim json As New Chilkat.JsonObject
|
||
json.UpdateString("direction", "UK_INBOUND")
|
||
json.UpdateBool("isUnaccompanied", False)
|
||
json.UpdateString("vehicleRegNum", "SD123TEST") 'LKW
|
||
Dim array As New Chilkat.JsonObject
|
||
' json.UpdateNewArray(array) 'Empty array
|
||
|
||
|
||
json.UpdateString("plannedCrossing.routeId", "20") '--> Calais - Dover - DFDS
|
||
json.UpdateString("plannedCrossing.localDateTimeOfDeparture", "2021-08-11T10:58")
|
||
|
||
Dim cnt = 0
|
||
|
||
json.UpdateString("customsDeclarations[" & cnt & "].customsDeclarationId", "0GB689223596000-SE119404")
|
||
json.UpdateString("customsDeclarations[" & cnt & "].sAndSMasterRefNum", "20GB01I0XLM976S001")
|
||
|
||
cnt = 0
|
||
|
||
'json.UpdateString("transitDeclarations[" & cnt & "].transitDeclarationId", "10GB00002910B75BE5")
|
||
''json.UpdateString("transitDeclarations[" & cnt & "].sAndSMasterRefNum", "20GB01I0XLM976S004")
|
||
'json.UpdateBool("transitDeclarations[" & cnt & "].isTSAD", True)
|
||
|
||
'Dim o As New Chilkat.JsonObject
|
||
|
||
' json.UpdateNewObject("vehicleRegNum", o) '"SD 123TEST") 'LKW
|
||
|
||
'plannedCrossing
|
||
'json.UpdateNumber("vatDueSales", "100.00")
|
||
'json.UpdateNumber("vatDueAcquisitions", "100.00")
|
||
'json.UpdateNumber("totalVatDue", "200")
|
||
'json.UpdateNumber("vatReclaimedCurrPeriod", "100.00")
|
||
'json.UpdateNumber("netVatDue", "100")
|
||
'json.UpdateNumber("totalValueSalesExVAT", "500")
|
||
'json.UpdateNumber("totalValuePurchasesExVAT", "500")
|
||
'json.UpdateNumber("totalValueGoodsSuppliedExVAT", "500")
|
||
'json.UpdateNumber("totalAcquisitionsExVAT", "500")
|
||
'json.UpdateBool("finalised", True)
|
||
|
||
|
||
rest.AddHeader("Content-Type", "application/json")
|
||
rest.AddHeader("Authorization", "Bearer " & hmrc_app_server_token)
|
||
rest.AddHeader("Accept", "application/vnd.hmrc.1.0+json")
|
||
|
||
Dim sbRequestBody As New Chilkat.StringBuilder
|
||
json.EmitSb(sbRequestBody)
|
||
|
||
' MsgBox(sbRequestBody.ToString)
|
||
|
||
Dim sbResponseBody As New Chilkat.StringBuilder
|
||
success = rest.FullRequestSb("POST", "/customs/goods-movement-system/movements", sbRequestBody, sbResponseBody)
|
||
If (success <> True) Then
|
||
Debug.WriteLine(rest.LastErrorText)
|
||
Return False
|
||
End If
|
||
Dim respStatusCode As Integer = rest.ResponseStatusCode
|
||
|
||
'MsgBox(respStatusCode)
|
||
|
||
If (respStatusCode >= 200) Then
|
||
|
||
|
||
' MsgBox(sbResponseBody.GetAsString())
|
||
|
||
|
||
'Dim res = Chilkat.HttpResponse()
|
||
'res.GetHeaderField
|
||
'rest.ResponseHeader.
|
||
' "Notification_Box_Id"
|
||
IO.File.WriteAllText("C:\Users\DEVELOPER1\Desktop\hmrc5.json", rest.ResponseHeader.ToString())
|
||
' MsgBox(rest.ResponseHeader.ToString())
|
||
|
||
' MsgBox(sbResponseBody.GetAsString())
|
||
For Each rh In rest.ResponseHeader.Split(vbNewLine)
|
||
If (rh.Contains("Notification-Box-Id: ")) Then
|
||
Notification_Box_Id = rh.Replace("Notification-Box-Id: ", "")
|
||
MsgBox(Notification_Box_Id)
|
||
End If
|
||
If (rh.Contains("Notification-Message-Id: ")) Then
|
||
Notification_Message_Id = rh.Replace("Notification-Message-Id: ", "")
|
||
MsgBox(Notification_Message_Id)
|
||
End If
|
||
Next
|
||
Debug.WriteLine("Response Status Code = " & respStatusCode)
|
||
Debug.WriteLine("Response Header:")
|
||
Debug.WriteLine(rest.ResponseHeader)
|
||
Debug.WriteLine("Response Body:")
|
||
Debug.WriteLine(sbResponseBody.GetAsString())
|
||
Return True
|
||
End If
|
||
Return False
|
||
|
||
End Function
|
||
|
||
|
||
Shared Sub getGVMS_Notifications(hmrc_app_server_token As String, Notification_Box_Id As String)
|
||
|
||
Dim rest As New Chilkat.Rest
|
||
Dim success As Boolean
|
||
|
||
Dim bTls As Boolean = True
|
||
Dim port As Integer = 443
|
||
Dim bAutoReconnect As Boolean = True
|
||
success = rest.Connect(API_STRING, port, bTls, bAutoReconnect)
|
||
If (success <> True) Then
|
||
Debug.WriteLine("ConnectFailReason: " & rest.ConnectFailReason)
|
||
Debug.WriteLine(rest.LastErrorText)
|
||
|
||
Exit Sub
|
||
End If
|
||
|
||
'"UK_INBOUND",
|
||
' "UK_OUTBOUND",
|
||
' "GB_TO_NI",
|
||
' "NI_TO_GB"
|
||
|
||
' See the Online Tool for Generating JSON Creation Code
|
||
Dim json As New Chilkat.JsonObject
|
||
' json.UpdateString("boxId", Notification_Box_Id)
|
||
json.UpdateString("status", "PENDING")
|
||
json.UpdateString("fromDate", "2020-06-03T14:20:54.987")
|
||
json.UpdateString("toDate", "2020-06-03T14:20:54.987")
|
||
|
||
|
||
rest.AddHeader("Content-Type", "application/json")
|
||
rest.AddHeader("Authorization", "Bearer " & hmrc_app_server_token)
|
||
rest.AddHeader("Accept", "application/vnd.hmrc.1.0+json")
|
||
|
||
|
||
Dim sbRequestBody As New Chilkat.StringBuilder
|
||
json.EmitSb(sbRequestBody)
|
||
|
||
MsgBox(sbRequestBody.ToString)
|
||
|
||
Dim sbResponseBody As New Chilkat.StringBuilder
|
||
success = rest.FullRequestSb("GET", "/misc/push-pull-notification/box/" & Notification_Box_Id & "/notifications", sbRequestBody, sbResponseBody)
|
||
If (success <> True) Then
|
||
Debug.WriteLine(rest.LastErrorText)
|
||
Exit Sub
|
||
End If
|
||
Dim respStatusCode As Integer = rest.ResponseStatusCode
|
||
|
||
MsgBox(respStatusCode)
|
||
|
||
If (respStatusCode >= 200) Then
|
||
IO.File.WriteAllText("C:\Users\DEVELOPER1\Desktop\hmrc3.json", rest.ResponseHeader.ToString())
|
||
MsgBox(rest.ResponseHeader.ToString())
|
||
|
||
MsgBox(sbResponseBody.GetAsString())
|
||
|
||
Debug.WriteLine("Response Status Code = " & respStatusCode)
|
||
Debug.WriteLine("Response Header:")
|
||
Debug.WriteLine(rest.ResponseHeader)
|
||
Debug.WriteLine("Response Body:")
|
||
Debug.WriteLine(sbResponseBody.GetAsString())
|
||
Exit Sub
|
||
End If
|
||
|
||
|
||
End Sub
|
||
|
||
|
||
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
|
||
'''
|
||
'Shared Sub testToken2()
|
||
' Dim refresh_token As String
|
||
' Dim token As String
|
||
|
||
' ' construct the request for access token
|
||
' Dim client = New RestClient(API_STRING & "/oauth/token")
|
||
' Dim request = New RestRequest("dfe/application-info", Method.POST)
|
||
' request.AddHeader("content-type", "application/x-www-form-urlencoded")
|
||
' request.AddParameter("grant_type", "client_credentials")
|
||
' request.AddParameter("client_id", "<Paste-your-client-id-here>")
|
||
' request.AddParameter("client_secret", "<Paste-your-client-secret-here>")
|
||
|
||
' ' make call to get token
|
||
' Dim response As IRestResponse = client.Execute(request)
|
||
|
||
' ' Extract token
|
||
' token = JObject.Parse(response.Content).SelectToken("$..access_token").ToString()
|
||
' refresh_token = JObject.Parse(response.Content).SelectToken("$..refresh_token").ToString()
|
||
'End Sub
|
||
Shared Sub getHMRC_Token(ByRef AccessToken, scope)
|
||
'oauth2.ListenPort = 3017
|
||
''oauth2.AuthorizationEndpoint = "https://test-api.service.hmrc.gov.uk/oauth/authorize"
|
||
''oauth2.TokenEndpoint = "https://test-api.service.hmrc.gov.uk/oauth/token"
|
||
'oauth2.AuthorizationEndpoint = API_STRING & "/oauth/authorize"
|
||
'oauth2.TokenEndpoint = API_STRING & "/oauth/token"
|
||
'oauth2.ClientId = "xybTOMaQWcuifeW5xnGggojlACWC"
|
||
'oauth2.ClientSecret = "bf1cfb6e-1bcb-4282-b7a0-3d3ccb2b1dc1"
|
||
''oauth2.Scope = "read:vat write:vat" !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||
'oauth2.Scope = scope '"write:goods-movement-system"
|
||
'Dim url As String = oauth2.StartAuth()
|
||
|
||
|
||
' Refer to the documentation for more information on how to get the client id/secret
|
||
Dim clientId As String = "xybTOMaQWcuifeW5xnGggojlACWC"
|
||
Dim clientSecret As String = "bf1cfb6e-1bcb-4282-b7a0-3d3ccb2b1dc1"
|
||
' Refer to the documentation for more information on how to get the tokens
|
||
'Dim accessToken As String = "" '"OaOXXXXTaSucp8XXcgXXH"
|
||
' Dim refreshToken As String = "kE4HXXXXXXXhxvtUHlboSF"
|
||
|
||
' -- Refresh the access token
|
||
Dim request As System.Net.WebRequest = System.Net.HttpWebRequest.Create(API_STRING & "/oauth/token")
|
||
'oauth2.AuthorizationEndpoint = API_STRING & "/oauth/authorize"
|
||
' oauth2.TokenEndpoint = API_STRING & "/oauth/token"
|
||
request.Method = "POST"
|
||
request.ContentType = "application/x-www-form-urlencoded"
|
||
'request.ContentType = "application/json"
|
||
|
||
Dim outgoingQueryString As NameValueCollection = HttpUtility.ParseQueryString(String.Empty)
|
||
outgoingQueryString.Add("grant_type", "client_credentials")
|
||
' outgoingQueryString.Add("refresh_token", refreshToken)
|
||
outgoingQueryString.Add("client_id", clientId)
|
||
outgoingQueryString.Add("client_secret", clientSecret)
|
||
outgoingQueryString.Add("scope", scope) '"write:goods-movement-system")
|
||
Dim postBytes As Byte() = New ASCIIEncoding().GetBytes(outgoingQueryString.ToString())
|
||
|
||
Dim postStream As Stream = request.GetRequestStream()
|
||
postStream.Write(postBytes, 0, postBytes.Length)
|
||
postStream.Flush()
|
||
postStream.Close()
|
||
Try
|
||
|
||
Using response As System.Net.WebResponse = request.GetResponse()
|
||
Using streamReader As System.IO.StreamReader = New System.IO.StreamReader(response.GetResponseStream())
|
||
' Parse the JSON the way you prefer
|
||
Dim jsonResult As RefreshTokenResultJSON = New System.Web.Script.Serialization.JavaScriptSerializer().Deserialize(streamReader.ReadToEnd(), GetType(RefreshTokenResultJSON))
|
||
AccessToken = jsonResult.access_token
|
||
|
||
|
||
' For more information, refer to the documentation
|
||
End Using
|
||
End Using
|
||
|
||
Catch ex As Exception
|
||
MsgBox(ex.Message & ex.StackTrace)
|
||
End Try
|
||
'' -- Get current user profile
|
||
'request = System.Net.HttpWebRequest.Create("https://api.clicdata.com/profile/user")
|
||
'request.Method = "GET"
|
||
'request.Headers.Add("Authorization", "Bearer " & accessToken)
|
||
'Using response As System.Net.WebResponse = request.GetResponse()
|
||
' Using streamReader As System.IO.StreamReader = New System.IO.StreamReader(response.GetResponseStream())
|
||
' Dim jsonResponseText = streamReader.ReadToEnd()
|
||
' ' Parse the JSON the way you prefer
|
||
' ' In this example the JSON will be something Like:
|
||
' ' {"email_address" "john.smith@clicdata.com", "first_name": "John", "last_login_date": "2016−04−26T14:24:58+00:00", "last_name": "Smith", ...
|
||
' ' For more information, refer to the documentation
|
||
' End Using
|
||
'End Using
|
||
|
||
'' -- List the current data
|
||
'request = System.Net.HttpWebRequest.Create("https://api.clicdata.com/data/")
|
||
'request.Method = "GET"
|
||
'request.Headers.Add("Authorization", "Bearer " & accessToken)
|
||
'Using response As System.Net.WebResponse = request.GetResponse()
|
||
' Using streamReader As System.IO.StreamReader = New System.IO.StreamReader(response.GetResponseStream())
|
||
' Dim jsonResponseText = streamReader.ReadToEnd()
|
||
' ' Parse the JSON the way you prefer
|
||
' ' In this example the JSON will be something Like:
|
||
' ' { "count" 265, "data": [{ "description": "","source": "merge", "creation_date": "2015−06−10T12:47:32+02:00", "last_data_update_date": "2015−06−10T12:47:49+02:00", ...
|
||
' ' For more information, refer to the documentation
|
||
' End Using
|
||
'End Using
|
||
|
||
'' -- Create some new data
|
||
'Dim dataId As Integer = 0
|
||
'request = System.Net.HttpWebRequest.Create("https://api.clicdata.com/data/")
|
||
'request.Method = "POST"
|
||
'request.ContentType = "application/json"
|
||
'request.Headers.Add("Authorization", "Bearer " & accessToken)
|
||
'Using tStreamWriter As System.IO.StreamWriter = New System.IO.StreamWriter(request.GetRequestStream())
|
||
' tStreamWriter.Write(New System.Web.Script.Serialization.JavaScriptSerializer().Serialize(New With {Key .name = "my test data " & Guid.NewGuid().ToString(), Key .description = "some description for my test data", Key .columns = {New With {Key .name = "column 1", Key .data_type = "text"}, New With {Key .name = "column 2", Key .data_type = "number"}}}))
|
||
'End Using
|
||
|
||
'Using response As System.Net.WebResponse = request.GetResponse()
|
||
' Using streamReader As System.IO.StreamReader = New System.IO.StreamReader(response.GetResponseStream())
|
||
' Dim jsonResponseText = streamReader.ReadToEnd()
|
||
' ' The JSON returned in this example is the ID of our newly created data
|
||
' dataId = Integer.Parse(jsonResponseText)
|
||
' ' For more information, refer to the documentation
|
||
' End Using
|
||
'End Using
|
||
|
||
'' -- Add a few rows of data
|
||
'request = System.Net.HttpWebRequest.Create("https://api.clicdata.com/data/" & dataId & "/rows")
|
||
'request.Method = "POST"
|
||
'request.ContentType = "application/json"
|
||
'request.Headers.Add("Authorization", "Bearer " & accessToken)
|
||
'Using tStreamWriter As System.IO.StreamWriter = New System.IO.StreamWriter(request.GetRequestStream())
|
||
' tStreamWriter.Write(New System.Web.Script.Serialization.JavaScriptSerializer().Serialize(New With {Key .data = New Object() {New Object() {"one", 1}, New Object() {"two", 2}}}))
|
||
'End Using
|
||
|
||
'Using response As System.Net.WebResponse = request.GetResponse()
|
||
' Using streamReader As System.IO.StreamReader = New System.IO.StreamReader(response.GetResponseStream())
|
||
' Dim jsonResponseText = streamReader.ReadToEnd()
|
||
' ' The JSON returned in this example tells us if the rows were inserted successfully:
|
||
' ' { "success" true }
|
||
' ' For more information, refer to the documentation
|
||
' End Using
|
||
'End Using
|
||
End Sub
|
||
|
||
Public Class RefreshTokenResultJSON
|
||
Public Property access_token As String
|
||
End Class
|
||
End Class
|