wise webhook

This commit is contained in:
2025-12-10 10:14:10 +01:00
parent e8e5f8610a
commit a6cbf6ab56
7 changed files with 179 additions and 10 deletions

View File

@@ -8,7 +8,6 @@ Imports VERAG_PROG_ALLGEMEIN
Public Class BasicAuthenticationAttribute Public Class BasicAuthenticationAttribute
Inherits AuthorizationFilterAttribute Inherits AuthorizationFilterAttribute

View File

@@ -1,12 +1,11 @@
Imports System.IO Imports System.Web.Http
Imports System.Net.Http
Imports System.Reflection
Imports System.Web.Http
Imports System.Web.Http.Description Imports System.Web.Http.Description
Imports System.Web.Routing Imports System.Web.Routing
Imports Microsoft.Extensions.Options Imports Microsoft.Extensions.Options
Imports Swashbuckle.Application Imports Swashbuckle.Application
Imports NSwag.AspNet.Owin Imports NSwag.AspNet.Owin
Imports System.IO
Imports System.Reflection
@@ -87,12 +86,13 @@ Public Class SwaggerConfig
Public Shared Sub Register(ByVal config As HttpConfiguration) Public Shared Sub Register(ByVal config As HttpConfiguration)
RouteTable.Routes.MapOwinPath("swagger", Function(app) As Owin.IAppBuilder RouteTable.Routes.MapOwinPath("swagger", Sub(app)
app.UseSwaggerUi(GetType(WebApiApplication).Assembly, Function(settings) As SwaggerUiSettings(Of NSwag.Generation.WebApi.WebApiOpenApiDocumentGeneratorSettings) app.UseSwaggerUi(GetType(WebApiApplication).Assembly, Function(settings) As SwaggerUiSettings(Of NSwag.Generation.WebApi.WebApiOpenApiDocumentGeneratorSettings)
settings.MiddlewareBasePath = "/swagger" settings.MiddlewareBasePath = "/swagger"
settings.GeneratorSettings.OperationProcessors.Add(New WiseWebhookExampleProcessor())
settings.GeneratorSettings.DefaultUrlTemplate = "api/{controller}/{id}" settings.GeneratorSettings.DefaultUrlTemplate = "api/{controller}/{id}"
settings.DocumentTitle = descr settings.DocumentTitle = descr
settings.DocExpansion = "list" settings.DocExpansion = "list"
@@ -102,8 +102,7 @@ Public Class SwaggerConfig
Return app End Sub)
End Function)
End Sub End Sub

View File

@@ -0,0 +1,31 @@
Imports NSwag.Generation.Processors
Imports NSwag.Generation.Processors.Contexts
Imports NJsonSchema
Public Class WiseWebhookExampleProcessor
Implements IOperationProcessor
Public Function Process(context As OperationProcessorContext) As Boolean Implements IOperationProcessor.Process
If context.ControllerType.Name = "WiseController" AndAlso context.MethodInfo.Name = "Webhook" Then
Dim operation = context.OperationDescription.Operation
If operation.RequestBody IsNot Nothing AndAlso operation.RequestBody.Content.ContainsKey("application/json") Then
Dim content = operation.RequestBody.Content("application/json")
content.Example = New With {
.event_type = "balance.credit",
.data = New With {
.id = 123456789,
.balance_id = 99887766,
.amount = New With {
.value = 1500.00,
.currency = "EUR"
},
.occurred_at = "2025-12-06T10:15:30Z",
.description = "Incoming transfer",
.sender_name = "ACME GmbH"
}
}
End If
End If
Return True
End Function
End Class

View File

@@ -0,0 +1,100 @@
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

View File

@@ -0,0 +1,37 @@
Imports Newtonsoft.Json
Public Class WiseWebhookRequest
<JsonProperty("event_type")>
Public Property EventType As String
<JsonProperty("data")>
Public Property Data As WiseWebhookData
End Class
Public Class WiseWebhookData
<JsonProperty("id")>
Public Property Id As Long
<JsonProperty("balance_id")>
Public Property BalanceId As Long
<JsonProperty("amount")>
Public Property Amount As WiseWebhookAmount
<JsonProperty("occurred_at")>
Public Property OccurredAt As DateTime
<JsonProperty("description")>
Public Property Description As String
<JsonProperty("sender_name")>
Public Property SenderName As String
End Class
Public Class WiseWebhookAmount
<JsonProperty("value")>
Public Property Value As Decimal
<JsonProperty("currency")>
Public Property Currency As String
End Class

View File

@@ -377,8 +377,10 @@
<Compile Include="App_Start\BasicAuthenticationAttribute.vb" /> <Compile Include="App_Start\BasicAuthenticationAttribute.vb" />
<Compile Include="App_Start\RouteConfig.vb" /> <Compile Include="App_Start\RouteConfig.vb" />
<Compile Include="App_Start\SwaggerConfig.vb" /> <Compile Include="App_Start\SwaggerConfig.vb" />
<Compile Include="App_Start\WiseWebhookExampleProcessor.vb" />
<Compile Include="App_Start\WebApiConfig.vb" /> <Compile Include="App_Start\WebApiConfig.vb" />
<Compile Include="Controllers\ATEZ\ATEZController.vb" /> <Compile Include="Controllers\ATEZ\ATEZController.vb" />
<Compile Include="Controllers\Wise\WiseController.vb" />
<Compile Include="Controllers\AvisoController\AVISOController.vb" /> <Compile Include="Controllers\AvisoController\AVISOController.vb" />
<Compile Include="Controllers\CustomsDeclaration\CustomsDeclarationController.vb" /> <Compile Include="Controllers\CustomsDeclaration\CustomsDeclarationController.vb" />
<Compile Include="Controllers\NCTS\NCTSController.vb" /> <Compile Include="Controllers\NCTS\NCTSController.vb" />
@@ -390,6 +392,7 @@
<DependentUpon>Global.asax</DependentUpon> <DependentUpon>Global.asax</DependentUpon>
</Compile> </Compile>
<Compile Include="Models\Aviso.vb" /> <Compile Include="Models\Aviso.vb" />
<Compile Include="Models\WiseWebhookModels.vb" />
<Compile Include="Models\cTEST.vb" /> <Compile Include="Models\cTEST.vb" />
<Compile Include="Models\cVERAG_in_shippmentOLD.vb" /> <Compile Include="Models\cVERAG_in_shippmentOLD.vb" />
<Compile Include="My Project\AssemblyInfo.vb" /> <Compile Include="My Project\AssemblyInfo.vb" />

View File

@@ -1,4 +1,4 @@
<?xml version="1.0"?> <?xml version="1.0"?>
<!-- <!--
Weitere Informationen zum Konfigurieren Ihrer ASP.NET-Anwendung finden Sie unter Weitere Informationen zum Konfigurieren Ihrer ASP.NET-Anwendung finden Sie unter
https://go.microsoft.com/fwlink/?LinkId=301879 https://go.microsoft.com/fwlink/?LinkId=301879
@@ -246,4 +246,4 @@
<add name="NSwag" path="swagger" verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0"/> <add name="NSwag" path="swagger" verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0"/>
</handlers> </handlers>
</system.webServer> </system.webServer>
</configuration> </configuration>