Invoice Middleware
Invoica operates as transparent middleware in the x402 payment flow. It intercepts payment requests to automatically generate invoices, calculate taxes, enforce budgets, and monitor settlements.Architecture
The middleware consists of three core components:1. Proxy Layer
The proxy intercepts HTTP requests between AI agents and resource servers:- Detects 402 Payment Required responses
- Injects invoice generation into the payment flow
- Passes through non-payment requests transparently
- Adds invoice metadata to successful responses
2. Invoice Engine
When a payment is detected, the invoice engine:- Generates a unique invoice number
- Calculates line items from the payment amount
- Applies tax rules based on buyer/seller jurisdictions
- Creates a PDF invoice document
- Stores the invoice in the database
3. Settlement Monitor
After payment is sent, the settlement monitor:- Poll-based detection — Periodically checks the blockchain for transaction confirmation
- Event-driven detection — Listens for on-chain events via WebSocket
- Updates invoice status when settlement is confirmed
- Triggers webhook notifications
Invoice Lifecycle
| Status | Description |
|---|---|
draft | Invoice created, not yet sent |
sent | Invoice delivered to buyer |
paid | Payment transaction submitted |
settled | On-chain confirmation received |
completed | Settlement finalized, invoice closed |
failed | Payment failed or timed out |