Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -22,6 +22,7 @@ import uuid
|
|
22 |
from sqlalchemy.exc import SQLAlchemyError
|
23 |
from pydantic import BaseModel
|
24 |
import os
|
|
|
25 |
|
26 |
PAYSTACK_SECRET_KEY = os.getenv("PAYSTACK_SECRET_KEY", "your_default_secret_key")
|
27 |
|
@@ -266,43 +267,48 @@ async def on_startup():
|
|
266 |
|
267 |
logger = logging.getLogger(__name__)
|
268 |
|
|
|
269 |
@app.post("/delivery")
|
270 |
async def create_delivery_order(order_req: DeliveryOrderRequest):
|
271 |
try:
|
272 |
-
|
273 |
-
|
274 |
-
# Generate a unique order ID
|
275 |
-
order_id = str(uuid.uuid4())
|
276 |
|
277 |
-
# Extract delivery location
|
278 |
delivery_address = order_req.delivery_address.lower()
|
279 |
-
|
280 |
-
# Determine the shipping cost based on the location
|
281 |
-
shipping_cost = TOWN_SHIPPING_COSTS.get("default", 1000) # Default cost
|
282 |
for town, cost in TOWN_SHIPPING_COSTS.items():
|
283 |
if town in delivery_address:
|
284 |
shipping_cost = cost
|
285 |
-
break
|
286 |
|
287 |
-
# Set
|
288 |
-
item_price = 5000
|
289 |
-
|
290 |
-
# Calculate the total amount (item price + shipping cost)
|
291 |
total_amount = item_price + shipping_cost
|
292 |
-
|
293 |
-
# Convert to kobo (since Paystack expects amount in kobo)
|
294 |
total_amount_kobo = total_amount * 100
|
295 |
|
296 |
-
#
|
297 |
email = getattr(order_req, "email", "[email protected]")
|
298 |
|
299 |
# Generate Paystack payment link
|
300 |
payment_data = create_paystack_payment_link(email, total_amount_kobo, order_id)
|
301 |
|
302 |
-
logger.info(f"Generated payment data: {payment_data}")
|
303 |
-
|
304 |
if payment_data.get("status"):
|
305 |
payment_link = payment_data["data"]["authorization_url"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
306 |
return {
|
307 |
"order_id": order_id,
|
308 |
"total_amount": f"β¦{total_amount}",
|
@@ -319,7 +325,6 @@ async def create_delivery_order(order_req: DeliveryOrderRequest):
|
|
319 |
}
|
320 |
|
321 |
except Exception as e:
|
322 |
-
logger.error(f"Error creating delivery order: {e}", exc_info=True)
|
323 |
raise HTTPException(status_code=500, detail=f"Error: {str(e)}")
|
324 |
|
325 |
@app.post("/chatbot")
|
|
|
22 |
from sqlalchemy.exc import SQLAlchemyError
|
23 |
from pydantic import BaseModel
|
24 |
import os
|
25 |
+
import random
|
26 |
|
27 |
PAYSTACK_SECRET_KEY = os.getenv("PAYSTACK_SECRET_KEY", "your_default_secret_key")
|
28 |
|
|
|
267 |
|
268 |
logger = logging.getLogger(__name__)
|
269 |
|
270 |
+
|
271 |
@app.post("/delivery")
|
272 |
async def create_delivery_order(order_req: DeliveryOrderRequest):
|
273 |
try:
|
274 |
+
# Generate a unique order ID like "DEL-123456789"
|
275 |
+
order_id = f"DEL-{random.randint(100000000, 999999999)}"
|
|
|
|
|
276 |
|
277 |
+
# Extract delivery location
|
278 |
delivery_address = order_req.delivery_address.lower()
|
279 |
+
shipping_cost = TOWN_SHIPPING_COSTS.get("default", 1000)
|
|
|
|
|
280 |
for town, cost in TOWN_SHIPPING_COSTS.items():
|
281 |
if town in delivery_address:
|
282 |
shipping_cost = cost
|
283 |
+
break
|
284 |
|
285 |
+
# Set item price (adjustable)
|
286 |
+
item_price = 5000
|
|
|
|
|
287 |
total_amount = item_price + shipping_cost
|
|
|
|
|
288 |
total_amount_kobo = total_amount * 100
|
289 |
|
290 |
+
# Get user email or default
|
291 |
email = getattr(order_req, "email", "[email protected]")
|
292 |
|
293 |
# Generate Paystack payment link
|
294 |
payment_data = create_paystack_payment_link(email, total_amount_kobo, order_id)
|
295 |
|
|
|
|
|
296 |
if payment_data.get("status"):
|
297 |
payment_link = payment_data["data"]["authorization_url"]
|
298 |
+
|
299 |
+
# WhatsApp Notification
|
300 |
+
whatsapp_message = (
|
301 |
+
f"π¦ *New Delivery Order!*\n"
|
302 |
+
f"π Order ID: {order_id}\n"
|
303 |
+
f"π Pickup: {order_req.pickup_address}\n"
|
304 |
+
f"π Delivery: {order_req.delivery_address}\n"
|
305 |
+
f"βοΈ Contact: {order_req.contact_number}\n"
|
306 |
+
f"π° Amount: β¦{total_amount}\n"
|
307 |
+
f"β
Payment Link: {payment_link}\n"
|
308 |
+
f"π Please confirm and process."
|
309 |
+
)
|
310 |
+
send_whatsapp_message(MANAGEMENT_WHATSAPP_NUMBER, whatsapp_message)
|
311 |
+
|
312 |
return {
|
313 |
"order_id": order_id,
|
314 |
"total_amount": f"β¦{total_amount}",
|
|
|
325 |
}
|
326 |
|
327 |
except Exception as e:
|
|
|
328 |
raise HTTPException(status_code=500, detail=f"Error: {str(e)}")
|
329 |
|
330 |
@app.post("/chatbot")
|