Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -251,6 +251,64 @@ app = FastAPI(title="Delivery Service Chatbot")
|
|
| 251 |
async def on_startup():
|
| 252 |
await init_db()
|
| 253 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 254 |
@app.post("/chatbot")
|
| 255 |
async def enhanced_chatbot_handler(request: Request, bg: BackgroundTasks):
|
| 256 |
data = await request.json()
|
|
@@ -282,6 +340,7 @@ async def enhanced_chatbot_handler(request: Request, bg: BackgroundTasks):
|
|
| 282 |
bg.add_task(update_user_last_interaction, user_id)
|
| 283 |
return JSONResponse(response)
|
| 284 |
|
|
|
|
| 285 |
def create_paystack_payment_link(email: str, amount: int, reference: str) -> dict:
|
| 286 |
url = "https://api.paystack.co/transaction/initialize"
|
| 287 |
headers = {
|
|
@@ -290,18 +349,13 @@ def create_paystack_payment_link(email: str, amount: int, reference: str) -> dic
|
|
| 290 |
}
|
| 291 |
data = {
|
| 292 |
"email": email,
|
| 293 |
-
"amount": amount, # Amount in kobo (
|
| 294 |
"reference": reference,
|
| 295 |
-
"callback_url":
|
| 296 |
}
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
return response.json()
|
| 301 |
-
else:
|
| 302 |
-
return {"status": False, "message": "Failed to initialize payment."}
|
| 303 |
-
except Exception as e:
|
| 304 |
-
return {"status": False, "message": str(e)}
|
| 305 |
|
| 306 |
@app.post("/ux/preferences")
|
| 307 |
async def update_ux_preferences(request: Request):
|
|
@@ -544,56 +598,6 @@ async def send_proactive_update(user_id: str, update_type: str):
|
|
| 544 |
return response
|
| 545 |
|
| 546 |
|
| 547 |
-
@app.post("/delivery")
|
| 548 |
-
async def create_delivery_order(order_req: DeliveryOrderRequest):
|
| 549 |
-
try:
|
| 550 |
-
# Generate a unique order ID
|
| 551 |
-
order_id = str(uuid.uuid4())
|
| 552 |
-
|
| 553 |
-
# Extract delivery location from the request
|
| 554 |
-
delivery_address = order_req.delivery_address.lower()
|
| 555 |
-
|
| 556 |
-
# Determine the shipping cost based on the location
|
| 557 |
-
shipping_cost = TOWN_SHIPPING_COSTS.get("default", 1000) # Default cost
|
| 558 |
-
for town, cost in TOWN_SHIPPING_COSTS.items():
|
| 559 |
-
if town in delivery_address:
|
| 560 |
-
shipping_cost = cost
|
| 561 |
-
break # Stop once a match is found
|
| 562 |
-
|
| 563 |
-
# Set a fixed item price (Modify this if price varies per package)
|
| 564 |
-
item_price = 5000 # ₦5000 for the package (adjust as needed)
|
| 565 |
-
|
| 566 |
-
# Calculate the total amount (item price + shipping cost)
|
| 567 |
-
total_amount = item_price + shipping_cost
|
| 568 |
-
|
| 569 |
-
# Convert to kobo (since Paystack expects amount in kobo)
|
| 570 |
-
total_amount_kobo = total_amount * 100
|
| 571 |
-
|
| 572 |
-
# Retrieve user's email (use a default if not provided)
|
| 573 |
-
email = getattr(order_req, "email", "[email protected]")
|
| 574 |
-
|
| 575 |
-
# Generate Paystack payment link
|
| 576 |
-
payment_data = create_paystack_payment_link(email, total_amount_kobo, order_id)
|
| 577 |
-
|
| 578 |
-
if payment_data.get("status"):
|
| 579 |
-
payment_link = payment_data["data"]["authorization_url"]
|
| 580 |
-
return {
|
| 581 |
-
"order_id": order_id,
|
| 582 |
-
"total_amount": f"₦{total_amount}",
|
| 583 |
-
"shipping_cost": f"₦{shipping_cost}",
|
| 584 |
-
"payment_link": payment_link,
|
| 585 |
-
"message": "Order created successfully. Please complete payment using the link provided."
|
| 586 |
-
}
|
| 587 |
-
else:
|
| 588 |
-
return {
|
| 589 |
-
"order_id": order_id,
|
| 590 |
-
"total_amount": f"₦{total_amount}",
|
| 591 |
-
"shipping_cost": f"₦{shipping_cost}",
|
| 592 |
-
"message": "Order created, but payment initialization failed. Please try again later."
|
| 593 |
-
}
|
| 594 |
-
|
| 595 |
-
except Exception as e:
|
| 596 |
-
raise HTTPException(status_code=500, detail=f"Error: {str(e)}")
|
| 597 |
|
| 598 |
# --------------------
|
| 599 |
# ERROR HANDLING
|
|
|
|
| 251 |
async def on_startup():
|
| 252 |
await init_db()
|
| 253 |
|
| 254 |
+
logger = logging.getLogger(__name__)
|
| 255 |
+
|
| 256 |
+
@app.post("/delivery")
|
| 257 |
+
async def create_delivery_order(order_req: DeliveryOrderRequest):
|
| 258 |
+
try:
|
| 259 |
+
logger.info(f"Received order request: {order_req}")
|
| 260 |
+
|
| 261 |
+
# Generate a unique order ID
|
| 262 |
+
order_id = str(uuid.uuid4())
|
| 263 |
+
|
| 264 |
+
# Extract delivery location from the request
|
| 265 |
+
delivery_address = order_req.delivery_address.lower()
|
| 266 |
+
|
| 267 |
+
# Determine the shipping cost based on the location
|
| 268 |
+
shipping_cost = TOWN_SHIPPING_COSTS.get("default", 1000) # Default cost
|
| 269 |
+
for town, cost in TOWN_SHIPPING_COSTS.items():
|
| 270 |
+
if town in delivery_address:
|
| 271 |
+
shipping_cost = cost
|
| 272 |
+
break # Stop once a match is found
|
| 273 |
+
|
| 274 |
+
# Set a fixed item price (Modify this if price varies per package)
|
| 275 |
+
item_price = 5000 # ₦5000 for the package (adjust as needed)
|
| 276 |
+
|
| 277 |
+
# Calculate the total amount (item price + shipping cost)
|
| 278 |
+
total_amount = item_price + shipping_cost
|
| 279 |
+
|
| 280 |
+
# Convert to kobo (since Paystack expects amount in kobo)
|
| 281 |
+
total_amount_kobo = total_amount * 100
|
| 282 |
+
|
| 283 |
+
# Retrieve user's email (use a default if not provided)
|
| 284 |
+
email = getattr(order_req, "email", "[email protected]")
|
| 285 |
+
|
| 286 |
+
# Generate Paystack payment link
|
| 287 |
+
payment_data = create_paystack_payment_link(email, total_amount_kobo, order_id)
|
| 288 |
+
|
| 289 |
+
logger.info(f"Generated payment data: {payment_data}")
|
| 290 |
+
|
| 291 |
+
if payment_data.get("status"):
|
| 292 |
+
payment_link = payment_data["data"]["authorization_url"]
|
| 293 |
+
return {
|
| 294 |
+
"order_id": order_id,
|
| 295 |
+
"total_amount": f"₦{total_amount}",
|
| 296 |
+
"shipping_cost": f"₦{shipping_cost}",
|
| 297 |
+
"payment_link": payment_link,
|
| 298 |
+
"message": "Order created successfully. Please complete payment using the link provided."
|
| 299 |
+
}
|
| 300 |
+
else:
|
| 301 |
+
return {
|
| 302 |
+
"order_id": order_id,
|
| 303 |
+
"total_amount": f"₦{total_amount}",
|
| 304 |
+
"shipping_cost": f"₦{shipping_cost}",
|
| 305 |
+
"message": "Order created, but payment initialization failed. Please try again later."
|
| 306 |
+
}
|
| 307 |
+
|
| 308 |
+
except Exception as e:
|
| 309 |
+
logger.error(f"Error creating delivery order: {e}", exc_info=True)
|
| 310 |
+
raise HTTPException(status_code=500, detail=f"Error: {str(e)}")
|
| 311 |
+
|
| 312 |
@app.post("/chatbot")
|
| 313 |
async def enhanced_chatbot_handler(request: Request, bg: BackgroundTasks):
|
| 314 |
data = await request.json()
|
|
|
|
| 340 |
bg.add_task(update_user_last_interaction, user_id)
|
| 341 |
return JSONResponse(response)
|
| 342 |
|
| 343 |
+
|
| 344 |
def create_paystack_payment_link(email: str, amount: int, reference: str) -> dict:
|
| 345 |
url = "https://api.paystack.co/transaction/initialize"
|
| 346 |
headers = {
|
|
|
|
| 349 |
}
|
| 350 |
data = {
|
| 351 |
"email": email,
|
| 352 |
+
"amount": amount, # Amount must be in kobo (₦100 = 10000 kobo)
|
| 353 |
"reference": reference,
|
| 354 |
+
"callback_url": "https://your-website.com/payment_callback"
|
| 355 |
}
|
| 356 |
+
response = requests.post(url, json=data, headers=headers, timeout=10)
|
| 357 |
+
return response.json() if response.status_code == 200 else {"status": False, "message": "Payment initialization failed"}
|
| 358 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 359 |
|
| 360 |
@app.post("/ux/preferences")
|
| 361 |
async def update_ux_preferences(request: Request):
|
|
|
|
| 598 |
return response
|
| 599 |
|
| 600 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 601 |
|
| 602 |
# --------------------
|
| 603 |
# ERROR HANDLING
|