using System.Diagnostics.CodeAnalysis; using System.Linq; using System.ServiceModel; using System.Threading.Tasks; using gehGassi.Common; using gehGassi.Core.Interfaces; using Microsoft.Extensions.Options; using VatServiceReference; namespace gehGassi.External.Services { /// /// Service der EU-UID-Nummern auf deren Gültigkeit prüft /// public class VatValidationService : IVatValidationService { private readonly IOptions _settings; /// /// Erstellt eine Instanz /// /// Instanz eines IOptions VatValidationSettings [ExcludeFromCodeCoverage] public VatValidationService(IOptions settings) { _settings = settings; } /// /// Prüft eine UID-Nummer auf deren Gültigkeit /// /// UID-Nummer /// ISO-2 Code des Landes /// true wenn erfolgreich, false sonst public async Task IsValidAsync(string vat, string countryIso2) { if (string.IsNullOrWhiteSpace(vat)) return false; if (string.IsNullOrWhiteSpace(countryIso2)) return false; if (!_settings.Value.Enabled) return true; var countriesToCheck = _settings.Value.Countries.Split(';'); if (!countriesToCheck.Contains(countryIso2.ToUpper())) return true; if (string.IsNullOrWhiteSpace(vat)) return false; System.Diagnostics.Debug.WriteLine($"VAT Prüfung: {vat} - {countryIso2}"); var proxy = new VatServiceReference.checkVatPortTypeClient(); proxy.Endpoint.Address = new EndpointAddress(_settings.Value.Address); bool valid; //Die Landeskennzeichnung wegnehmen vat = vat.ToUpperInvariant().Replace(countryIso2.ToUpperInvariant(), ""); try { var result = await proxy.checkVatAsync(new checkVatRequest(countryIso2, vat)); valid = result.valid; } catch { valid = false; } return valid; } } }