Добавление сервиса для биллинг
This commit is contained in:
56
app/services/billing_service.py
Normal file
56
app/services/billing_service.py
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
import aiohttp
|
||||||
|
import logging
|
||||||
|
from typing import Optional, Dict, Any
|
||||||
|
|
||||||
|
class BillingService:
|
||||||
|
def __init__(self, base_url: str, api_key: str):
|
||||||
|
self.base_url = base_url
|
||||||
|
self.api_key = api_key
|
||||||
|
self.logger = logging.getLogger(__name__)
|
||||||
|
self.session = None
|
||||||
|
|
||||||
|
async def get_session(self):
|
||||||
|
if self.session is None:
|
||||||
|
self.session = aiohttp.ClientSession(
|
||||||
|
headers={'Authorization': f'Bearer {self.api_key}'}
|
||||||
|
)
|
||||||
|
return self.session
|
||||||
|
|
||||||
|
async def process_payment(self, telegram_id: int, amount: float, plan_name: str) -> Dict[str, Any]:
|
||||||
|
"""Обработка платежа через биллинг-сервис"""
|
||||||
|
try:
|
||||||
|
session = await self.get_session()
|
||||||
|
payload = {
|
||||||
|
"user_id": telegram_id,
|
||||||
|
"amount": amount,
|
||||||
|
"plan_name": plan_name,
|
||||||
|
"currency": "USD"
|
||||||
|
}
|
||||||
|
|
||||||
|
async with session.post(f"{self.base_url}/payments/process", json=payload) as response:
|
||||||
|
if response.status == 200:
|
||||||
|
return await response.json()
|
||||||
|
else:
|
||||||
|
self.logger.error(f"Billing service error: {response.status}")
|
||||||
|
return {"status": "error", "code": "BILLING_SERVICE_ERROR"}
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
self.logger.error(f"Billing service connection error: {str(e)}")
|
||||||
|
return {"status": "error", "code": "CONNECTION_ERROR"}
|
||||||
|
|
||||||
|
async def check_payment_status(self, payment_id: str) -> Dict[str, Any]:
|
||||||
|
"""Проверка статуса платежа"""
|
||||||
|
try:
|
||||||
|
session = await self.get_session()
|
||||||
|
async with session.get(f"{self.base_url}/payments/{payment_id}") as response:
|
||||||
|
if response.status == 200:
|
||||||
|
return await response.json()
|
||||||
|
else:
|
||||||
|
return {"status": "error"}
|
||||||
|
except Exception as e:
|
||||||
|
self.logger.error(f"Billing service check error: {str(e)}")
|
||||||
|
return {"status": "error"}
|
||||||
|
|
||||||
|
async def close(self):
|
||||||
|
if self.session:
|
||||||
|
await self.session.close()
|
||||||
Reference in New Issue
Block a user