2026-07-22 1.1.21 Corrección de clase ValidacionDocumentosIdentidad

This commit is contained in:
2026-07-22 10:18:36 +02:00
parent 385b9079d2
commit 4795f9b68f
2 changed files with 80 additions and 30 deletions

View File

@@ -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
<JsonPropertyName("matchName")>
Public Property MatchName As Integer
<JsonPropertyName("matchStreet")>
Public Property MatchStreet As Integer
<JsonPropertyName("matchPostalCode")>
Public Property MatchPostalCode As Integer
<JsonPropertyName("matchCity")>
Public Property MatchCity As Integer
<JsonPropertyName("matchCompanyType")>
Public Property MatchCompanyType As Integer
End Class
Public Class ValidacionDocumentosIdentidad
Public Class ViesRestResponse
<JsonPropertyName("isValid")>
Public Property IsValid As Boolean
<JsonPropertyName("requestDate")>
Public Property RequestDate As Date
<JsonPropertyName("userError")>
Public Property UserError As String
<JsonPropertyName("name")>
Public Property Name As String
<JsonPropertyName("address")>
Public Property Address As String
<JsonPropertyName("requestIdentifier")>
Public Property RequestIdentifier As String
<JsonPropertyName("originalVatNumber")>
Public Property OriginalVatNumber As String
<JsonPropertyName("vatNumber")>
Public Property VatNumber As String
<JsonPropertyName("viesApproximate")>
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

View File

@@ -16,11 +16,12 @@
<TargetFramework>net8.0</TargetFramework>
<PackageId>tsUtilidades</PackageId>
<PackageTags>net8.0, libreria</PackageTags>
<Version>1.1.19</Version>
<Version>1.1.21</Version>
<Authors>Manuel</Authors>
<Company>Tecnosis S.A</Company>
<Description>Utilidades Varias</Description>
<PackageReleaseNotes>
- 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