Skip to main content

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​

  1. Webhook Handling - Ricevere ed elaborare webhook dai provider
  2. State Normalization - Convertire stati provider-specifici in uno stato unificato
  3. Idempotenza - Processare ogni evento esattamente una volta
  4. Persistenza - Mantenere lo stato degli abbonamenti nel database
  5. Event Publishing - Notificare altri servizi tramite Redis

πŸ“Š Architettura​

πŸ”„ Stati Normalizzati​

StatoDescrizioneAccesso Garantito
ACTIVEAbbonamento attivoβœ… SΓ¬
GRACE_PERIODProblema di pagamento, retry in corsoβœ… SΓ¬
PAST_DUEPagamento fallito❌ No
CANCELEDCancellato, valido fino a fine periodoβœ… SΓ¬ (fino a scadenza)
EXPIREDScaduto 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​

SecretDescrizione
db_password_billingPassword PostgreSQL
stripe_api_keyStripe API key (sk_live_xxx)
stripe_webhook_secretStripe webhook secret (whsec_xxx)
apple_shared_secretApple App Store shared secret
google_service_account_billing.jsonGoogle 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​