Imports System.Diagnostics.Eventing.Reader 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 Dim pdfData As New Chilkat.BinData If acceptContentType.Contains("application/pdf") Then responseJson = rest.FullRequestNoBodyBd(method, url, pdfData) If (rest.LastMethodSuccess <> True) Then Debug.WriteLine(rest.LastErrorText) MsgBox(rest.LastErrorText) Else Return pdfData.ToString End If Else responseJson = rest.FullRequestNoBody(method, url) If (rest.LastMethodSuccess <> True) Then Debug.WriteLine(rest.LastErrorText) MsgBox(rest.LastErrorText) Else Return responseJson End If End If 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 For i = 0 To 1 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("status") = compObj.StringOf("status") R("dateOfLatestChange") = dateTime.GetAsTimestamp(getAsLocal) R("phoneNo") = compObj.StringOf("phoneNumbers[0]") If adressObj IsNot Nothing Then R("street") = adressObj.StringOf("street") R("city") = adressObj.StringOf("city") R("street") = adressObj.StringOf("street") R("postCode") = adressObj.StringOf("postCode") Else R("street") = "" R("city") = "" R("street") = "" R("postCode") = "" End If 'Zuerst aktive Firmen, dann inaktive.. Select Case i Case 0 If R("status").ToString.ToLower = "active" Then dataTable.Rows.Add(R) End If Case Else If R("status").ToString.ToLower <> "active" Then dataTable.Rows.Add(R) End If End Select j = j + 1 End While Next Return "Anzahl gefundener Datensätze: " & numCompanies End Function Shared Function getReport(ByRef company As Company, withPDF As Boolean) As String '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 If company.country = "DE" Then ' nur bei DE ausführen! Dim companyAddInformObj As Chilkat.JsonObject = reportObj.ObjectOf("additionalInformation") If (reportObj.LastMethodSuccess = True) Then Dim companyMiscObj As Chilkat.JsonObject = companyAddInformObj.ObjectOf("misc") If (companyAddInformObj.LastMethodSuccess = True) Then Dim deCurrentRatingObj As Chilkat.JsonObject = companyMiscObj.ObjectOf("deCurrentRating") If (companyAddInformObj.LastMethodSuccess = True) Then If (deCurrentRatingObj.LastMethodSuccess = True) Then company.csIndex = deCurrentRatingObj.StringOf("value") Else Debug.WriteLine("deCurrentRatingObj object not found.") End If Debug.WriteLine("companyMiscObj object not found.") End If Debug.WriteLine("companyAddInformObj object not found.") End If Debug.WriteLine("reportObj object not found.") End If End If 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.csScore = 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 Return "ohnePDF" End Function Shared Function getPDF(ByRef company As Company) As Byte() 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/pdf" Dim jsonRespString = SendGetRequestWithAuthHeader(myUrl, Nothing, acceptContentType, "GET", token) If jsonRespString IsNot Nothing Then Return Convert.FromBase64String(jsonRespString) End If End Function Shared Sub setSearchParam(ByRef rest As Chilkat.Rest, ByRef company As Company) rest.AddQueryParam("countries", company.country) If company.creditsafeNo <> "" Then 'Eindeutiger Schlüssel 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) If company.Street <> "" Then rest.AddQueryParam("street", company.Street) If company.Postalcode <> "" Then rest.AddQueryParam("postCode", company.Postalcode) If company.City <> "" Then rest.AddQueryParam("city", company.City) 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 New Date() End If Dim companies As Chilkat.JsonArray = json.ArrayOf("companies") If companies IsNot Nothing Then 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) Else Return New Date("01.01.1900") End If 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 City As String Public Property Postalcode As String Public Property Street As String Public Property csIndex As String Public Property csScore 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, _street As String, _postalCode As String, _city As String) creditSafeId = _creditSafeId name = _name vatNo = _vatNo country = _country creditsafeNo = _creditsafeNo lastChecked = _lastChecked Street = _street Postalcode = _postalCode City = _city End Sub Public Sub New() End Sub End Class End Class