feat: implement billing system with Pally integration
- Added billing models and repository for handling bills. - Introduced BillingService to manage payment initiation and bill creation. - Updated user service to remove ticket-related functionality. - Refactored settings to include new billing-related fields. - Removed ticket-related handlers and schemas. - Added new billing handlers for processing payments. - Updated database schema with new bills table and status enum. - Enhanced utility functions to calculate prices considering discounts. - Introduced prehandling for non-private messages. - Updated main application to initialize new billing services and repositories.
This commit is contained in:
41
services/billing_service.py
Normal file
41
services/billing_service.py
Normal file
@@ -0,0 +1,41 @@
|
||||
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
|
||||
Reference in New Issue
Block a user