Imports System.Net Imports System.IO Imports System.Web.Http Imports System.Web.Http.Description Imports System.Diagnostics Imports System.Threading.Tasks Imports Newtonsoft.Json Imports VERAG_PROG_ALLGEMEIN Imports VERAG_REST_SERVER Namespace ApiController.Controllers Public Class WiseController Inherits System.Web.Http.ApiController Public Async Function Webhook() As Task(Of IHttpActionResult) Dim rawContent As String = "" Dim payload As WiseWebhookRequest = Nothing Try If Request IsNot Nothing AndAlso Request.Content IsNot Nothing Then rawContent = Await Request.Content.ReadAsStringAsync() End If If Not String.IsNullOrEmpty(rawContent) Then Try payload = JsonConvert.DeserializeObject(Of WiseWebhookRequest)(rawContent) Catch End Try End If Catch End Try Dim saveResult = SaveWiseWebhook(rawContent, payload) If payload Is Nothing Then ' Invalid JSON or empty body, but Wise expects 200 OK Return Ok() End If Try If payload.EventType = "balance.credit" AndAlso payload.Data IsNot Nothing Then Dim amountVal As Decimal = 0 Dim currency As String = "" If payload.Data.Amount IsNot Nothing Then amountVal = payload.Data.Amount.Value currency = payload.Data.Amount.Currency End If Dim balanceId = payload.Data.BalanceId Dim senderName = payload.Data.SenderName ' Log the details Trace.WriteLine(String.Format("Wise Webhook: Event={0}, Amount={1} {2}, BalanceId={3}, Sender={4}", payload.EventType, amountVal, currency, balanceId, senderName)) End If Catch ex As Exception ' Log exception but return 200 OK as per requirements Trace.WriteLine("Wise Webhook Error: " & ex.Message) End Try Return Ok() End Function Private Shared fileLock As New Object() Private Function SaveWiseWebhook(json As String, payload As WiseWebhookRequest) As String Try Dim tmpPath As String = "" If Environment.GetFolderPath(Environment.SpecialFolder.Personal) = "" Then tmpPath = "G:\temp\Verag\DatenTMP\WISE\" Else tmpPath = Path.GetTempPath() & "\VERAG\DatenTMP\" End If tmpPath = tmpPath.Replace("\\", "\") If Not My.Computer.FileSystem.DirectoryExists(tmpPath) Then My.Computer.FileSystem.CreateDirectory(tmpPath) End If Dim evt = If(payload IsNot Nothing AndAlso payload.EventType IsNot Nothing, payload.EventType, "unknown") Dim bal = If(payload IsNot Nothing AndAlso payload.Data IsNot Nothing, payload.Data.BalanceId.ToString(), "unknown") Dim name = VERAG_PROG_ALLGEMEIN.cDATENSERVER.replaceInvalidCahr($"WISE_{evt}_{bal}_{Now:ddMMyy_HHmmss}") Dim extension = ".txt" Dim destPath = tmpPath & name SyncLock fileLock While File.Exists(destPath & extension) destPath = tmpPath & name & "_" & Now.ToString("yyyyMMddHHmmssfff") End While End SyncLock Dim contentToWrite = If(String.IsNullOrEmpty(json), "", json) File.WriteAllText(destPath & extension, contentToWrite, System.Text.Encoding.UTF8) Return "OK" Catch ex As Exception VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace & vbNewLine, System.Reflection.MethodInfo.GetCurrentMethod.Name) Return "ERR" End Try End Function End Class End Namespace