invoices, order handling, db updates
This commit is contained in:
@@ -1 +1,3 @@
|
||||
routers = []
|
||||
from .inline_mode import router as inline_router
|
||||
|
||||
routers = [inline_router]
|
||||
|
||||
71
handlers/admins/inline_mode.py
Normal file
71
handlers/admins/inline_mode.py
Normal file
@@ -0,0 +1,71 @@
|
||||
import logging
|
||||
|
||||
from aiogram import Router, F
|
||||
from aiogram.types import (
|
||||
ChosenInlineResult,
|
||||
InlineQuery,
|
||||
InlineQueryResultArticle,
|
||||
InputTextMessageContent,
|
||||
)
|
||||
from aiogram.utils.deep_linking import create_start_link
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from misc.filters import IsVerified
|
||||
from misc.kb.admins import payment_link, placeholder_kb
|
||||
from misc.utils import b64_to_dict, dict_to_b64
|
||||
from repositories.invoices import InvoiceRepository
|
||||
|
||||
router = Router()
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@router.inline_query(IsVerified(), F.query.cast(int).as_("amount"))
|
||||
async def send_invoice(iq: InlineQuery, amount: int):
|
||||
results = [
|
||||
InlineQueryResultArticle(
|
||||
id=dict_to_b64({"a": amount}),
|
||||
title=f"💸 Счёт на {amount}₽",
|
||||
input_message_content=InputTextMessageContent(
|
||||
message_text="⏳ Создаю счёт..."
|
||||
),
|
||||
reply_markup=placeholder_kb,
|
||||
description="Нажмите, чтобы создать счёт.",
|
||||
),
|
||||
]
|
||||
await iq.answer(results, is_personal=True)
|
||||
|
||||
|
||||
@router.chosen_inline_result()
|
||||
async def chosen_inline(
|
||||
ev: ChosenInlineResult, session: AsyncSession, invoice_repo: InvoiceRepository
|
||||
):
|
||||
data = b64_to_dict(ev.result_id)
|
||||
amount = data.get("a")
|
||||
if not amount:
|
||||
await ev.bot.edit_message_text(
|
||||
inline_message_id=ev.inline_message_id, text="🍃 Что-то пошло не так..."
|
||||
)
|
||||
logger.critical("amount is null during the invoice creation.")
|
||||
return
|
||||
|
||||
try:
|
||||
amount = int(amount)
|
||||
except Exception as e:
|
||||
await ev.bot.edit_message_text(
|
||||
inline_message_id=ev.inline_message_id, text="🍃 Что-то пошло не так..."
|
||||
)
|
||||
logger.exception(e)
|
||||
return
|
||||
|
||||
invoice = await invoice_repo.create_invoice(
|
||||
session,
|
||||
amount=amount,
|
||||
creator_id=ev.from_user.id,
|
||||
inline_message_id=ev.inline_message_id,
|
||||
)
|
||||
invoice_link = await create_start_link(ev.bot, f"invoice:{invoice.id}", encode=True)
|
||||
await ev.bot.edit_message_text(
|
||||
inline_message_id=ev.inline_message_id,
|
||||
text=f"<b>💸 Счёт на {amount}₽.</b>",
|
||||
reply_markup=payment_link(invoice_link),
|
||||
)
|
||||
@@ -1,3 +1,4 @@
|
||||
from .invoices import router as invoice_router
|
||||
from .menu import router as menu_router
|
||||
from .catalogue import router as catalogue_router
|
||||
from .products import router as products_router
|
||||
@@ -6,6 +7,7 @@ from .checkout import router as checkout_router
|
||||
from .security import router as security_router
|
||||
|
||||
routers = [
|
||||
invoice_router,
|
||||
menu_router,
|
||||
catalogue_router,
|
||||
products_router,
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
import logging
|
||||
from typing import Optional
|
||||
from aiogram import Router, F
|
||||
from aiogram.types import CallbackQuery, Message
|
||||
from aiogram.fsm.context import FSMContext
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from config import NOTIFICATION_CHANNEL
|
||||
from dto.checkout import CheckoutContext
|
||||
from misc.kb.admins import customer_contacts
|
||||
from misc.kb.client import (
|
||||
order_confirmation,
|
||||
order_specs_confirmation,
|
||||
@@ -16,6 +20,7 @@ from repositories.orders import OrderRepository
|
||||
from services.orders import OrderService
|
||||
|
||||
router = Router()
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@router.callback_query(F.data == "checkout")
|
||||
@@ -119,4 +124,55 @@ async def checkout_address(msg: Message, state: FSMContext):
|
||||
f"<i>📍 Ваш адрес: {ctx.address}</i>\n\n"
|
||||
"━━━━━━━━━━━━━━",
|
||||
reply_markup=order_specs_confirmation(ctx.order_id),
|
||||
) # TODO: State-driven Payment System.
|
||||
)
|
||||
|
||||
await state.set_state(CheckoutStorage.confirmation)
|
||||
await state.set_data({"ctx": ctx})
|
||||
|
||||
|
||||
@router.callback_query(CheckoutStorage.confirmation)
|
||||
@router.callback_query(F.data.startswith("payment:"))
|
||||
async def payment_create(
|
||||
cb: CallbackQuery,
|
||||
state: FSMContext,
|
||||
session: AsyncSession,
|
||||
order_service: OrderService,
|
||||
):
|
||||
data = await state.get_data()
|
||||
ctx: Optional[CheckoutContext] = data.get("ctx")
|
||||
if not ctx:
|
||||
await cb.message.edit_text(
|
||||
"🍃 Что-то пошло не так, повторите попытку позже...",
|
||||
reply_markup=return_menu,
|
||||
)
|
||||
logger.warning("ctx not found on confirmation screen")
|
||||
return
|
||||
|
||||
await state.clear()
|
||||
|
||||
await cb.message.edit_text(
|
||||
"<b>🟢 Ваш заказ успешно отправлен!</b>\n"
|
||||
"<i>📦 Мы свяжемся с вами для уточнения деталей в ближайшее время.</i>\n\n"
|
||||
"<b>‼️ Чтобы убедиться, что вы общаетесь с доверенным контактом ШВЕЙТЕХЦЕНТР, перешлите любое сообщение человека в этот чат.</b>",
|
||||
reply_markup=return_menu,
|
||||
)
|
||||
|
||||
cart = await order_service.build_full_cart_dto(session, cb.from_user.id)
|
||||
|
||||
try:
|
||||
await cb.bot.send_message(
|
||||
NOTIFICATION_CHANNEL,
|
||||
"🟢 Новый заказ.\n\n"
|
||||
"━━━━━━━━━━━━━━\n\n"
|
||||
f"<i>👤 Имя: {ctx.name}</i>\n"
|
||||
f"<i>📱 Номер телефона: {ctx.phone}</i>\n"
|
||||
f"<i>📍 Адрес: {ctx.address}</i>\n\n"
|
||||
"━━━━━━━━━━━━━━\n"
|
||||
f"{get_order_item_list(cart)}",
|
||||
reply_markup=customer_contacts(
|
||||
cb.from_user.id, bool(cb.from_user.username), cb.from_user.username
|
||||
),
|
||||
)
|
||||
except Exception as e:
|
||||
logger.exception(e)
|
||||
return
|
||||
|
||||
29
handlers/client/invoices.py
Normal file
29
handlers/client/invoices.py
Normal file
@@ -0,0 +1,29 @@
|
||||
import logging
|
||||
|
||||
from aiogram import Router
|
||||
from aiogram.filters import CommandObject, CommandStart
|
||||
from aiogram.types import (
|
||||
Message,
|
||||
)
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from repositories.invoices import InvoiceRepository
|
||||
|
||||
router = Router()
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@router.message(CommandStart(deep_link=True, deep_link_encoded=True))
|
||||
async def activate_invoice(
|
||||
msg: Message,
|
||||
command: CommandObject,
|
||||
session: AsyncSession,
|
||||
invoice_repo: InvoiceRepository,
|
||||
):
|
||||
payload = command.args
|
||||
|
||||
invoice_id = int(payload.split(":")[1])
|
||||
invoice = await invoice_repo.get_invoice_by_id(session, invoice_id)
|
||||
await msg.answer(
|
||||
f"<b>🟢 Получен счёт на {invoice.amount}₽</b>"
|
||||
) # TODO: Process payment
|
||||
@@ -5,6 +5,7 @@ from aiogram.fsm.context import FSMContext
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from misc.kb import main_menu_kb
|
||||
from repositories.order_items import OrderItemRepository
|
||||
from services.orders import OrderService
|
||||
|
||||
router = Router()
|
||||
@@ -12,11 +13,16 @@ router = Router()
|
||||
|
||||
@router.message(Command("start"))
|
||||
async def main_menu(
|
||||
msg: Message, state: FSMContext, session: AsyncSession, order_service: OrderService
|
||||
msg: Message,
|
||||
state: FSMContext,
|
||||
session: AsyncSession,
|
||||
order_items_repo: OrderItemRepository,
|
||||
):
|
||||
await state.clear()
|
||||
|
||||
cart_items = await order_service.get_cart_items_count(session, msg.from_user.id)
|
||||
cart_items = await order_items_repo.get_items_count_by_customer(
|
||||
session, msg.from_user.id
|
||||
)
|
||||
await msg.answer(
|
||||
"hii!", reply_markup=main_menu_kb(cart_items)
|
||||
) # TODO: Write a welcome message
|
||||
@@ -27,11 +33,13 @@ async def main_menu_cb(
|
||||
cb: CallbackQuery,
|
||||
state: FSMContext,
|
||||
session: AsyncSession,
|
||||
order_service: OrderService,
|
||||
order_items_repo: OrderService,
|
||||
):
|
||||
await state.clear()
|
||||
|
||||
cart_items = await order_service.get_cart_items_count(session, cb.from_user.id)
|
||||
cart_items = await order_items_repo.get_items_count_by_customer(
|
||||
session, cb.from_user.id
|
||||
)
|
||||
await cb.message.edit_text(
|
||||
"hii!", reply_markup=main_menu_kb(cart_items)
|
||||
) # TODO: Write a welcome message
|
||||
|
||||
Reference in New Issue
Block a user