2026-07-22 1.1.21 Corrección de clase ValidacionDocumentosIdentidad
This commit is contained in:
@@ -1,17 +1,68 @@
|
|||||||
Imports System.Net.Http
|
Imports System.Net.Http
|
||||||
Imports System.Text.Json
|
Imports System.Text.Json
|
||||||
|
Imports System.Text.Json.Serialization
|
||||||
Imports System.Text.RegularExpressions
|
Imports System.Text.RegularExpressions
|
||||||
Imports System.Threading.Tasks
|
Imports System.Threading.Tasks
|
||||||
|
|
||||||
Public Class VatValidationResult
|
' ============================================================
|
||||||
Public Property IsValid As Boolean
|
' CLASES DE MODELO PARA EL JSON DEVUELTO POR VIES (REST)
|
||||||
Public Property Name As String
|
' ============================================================
|
||||||
Public Property Address As String
|
|
||||||
Public Property RawJson As String
|
Public Class ViesApproximate
|
||||||
Public Property ErrorMessage As String
|
<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
|
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
|
' 1) Validación de formato VAT
|
||||||
@@ -44,7 +95,7 @@ Public Class ValidacionDocumentosIdentidad
|
|||||||
{"SE", "^[0-9]{12}$"},
|
{"SE", "^[0-9]{12}$"},
|
||||||
{"SI", "^[0-9]{8}$"},
|
{"SI", "^[0-9]{8}$"},
|
||||||
{"SK", "^[0-9]{10}$"},
|
{"SK", "^[0-9]{10}$"},
|
||||||
{"XI", "^[0-9]{9}$"} ' Irlanda del Norte
|
{"XI", "^[0-9]{9}$"}
|
||||||
}
|
}
|
||||||
|
|
||||||
Public Shared Function IsValidVatFormat(countryCode As String,
|
Public Shared Function IsValidVatFormat(countryCode As String,
|
||||||
@@ -73,41 +124,35 @@ Public Class ValidacionDocumentosIdentidad
|
|||||||
' 3) Validación VIES vía REST (ASÍNCRONA)
|
' 3) Validación VIES vía REST (ASÍNCRONA)
|
||||||
' -----------------------------
|
' -----------------------------
|
||||||
Public Shared Async Function ValidateVatAsync(countryCode As String,
|
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
|
Try
|
||||||
|
' Normalización
|
||||||
|
countryCode = countryCode.Trim().ToUpper()
|
||||||
|
vatNumber = vatNumber.Trim().ToUpper()
|
||||||
|
|
||||||
' Validación de formato previa
|
' Validación de formato previa
|
||||||
If Not IsValidVatFormat(countryCode, vatNumber) Then
|
If Not IsValidVatFormat(countryCode, vatNumber) Then
|
||||||
result.IsValid = False
|
result.LocalError = $"Formato VAT inválido para {countryCode}"
|
||||||
result.ErrorMessage = $"Formato VAT inválido para {countryCode}"
|
|
||||||
Return result
|
Return result
|
||||||
End If
|
End If
|
||||||
|
|
||||||
Dim url = $"https://ec.europa.eu/taxation_customs/vies/rest-api/ms/{countryCode}/vat/{vatNumber}"
|
Dim url = $"https://ec.europa.eu/taxation_customs/vies/rest-api/ms/{countryCode}/vat/{vatNumber}"
|
||||||
|
|
||||||
Dim json As String = Await _http.GetStringAsync(url)
|
Dim json As String = Await _http.GetStringAsync(url).ConfigureAwait(False)
|
||||||
result.RawJson = json
|
|
||||||
|
|
||||||
Dim doc = JsonDocument.Parse(json)
|
result = JsonSerializer.Deserialize(Of ViesRestResponse)(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
|
|
||||||
|
|
||||||
Catch ex As HttpRequestException
|
Catch ex As HttpRequestException
|
||||||
result.ErrorMessage = "Error HTTP: " & ex.Message
|
result.LocalError = "Error HTTP: " & ex.Message
|
||||||
|
|
||||||
Catch ex As TaskCanceledException
|
Catch ex As TaskCanceledException
|
||||||
result.ErrorMessage = "Timeout al consultar VIES"
|
result.LocalError = "Timeout al consultar VIES"
|
||||||
|
|
||||||
Catch ex As Exception
|
Catch ex As Exception
|
||||||
result.ErrorMessage = "Error inesperado: " & ex.Message
|
result.LocalError = "Error inesperado: " & ex.Message
|
||||||
End Try
|
End Try
|
||||||
|
|
||||||
Return result
|
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,
|
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
|
End Function
|
||||||
|
|
||||||
|
|||||||
@@ -16,11 +16,12 @@
|
|||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<PackageId>tsUtilidades</PackageId>
|
<PackageId>tsUtilidades</PackageId>
|
||||||
<PackageTags>net8.0, libreria</PackageTags>
|
<PackageTags>net8.0, libreria</PackageTags>
|
||||||
<Version>1.1.19</Version>
|
<Version>1.1.21</Version>
|
||||||
<Authors>Manuel</Authors>
|
<Authors>Manuel</Authors>
|
||||||
<Company>Tecnosis S.A</Company>
|
<Company>Tecnosis S.A</Company>
|
||||||
<Description>Utilidades Varias</Description>
|
<Description>Utilidades Varias</Description>
|
||||||
<PackageReleaseNotes>
|
<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-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.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
|
- 2026-05-29 1.1.17 Corrección rutina LeeMysql para que como maximo se admitan 2048 caracteres string
|
||||||
|
|||||||
Reference in New Issue
Block a user