Billing, Stripe & Monthly Invoicing
PLANOVI runs two separate financial pipelines inside planovi-backend and planovi-flutter-app:
- SaaS Platform Licensing (Stripe): Organizations subscribe to Planovi plans, apply operator coupons, upgrade/downgrade, and sync entitlements via signed Stripe webhooks.
- Cooperative Energy Invoicing: Monthly kWh settlement between energy cooperative members (
billing-calculator→ PDF generation → Resend email).
Where Is It in Code?
| Symptom / Task | Repository | Exact File / Path |
|---|---|---|
| Billing UI, plan upgrade modal & invoice history in app | planovi-flutter-app | lib/features/billing/, lib/features/operator/ |
| Starting Stripe Checkout or Customer Billing Portal | planovi-backend | supabase/functions/start-checkout/, create-portal-session/ |
| Plan upgrades, proration preview, coupons & auto-renew | planovi-backend | supabase/functions/{preview-plan-change,change-plan,operator-coupons,set-auto-renew}/ |
Incoming Stripe webhooks (Stripe-Signature HMAC check) | planovi-backend | supabase/functions/stripe-webhook/, handle-stripe-webhook/ |
| Subscription updated / cancelled emails | planovi-backend | supabase/functions/send-subscription-updated/, send-subscription-cancelled/ |
| Cooperative tariff math & energy balancing | planovi-backend | supabase/functions/billing-calculator/, ai-energy-balancer/ |
| Monthly batch PDF invoices & CSV exports | planovi-backend | supabase/functions/{generate-monthly-invoices,generate-invoice-pdf,generate-report,export-csv}/ |
| Delivering & fetching member invoices | planovi-backend | supabase/functions/send-invoice-email/, get-billing-invoices/ |
Common Issues & How to Debug
1. Stripe Payment Succeeded, But Plan Didn’t Unlock in App
- Check Stripe Dashboard → Developers → Webhooks for delivery status (
200vs400/500) targetinghttps://api.planovi.app/functions/v1/stripe-webhook(orhandle-stripe-webhook). - Verify
STRIPE_WEBHOOK_SECRETin/opt/vps-stack/.envmatches the signing secret of the active Stripe endpoint (PROD vs DEV have different secrets!). - Inspect Edge Runtime logs on the VPS:
Terminal window docker logs --tail 150 supabase-edge-runtime-prod | grep -i stripe
2. Monthly Invoice PDF Failed or Email Not Received
- Check
RESEND_API_KEYandFROM_EMAILinsupabase-edge-runtime-prod. - Verify
aggregate-dailycompleted for the target billing month beforegenerate-monthly-invoicesran. - Check stored PDFs in Supabase Studio → Storage (
https://supabase-studio.planovi.app, VPN required).