101 lines
4.1 KiB
VB.net
101 lines
4.1 KiB
VB.net
Imports System.Net
|
|
Imports System.IO
|
|
Imports System.Web.Http
|
|
Imports System.Web.Http.Description
|
|
Imports System.Diagnostics
|
|
Imports Newtonsoft.Json
|
|
Imports VERAG_PROG_ALLGEMEIN
|
|
Imports VERAG_REST_SERVER
|
|
|
|
Namespace ApiController.Controllers
|
|
<RoutePrefix("wise")>
|
|
Public Class WiseController
|
|
Inherits System.Web.Http.ApiController
|
|
|
|
<HttpPost>
|
|
<Route("webhook")>
|
|
<AllowAnonymous>
|
|
<ResponseType(GetType(Void))>
|
|
Public Function Webhook(<FromBody> payload As WiseWebhookRequest) As IHttpActionResult
|
|
Dim json As String = ""
|
|
Try
|
|
If payload IsNot Nothing Then
|
|
json = JsonConvert.SerializeObject(payload)
|
|
Else
|
|
If Request IsNot Nothing AndAlso Request.Content IsNot Nothing Then
|
|
json = Request.Content.ReadAsStringAsync().Result
|
|
End If
|
|
End If
|
|
Catch
|
|
End Try
|
|
|
|
Dim saveResult = SaveWiseWebhook(json, 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
|