Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -349,8 +349,8 @@ def update_quantity():
|
|
349 |
return jsonify({"success": False, "error": str(e)}), 500
|
350 |
@app.route("/checkout", methods=["POST"])
|
351 |
def checkout():
|
352 |
-
email = session.get('user_email')
|
353 |
-
user_id = session.get('user_id')
|
354 |
if not email or not user_id:
|
355 |
return jsonify({"success": False, "message": "User not logged in"})
|
356 |
|
@@ -368,18 +368,19 @@ def checkout():
|
|
368 |
# Calculate the total price of the order
|
369 |
total_price = sum(item['Price__c'] for item in cart_items)
|
370 |
|
|
|
|
|
|
|
|
|
|
|
|
|
371 |
# Create the order in Salesforce
|
372 |
order_data = {
|
373 |
"Customer_Name__c": user_id,
|
374 |
"Customer_Email__c": email,
|
375 |
"Total_Amount__c": total_price,
|
376 |
-
"Order_Status__c": "Pending",
|
377 |
-
"
|
378 |
-
[f"{item['Name']} (Qty: {item['Quantity__c']})" for item in cart_items]
|
379 |
-
),
|
380 |
-
"Add_Ons__c": "\n".join(
|
381 |
-
[item['Add_Ons__c'] if item.get('Add_Ons__c') else "None" for item in cart_items]
|
382 |
-
),
|
383 |
}
|
384 |
sf.Order__c.create(order_data)
|
385 |
|
@@ -387,13 +388,11 @@ def checkout():
|
|
387 |
for item in cart_items:
|
388 |
sf.Cart_Item__c.delete(item["Id"])
|
389 |
|
|
|
390 |
return jsonify({"success": True, "redirect": "/order", "message": "Order placed successfully!"})
|
391 |
-
|
392 |
except Exception as e:
|
393 |
print(f"Error during checkout: {str(e)}")
|
394 |
return jsonify({"success": False, "error": str(e)})
|
395 |
-
|
396 |
-
|
397 |
@app.route("/order", methods=["GET"])
|
398 |
def order_summary():
|
399 |
email = session.get('user_email') # Fetch logged-in user's email
|
@@ -401,9 +400,9 @@ def order_summary():
|
|
401 |
return redirect(url_for("login"))
|
402 |
|
403 |
try:
|
404 |
-
# Fetch the most recent order for the
|
405 |
result = sf.query(f"""
|
406 |
-
SELECT Id, Customer_Name__c, Customer_Email__c, Total_Amount__c,
|
407 |
FROM Order__c
|
408 |
WHERE Customer_Email__c = '{email}'
|
409 |
ORDER BY CreatedDate DESC
|
@@ -419,6 +418,5 @@ def order_summary():
|
|
419 |
print(f"Error fetching order details: {str(e)}")
|
420 |
return render_template("order.html", order=None, error=str(e))
|
421 |
|
422 |
-
|
423 |
if __name__ == "__main__":
|
424 |
app.run(debug=False, host="0.0.0.0", port=7860)
|
|
|
349 |
return jsonify({"success": False, "error": str(e)}), 500
|
350 |
@app.route("/checkout", methods=["POST"])
|
351 |
def checkout():
|
352 |
+
email = session.get('user_email') # Fetch logged-in user's email
|
353 |
+
user_id = session.get('user_id') # Fetch logged-in user's ID
|
354 |
if not email or not user_id:
|
355 |
return jsonify({"success": False, "message": "User not logged in"})
|
356 |
|
|
|
368 |
# Calculate the total price of the order
|
369 |
total_price = sum(item['Price__c'] for item in cart_items)
|
370 |
|
371 |
+
# Concatenate order details for all items
|
372 |
+
order_details = "\n".join([
|
373 |
+
f"{item['Name']} (Qty: {item['Quantity__c']}, Add-Ons: {item.get('Add_Ons__c', 'None')}, Price: ${item['Price__c']})"
|
374 |
+
for item in cart_items
|
375 |
+
])
|
376 |
+
|
377 |
# Create the order in Salesforce
|
378 |
order_data = {
|
379 |
"Customer_Name__c": user_id,
|
380 |
"Customer_Email__c": email,
|
381 |
"Total_Amount__c": total_price,
|
382 |
+
"Order_Status__c": "Pending", # Default status
|
383 |
+
"Order_Details__c": order_details # Storing concatenated order details
|
|
|
|
|
|
|
|
|
|
|
384 |
}
|
385 |
sf.Order__c.create(order_data)
|
386 |
|
|
|
388 |
for item in cart_items:
|
389 |
sf.Cart_Item__c.delete(item["Id"])
|
390 |
|
391 |
+
# Return success response with redirect URL
|
392 |
return jsonify({"success": True, "redirect": "/order", "message": "Order placed successfully!"})
|
|
|
393 |
except Exception as e:
|
394 |
print(f"Error during checkout: {str(e)}")
|
395 |
return jsonify({"success": False, "error": str(e)})
|
|
|
|
|
396 |
@app.route("/order", methods=["GET"])
|
397 |
def order_summary():
|
398 |
email = session.get('user_email') # Fetch logged-in user's email
|
|
|
400 |
return redirect(url_for("login"))
|
401 |
|
402 |
try:
|
403 |
+
# Fetch the most recent order for the user
|
404 |
result = sf.query(f"""
|
405 |
+
SELECT Id, Customer_Name__c, Customer_Email__c, Total_Amount__c, Order_Details__c, Order_Status__c
|
406 |
FROM Order__c
|
407 |
WHERE Customer_Email__c = '{email}'
|
408 |
ORDER BY CreatedDate DESC
|
|
|
418 |
print(f"Error fetching order details: {str(e)}")
|
419 |
return render_template("order.html", order=None, error=str(e))
|
420 |
|
|
|
421 |
if __name__ == "__main__":
|
422 |
app.run(debug=False, host="0.0.0.0", port=7860)
|