Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -489,35 +489,52 @@ def checkout():
|
|
489 |
return jsonify({"success": False, "error": str(e)})
|
490 |
|
491 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
492 |
@app.route('/apply_coupon', methods=['POST'])
|
493 |
def apply_coupon():
|
494 |
-
# Get the coupon code and subtotal from the POST request
|
495 |
data = request.json
|
496 |
coupon_code = data.get('coupon_code')
|
497 |
subtotal = data.get('subtotal')
|
498 |
-
user_email = session.get('user_email') # Assuming user is logged in and email is in session
|
499 |
|
500 |
if not coupon_code:
|
501 |
return jsonify({'success': False, 'message': 'Coupon code is required.'})
|
502 |
|
503 |
try:
|
504 |
-
# Query
|
505 |
-
query = f"SELECT Coupon_Code__c, Discount__c FROM Referral_Coupon__c WHERE Coupon_Code__c = '{coupon_code}' AND
|
506 |
result = sf.query(query)
|
507 |
|
508 |
if not result['records']:
|
509 |
return jsonify({'success': False, 'message': 'Invalid or expired coupon code.'})
|
510 |
|
511 |
-
# Assuming the discount is a percentage, we will calculate the discount
|
512 |
coupon = result['records'][0]
|
513 |
-
discount = coupon['Discount__c'] #
|
514 |
|
515 |
# Apply discount (if it's a percentage)
|
516 |
-
|
517 |
-
|
518 |
-
else: # Percentage discount
|
519 |
-
discount_amount = subtotal * discount # Calculate discount on the subtotal
|
520 |
-
return jsonify({'success': True, 'discount': discount_amount})
|
521 |
|
522 |
except Exception as e:
|
523 |
return jsonify({'success': False, 'message': f'Error applying coupon: {str(e)}'})
|
|
|
489 |
return jsonify({"success": False, "error": str(e)})
|
490 |
|
491 |
|
492 |
+
@app.route('/get_coupon_code', methods=['GET'])
|
493 |
+
def get_coupon_code():
|
494 |
+
email = request.args.get('email') # Get the email from the request
|
495 |
+
|
496 |
+
if not email:
|
497 |
+
return jsonify({'success': False, 'message': 'Email is required.'})
|
498 |
+
|
499 |
+
try:
|
500 |
+
# Query Salesforce to find the coupon code for the given email
|
501 |
+
query = f"SELECT Coupon_Code__c FROM Referral_Coupon__c WHERE Referral_Email__c = '{email}' AND Coupon_Status__c = 'Active'"
|
502 |
+
result = sf.query(query)
|
503 |
+
|
504 |
+
if result['records']:
|
505 |
+
# If a valid coupon code is found, return it
|
506 |
+
coupon_code = result['records'][0]['Coupon_Code__c']
|
507 |
+
return jsonify({'success': True, 'coupon_code': coupon_code})
|
508 |
+
|
509 |
+
return jsonify({'success': False, 'message': 'No active coupon found for this email.'})
|
510 |
+
|
511 |
+
except Exception as e:
|
512 |
+
return jsonify({'success': False, 'message': f"Error fetching coupon code: {str(e)}"})
|
513 |
+
|
514 |
+
|
515 |
@app.route('/apply_coupon', methods=['POST'])
|
516 |
def apply_coupon():
|
|
|
517 |
data = request.json
|
518 |
coupon_code = data.get('coupon_code')
|
519 |
subtotal = data.get('subtotal')
|
|
|
520 |
|
521 |
if not coupon_code:
|
522 |
return jsonify({'success': False, 'message': 'Coupon code is required.'})
|
523 |
|
524 |
try:
|
525 |
+
# Query Salesforce to find the discount for the provided coupon code
|
526 |
+
query = f"SELECT Coupon_Code__c, Discount__c FROM Referral_Coupon__c WHERE Coupon_Code__c = '{coupon_code}' AND Coupon_Status__c = 'Active'"
|
527 |
result = sf.query(query)
|
528 |
|
529 |
if not result['records']:
|
530 |
return jsonify({'success': False, 'message': 'Invalid or expired coupon code.'})
|
531 |
|
|
|
532 |
coupon = result['records'][0]
|
533 |
+
discount = coupon['Discount__c'] # Assuming discount is a percentage (e.g., 0.10 for 10%)
|
534 |
|
535 |
# Apply discount (if it's a percentage)
|
536 |
+
discount_amount = subtotal * discount
|
537 |
+
return jsonify({'success': True, 'discount': discount_amount})
|
|
|
|
|
|
|
538 |
|
539 |
except Exception as e:
|
540 |
return jsonify({'success': False, 'message': f'Error applying coupon: {str(e)}'})
|