Imports System.IO Imports System.Net Imports System.Text Imports System.Web.UI.WebControls Imports System.Windows.Forms Imports Chilkat Imports Microsoft.Office.Interop.Outlook Imports Newtonsoft.Json Imports Spire.Pdf.Lists Imports VERAG_PROG_ALLGEMEIN.IDEV_Intrastat Imports VERAG_PROG_ALLGEMEIN.TESTJSON Public Class cCreditSafeAPI 'Test 'Shared API_STRING As String = "https://connect.sandbox.creditsafe.com" 'PROD Shared API_STRING As String = "https://connect.creditsafe.com" Shared token As String = "" Public dataTable As New DataTable() Shared Function SendRequestAuthentificationToken(myuri As String, user As CreditSafeUser, contentType As String, method As String) As String Try VERAG_PROG_ALLGEMEIN.cChilkat_Helper.UnlockCilkat() 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) Return rest.LastErrorText End If Dim json As New Chilkat.JsonObject json.UpdateString("username", user.username) json.UpdateString("password", user.password) rest.AddHeader("Content-Type", contentType) Dim sbRequestBody As New Chilkat.StringBuilder json.EmitSb(sbRequestBody) Dim sbResponseBody As New Chilkat.StringBuilder Dim ResponseStr = rest.FullRequestSb(method, myuri, sbRequestBody, sbResponseBody) If (rest.ResponseStatusCode <> 200) Then Return rest.ResponseStatusCode End If Dim jsonResp = New Chilkat.JsonObject() jsonResp.LoadSb(sbResponseBody) token = jsonResp.StringOf("token") Return rest.ResponseStatusCode Catch ex As WebException VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name) End Try End Function Shared Function SendGetRequestWithAuthHeader(url As String, company As Company, acceptContentType As String, method As String, authenticationToken As String) As String Try VERAG_PROG_ALLGEMEIN.cChilkat_Helper.UnlockCilkat() 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) End If rest.ClearAllQueryParams() If company IsNot Nothing Then setSearchParam(rest, company) End If rest.AddHeader("Content-Type", "application/json") rest.AddHeader("Authorization", "Bearer " & authenticationToken) rest.AddHeader("Accept", acceptContentType) Dim responseJson As String = rest.FullRequestNoBody(method, url) If (rest.LastMethodSuccess <> True) Then Debug.WriteLine(rest.LastErrorText) MsgBox(rest.LastErrorText) End If Return responseJson Catch ex As WebException VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name) End Try End Function Shared Function authenticate(username As String, passwort As String) As String Dim myUri As String = API_STRING & "/v1/authenticate" Dim csUser = New CreditSafeUser(username, passwort) Dim response = SendRequestAuthentificationToken(myUri, csUser, "application/json", "POST") Return response End Function Shared Function searchCompanies(company As Company, ByRef dataTable As DataTable) As String VERAG_PROG_ALLGEMEIN.cChilkat_Helper.UnlockCilkat() Dim myUrl As String = API_STRING & "/v1/companies" Dim jsonRespString = SendGetRequestWithAuthHeader(myUrl, company, "application/json", "GET", token) Dim json As New Chilkat.JsonObject Dim success As Boolean = json.Load(jsonRespString) If (success <> True) Then Debug.WriteLine(json.LastErrorText) Return "Verbindungsfehler" End If Dim num As Integer = json.SizeOfArray("companies") If num = 0 Then Return json.StringOf("messages[0].text") End If Dim companies As Chilkat.JsonArray = json.ArrayOf("companies") If (json.LastMethodSuccess = False) Then Return "companies member not found." End If Dim numCompanies As Integer = companies.Size Dim j As Integer = 0 While j < numCompanies Dim compObj As Chilkat.JsonObject = companies.ObjectAt(j) Dim index = companies.FindString("address", False) Dim adressObj As Chilkat.JsonObject = compObj.ObjectOf("address") Dim dateTime As New Chilkat.CkDateTime Dim dt As New Chilkat.DtObj Dim getAsLocal As Boolean = False success = compObj.DateOf("dateOfLatestChange", dateTime) Debug.WriteLine(dateTime.GetAsTimestamp(getAsLocal)) 'Debug.WriteLine(adressObj.StringOf("simpleValue")) 'Debug.WriteLine(compObj.StringOf("id") & " " & compObj.StringOf("phoneNo") & " " & compObj.StringOf("phoneNumbers[0]")) Dim R As DataRow = dataTable.NewRow R("id") = compObj.StringOf("id") R("name") = compObj.StringOf("name") R("country") = compObj.StringOf("country") R("safeNo") = compObj.StringOf("safeNo") R("vatNo") = compObj.StringOf("vatNo[0]") R("regNo") = compObj.StringOf("regNo") R("street") = adressObj.StringOf("street") R("city") = adressObj.StringOf("city") R("postCode") = adressObj.StringOf("postCode") R("status") = compObj.StringOf("status") R("dateOfLatestChange") = dateTime.GetAsTimestamp(getAsLocal) R("phoneNo") = compObj.StringOf("phoneNumbers[0]") dataTable.Rows.Add(R) j = j + 1 End While Return "Anzahl gefundener Datensätze: " & numCompanies End Function Shared Function getReport(ByRef company As Company, withPDF As Boolean, ByRef bytes As Byte()) As String Dim myUrl As String = API_STRING & "/v1/companies/" & company.creditSafeId & "/" If company.country = "DE" Then 'Abfragen für DE benötigen einen Reason-Code myUrl &= "?customData=de_reason_code::2" myUrl &= "&?language=DE&?template=full" Else myUrl &= "?language=DE&?template=full" End If Dim acceptContentType = "application/json" If withPDF Then acceptContentType &= "+pdf" End If Dim jsonRespString = SendGetRequestWithAuthHeader(myUrl, Nothing, acceptContentType, "GET", token) Dim json As New Chilkat.JsonObject Dim success As Boolean = json.Load(jsonRespString) If (success <> True) Then Debug.WriteLine(json.LastErrorText) Return "Verbindungsfehler" End If 'Aubbau JSON Object ' "report" { ' "companyIdentification" : { ' "basicInformation" : { ' "companyRegistrationDate" : "01.01.2022", ' "creditScore" : { ' "currentCreditRating" : { ' "commonValue" : "A", ' "providerValue" : { ' "value" : "350", '....} ' "pdfReportStream": "base64" '}, Dim reportObj As Chilkat.JsonObject = json.ObjectOf("report") If (json.LastMethodSuccess = True) Then Dim companyIDObj As Chilkat.JsonObject = reportObj.ObjectOf("companyIdentification") If (reportObj.LastMethodSuccess = True) Then Dim basicInfoObj As Chilkat.JsonObject = companyIDObj.ObjectOf("basicInformation") If (companyIDObj.LastMethodSuccess = True) Then Dim dateTime As New Chilkat.CkDateTime Dim getAsLocal As Boolean = False basicInfoObj.DateOf("companyRegistrationDate", dateTime) company.csDFoundingDate = dateTime.GetAsTimestamp(getAsLocal) Debug.WriteLine(dateTime) Else Debug.WriteLine("basicInfoObj object not found.") End If Else Debug.WriteLine("companyIDObj object not found.") End If Dim creditScoreObj As Chilkat.JsonObject = reportObj.ObjectOf("creditScore") If (reportObj.LastMethodSuccess = True) Then Dim creditRatingObj As Chilkat.JsonObject = creditScoreObj.ObjectOf("currentCreditRating") If (creditScoreObj.LastMethodSuccess = True) Then company.csRiskclass = creditRatingObj.StringOf("commonValue") Dim creditLimitObj As Chilkat.JsonObject = creditRatingObj.ObjectOf("creditLimit") If (creditRatingObj.LastMethodSuccess = True) Then company.csMaxCreditAmount = creditLimitObj.StringOf("value") Else Debug.WriteLine("creditRating object not found.") End If Dim providerValueObj As Chilkat.JsonObject = creditRatingObj.ObjectOf("providerValue") If (creditRatingObj.LastMethodSuccess = True) Then company.csIndex = providerValueObj.StringOf("value") Else Debug.WriteLine("providerValue object not found.") End If Else Debug.WriteLine("currentCreditRating object not found.") End If Else Debug.WriteLine("creditScore object not found.") Return "creditScore object not found" End If Else company.csFailure = json.StringOf("details") End If If withPDF Then Dim sb As New Chilkat.StringBuilder json.StringOfSb("pdfReportStream", sb) Dim bd As New Chilkat.BinData bd.AppendEncodedSb(sb, "base64") bytes = Convert.FromBase64String(sb.ToString) Return "mitPDF" End If Return "ohnePDF" End Function Shared Sub setSearchParam(ByRef rest As Chilkat.Rest, ByRef company As Company) rest.AddQueryParam("countries", company.country) If company.creditsafeNo <> "" Then rest.AddQueryParam("safeNo", company.creditsafeNo) Else If company.vatNo <> "" Then rest.AddQueryParam("vatNo", company.vatNo) Else If company.name <> "" Then rest.AddQueryParam("name", company.name) End If End If End If End Sub Shared Function checkDateOfLastRequest(company As Company) As Date Dim myUrl As String = API_STRING & "/v1/companies" Dim jsonRespString = SendGetRequestWithAuthHeader(myUrl, company, "application/json", "GET", token) Dim json As New Chilkat.JsonObject Dim success As Boolean = json.Load(jsonRespString) If (success <> True) Then Debug.WriteLine(json.LastErrorText) Return "Verbindungsfehler" End If Dim companies As Chilkat.JsonArray = json.ArrayOf("companies") Dim compObj As Chilkat.JsonObject = companies.ObjectAt(0) Dim dateTime As New Chilkat.CkDateTime Dim dt As New Chilkat.DtObj Dim getAsLocal As Boolean = False success = compObj.DateOf("dateOfLatestChange", dateTime) Debug.WriteLine(dateTime.GetAsTimestamp(getAsLocal)) Return dateTime.GetAsTimestamp(getAsLocal) End Function Public Class CreditSafeUser Public Property username As String Public Property password As String Public Property token As String Public Sub New(_username As String, _password As String) username = _username password = _password End Sub Public Sub New(_username As String, _password As String, _token As String) username = _username password = _password End Sub End Class Public Class Company Public Property creditSafeId As String Public Property name As String Public Property vatNo As String Public Property country As String Public Property creditsafeNo As String Public Property lastChecked As Date Public Property csIndex As String Public Property csRiskclass As String Public Property csMaxCreditAmount As String Public Property csDFoundingDate As Date Public Property csPDF As String Public Property csFailure As String Public Sub New(_name As String, _vatNo As String, _country As String, _creditsafeNo As String, _creditSafeId As String, _lastChecked As Date) creditSafeId = _creditSafeId name = _name vatNo = _vatNo country = _country creditsafeNo = _creditsafeNo lastChecked = _lastChecked End Sub Public Sub New() End Sub End Class End Class