Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -472,7 +472,6 @@ def checkout():
|
|
472 |
|
473 |
try:
|
474 |
data = request.json
|
475 |
-
use_reward_points = data.get("useRewardPoints", False)
|
476 |
selected_coupon = data.get("selectedCoupon", "").strip() # Get selected coupon
|
477 |
|
478 |
# Fetch cart items
|
@@ -486,48 +485,53 @@ def checkout():
|
|
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 |
-
#
|
492 |
-
|
493 |
-
SELECT Id,
|
494 |
-
WHERE Email__c = '{email}'
|
495 |
""")
|
496 |
-
customer = customer_record.get("records", [])[0] if customer_record else None
|
497 |
|
498 |
-
if
|
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 and remove used coupon
|
508 |
if selected_coupon:
|
509 |
-
|
|
|
|
|
|
|
510 |
|
511 |
-
#
|
512 |
-
|
513 |
-
|
514 |
-
|
|
|
515 |
|
516 |
-
|
517 |
-
|
518 |
-
|
519 |
-
|
|
|
|
|
|
|
520 |
|
521 |
-
|
522 |
-
|
|
|
|
|
|
|
|
|
523 |
|
524 |
-
|
525 |
-
|
|
|
526 |
|
527 |
-
|
528 |
-
|
529 |
-
|
|
|
|
|
530 |
})
|
|
|
531 |
|
532 |
total_bill = total_price - discount
|
533 |
|
@@ -551,7 +555,9 @@ def checkout():
|
|
551 |
return jsonify({"success": True, "message": "Order placed successfully!"})
|
552 |
|
553 |
except Exception as e:
|
|
|
554 |
return jsonify({"success": False, "error": str(e)})
|
|
|
555 |
@app.route("/order", methods=["GET"])
|
556 |
def order_summary():
|
557 |
email = session.get('user_email') # Fetch logged-in user's email
|
|
|
472 |
|
473 |
try:
|
474 |
data = request.json
|
|
|
475 |
selected_coupon = data.get("selectedCoupon", "").strip() # Get selected coupon
|
476 |
|
477 |
# Fetch cart items
|
|
|
485 |
return jsonify({"success": False, "message": "Cart is empty"})
|
486 |
|
487 |
total_price = sum(item['Price__c'] for item in cart_items)
|
488 |
+
discount = 0 # Default discount is 0
|
489 |
|
490 |
+
# Fetch the user's existing coupons
|
491 |
+
coupon_query = sf.query(f"""
|
492 |
+
SELECT Id, Coupon_Code__c FROM Referral_Coupon__c WHERE Referral_Email__c = '{email}'
|
|
|
493 |
""")
|
|
|
494 |
|
495 |
+
has_coupons = bool(coupon_query["records"]) # Check if user has any coupons
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
496 |
|
|
|
497 |
if selected_coupon:
|
498 |
+
# Case 3: User selected a valid coupon → Apply discount & remove coupon
|
499 |
+
discount = total_price * 0.10 # 10% discount
|
500 |
+
referral_coupon_id = coupon_query["records"][0]["Id"]
|
501 |
+
existing_coupons = coupon_query["records"][0]["Coupon_Code__c"].split("\n")
|
502 |
|
503 |
+
# Remove only the selected coupon
|
504 |
+
updated_coupons = [coupon for coupon in existing_coupons if coupon.strip() != selected_coupon]
|
505 |
+
|
506 |
+
# Convert list back to a string with newlines
|
507 |
+
updated_coupons_str = "\n".join(updated_coupons).strip()
|
508 |
|
509 |
+
# Update the Referral_Coupon__c record with the remaining coupons
|
510 |
+
sf.Referral_Coupon__c.update(referral_coupon_id, {
|
511 |
+
"Coupon_Code__c": updated_coupons_str
|
512 |
+
})
|
513 |
+
else:
|
514 |
+
# Case 1 & Case 2: User has no coupons or has coupons but didn’t select one → Add 10% to reward points
|
515 |
+
reward_points_to_add = total_price * 0.10
|
516 |
|
517 |
+
# Fetch current reward points
|
518 |
+
customer_record = sf.query(f"""
|
519 |
+
SELECT Id, Reward_Points__c FROM Customer_Login__c
|
520 |
+
WHERE Email__c = '{email}'
|
521 |
+
""")
|
522 |
+
customer = customer_record.get("records", [])[0] if customer_record else None
|
523 |
|
524 |
+
if customer:
|
525 |
+
current_reward_points = customer.get("Reward_Points__c") or 0
|
526 |
+
new_reward_points = current_reward_points + reward_points_to_add
|
527 |
|
528 |
+
print(f"Updating reward points: Current = {current_reward_points}, Adding = {reward_points_to_add}, New = {new_reward_points}")
|
529 |
+
|
530 |
+
# Update reward points in Salesforce
|
531 |
+
sf.Customer_Login__c.update(customer["Id"], {
|
532 |
+
"Reward_Points__c": new_reward_points
|
533 |
})
|
534 |
+
print(f"Successfully updated reward points for {email}")
|
535 |
|
536 |
total_bill = total_price - discount
|
537 |
|
|
|
555 |
return jsonify({"success": True, "message": "Order placed successfully!"})
|
556 |
|
557 |
except Exception as e:
|
558 |
+
print(f"Error updating reward points: {str(e)}")
|
559 |
return jsonify({"success": False, "error": str(e)})
|
560 |
+
|
561 |
@app.route("/order", methods=["GET"])
|
562 |
def order_summary():
|
563 |
email = session.get('user_email') # Fetch logged-in user's email
|