diff --git a/ValidacionDocumentosIdentidadVIES.vb b/ValidacionDocumentosIdentidadVIES.vb index 0e4810d..902104e 100644 --- a/ValidacionDocumentosIdentidadVIES.vb +++ b/ValidacionDocumentosIdentidadVIES.vb @@ -1,17 +1,68 @@ Imports System.Net.Http Imports System.Text.Json +Imports System.Text.Json.Serialization Imports System.Text.RegularExpressions Imports System.Threading.Tasks -Public Class VatValidationResult - Public Property IsValid As Boolean - Public Property Name As String - Public Property Address As String - Public Property RawJson As String - Public Property ErrorMessage As String +' ============================================================ +' CLASES DE MODELO PARA EL JSON DEVUELTO POR VIES (REST) +' ============================================================ + +Public Class ViesApproximate + + Public Property MatchName As Integer + + + Public Property MatchStreet As Integer + + + Public Property MatchPostalCode As Integer + + + Public Property MatchCity As Integer + + + Public Property MatchCompanyType As Integer End Class -Public Class ValidacionDocumentosIdentidad +Public Class ViesRestResponse + + Public Property IsValid As Boolean + + + Public Property RequestDate As Date + + + Public Property UserError As String + + + Public Property Name As String + + + Public Property Address As String + + + Public Property RequestIdentifier As String + + + Public Property OriginalVatNumber As String + + + Public Property VatNumber As String + + + Public Property ViesApproximate As ViesApproximate + + ' Campo adicional para errores locales + Public Property LocalError As String +End Class + + +' ============================================================ +' VALIDADOR COMPLETO VAT + VIES REST +' ============================================================ + +Public Class ValidacionDocumentosIdentidadVIES ' ----------------------------- ' 1) Validación de formato VAT @@ -44,7 +95,7 @@ Public Class ValidacionDocumentosIdentidad {"SE", "^[0-9]{12}$"}, {"SI", "^[0-9]{8}$"}, {"SK", "^[0-9]{10}$"}, - {"XI", "^[0-9]{9}$"} ' Irlanda del Norte + {"XI", "^[0-9]{9}$"} } Public Shared Function IsValidVatFormat(countryCode As String, @@ -73,41 +124,35 @@ Public Class ValidacionDocumentosIdentidad ' 3) Validación VIES vía REST (ASÍNCRONA) ' ----------------------------- Public Shared Async Function ValidateVatAsync(countryCode As String, - vatNumber As String) As Task(Of VatValidationResult) + vatNumber As String) As Task(Of ViesRestResponse) - Dim result As New VatValidationResult() + Dim result As New ViesRestResponse() Try + ' Normalización + countryCode = countryCode.Trim().ToUpper() + vatNumber = vatNumber.Trim().ToUpper() + ' Validación de formato previa If Not IsValidVatFormat(countryCode, vatNumber) Then - result.IsValid = False - result.ErrorMessage = $"Formato VAT inválido para {countryCode}" + result.LocalError = $"Formato VAT inválido para {countryCode}" Return result End If Dim url = $"https://ec.europa.eu/taxation_customs/vies/rest-api/ms/{countryCode}/vat/{vatNumber}" - Dim json As String = Await _http.GetStringAsync(url) - result.RawJson = json + Dim json As String = Await _http.GetStringAsync(url).ConfigureAwait(False) - Dim doc = JsonDocument.Parse(json) - Dim root = doc.RootElement - - result.IsValid = root.GetProperty("isValid").GetBoolean() - - If result.IsValid Then - result.Name = root.GetProperty("traderName").GetString() - result.Address = root.GetProperty("traderAddress").GetString() - End If + result = JsonSerializer.Deserialize(Of ViesRestResponse)(json) Catch ex As HttpRequestException - result.ErrorMessage = "Error HTTP: " & ex.Message + result.LocalError = "Error HTTP: " & ex.Message Catch ex As TaskCanceledException - result.ErrorMessage = "Timeout al consultar VIES" + result.LocalError = "Timeout al consultar VIES" Catch ex As Exception - result.ErrorMessage = "Error inesperado: " & ex.Message + result.LocalError = "Error inesperado: " & ex.Message End Try Return result @@ -115,12 +160,16 @@ Public Class ValidacionDocumentosIdentidad ' ----------------------------- - ' 4) Validación VIES vía REST (SINCRONA) + ' 4) Validación VIES vía REST (SINCRONA SEGURA PARA WPF) ' ----------------------------- Public Shared Function ValidateVat(countryCode As String, - vatNumber As String) As VatValidationResult + vatNumber As String) As ViesRestResponse - Return ValidateVatAsync(countryCode, vatNumber).GetAwaiter().GetResult() + Return Task.Run( + Async Function() + Return Await ValidateVatAsync(countryCode, vatNumber).ConfigureAwait(False) + End Function + ).Result End Function diff --git a/tsUtilidades.vbproj b/tsUtilidades.vbproj index 54d52bc..561cd97 100644 --- a/tsUtilidades.vbproj +++ b/tsUtilidades.vbproj @@ -16,11 +16,12 @@ net8.0 tsUtilidades net8.0, libreria - 1.1.19 + 1.1.21 Manuel Tecnosis S.A Utilidades Varias + - 2026-07-22 1.1.21 Corrección de clase ValidacionDocumentosIdentidad - 2026-07-22 1.1.19 Nueva Clase ValidacionDocumentosIdentidad para documentos extranjeros - 2026-05-29 1.1.18 Corrección rutina LeeMysql y LeeMysqlBytArray para que como maximo se admitan 2048 caracteres string - 2026-05-29 1.1.17 Corrección rutina LeeMysql para que como maximo se admitan 2048 caracteres string