Stripe banned Hanzo. Removed Stripe SDK, webhook handlers, hardcoded keys, and StripePricingTable. Billing redirects to billing.hanzo.ai.
7.6 KiB
Invoice System Documentation
Overview
The Hanzo Computer marketplace now includes a comprehensive PDF invoice generation system that allows customers to download professional invoices for their orders and subscriptions.
Features
Customer Features
- Download PDF Invoices: Generate and download professional PDF invoices for all paid orders
- Invoice History: View all past invoices in a dedicated tab on the dashboard
- Invoice Preview: Preview invoices before downloading
- Automatic Generation: Invoices are automatically generated when orders are paid
- Professional Layout: Clean, branded invoice design with Hanzo Computer branding
Admin Features
- Bulk Generation: Generate invoices for multiple orders at once
- Email Invoices: Send invoices directly to customers via email
- Regenerate Invoices: Update and regenerate existing invoices
- Invoice Management: Mark invoices as paid, void invoices, add notes
- Missing Invoice Detection: Identify paid orders without invoices
Technical Implementation
Components
1. Invoice Generator (/src/lib/invoices.ts)
Core invoice functionality including:
- Invoice data structure definition
- Invoice creation from orders and subscriptions
- PDF storage in Supabase
- Invoice number generation
- Currency and date formatting
2. Invoice Component (/src/components/Invoice.tsx)
React PDF component using @react-pdf/renderer:
- Professional invoice template
- Hanzo Computer branding
- Line items with GPU specifications
- Tax calculations
- Payment terms and conditions
3. Dashboard Integration (/src/components/DashboardInvoices.tsx)
Customer-facing invoice interface:
- Invoice download buttons
- Preview functionality
- Invoice history display
- Real-time generation
4. Admin Actions (/src/lib/adminInvoiceActions.ts)
Administrative invoice operations:
- Bulk invoice generation
- Email sending integration
- Invoice status management
- Regeneration capabilities
Database Schema
Invoices Table
- id: UUID (Primary Key)
- invoice_number: VARCHAR (Unique)
- user_id: UUID (Foreign Key)
- order_id: UUID (Foreign Key)
- subscription_id: UUID (Foreign Key)
- external_invoice_id: VARCHAR
- amount_due: DECIMAL
- amount_paid: DECIMAL
- status: VARCHAR ('draft', 'open', 'paid', 'void', 'uncollectible')
- due_date: DATE
- paid_at: TIMESTAMP
- pdf_url: TEXT
- generated_at: TIMESTAMP
- line_items: JSONB
- customer_info: JSONB
- subtotal: DECIMAL
- tax: DECIMAL
Storage Configuration
Supabase Storage Buckets
- invoices: Public bucket for PDF storage
- Organized by year:
/invoices/2025/INV-20250131-0001.pdf - Automatic public URL generation
- Signed URLs for private access
- Organized by year:
Usage
For Customers
-
View Invoices:
- Log in to your dashboard
- Click the "Invoices" tab
- View all your orders and their invoice status
-
Download Invoice:
- Find the order you need an invoice for
- Click "Download" if invoice exists
- Or click "Generate Invoice" for new invoices
- PDF will download to your computer
-
Preview Invoice:
- Click "Preview" button next to any order
- View invoice in modal before downloading
- Close modal when done
For Administrators
-
Generate Invoice for Order:
import { generateAndSendInvoice } from '@/lib/adminInvoiceActions'; const result = await generateAndSendInvoice(orderId, true); // true = send email -
Bulk Generate Invoices:
import { bulkGenerateInvoices } from '@/lib/adminInvoiceActions'; const result = await bulkGenerateInvoices(orderIds, false); // false = no emails console.log(`Generated: ${result.generated}, Failed: ${result.failed}`); -
Regenerate Invoice:
import { regenerateInvoice } from '@/lib/adminInvoiceActions'; const result = await regenerateInvoice(invoiceId, true); // true = send email
API Endpoints
Generate Invoice API (/api/generate-invoice.ts)
Serverless function for invoice generation:
POST /api/generate-invoice
{
"type": "order" | "subscription" | "invoice",
"id": "order-uuid",
"regenerate": false,
"period": { // optional, for subscriptions
"start": "2025-01-01",
"end": "2025-01-31"
}
}
Response:
{
"success": true,
"data": {
"invoiceId": "invoice-uuid",
"invoiceNumber": "INV-20250131-0001",
"pdfUrl": "https://...",
"downloadUrl": "https://..." // signed URL
}
}
Configuration
Environment Variables
VITE_SUPABASE_URL=your-supabase-url
VITE_SUPABASE_ANON_KEY=your-supabase-anon-key
SUPABASE_SERVICE_KEY=your-service-key # For API operations
Company Information
Edit /src/lib/invoices.ts to update company details:
export const COMPANY_INFO = {
name: 'Hanzo Computer',
address: '2100 Geng Road',
city: 'Palo Alto',
state: 'CA',
zip: '94303',
country: 'United States',
email: '[email protected]',
phone: '+1 (650) 555-0100',
website: 'https://hanzo.computer',
taxId: 'XX-XXXXXXX'
};
Email Integration
To enable email sending, integrate with your preferred email service:
-
SendGrid Integration:
import sgMail from '@sendgrid/mail'; sgMail.setApiKey(process.env.SENDGRID_API_KEY); // Update sendInvoiceEmail function in adminInvoiceActions.ts -
AWS SES Integration:
import { SESClient, SendEmailCommand } from "@aws-sdk/client-ses"; const client = new SESClient({ region: "us-west-2" }); // Update sendInvoiceEmail function
Testing
Unit Tests
npm test -- invoice.test.ts
Manual Testing
- Create a test order and mark it as paid
- Navigate to Dashboard > Invoices tab
- Click "Generate Invoice" for the test order
- Verify PDF downloads correctly
- Check invoice appears in history
Troubleshooting
Common Issues
-
Invoice Generation Fails:
- Check Supabase connection
- Verify storage bucket exists and has proper permissions
- Check console for detailed error messages
-
PDF Not Downloading:
- Ensure browser allows pop-ups from the site
- Check if PDF URL is accessible
- Verify storage bucket is public or signed URL is valid
-
Missing Invoice Data:
- Ensure order has complete information
- Check user details are properly loaded
- Verify line items are properly formatted
Security Considerations
-
Access Control:
- Users can only view/download their own invoices
- Admin role required for bulk operations
- RLS policies enforce data isolation
-
Storage Security:
- Invoices stored in Supabase Storage
- Signed URLs for temporary access
- Public URLs only for authorized invoices
-
Data Privacy:
- Customer information stored securely
- PII handled according to privacy policy
- Audit trail for all invoice operations
Future Enhancements
- Recurring Invoices: Automatic monthly invoice generation for subscriptions
- Custom Templates: Multiple invoice templates for different use cases
- Multi-language Support: Invoices in customer's preferred language
- Payment Links: Direct payment links in invoice emails
- Webhook Integration: Webhook for automatic invoice generation
- Credit Notes: Support for refunds and adjustments
- Batch Download: ZIP file with multiple invoices
- Invoice Scheduling: Schedule invoice generation for future dates
Support
For issues or questions about the invoice system:
- Technical Support: [email protected]
- Billing Questions: [email protected]
- Documentation: This file and inline code comments