Update app.py
Browse files
app.py
CHANGED
|
@@ -567,76 +567,76 @@ async def process_order_flow(user_id: str, message: str) -> str:
|
|
| 567 |
return summary
|
| 568 |
|
| 569 |
if state.step == 7:
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
|
| 587 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 588 |
)
|
| 589 |
-
|
| 590 |
-
|
| 591 |
-
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
|
| 595 |
-
|
| 596 |
-
|
| 597 |
-
|
| 598 |
-
|
| 599 |
-
|
| 600 |
-
|
| 601 |
-
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
|
| 609 |
-
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
|
| 615 |
-
"price": state.data["price"],
|
| 616 |
-
"address": state.data.get("address", "")
|
| 617 |
-
}
|
| 618 |
-
asyncio.create_task(notify_management_order(order_details))
|
| 619 |
-
|
| 620 |
-
email = "customer@example.com"
|
| 621 |
-
payment_data = create_paystack_payment_link(email, total_price * 100, order_id)
|
| 622 |
-
dish_name = state.data.get("dish", "")
|
| 623 |
-
state.reset()
|
| 624 |
-
if user_id in user_state:
|
| 625 |
-
del user_state[user_id]
|
| 626 |
-
if payment_data.get("status"):
|
| 627 |
-
payment_link = payment_data["data"]["authorization_url"]
|
| 628 |
-
return (f"Thank you for your order of {quantity} serving(s) of {dish_name}! "
|
| 629 |
-
f"Your Order ID is {order_id}.\nPlease complete payment here: {payment_link}\n"
|
| 630 |
-
"You can track your order status using your Order ID.\n"
|
| 631 |
-
"Is there anything else you'd like to order?")
|
| 632 |
else:
|
| 633 |
-
|
| 634 |
-
|
| 635 |
-
|
| 636 |
-
|
| 637 |
-
if user_id in user_state:
|
| 638 |
-
del user_state[user_id]
|
| 639 |
-
return "Order canceled. Let me know if you'd like to try again."
|
| 640 |
return ""
|
| 641 |
|
| 642 |
|
|
|
|
| 567 |
return summary
|
| 568 |
|
| 569 |
if state.step == 7:
|
| 570 |
+
if message.lower() in ["yes", "y"]:
|
| 571 |
+
order_id = f"ORD-{int(time.time())}"
|
| 572 |
+
state.data["order_id"] = order_id
|
| 573 |
+
price_per_serving = 1500
|
| 574 |
+
quantity = state.data.get("quantity", 1)
|
| 575 |
+
total_price = quantity * price_per_serving # Removed shipping cost
|
| 576 |
+
state.data["price"] = str(total_price)
|
| 577 |
+
|
| 578 |
+
async def save_order():
|
| 579 |
+
async with async_session() as session:
|
| 580 |
+
order = Order(
|
| 581 |
+
order_id=order_id,
|
| 582 |
+
user_id=user_id,
|
| 583 |
+
dish=state.data["dish"],
|
| 584 |
+
quantity=str(quantity),
|
| 585 |
+
price=str(total_price),
|
| 586 |
+
status="Pending Payment",
|
| 587 |
+
delivery_address=state.data.get("address", "")
|
| 588 |
+
)
|
| 589 |
+
session.add(order)
|
| 590 |
+
await session.commit()
|
| 591 |
+
|
| 592 |
+
# Await the save_order task to ensure the order is committed before proceeding.
|
| 593 |
+
await save_order()
|
| 594 |
+
|
| 595 |
+
asyncio.create_task(log_order_tracking(order_id, "Order Placed", "Order placed and awaiting payment."))
|
| 596 |
+
|
| 597 |
+
async def notify_management_order(order_details: dict):
|
| 598 |
+
message_body = (
|
| 599 |
+
f"New Order Received:\n"
|
| 600 |
+
f"Order ID: {order_details['order_id']}\n"
|
| 601 |
+
f"Dish: {order_details['dish']}\n"
|
| 602 |
+
f"Quantity: {order_details['quantity']}\n"
|
| 603 |
+
f"Total Price: {order_details['price']}\n"
|
| 604 |
+
f"Phone: {state.data.get('phone_number', '')}\n"
|
| 605 |
+
f"Delivery Address: {order_details.get('address', 'Not Provided')}\n"
|
| 606 |
+
f"Extras: {state.data.get('extras', 'None')}\n"
|
| 607 |
+
f"Status: Pending Payment"
|
| 608 |
)
|
| 609 |
+
await asyncio.to_thread(send_whatsapp_message, MANAGEMENT_WHATSAPP_NUMBER, message_body)
|
| 610 |
+
|
| 611 |
+
order_details = {
|
| 612 |
+
"order_id": order_id,
|
| 613 |
+
"dish": state.data["dish"],
|
| 614 |
+
"quantity": state.data["quantity"],
|
| 615 |
+
"price": state.data["price"],
|
| 616 |
+
"address": state.data.get("address", "")
|
| 617 |
+
}
|
| 618 |
+
asyncio.create_task(notify_management_order(order_details))
|
| 619 |
+
|
| 620 |
+
email = "customer@example.com"
|
| 621 |
+
payment_data = create_paystack_payment_link(email, total_price * 100, order_id)
|
| 622 |
+
dish_name = state.data.get("dish", "")
|
| 623 |
+
state.reset()
|
| 624 |
+
if user_id in user_state:
|
| 625 |
+
del user_state[user_id]
|
| 626 |
+
if payment_data.get("status"):
|
| 627 |
+
payment_link = payment_data["data"]["authorization_url"]
|
| 628 |
+
return (f"Thank you for your order of {quantity} serving(s) of {dish_name}! "
|
| 629 |
+
f"Your Order ID is {order_id}.\nPlease complete payment here: {payment_link}\n"
|
| 630 |
+
"You can track your order status using your Order ID.\n"
|
| 631 |
+
"Is there anything else you'd like to order?")
|
| 632 |
+
else:
|
| 633 |
+
return (f"Your order has been placed with Order ID {order_id}, "
|
| 634 |
+
"but we could not initialize payment. Please try again later.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 635 |
else:
|
| 636 |
+
state.reset()
|
| 637 |
+
if user_id in user_state:
|
| 638 |
+
del user_state[user_id]
|
| 639 |
+
return "Order canceled. Let me know if you'd like to try again."
|
|
|
|
|
|
|
|
|
|
| 640 |
return ""
|
| 641 |
|
| 642 |
|