Greeva.Sunat.GRE
1.0.5
dotnet add package Greeva.Sunat.GRE --version 1.0.5
NuGet\Install-Package Greeva.Sunat.GRE -Version 1.0.5
<PackageReference Include="Greeva.Sunat.GRE" Version="1.0.5" />
<PackageVersion Include="Greeva.Sunat.GRE" Version="1.0.5" />
<PackageReference Include="Greeva.Sunat.GRE" />
paket add Greeva.Sunat.GRE --version 1.0.5
#r "nuget: Greeva.Sunat.GRE, 1.0.5"
#:package Greeva.Sunat.GRE@1.0.5
#addin nuget:?package=Greeva.Sunat.GRE&version=1.0.5
#tool nuget:?package=Greeva.Sunat.GRE&version=1.0.5
Greeva.Sunat.GRE
SUNAT web services integration for Greeva electronic invoicing - CPE services and authentication.
Overview
Greeva.Sunat.GRE provides complete integration with SUNAT web services for electronic document submission, status checking, and authentication. It handles both production and beta environments with proper SOAP communication.
Key Features
- CPE Services: Complete electronic document (CPE) submission
- Authentication: SUNAT web service authentication
- Environment Support: Production and beta environments
- Document Status: Check document processing status
- Summary Operations: Handle summary and voided documents
- Error Handling: Comprehensive SUNAT error management
- Response Processing: CDR (Constancia de Recepción) handling
Services Provided
ICpeService - Document Operations
Send Invoice/Note Documents
var result = await cpeService.SendBillAsync("F001-123.zip", zipContent);
Send Summary Documents
var result = await cpeService.SendSummaryAsync("RC-20231011-001.zip", zipContent);
Send Voided Documents
var result = await cpeService.SendPackAsync("RA-20231011-001.zip", zipContent);
Check Document Status
var status = await cpeService.GetStatusAsync("1234567890123456");
IAuthService - Authentication
// Authenticate with SUNAT
var token = await authService.GetTokenAsync("20123456789MODDATOS", "MODDATOS");
Configuration
SUNAT Endpoints
The library provides predefined endpoints for both production and beta environments:
Production Environment
- FE_BILL:
https://e-factura.sunat.gob.pe/ol-ti-itcpfegem/billService - FE_CONSULTA_CDR:
https://e-factura.sunat.gob.pe/ol-it-wsconscpegem/billConsultService - FE_GUIA:
https://api-cpe.sunat.gob.pe/v1/contribuyente/gem - FE_RETENTION:
https://e-factura.sunat.gob.pe/ol-ti-itemision-otroscpe-gem/billService
Beta Environment
- FE_BILL:
https://e-beta.sunat.gob.pe/ol-ti-itcpfegem-beta/billService - FE_CONSULTA_CDR:
https://e-beta.sunat.gob.pe/ol-it-wsconscpegem/billConsultService - FE_GUIA:
https://gre-beta.sunat.gob.pe/v1/contribuyente/gem - FE_RETENTION:
https://e-beta.sunat.gob.pe/ol-ti-itemision-otroscpe-gem/billService
Basic Configuration
Option 1: Use Predefined Endpoints
using Greeva.Sunat.GRE;
// Get endpoints for production or beta
var endpoints = SunatEndpoints.GetEndpoints(isProduction: false); // false = Beta
// Initialize services
var httpClient = new HttpClient();
var authService = new AuthService(httpClient, endpoints);
var cpeService = new CpeService(httpClient, endpoints, authService, ruc, username, password);
Option 2: Custom Endpoints Configuration
// Completely custom endpoints
var customEndpoints = SunatEndpointsConfig.CreateCustom(
feBill: "https://my-proxy.com/billService",
feConsultaCdr: "https://my-proxy.com/billConsultService",
feGuia: "https://my-proxy.com/gem",
feRetention: "https://my-proxy.com/retention"
);
var cpeService = new CpeService(httpClient, customEndpoints, authService, ruc, username, password);
Option 3: Override Specific Endpoints
// Use production endpoints but override FE_CONSULTA_CDR
var endpoints = SunatEndpointsConfig.CreateFromProduction(
feConsultaCdr: "https://custom-cdr-service.com/billConsultService"
);
// Use beta endpoints but override FE_BILL
var betaEndpoints = SunatEndpointsConfig.CreateFromBeta(
feBill: "https://test-server.com/billService"
);
Option 4: Dependency Injection (ASP.NET Core)
using Greeva.Sunat.GRE;
using Microsoft.Extensions.DependencyInjection;
// In Startup.cs or Program.cs
services.AddHttpClient();
// Register endpoints
services.AddSingleton(sp =>
{
var isProduction = Configuration.GetValue<bool>("Sunat:IsProduction");
return SunatEndpoints.GetEndpoints(isProduction);
});
// Or use custom endpoints from configuration
services.AddSingleton(sp =>
{
var config = Configuration.GetSection("Sunat:Endpoints");
return SunatEndpointsConfig.CreateCustom(
feBill: config["FeBill"],
feConsultaCdr: config["FeConsultaCdr"],
feGuia: config["FeGuia"],
feRetention: config["FeRetention"]
);
});
// Register services
services.AddScoped<IAuthService, AuthService>();
services.AddScoped<ICpeService, CpeService>();
Document Submission Workflow
1. Prepare Document
// Generate and sign XML
var xml = await xmlBuilder.BuildAsync(invoice);
var signedXml = await xmlSigner.SignXmlAsync(xml);
// Create ZIP file
var zipContent = CreateZipFile("F001-123.xml", signedXml);
2. Submit to SUNAT
var result = await cpeService.SendBillAsync("F001-123.zip", zipContent);
if (result.Success == true)
{
Console.WriteLine("Document accepted by SUNAT");
// Process CDR (Constancia de Recepción)
if (!string.IsNullOrEmpty(result.CdrZip))
{
var cdrBytes = Convert.FromBase64String(result.CdrZip);
await File.WriteAllBytesAsync($"R-F001-123.zip", cdrBytes);
}
}
else
{
Console.WriteLine($"Error: {result.Error?.Code} - {result.Error?.Message}");
}
3. Handle Summary Documents
var summaryResult = await cpeService.SendSummaryAsync("RC-20231011-001.zip", zipContent);
if (summaryResult.Success == true)
{
Console.WriteLine($"Summary accepted. Ticket: {summaryResult.Ticket}");
// Check status later
var statusResult = await cpeService.GetStatusAsync(summaryResult.Ticket);
if (statusResult.Success == true)
{
Console.WriteLine("Summary processed successfully");
}
}
Response Types
BillResult
public class BillResult
{
public bool? Success { get; set; }
public string? CdrZip { get; set; } // Base64 CDR content
public string? CdrResponse { get; set; } // CDR XML content
public Error? Error { get; set; }
}
SummaryResult
public class SummaryResult
{
public bool? Success { get; set; }
public string? Ticket { get; set; } // Status check ticket
public Error? Error { get; set; }
}
StatusResult
public class StatusResult
{
public bool? Success { get; set; }
public string? CdrZip { get; set; } // Base64 CDR content
public string? CdrResponse { get; set; } // CDR XML content
public Error? Error { get; set; }
}
Error Handling
SUNAT Error Codes
if (result.Error != null)
{
switch (result.Error.Code)
{
case "0001":
Console.WriteLine("RUC emisor no existe");
break;
case "2324":
Console.WriteLine("Documento ya fue informado anteriormente");
break;
case "2335":
Console.WriteLine("Documento electrónico observado");
break;
default:
Console.WriteLine($"Error: {result.Error.Code} - {result.Error.Message}");
break;
}
}
Connection Errors
try
{
var result = await cpeService.SendBillAsync(fileName, zipContent);
}
catch (HttpRequestException ex)
{
Console.WriteLine($"Network error: {ex.Message}");
}
catch (TimeoutException ex)
{
Console.WriteLine($"Request timeout: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"Unexpected error: {ex.Message}");
}
Endpoint Configuration Use Cases
Use Case 1: Development with Local Proxy
var endpoints = SunatEndpointsConfig.CreateFromBeta(
feBill: "http://localhost:8080/sunat/bill"
);
Use Case 2: Staging Environment
var endpoints = SunatEndpointsConfig.CreateFromBeta(
feConsultaCdr: "https://staging.mycompany.com/sunat/consult"
);
Use Case 3: Testing with Mock Services
var mockEndpoints = SunatEndpointsConfig.CreateCustom(
feBill: "http://localhost:5000/mock/bill",
feConsultaCdr: "http://localhost:5000/mock/consult",
feGuia: "http://localhost:5000/mock/guia",
feRetention: "http://localhost:5000/mock/retention"
);
Testing Credentials
SUNAT Beta Environment
- RUC: 20123456789
- Username: MODDATOS
- Password: MODDATOS
Test Document Numbers
- Series for testing: F001, B001
- Use valid test RUC numbers provided by SUNAT
Example: Complete Setup for Testing
// Configure for beta environment
var endpoints = SunatEndpoints.GetEndpoints(isProduction: false);
var httpClient = new HttpClient();
var authService = new AuthService(httpClient, endpoints);
var cpeService = new CpeService(
httpClient,
endpoints,
authService,
ruc: "20123456789",
username: "MODDATOS",
password: "MODDATOS"
);
// Now you can send test documents
var result = await cpeService.SendBillAsync("F001-1.zip", zipContent);
Security Considerations
- Never hardcode credentials in source code
- Use environment variables or secure configuration
- Implement proper authentication token management
- Handle certificate validation for HTTPS connections
- Log security events appropriately
Performance Optimization
- Connection pooling: Reuse HTTP connections
- Timeout configuration: Set appropriate timeouts
- Retry logic: Implement exponential backoff for failures
- Async operations: Use async/await for better scalability
Dependencies
- .NET 8.0
- System.ServiceModel.* packages (for SOAP)
- Microsoft.Extensions.Http 9.0.0
- System.Text.Json 9.0.0
- Greeva.Core (domain models and responses)
Related Packages
- Greeva.Core: Core domain models and response types
- Greeva.Xml: XML generation for documents
- Greeva.Xml.Sign: Digital signature functionality
- Greeva.WebApi: Web API integration with SUNAT services
License
MIT License - see LICENSE file for details.
Support
For issues and questions, please visit: https://github.com/augustoteles/greeva/issues
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- Greeva.Core (>= 1.0.5)
- Microsoft.Extensions.Http (>= 9.0.0)
- System.ServiceModel.Duplex (>= 6.0.0)
- System.ServiceModel.Http (>= 6.0.0)
- System.ServiceModel.NetTcp (>= 6.0.0)
- System.ServiceModel.Security (>= 6.0.0)
- System.Text.Json (>= 9.0.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Greeva.Sunat.GRE:
| Package | Downloads |
|---|---|
|
Greeva.WebApi
Greeva library optimized for Web APIs with DI support and embedded templates |
GitHub repositories
This package is not used by any popular GitHub repositories.