Update app.py
Browse files
app.py
CHANGED
|
@@ -693,68 +693,68 @@ async def process_order_flow(user_id: str, message: str) -> str:
|
|
| 693 |
|
| 694 |
# 7) Step 7: Order Confirmation and Payment Link Generation
|
| 695 |
if state and state.flow == "order" and state.step == 7:
|
| 696 |
-
|
| 697 |
-
|
| 698 |
-
|
| 699 |
-
|
| 700 |
-
|
| 701 |
-
|
| 702 |
-
|
| 703 |
-
|
| 704 |
-
|
| 705 |
-
|
| 706 |
-
|
| 707 |
-
|
| 708 |
-
|
| 709 |
-
|
| 710 |
-
|
| 711 |
-
|
| 712 |
-
|
| 713 |
-
|
| 714 |
-
|
| 715 |
-
|
| 716 |
-
|
| 717 |
-
|
| 718 |
-
|
| 719 |
-
|
| 720 |
-
|
| 721 |
-
|
| 722 |
-
|
| 723 |
-
|
| 724 |
-
|
| 725 |
-
|
| 726 |
-
|
| 727 |
-
|
| 728 |
-
|
| 729 |
-
|
| 730 |
-
|
| 731 |
-
|
| 732 |
-
|
| 733 |
-
|
| 734 |
-
|
| 735 |
-
|
| 736 |
-
|
| 737 |
-
|
| 738 |
-
|
| 739 |
-
|
| 740 |
-
|
| 741 |
-
|
| 742 |
-
|
| 743 |
-
|
| 744 |
-
|
| 745 |
-
|
| 746 |
-
|
| 747 |
-
|
| 748 |
-
|
| 749 |
-
|
| 750 |
-
|
| 751 |
-
|
| 752 |
-
|
| 753 |
-
|
| 754 |
-
|
| 755 |
-
|
| 756 |
-
|
| 757 |
-
|
| 758 |
|
| 759 |
# Final fallback
|
| 760 |
return ""
|
|
|
|
| 693 |
|
| 694 |
# 7) Step 7: Order Confirmation and Payment Link Generation
|
| 695 |
if state and state.flow == "order" and state.step == 7:
|
| 696 |
+
if message.lower() in ["yes", "y"]:
|
| 697 |
+
order_id = f"ORD-{int(time.time())}"
|
| 698 |
+
state.data["order_id"] = order_id
|
| 699 |
+
# Calculate total price differently for multi-dish orders:
|
| 700 |
+
if "orders" in state.data:
|
| 701 |
+
total_price = 0
|
| 702 |
+
for dish, qty in state.data["orders"].items():
|
| 703 |
+
total_price += get_dish_price(dish) * qty
|
| 704 |
+
total_price += state.data.get("shipping_cost", 0)
|
| 705 |
+
dish_summary = ", ".join(state.data["orders"].keys())
|
| 706 |
+
quantity_total = sum(state.data["orders"].values())
|
| 707 |
+
else:
|
| 708 |
+
dish_summary = state.data.get("dish", "")
|
| 709 |
+
quantity_total = state.data.get("quantity", 1)
|
| 710 |
+
total_price = (quantity_total * get_dish_price(dish_summary)) + state.data.get("shipping_cost", 0)
|
| 711 |
+
state.data["price"] = str(total_price)
|
| 712 |
+
|
| 713 |
+
async def save_order():
|
| 714 |
+
async with async_session() as session:
|
| 715 |
+
order = Order(
|
| 716 |
+
order_id=order_id,
|
| 717 |
+
user_id=user_id,
|
| 718 |
+
dish=dish_summary,
|
| 719 |
+
quantity=str(quantity_total),
|
| 720 |
+
price=str(total_price),
|
| 721 |
+
status="Pending Payment",
|
| 722 |
+
delivery_address=state.data.get("address", "")
|
| 723 |
+
)
|
| 724 |
+
session.add(order)
|
| 725 |
+
await session.commit()
|
| 726 |
+
asyncio.create_task(save_order())
|
| 727 |
+
asyncio.create_task(log_order_tracking(order_id, "Order Placed", "Order placed and awaiting payment."))
|
| 728 |
+
|
| 729 |
+
# Email notification and payment link code here (omitted for brevity)
|
| 730 |
+
email_for_paystack = "customer@example.com"
|
| 731 |
+
payment_data = create_paystack_payment_link(email_for_paystack, total_price * 100, order_id)
|
| 732 |
+
state.reset()
|
| 733 |
+
if user_id in user_state:
|
| 734 |
+
del user_state[user_id]
|
| 735 |
+
if payment_data.get("status"):
|
| 736 |
+
payment_link = payment_data["data"]["authorization_url"]
|
| 737 |
+
return (f"Thank you for your order of {quantity_total} serving(s) of {dish_summary}! "
|
| 738 |
+
f"Your Order ID is {order_id}.\n\n"
|
| 739 |
+
"Please complete your payment using one of the following options:\n"
|
| 740 |
+
f"1. Pay online via our Paystack link: {payment_link}\n"
|
| 741 |
+
"2. Alternatively, you can make a bank transfer to the following account:\n"
|
| 742 |
+
" Account Number: 1433042821\n"
|
| 743 |
+
" Bank: Access Bank\n"
|
| 744 |
+
" Account Name: Angelo Food Court 2\n\n"
|
| 745 |
+
"If you choose the bank transfer option, please send a screenshot of your payment confirmation to this chatbot.\n\n"
|
| 746 |
+
"You can track your order status using your Order ID.\n"
|
| 747 |
+
"Is there anything else you'd like to order?")
|
| 748 |
+
else:
|
| 749 |
+
return (f"Your order has been placed with Order ID {order_id}, "
|
| 750 |
+
"but we could not initialize online payment. Please try again later, or "
|
| 751 |
+
"you may opt to pay via bank transfer to Account Number 1433042821, Access Bank, Angelo Food Court 2 "
|
| 752 |
+
"and send your payment screenshot to this chatbot.")
|
| 753 |
+
elif message.lower() in ["no", "n"]:
|
| 754 |
+
state.reset()
|
| 755 |
+
if user_id in user_state:
|
| 756 |
+
del user_state[user_id]
|
| 757 |
+
return "Order canceled. Let me know if you'd like to try again."
|
| 758 |
|
| 759 |
# Final fallback
|
| 760 |
return ""
|