Skip to main content

Using RPAfelApi for ALL Document Types (Including Invoices)

🎯 Question​

Should we use RPAfelApi for regular invoices (FACT) as well, not just for credit notes, debit notes, and cancellations?

βœ… YES - Absolutely! This is the Right Decision​

Why Use RPAfelApi for Invoices Too​

  1. βœ… Consistency

    • Same certification path for all document types
    • Unified error handling
    • Single code path to maintain
  2. βœ… Immediate Benefits

    • Certificate caching for invoices (especially Infile)
    • Automatic "already signed" error handling
    • Production-tested reliability
  3. βœ… Simpler Architecture

    • One provider for everything
    • Less code to maintain
    • Easier to debug
  4. βœ… Future-Proof

    • All new features in RPAfelApi benefit all documents
    • Centralized improvements
    • Better scalability

πŸ“Š Current vs Proposed Architecture​

Current Architecture (Mixed)​

Invoice (FACT)
↓
XmlConversionService β†’ XML
↓
Direct Certifier (Digifact/Infile)
↓
Response

Credit Note (NCRE) - Future
↓
XmlToDteJsonService β†’ DTE JSON
↓
RPAfelApi
↓
Response

Problems:

  • ❌ Two different paths
  • ❌ Invoices don't get caching
  • ❌ Invoices don't get "already signed" handling
  • ❌ More code to maintain
  • ❌ Inconsistent behavior

Proposed Architecture (Unified)​

All Documents (FACT, NCRE, NDEB, ANULACION)
↓
XmlToDteJsonService β†’ DTE JSON
↓
RPAfelApi
↓
Response

Benefits:

  • βœ… Single path for all documents
  • βœ… All documents get caching
  • βœ… All documents get error handling
  • βœ… Less code to maintain
  • βœ… Consistent behavior

πŸ”„ Migration Strategy​

Phase 1: Add RPAfelApi Support (Keep Direct Certifiers)​

Timeline: Week 1

  1. Create RPAfelApi Provider

    • ProviderRpaFelApiService
    • HTTP client for RPAfelApi
  2. Create DTE JSON Converter

    • XmlToDteJsonService
    • Convert Invoice to DTE JSON
  3. Update FelService

    • Add routing logic
    • Feature flag: USE_RPA_FEL_API
    • Keep direct certifiers as fallback
  4. Test with Invoices

    • Test FACT documents via RPAfelApi
    • Compare with direct certifiers
    • Verify caching works

Phase 2: Enable for Invoices (Gradual Rollout)​

Timeline: Week 2

  1. Enable for Test Businesses

    • Set USE_RPA_FEL_API=true for test businesses
    • Monitor performance
    • Check cache hit rates
  2. Monitor & Optimize

    • Track errors
    • Measure response times
    • Verify caching benefits
  3. Enable for All Businesses

    • Roll out to production
    • Keep direct certifiers as fallback
    • Monitor closely

Phase 3: Deprecate Direct Certifiers (Optional)​

Timeline: Week 3+ (After confidence)

  1. Remove Direct Certifier Code (Optional)
    • Only if RPAfelApi is 100% reliable
    • Or keep as fallback forever

πŸ’‘ Implementation Changes​

1. Update FelService.processSaleEvent​

Current:

felProviderData: {
providerName: "digifact", // Hardcoded
},

Updated:

felProviderData: {
providerName: process.env.USE_RPA_FEL_API === 'true'
? "rpafelapi"
: business.felCertifier || "digifact",
},

2. Update certifyDocument Routing​

Current:

// Always uses direct certifiers
const provider = this.providerService.getProvider(
felProviderData.providerName
);

Updated:

// Route to RPAfelApi if enabled
const useRpaFelApi =
process.env.USE_RPA_FEL_API === 'true' ||
felProviderData.providerName === 'rpafelapi';

if (useRpaFelApi) {
return await this.certifyViaRpaFelApi(document, business, taxId);
}

// Fallback to direct certifiers
const provider = this.providerService.getProvider(
felProviderData.providerName
);

3. Remove XML Generation for Invoices (Eventually)​

Current:

  • XmlConversionService.convertToXml() for FACT
  • Direct certifier expects XML

Future:

  • XmlToDteJsonService.convertInvoiceToDteJson() for FACT
  • RPAfelApi expects DTE JSON
  • Can keep XML conversion for direct certifier fallback

πŸ“Š Benefits Comparison​

FeatureDirect CertifiersRPAfelApi (All Docs)
Certificate Caching❌ Noβœ… Yes (Infile)
Already Signed Handling❌ Manualβœ… Automatic
Consistency❌ Mixed pathsβœ… Single path
Code Maintenance⚠️ More codeβœ… Less code
Error Handling⚠️ Manualβœ… Built-in
Future Features❌ Need to buildβœ… Get automatically
Performance⚠️ No cachingβœ… Cached responses
Reliability⚠️ Network issuesβœ… Production-tested

Strategy:

  • Use RPAfelApi for ALL documents (FACT, NCRE, NDEB, ANULACION)
  • Keep direct certifiers as fallback
  • Feature flag for gradual rollout

Benefits:

  • βœ… Maximum consistency
  • βœ… All benefits for all documents
  • βœ… Simplest architecture
  • βœ… Easiest to maintain

Timeline: 2-3 weeks

Strategy:

  • Use RPAfelApi for NCRE/NDEB/ANULACION
  • Keep direct certifiers for FACT

Problems:

  • ❌ Invoices don't get caching
  • ❌ Two code paths
  • ❌ More complexity
  • ❌ Inconsistent behavior

πŸš€ Implementation Plan​

Step 1: Add RPAfelApi Support (Week 1)​

  1. Create ProviderRpaFelApiService
  2. Create XmlToDteJsonService
  3. Update FelService with routing
  4. Add feature flag

Step 2: Test with Invoices (Week 1)​

  1. Test FACT documents via RPAfelApi
  2. Compare results with direct certifiers
  3. Verify caching works
  4. Test error scenarios

Step 3: Enable for Invoices (Week 2)​

  1. Enable for test businesses
  2. Monitor performance
  3. Roll out to all businesses
  4. Keep direct certifiers as fallback

Step 4: Add NCRE/NDEB/ANULACION (Week 3+)​

  1. Implement credit note service
  2. Implement debit note service
  3. Implement cancellation service
  4. All use RPAfelApi (consistent!)

πŸ’‘ Key Insight​

If RPAfelApi is good enough for credit notes, debit notes, and cancellations, it's definitely good enough for invoices!

Why:

  • Same certification process
  • Same error handling needs
  • Same caching benefits
  • Same reliability requirements

Plus:

  • Invoices are the most common document type
  • Caching benefits are most valuable for invoices
  • Consistency across all document types

βœ… Final Recommendation​

Use RPAfelApi for ALL document types, including invoices (FACT).

Why:

  1. βœ… Consistency across all documents
  2. βœ… Caching benefits for invoices
  3. βœ… Simpler architecture
  4. βœ… Less code to maintain
  5. βœ… Better long-term

Implementation:

  • Add RPAfelApi support
  • Enable for invoices first (most common)
  • Then add NCRE/NDEB/ANULACION
  • Keep direct certifiers as fallback

Timeline:

  • Week 1: Add RPAfelApi, test with invoices
  • Week 2: Enable for all invoices
  • Week 3+: Add credit notes, debit notes, cancellations

Last Updated: 2025-12-18
Status: Recommendation - Use RPAfelApi for All Documents