Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -347,6 +347,50 @@ def update_quantity():
|
|
| 347 |
except Exception as e:
|
| 348 |
print(f"Error updating quantity: {str(e)}")
|
| 349 |
return jsonify({"success": False, "error": str(e)}), 500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 350 |
|
| 351 |
|
| 352 |
@app.route("/order", methods=["GET"])
|
|
|
|
| 347 |
except Exception as e:
|
| 348 |
print(f"Error updating quantity: {str(e)}")
|
| 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 |
+
|
| 357 |
+
try:
|
| 358 |
+
# Fetch cart items for the user
|
| 359 |
+
result = sf.query(f"""
|
| 360 |
+
SELECT Id, Name, Price__c, Add_Ons_Price__c, Quantity__c, Add_Ons__c
|
| 361 |
+
FROM Cart_Item__c
|
| 362 |
+
WHERE Customer_Email__c = '{email}'
|
| 363 |
+
""")
|
| 364 |
+
cart_items = result.get("records", [])
|
| 365 |
+
if not cart_items:
|
| 366 |
+
return jsonify({"success": False, "message": "Cart is empty"})
|
| 367 |
+
|
| 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 |
+
"Order_Items__c": "\n".join(
|
| 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 |
+
|
| 386 |
+
# Clear the cart after placing the order
|
| 387 |
+
for item in cart_items:
|
| 388 |
+
sf.Cart_Item__c.delete(item["Id"])
|
| 389 |
+
|
| 390 |
+
return jsonify({"success": True, "message": "Order placed successfully!"})
|
| 391 |
+
except Exception as e:
|
| 392 |
+
print(f"Error during checkout: {str(e)}")
|
| 393 |
+
return jsonify({"success": False, "error": str(e)})
|
| 394 |
|
| 395 |
|
| 396 |
@app.route("/order", methods=["GET"])
|