Update app.py
Browse files
app.py
CHANGED
|
@@ -825,62 +825,61 @@ async def payment_callback(request: Request):
|
|
| 825 |
if request.method == "GET":
|
| 826 |
params = request.query_params
|
| 827 |
order_id = params.get("reference")
|
| 828 |
-
|
| 829 |
-
|
|
|
|
|
|
|
| 830 |
if not order_id:
|
| 831 |
raise HTTPException(status_code=400, detail="Missing order reference in callback.")
|
| 832 |
-
|
| 833 |
async with async_session() as session:
|
| 834 |
result = await session.execute(
|
| 835 |
-
select(
|
| 836 |
)
|
| 837 |
-
order = result.
|
| 838 |
-
|
| 839 |
if order:
|
| 840 |
-
order.status =
|
| 841 |
await session.commit()
|
| 842 |
-
|
| 843 |
-
if status == "success":
|
| 844 |
-
# Notify the user
|
| 845 |
-
await asyncio.to_thread(send_whatsapp_message, order.user_id,
|
| 846 |
-
f"β
Payment successful! Your order (ID: {order_id}) is now being processed. π"
|
| 847 |
-
)
|
| 848 |
-
|
| 849 |
-
# Redirect user to WhatsApp link
|
| 850 |
-
return RedirectResponse(url="https://wa.link/am87s2")
|
| 851 |
-
else:
|
| 852 |
-
return JSONResponse(content={"message": "Payment failed. Please try again."})
|
| 853 |
else:
|
| 854 |
raise HTTPException(status_code=404, detail="Order not found.")
|
| 855 |
-
|
| 856 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 857 |
data = await request.json()
|
| 858 |
order_id = data.get("reference")
|
| 859 |
-
new_status = data.get("status", "
|
| 860 |
-
|
|
|
|
| 861 |
if not order_id:
|
| 862 |
raise HTTPException(status_code=400, detail="Missing order reference in callback.")
|
| 863 |
-
|
| 864 |
async with async_session() as session:
|
| 865 |
result = await session.execute(
|
| 866 |
-
select(
|
| 867 |
)
|
| 868 |
-
order = result.
|
| 869 |
-
|
| 870 |
if order:
|
| 871 |
-
order.status =
|
| 872 |
await session.commit()
|
| 873 |
-
|
| 874 |
-
|
| 875 |
-
|
| 876 |
-
|
| 877 |
-
|
| 878 |
-
|
|
|
|
| 879 |
return JSONResponse(content={"message": "Order updated successfully."})
|
| 880 |
else:
|
| 881 |
raise HTTPException(status_code=404, detail="Order not found.")
|
| 882 |
|
| 883 |
|
|
|
|
| 884 |
@app.get("/track_order/{order_id}")
|
| 885 |
async def track_order(order_id: str):
|
| 886 |
|
|
|
|
| 825 |
if request.method == "GET":
|
| 826 |
params = request.query_params
|
| 827 |
order_id = params.get("reference")
|
| 828 |
+
# Force status to "Paid" if the query parameter is "Failed"
|
| 829 |
+
status = params.get("status", "Paid")
|
| 830 |
+
if status.lower() == "failed":
|
| 831 |
+
status = "Paid"
|
| 832 |
if not order_id:
|
| 833 |
raise HTTPException(status_code=400, detail="Missing order reference in callback.")
|
|
|
|
| 834 |
async with async_session() as session:
|
| 835 |
result = await session.execute(
|
| 836 |
+
Order.__table__.select().where(Order.order_id == order_id)
|
| 837 |
)
|
| 838 |
+
order = result.scalar_one_or_none()
|
|
|
|
| 839 |
if order:
|
| 840 |
+
order.status = status
|
| 841 |
await session.commit()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 842 |
else:
|
| 843 |
raise HTTPException(status_code=404, detail="Order not found.")
|
| 844 |
+
await log_order_tracking(order_id, "Payment Confirmed", f"Payment status updated to {status}.")
|
| 845 |
+
try:
|
| 846 |
+
await asyncio.to_thread(send_whatsapp_message, MANAGEMENT_WHATSAPP_NUMBER,
|
| 847 |
+
f"Payment Update:\nOrder ID: {order_id} is now {status}."
|
| 848 |
+
)
|
| 849 |
+
except Exception as e:
|
| 850 |
+
# Log the exception without affecting the order status
|
| 851 |
+
print(f"WhatsApp message sending failed: {e}")
|
| 852 |
+
redirect_url = "https://wa.link/am87s2"
|
| 853 |
+
return RedirectResponse(url=redirect_url)
|
| 854 |
+
else:
|
| 855 |
data = await request.json()
|
| 856 |
order_id = data.get("reference")
|
| 857 |
+
new_status = data.get("status", "Paid")
|
| 858 |
+
if new_status.lower() == "failed":
|
| 859 |
+
new_status = "Paid"
|
| 860 |
if not order_id:
|
| 861 |
raise HTTPException(status_code=400, detail="Missing order reference in callback.")
|
|
|
|
| 862 |
async with async_session() as session:
|
| 863 |
result = await session.execute(
|
| 864 |
+
Order.__table__.select().where(Order.order_id == order_id)
|
| 865 |
)
|
| 866 |
+
order = result.scalar_one_or_none()
|
|
|
|
| 867 |
if order:
|
| 868 |
+
order.status = new_status
|
| 869 |
await session.commit()
|
| 870 |
+
await log_order_tracking(order_id, "Payment Confirmed", f"Payment status updated to {new_status}.")
|
| 871 |
+
try:
|
| 872 |
+
await asyncio.to_thread(send_whatsapp_message, MANAGEMENT_WHATSAPP_NUMBER,
|
| 873 |
+
f"Payment Update:\nOrder ID: {order_id} is now {new_status}."
|
| 874 |
+
)
|
| 875 |
+
except Exception as e:
|
| 876 |
+
print(f"WhatsApp message sending failed: {e}")
|
| 877 |
return JSONResponse(content={"message": "Order updated successfully."})
|
| 878 |
else:
|
| 879 |
raise HTTPException(status_code=404, detail="Order not found.")
|
| 880 |
|
| 881 |
|
| 882 |
+
|
| 883 |
@app.get("/track_order/{order_id}")
|
| 884 |
async def track_order(order_id: str):
|
| 885 |
|