From 61fadc5c0d012cf196e464dbaaabe7c8cb8c9912 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 28 Nov 2025 18:35:55 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D1=81=D0=B5=D1=80=D0=B2=D0=B8=D1=81=D0=B0?= =?UTF-8?q?=20=D0=B4=D0=BB=D1=8F=20=D0=B1=D0=B8=D0=BB=D0=BB=D0=B8=D0=BD?= =?UTF-8?q?=D0=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/services/billing_service.py | 56 +++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 app/services/billing_service.py diff --git a/app/services/billing_service.py b/app/services/billing_service.py new file mode 100644 index 0000000..fab8ec6 --- /dev/null +++ b/app/services/billing_service.py @@ -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() \ No newline at end of file