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β
-
β Consistency
- Same certification path for all document types
- Unified error handling
- Single code path to maintain
-
β Immediate Benefits
- Certificate caching for invoices (especially Infile)
- Automatic "already signed" error handling
- Production-tested reliability
-
β Simpler Architecture
- One provider for everything
- Less code to maintain
- Easier to debug
-
β 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
-
Create RPAfelApi Provider
ProviderRpaFelApiService- HTTP client for RPAfelApi
-
Create DTE JSON Converter
XmlToDteJsonService- Convert Invoice to DTE JSON
-
Update FelService
- Add routing logic
- Feature flag:
USE_RPA_FEL_API - Keep direct certifiers as fallback
-
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
-
Enable for Test Businesses
- Set
USE_RPA_FEL_API=truefor test businesses - Monitor performance
- Check cache hit rates
- Set
-
Monitor & Optimize
- Track errors
- Measure response times
- Verify caching benefits
-
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)
- 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β
| Feature | Direct Certifiers | RPAfelApi (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 |
π― Recommended Approachβ
Option 1: Full Migration to RPAfelApi (Recommended) ββββ
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
Option 2: Hybrid (Not Recommended) β οΈβ
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)β
- Create
ProviderRpaFelApiService - Create
XmlToDteJsonService - Update
FelServicewith routing - Add feature flag
Step 2: Test with Invoices (Week 1)β
- Test FACT documents via RPAfelApi
- Compare results with direct certifiers
- Verify caching works
- Test error scenarios
Step 3: Enable for Invoices (Week 2)β
- Enable for test businesses
- Monitor performance
- Roll out to all businesses
- Keep direct certifiers as fallback
Step 4: Add NCRE/NDEB/ANULACION (Week 3+)β
- Implement credit note service
- Implement debit note service
- Implement cancellation service
- 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:
- β Consistency across all documents
- β Caching benefits for invoices
- β Simpler architecture
- β Less code to maintain
- β 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