Public Access
Mise en veille PayPal, synchro solde OpenRouter et modale de rejet financier
Build and Push Docker Image / build-and-push (push) Successful in 34s
Build and Push Docker Image / build-and-push (push) Successful in 34s
This commit is contained in:
1 parent
2ea18701b5
commit
3e140d7d6f
8 files changed
+380
-293
No files matched your search
+26
-27
@@ -16,7 +16,6 @@ TARIFS_FILE = os.path.join(BASE_DIR, "tarifs_openrouter.json")
|
||||
VIP_ADMIN_USERS = ["xavier", "céline", "celine", "anaëlle", "anaelle", "renan", "audric"]
|
||||
|
||||
def obtenir_tarifs_dynamiques() -> dict:
|
||||
"""Récupère les tarifs via API toutes les 2 heures maximum pour éviter le hardcoding."""
|
||||
maintenant = time.time()
|
||||
|
||||
if os.path.exists(TARIFS_FILE):
|
||||
@@ -36,7 +35,6 @@ def obtenir_tarifs_dynamiques() -> dict:
|
||||
pricing = m.get("pricing", {})
|
||||
p_prompt = float(pricing.get("prompt", 0.0))
|
||||
p_comp = float(pricing.get("completion", 0.0))
|
||||
# On sécurise l'estimation en prenant la valeur la plus haute entre tokens in et out
|
||||
tarifs[m["id"]] = max(p_prompt, p_comp)
|
||||
|
||||
with open(TARIFS_FILE, "w", encoding="utf-8") as f:
|
||||
@@ -46,7 +44,6 @@ def obtenir_tarifs_dynamiques() -> dict:
|
||||
except Exception as e:
|
||||
log_event("ERROR", "BILLING", f"Échec récupération des tarifs : {e}")
|
||||
|
||||
# Repli sur le cache si l'API est injoignable
|
||||
if os.path.exists(TARIFS_FILE):
|
||||
try:
|
||||
with open(TARIFS_FILE, "r", encoding="utf-8") as f:
|
||||
@@ -150,6 +147,31 @@ def verifier_et_valider_budget(prompt: str, modele_id: str, utilisateur_nom: str
|
||||
return True, prix_facture
|
||||
|
||||
def obtenir_solde(utilisateur_nom: str) -> float:
|
||||
# --- SYNCHRONISATION DIRECTE POUR L'ADMINISTRATEUR PRINCIPAL ---
|
||||
if utilisateur_nom.lower() in ["xavier"]:
|
||||
api_key = os.getenv("OPENROUTER_API_KEY")
|
||||
if api_key:
|
||||
try:
|
||||
resp = requests.get("https://openrouter.ai/api/v1/auth/key", headers={"Authorization": f"Bearer {api_key}"}, timeout=3)
|
||||
if resp.status_code == 200:
|
||||
data = resp.json().get("data", {})
|
||||
|
||||
credit_total = data.get('usage', 0.0)
|
||||
limite_restante = data.get('limit_remaining')
|
||||
balance = data.get('balance', 0.0)
|
||||
|
||||
vrai_solde = float(limite_restante) if limite_restante is not None else float(balance)
|
||||
|
||||
if vrai_solde > 0 or limite_restante is not None:
|
||||
soldes = charger_soldes()
|
||||
soldes["cagnotte_commune_vip"] = vrai_solde
|
||||
soldes["xavier"] = vrai_solde
|
||||
sauvegarder_soldes(soldes)
|
||||
return vrai_solde
|
||||
except Exception as e:
|
||||
log_event("WARNING", "BILLING", f"Échec de synchronisation OpenRouter : {e}")
|
||||
|
||||
# --- COMPORTEMENT STANDARD POUR LES AUTRES UTILISATEURS ---
|
||||
soldes = charger_soldes()
|
||||
if utilisateur_nom.lower() in VIP_ADMIN_USERS:
|
||||
return soldes.get("cagnotte_commune_vip", 0.0)
|
||||
@@ -177,27 +199,4 @@ def generer_dossier_litige(utilisateur_nom: str, est_admin: bool = False) -> lis
|
||||
"tokens": item.get("tokens"),
|
||||
"montant_facture_ttc": item.get("prix_facture_client")
|
||||
})
|
||||
return dossier
|
||||
|
||||
def calculer_prix_recharge_ttc(montant_credits_usd: float, methode: str = "paypal") -> float:
|
||||
"""
|
||||
Calcule le coût final pour l'utilisateur qui souhaite acheter une recharge de crédits.
|
||||
Le coût final répercute les frais bancaires pour que l'entreprise soit à l'équilibre.
|
||||
|
||||
Frais estimés (Variables selon devises, basés sur des profils Sandbox/Standard) :
|
||||
- PayPal : 2.9% + 0.35$ fixes par transaction
|
||||
- Revolut Business : 1.0% + 0.20$ fixes par transaction en ligne
|
||||
|
||||
Formule d'absorption des frais : Montant_Final = (Credits + Frais_Fixes) / (1 - Pourcentage_Frais)
|
||||
"""
|
||||
if methode.lower() == "paypal":
|
||||
frais_fixes = 0.35
|
||||
frais_variable = 0.029
|
||||
elif methode.lower() == "revolut":
|
||||
frais_fixes = 0.20
|
||||
frais_variable = 0.010
|
||||
else:
|
||||
return montant_credits_usd # Aucune méthode bancaire spécifiée
|
||||
|
||||
montant_a_payer = (montant_credits_usd + frais_fixes) / (1 - frais_variable)
|
||||
return round(montant_a_payer, 2)
|
||||
return dossier
|
||||
Reference in new issue
Block a user