Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -467,12 +467,15 @@ def checkout():
|
|
| 467 |
email = session.get('user_email')
|
| 468 |
user_id = session.get('user_id')
|
| 469 |
|
| 470 |
-
# Check if the user is logged in
|
| 471 |
if not email or not user_id:
|
| 472 |
return jsonify({"success": False, "message": "User not logged in"})
|
| 473 |
|
| 474 |
try:
|
| 475 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 476 |
result = sf.query(f"""
|
| 477 |
SELECT Id, Name, Price__c, Add_Ons_Price__c, Quantity__c, Add_Ons__c, Instructions__c
|
| 478 |
FROM Cart_Item__c
|
|
@@ -482,73 +485,50 @@ def checkout():
|
|
| 482 |
if not cart_items:
|
| 483 |
return jsonify({"success": False, "message": "Cart is empty"})
|
| 484 |
|
| 485 |
-
# Calculate the total price of the order
|
| 486 |
total_price = sum(item['Price__c'] for item in cart_items)
|
|
|
|
| 487 |
|
| 488 |
-
#
|
| 489 |
-
order_details = []
|
| 490 |
-
for item in cart_items:
|
| 491 |
-
details = f"{item['Name']} (Qty: {item['Quantity__c']}, Add-Ons: {item['Add_Ons__c'] or 'None'}, Price: ${item['Price__c']})"
|
| 492 |
-
if item.get('Instructions__c'): # Include instructions if available
|
| 493 |
-
details += f", Instructions: {item['Instructions__c']}"
|
| 494 |
-
order_details.append(details)
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
# Fetch the current Reward_Points__c for the user
|
| 498 |
customer_record = sf.query(f"""
|
| 499 |
SELECT Id, Reward_Points__c FROM Customer_Login__c
|
| 500 |
WHERE Email__c = '{email}'
|
| 501 |
""")
|
| 502 |
customer = customer_record.get("records", [])[0] if customer_record else None
|
| 503 |
|
| 504 |
-
if customer:
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
if use_reward_points:
|
| 509 |
-
# Subtract 500 points if checkbox is selected
|
| 510 |
-
if customer['Reward_Points__c'] >= 500:
|
| 511 |
-
new_reward_points = customer['Reward_Points__c'] - 500
|
| 512 |
-
discount=total_price * 0.10
|
| 513 |
-
total_bill=total_price-discount
|
| 514 |
-
|
| 515 |
-
else:
|
| 516 |
-
return jsonify({"success": False, "message": "Insufficient reward points to apply."})
|
| 517 |
-
else:
|
| 518 |
-
# Add 10% of the subtotal if checkbox is not selected
|
| 519 |
-
reward_points_to_add = total_price * 0.10
|
| 520 |
-
new_reward_points = customer['Reward_Points__c'] + reward_points_to_add
|
| 521 |
-
discount=0
|
| 522 |
-
total_bill=total_price
|
| 523 |
-
order_data = {
|
| 524 |
-
"Customer_Name__c": user_id,
|
| 525 |
-
"Customer_Email__c": email,
|
| 526 |
-
"Total_Amount__c": total_price,
|
| 527 |
-
"Discount__c": discount,
|
| 528 |
-
"Total_Bill__c": total_bill,
|
| 529 |
-
"Order_Status__c": "Pending",
|
| 530 |
-
"Order_Details__c": "\n".join(order_details) # Store all item details
|
| 531 |
-
}
|
| 532 |
-
|
| 533 |
-
# Create the order record in Salesforce
|
| 534 |
-
order_result = sf.Order__c.create(order_data)
|
| 535 |
-
|
| 536 |
-
# Update the Reward_Points__c field in Salesforce
|
| 537 |
-
try:
|
| 538 |
sf.Customer_Login__c.update(customer['Id'], {
|
| 539 |
-
"Reward_Points__c":
|
| 540 |
})
|
| 541 |
-
|
| 542 |
-
return jsonify({"success": False, "message":
|
| 543 |
|
| 544 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 545 |
for item in cart_items:
|
| 546 |
sf.Cart_Item__c.delete(item["Id"])
|
| 547 |
|
| 548 |
return jsonify({"success": True, "message": "Order placed successfully!"})
|
| 549 |
-
|
| 550 |
except Exception as e:
|
| 551 |
-
print(f"Error during checkout: {str(e)}")
|
| 552 |
return jsonify({"success": False, "error": str(e)})
|
| 553 |
|
| 554 |
|
|
|
|
| 467 |
email = session.get('user_email')
|
| 468 |
user_id = session.get('user_id')
|
| 469 |
|
|
|
|
| 470 |
if not email or not user_id:
|
| 471 |
return jsonify({"success": False, "message": "User not logged in"})
|
| 472 |
|
| 473 |
try:
|
| 474 |
+
data = request.json
|
| 475 |
+
use_reward_points = data.get("useRewardPoints", False)
|
| 476 |
+
selected_coupon = data.get("selectedCoupon", "").strip()
|
| 477 |
+
|
| 478 |
+
# Fetch cart items
|
| 479 |
result = sf.query(f"""
|
| 480 |
SELECT Id, Name, Price__c, Add_Ons_Price__c, Quantity__c, Add_Ons__c, Instructions__c
|
| 481 |
FROM Cart_Item__c
|
|
|
|
| 485 |
if not cart_items:
|
| 486 |
return jsonify({"success": False, "message": "Cart is empty"})
|
| 487 |
|
|
|
|
| 488 |
total_price = sum(item['Price__c'] for item in cart_items)
|
| 489 |
+
discount = 0
|
| 490 |
|
| 491 |
+
# Apply reward points if selected
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 492 |
customer_record = sf.query(f"""
|
| 493 |
SELECT Id, Reward_Points__c FROM Customer_Login__c
|
| 494 |
WHERE Email__c = '{email}'
|
| 495 |
""")
|
| 496 |
customer = customer_record.get("records", [])[0] if customer_record else None
|
| 497 |
|
| 498 |
+
if customer and use_reward_points:
|
| 499 |
+
if customer['Reward_Points__c'] >= 500:
|
| 500 |
+
discount += total_price * 0.10 # 10% discount
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 501 |
sf.Customer_Login__c.update(customer['Id'], {
|
| 502 |
+
"Reward_Points__c": customer['Reward_Points__c'] - 500
|
| 503 |
})
|
| 504 |
+
else:
|
| 505 |
+
return jsonify({"success": False, "message": "Insufficient reward points to apply."})
|
| 506 |
|
| 507 |
+
# Apply coupon discount (assume 15% discount for now)
|
| 508 |
+
if selected_coupon:
|
| 509 |
+
discount += total_price * 0.15 # Example: 15% discount for coupon
|
| 510 |
+
|
| 511 |
+
total_bill = total_price - discount
|
| 512 |
+
|
| 513 |
+
order_data = {
|
| 514 |
+
"Customer_Name__c": user_id,
|
| 515 |
+
"Customer_Email__c": email,
|
| 516 |
+
"Total_Amount__c": total_price,
|
| 517 |
+
"Discount__c": discount,
|
| 518 |
+
"Total_Bill__c": total_bill,
|
| 519 |
+
"Order_Status__c": "Pending",
|
| 520 |
+
"Order_Details__c": "\n".join(f"{item['Name']} x{item['Quantity__c']}" for item in cart_items)
|
| 521 |
+
}
|
| 522 |
+
|
| 523 |
+
sf.Order__c.create(order_data)
|
| 524 |
+
|
| 525 |
+
# Clear cart
|
| 526 |
for item in cart_items:
|
| 527 |
sf.Cart_Item__c.delete(item["Id"])
|
| 528 |
|
| 529 |
return jsonify({"success": True, "message": "Order placed successfully!"})
|
| 530 |
+
|
| 531 |
except Exception as e:
|
|
|
|
| 532 |
return jsonify({"success": False, "error": str(e)})
|
| 533 |
|
| 534 |
|