import logging from sqlalchemy.ext.asyncio import AsyncSession from config import settings from misc.pally import BillCreateResponse, Currency, PallyAPIError, PallyClient from repositories import UserRepository from repositories.bills import BillsRepository logger = logging.getLogger(__name__) class BillingService: def __init__(self, user_repository: UserRepository, bills_repository: BillsRepository) -> None: self.user_repository = user_repository self.bills_repository = bills_repository async def pally_initiate_payment( self, session: AsyncSession, pally_client: PallyClient, *, user_id: int, amount: int, currency: Currency | None = Currency.RUB, ) -> BillCreateResponse | None: db_bill = await self.bills_repository.create(session, creator_id=user_id, amount=amount) try: async with pally_client as pl: bill = await pl.bills.create( db_bill.amount, shop_id=settings.pally_shop_id, order_id=db_bill.id, currency_in=currency, ) except PallyAPIError: logger.exception("caught an exception while trying to create a bill:") return None return bill