fcs-vies/ViesHttpRequest.cs

80 lines
3.4 KiB
C#
Raw Normal View History

2022-12-12 07:15:19 +01:00
// ***********************************************************************
// Assembly : FCS.Lib.Vies
2023-03-21 07:19:07 +01:00
// Author : fhdk
// Created : 2022 12 17 13:33
//
// Last Modified By: fhdk
// Last Modified On : 2023 03 14 09:16
2022-12-12 07:15:19 +01:00
// ***********************************************************************
2023-03-21 07:19:07 +01:00
// <copyright file="ViesHttpRequest.cs" company="FCS">
// Copyright (C) 2022-2023 FCS Frede's Computer Services.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see [https://www.gnu.org/licenses]
2022-12-12 07:15:19 +01:00
// </copyright>
// <summary></summary>
// ***********************************************************************
2023-03-21 07:19:07 +01:00
2022-12-12 07:15:19 +01:00
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
2023-03-21 07:19:07 +01:00
namespace FCS.Lib.Vies;
/// <summary>
/// http request to vies registrar
/// </summary>
public class ViesHttpRequest
2022-12-12 07:15:19 +01:00
{
/// <summary>
2023-03-21 07:19:07 +01:00
/// Async http request to vies registrar
2022-12-12 07:15:19 +01:00
/// </summary>
2023-03-21 07:19:07 +01:00
/// <param name="endpoint"></param>
/// <param name="countryCode"></param>
/// <param name="vatNumber"></param>
/// <param name="userAgent"></param>
/// <returns>Vies Response View model</returns>
/// <see cref="ViesResponseView"/>
/// <remarks>Service http://ec.europa.eu/taxation_customs/vies/services/checkVatService</remarks>
public async Task<ViesResponseView> GetResponseAsync(string endpoint, string countryCode, string vatNumber,
string userAgent)
2022-12-12 07:15:19 +01:00
{
2023-03-21 07:19:07 +01:00
var xml = new StringBuilder();
xml.Append(
"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:ec.europa.eu:taxud:vies:services:checkVat:types\">");
xml.Append("<soapenv:Header/>");
xml.Append("<soapenv:Body>");
xml.Append("<urn:checkVat>");
xml.Append($"<urn:countryCode>{countryCode.ToUpperInvariant()}</urn:countryCode>");
xml.Append($"<urn:vatNumber>{vatNumber}</urn:vatNumber>");
xml.Append("</urn:checkVat>");
xml.Append("</soapenv:Body>");
xml.Append("</soapenv:Envelope>");
2022-12-12 07:15:19 +01:00
2023-03-21 07:19:07 +01:00
using var content = new StringContent(xml.ToString(), Encoding.UTF8, "text/xml");
2022-12-12 07:15:19 +01:00
2023-03-21 07:19:07 +01:00
using var client = new HttpClient();
using var viesRequest = new HttpRequestMessage(HttpMethod.Post, endpoint);
viesRequest.Headers.Add("SOAPAction", "");
viesRequest.Headers.Add("User-Agent", userAgent);
viesRequest.Content = content;
var response = await client.SendAsync(viesRequest).ConfigureAwait(true);
var xmlResult = await response.Content.ReadAsStringAsync().ConfigureAwait(true);
2022-12-12 07:15:19 +01:00
2023-03-21 07:19:07 +01:00
return new ViesResponseView
{
Code = response.StatusCode,
IsSuccessStatusCode = response.IsSuccessStatusCode,
Message = xmlResult
};
2022-12-12 07:15:19 +01:00
}
2023-03-21 07:19:07 +01:00
}