Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,6 +18,8 @@ import logging
|
|
| 18 |
import hmac
|
| 19 |
import hashlib
|
| 20 |
import json
|
|
|
|
|
|
|
| 21 |
from pydantic import BaseModel
|
| 22 |
|
| 23 |
# Configure logging
|
|
@@ -277,6 +279,26 @@ async def enhanced_chatbot_handler(request: Request, bg: BackgroundTasks):
|
|
| 277 |
bg.add_task(update_user_last_interaction, user_id)
|
| 278 |
return JSONResponse(response)
|
| 279 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
|
| 281 |
@app.post("/ux/preferences")
|
| 282 |
async def update_ux_preferences(request: Request):
|
|
@@ -536,11 +558,37 @@ async def create_delivery_order(order_req: DeliveryOrderRequest):
|
|
| 536 |
session.add(new_order)
|
| 537 |
await session.commit()
|
| 538 |
await session.refresh(new_order)
|
| 539 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 540 |
except SQLAlchemyError as e:
|
| 541 |
await session.rollback()
|
| 542 |
raise HTTPException(status_code=500, detail="Error creating delivery order.") from e
|
| 543 |
|
|
|
|
| 544 |
# --------------------
|
| 545 |
# ERROR HANDLING
|
| 546 |
# --------------------
|
|
|
|
| 18 |
import hmac
|
| 19 |
import hashlib
|
| 20 |
import json
|
| 21 |
+
import uuid
|
| 22 |
+
from sqlalchemy.exc import SQLAlchemyError
|
| 23 |
from pydantic import BaseModel
|
| 24 |
|
| 25 |
# Configure logging
|
|
|
|
| 279 |
bg.add_task(update_user_last_interaction, user_id)
|
| 280 |
return JSONResponse(response)
|
| 281 |
|
| 282 |
+
def create_paystack_payment_link(email: str, amount: int, reference: str) -> dict:
|
| 283 |
+
url = "https://api.paystack.co/transaction/initialize"
|
| 284 |
+
headers = {
|
| 285 |
+
"Authorization": f"Bearer {PAYSTACK_SECRET_KEY}",
|
| 286 |
+
"Content-Type": "application/json",
|
| 287 |
+
}
|
| 288 |
+
data = {
|
| 289 |
+
"email": email,
|
| 290 |
+
"amount": amount, # Amount in kobo (i.e. 10000 = ₦100)
|
| 291 |
+
"reference": reference,
|
| 292 |
+
"callback_url": os.getenv("PAYMENT_REDIRECT_URL", "https://default-redirect.com")
|
| 293 |
+
}
|
| 294 |
+
try:
|
| 295 |
+
response = requests.post(url, json=data, headers=headers, timeout=10)
|
| 296 |
+
if response.status_code == 200:
|
| 297 |
+
return response.json()
|
| 298 |
+
else:
|
| 299 |
+
return {"status": False, "message": "Failed to initialize payment."}
|
| 300 |
+
except Exception as e:
|
| 301 |
+
return {"status": False, "message": str(e)}
|
| 302 |
|
| 303 |
@app.post("/ux/preferences")
|
| 304 |
async def update_ux_preferences(request: Request):
|
|
|
|
| 558 |
session.add(new_order)
|
| 559 |
await session.commit()
|
| 560 |
await session.refresh(new_order)
|
| 561 |
+
|
| 562 |
+
# Calculate the total amount (update this logic as needed)
|
| 563 |
+
# For example, you might calculate the amount based on package details or add a fixed delivery fee
|
| 564 |
+
total_amount = 10000 # Amount in kobo (₦100.00); replace with actual logic
|
| 565 |
+
|
| 566 |
+
# Retrieve user's email - use a default if not provided
|
| 567 |
+
email = getattr(order_req, "email", "[email protected]")
|
| 568 |
+
|
| 569 |
+
# Initialize the payment with Paystack
|
| 570 |
+
payment_data = create_paystack_payment_link(email, total_amount, order_id)
|
| 571 |
+
|
| 572 |
+
# Optional: Notify management of the new order (using a WhatsApp API call or similar)
|
| 573 |
+
if payment_data.get("status"):
|
| 574 |
+
payment_link = payment_data["data"]["authorization_url"]
|
| 575 |
+
# You could also schedule a background task to notify management
|
| 576 |
+
# e.g., asyncio.create_task(send_whatsapp_message(MANAGEMENT_WHATSAPP_NUMBER, f"New order {order_id} created."))
|
| 577 |
+
return {
|
| 578 |
+
"delivery_id": new_order.order_id,
|
| 579 |
+
"payment_link": payment_link,
|
| 580 |
+
"message": "Order created successfully. Please complete payment using the link provided."
|
| 581 |
+
}
|
| 582 |
+
else:
|
| 583 |
+
return {
|
| 584 |
+
"delivery_id": new_order.order_id,
|
| 585 |
+
"message": "Order created, but we could not initialize payment. Please try again later."
|
| 586 |
+
}
|
| 587 |
except SQLAlchemyError as e:
|
| 588 |
await session.rollback()
|
| 589 |
raise HTTPException(status_code=500, detail="Error creating delivery order.") from e
|
| 590 |
|
| 591 |
+
|
| 592 |
# --------------------
|
| 593 |
# ERROR HANDLING
|
| 594 |
# --------------------
|