Fred808 commited on
Commit
e22ce36
·
verified ·
1 Parent(s): d2b4fad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -49
app.py CHANGED
@@ -543,59 +543,58 @@ async def send_proactive_update(user_id: str, update_type: str):
543
 
544
  return response
545
 
 
546
  @app.post("/delivery")
547
  async def create_delivery_order(order_req: DeliveryOrderRequest):
548
- async with async_session() as session:
549
- try:
550
- # Generate a unique order ID
551
- order_id = str(uuid4())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
552
 
553
- # Ensure all fields are properly assigned (avoid NULL errors)
554
- new_order = Order(
555
- order_id=order_id,
556
- user_id=order_req.user_id,
557
- status="pending",
558
- payment_reference=None, # Ensure it is explicitly set
559
- last_location=None, # Ensure it is explicitly set
560
- package_description=order_req.package_description or "Food package",
561
- pickup_address=order_req.pickup_address or "Unknown Pickup Address",
562
- delivery_address=order_req.delivery_address or "Unknown Delivery Address",
563
- contact_number=order_req.contact_number or "Unknown Contact Number",
564
- )
565
 
566
- # Add order to the database
567
- session.add(new_order)
568
- await session.commit()
569
- await session.refresh(new_order) # Refresh object to ensure DB commit
570
-
571
- # Calculate the total amount (update logic as needed)
572
- total_amount = 10000 # Amount in kobo (₦100.00); update this as required
573
-
574
- # Retrieve user's email (use a default if not provided)
575
- email = getattr(order_req, "email", "[email protected]")
576
-
577
- # Initialize payment with Paystack
578
- payment_data = create_paystack_payment_link(email, total_amount, order_id)
579
-
580
- if payment_data.get("status"):
581
- payment_link = payment_data["data"]["authorization_url"]
582
- return {
583
- "delivery_id": new_order.order_id,
584
- "payment_link": payment_link,
585
- "message": "Order created successfully. Please complete payment using the link provided."
586
- }
587
- else:
588
- return {
589
- "delivery_id": new_order.order_id,
590
- "message": "Order created, but payment initialization failed. Please try again later."
591
- }
592
-
593
- except SQLAlchemyError as e:
594
- await session.rollback()
595
- raise HTTPException(status_code=500, detail=f"Database error: {str(e)}") from e
596
- except Exception as e:
597
- await session.rollback()
598
- raise HTTPException(status_code=500, detail=f"Unexpected error: {str(e)}") from e
599
  # --------------------
600
  # ERROR HANDLING
601
  # --------------------
 
543
 
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
600
  # --------------------