Ayuda a desarrolladores para integrar Verifactu con C#: aprende a realizar peticiones REST, generar facturas con QR, firmar con certificado digital y crear XML de forma eficiente.

Para realizar peticiones HTTP en C#, se pueden utilizar varias librerías, entre ellas:
Ejemplo de petición con HttpClient:
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace VerifactiApiCall
{
class Program
{
static async Task Main(string[] args)
{
var apiKey = "<API_KEY>";
var url = "https://api.verifacti.com/verifactu/create";
var json = @"{
""serie"": ""A"",
""numero"": ""234634"",
""fecha_expedicion"": ""02-12-2024"",
""tipo_factura"": ""F1"",
""descripcion"": ""Descripcion de la operacion"",
""nif"": ""A15022510"",
""nombre"": ""Nombre cliente"",
""lineas"": [{
""base_imponible"": ""200"",
""tipo_impositivo"": ""21"",
""cuota_repercutida"": ""42""
}],
""importe_total"": ""242""
}";
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await client.PostAsync(url, content);
var responseString = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseString);
}
}
}
}Para incrustar un código QR en un PDF, se pueden usar librerías como:
Ejemplo con iTextSharp:
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
class Program
{
static void Main()
{
Document doc = new Document();
PdfWriter.GetInstance(doc, new FileStream("FacturaConQR.pdf", FileMode.Create));
doc.Open();
Image qrImage = Image.GetInstance("QRFactura.png");
doc.Add(qrImage);
doc.Close();
}
}Para generar códigos QR en C#, se pueden usar librerías como:
Ejemplo con QRCoder:
using QRCoder;
using System.Drawing;
class Program
{
static void Main()
{
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode("https://verifacti.com", QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(qrCodeData);
Bitmap qrCodeImage = qrCode.GetGraphic(20);
qrCodeImage.Save("QRFactura.png");
}
}