Billing Service - Overview
Il billing-service gestisce gli abbonamenti provenienti da tre provider: Stripe (web), Apple App Store (iOS) e Google Play Store (Android).
π― Obiettiviβ
- Webhook Handling - Ricevere ed elaborare webhook dai provider
- State Normalization - Convertire stati provider-specifici in uno stato unificato
- Idempotenza - Processare ogni evento esattamente una volta
- Persistenza - Mantenere lo stato degli abbonamenti nel database
- Event Publishing - Notificare altri servizi tramite Redis
π Architetturaβ
π Stati Normalizzatiβ
| Stato | Descrizione | Accesso Garantito |
|---|---|---|
ACTIVE | Abbonamento attivo | β SΓ¬ |
GRACE_PERIOD | Problema di pagamento, retry in corso | β SΓ¬ |
PAST_DUE | Pagamento fallito | β No |
CANCELED | Cancellato, valido fino a fine periodo | β SΓ¬ (fino a scadenza) |
EXPIRED | Scaduto definitivamente | β No |
π Struttura del Progettoβ
services/billing/
βββ app.py # Flask entry point
βββ config.py # Configurazione
βββ Dockerfile
βββ requirements.txt
βββ db/
β βββ models.py # SQLAlchemy models
β βββ database.py # Session management
βββ providers/
β βββ base.py # Abstract provider
β βββ stripe_provider.py # Stripe implementation
β βββ apple_provider.py # Apple v2 S2S
β βββ google_provider.py # Google RTDN
βββ services/
β βββ subscription_service.py # Business logic
β βββ event_publisher.py # Redis publishing
βββ routes/
β βββ health.py # Health check
β βββ subscriptions.py # Internal API
β βββ webhooks/
β βββ stripe.py
β βββ apple.py
β βββ google.py
βββ utils/
βββ state_machine.py # Status normalization
βββ idempotency.py # Duplicate handling
βοΈ Databaseβ
Il billing-service usa un database PostgreSQL dedicato sulla porta 5433:
- Host:
postgres-billing - Database:
billing_db - User:
billing
Questo isolamento permette:
- Scaling indipendente
- Backup/restore specifici
- Nessun conflitto con le altre tabelle
π Secrets Richiestiβ
| Secret | Descrizione |
|---|---|
db_password_billing | Password PostgreSQL |
stripe_api_key | Stripe API key (sk_live_xxx) |
stripe_webhook_secret | Stripe webhook secret (whsec_xxx) |
apple_shared_secret | Apple App Store shared secret |
google_service_account_billing.json | Google service account |
π Quick Startβ
# Avvia solo billing + database
docker-compose up -d billing postgres-billing redis
# Verifica health
curl http://localhost:8088/health
# Logs
docker-compose logs -f billing
π Navigazioneβ
- API Reference - Endpoint API
- Database Models - Schema database
- State Machine - Mappatura stati
- Webhook Flow - Flusso elaborazione
- Integration Guide - Guida integrazione