Yaswanth56's picture
Create flask-salesforce-app/app.py
a3f18b1 verified
raw
history blame
3.52 kB
from flask import Flask, request, render_template, redirect, url_for
from simple_salesforce import Salesforce
from dotenv import load_dotenv
import os
# Load environment variables
load_dotenv()
# Initialize Flask app
app = Flask(__name__)
# Salesforce connection
try:
sf = Salesforce(
username=os.getenv("SF_USERNAME"),
password=os.getenv("SF_PASSWORD"),
security_token=os.getenv("SF_SECURITY_TOKEN"),
domain="login" # Use "test" for sandbox
)
print("Salesforce connection successful!")
except Exception as e:
print(f"Salesforce connection failed: {e}")
sf = None
@app.route('/')
def login():
return render_template('login.html')
@app.route('/auth', methods=['POST'])
def auth():
email = request.form['email']
password = request.form['password']
if not sf:
return "Salesforce connection failed. Please check credentials and try again."
try:
# Query Salesforce for user authentication
query = f"SELECT Id, Reward_Points__c FROM Customer_Login__c WHERE Email__c = '{email}' AND Password__c = '{password}'"
result = sf.query(query)
if result['totalSize'] == 0:
return "Invalid Login Details"
customer = result['records'][0]
reward_points = customer['Reward_Points__c']
# Redirect to rewards page
return redirect(url_for('rewards', customer_id=customer['Id'], points=reward_points))
except Exception as e:
return f"Error during authentication: {e}"
@app.route('/rewards/<customer_id>')
def rewards(customer_id):
try:
customer = sf.Customer_Login__c.get(customer_id)
points = customer['Reward_Points__c']
return render_template('rewards.html', points=points, customer_id=customer_id)
except Exception as e:
return f"Error fetching rewards: {e}"
@app.route('/apply_rewards', methods=['POST'])
def apply_rewards():
customer_id = request.form['customer_id']
bill_amount = float(request.form['bill_amount'])
apply_rewards = request.form.get('apply_rewards')
try:
customer = sf.Customer_Login__c.get(customer_id)
points = customer['Reward_Points__c']
gst = 0.18 * bill_amount
if points >= 500 and apply_rewards:
discount = 0.1 * bill_amount
final_bill = bill_amount - discount + gst
updated_points = points - 500
# Update the customer's reward points in Salesforce
sf.Customer_Login__c.update(customer_id, {'Reward_Points__c': updated_points})
message = "You saved 10% on your total bill!"
else:
discount = 0
earned_points = 0.1 * bill_amount
final_bill = bill_amount + gst
updated_points = points + earned_points
# Update the customer's reward points in Salesforce
sf.Customer_Login__c.update(customer_id, {'Reward_Points__c': updated_points})
message = f"You earned {earned_points:.2f} reward points!"
# Render the summary page
return render_template(
'apply_rewards.html',
original_bill=bill_amount,
discount=discount,
gst=gst,
final_bill=final_bill,
updated_points=updated_points,
message=message
)
except Exception as e:
return f"Error applying rewards: {e}"
if __name__ == '__main__':
from waitress import serve
serve(app, host="0.0.0.0", port=8080)