from appservices.common.util import *
import threading
import queue
from appservices.common.payment_gateways.axis_payment_gateways import *
from appservices.common.payment_gateways.idfc_payment_gateways import *

payments = Blueprint("payments",__name__)
q = queue.Queue()
thread_lock = threading.Lock()
queue_lock = threading.Lock()
worker_thread = None

@payments.route("/enctyptionData", methods=["POST","GET"])
def enctyptionData():
    data_status = {"responseStatus":0,"result":""}
    try:
        print('encription start')
        dataObject={}
        sample_static_wowpe_payin_data = {
        "api_key":"668ebaa40b56efdd9c8c004b-6de678ae620c5b60a706609eaf0bd078",
        "order_id":"245467538678",
        "mode":"TEST",
        "amount":"230.00",
        "currency":"INR",
        "description":"TESTING",
        "name":"Test",
        "email":"Test@gmail.com",
        "phone":"9988776655",
        "address_line_1":"Hyderabad",
        "address_line_2":"Hyderabad",
        "city":"Hyderabad",
        "state":"Telangana",
        "country":"India",
        "zip_code":"500081",
        "udf1":"",
        "udf2":"",
        "udf3":"",
        "udf4":"",
        "udf5":"",
        "return_url":"https://google.com/"
        }
        secret_key=bytes("522e8cb1501b860a839e7bfb035d802d","utf-8")
        encryptdata=encrypt(sample_static_wowpe_payin_data,secret_key)
        print(encryptdata,"**********encryptdata************")

        data_status["responseStatus"]=1
        data_status["result"]="Success"
        data_status["data"]=encryptdata
        return data_status
    except Exception as e:
        print(traceback.format_exc())
        return "failed"

@payments.route("/paymentpage/<payInType>/<paymentLink>",methods=["POST","GET"])
def paymentpage(payInType,paymentLink):
    try:
        if request.method == "GET":
            if paymentLink and (payInType in ["page","button","link","api"]):
                if payInType == "page":
                    payment_page_queryset = Users.objects(merchantPaymentLink=paymentLink).first()
                    if payment_page_queryset:
                        paymentPageDict = {}
                        paymentPageDict = {
                        "userId":str(payment_page_queryset.id),
                        "userName":payment_page_queryset.fullName,
                        "email":payment_page_queryset.email,
                        "phoneNumber":payment_page_queryset.phoneNumber,
                        'payInType':payInType,
                        'paymentLink':paymentLink,
                        "amount":0
                        }
                        return render_template("frontend/paymentpage.html",userDetails=paymentPageDict)
                    else:
                        error = "Invalid Payment"
                        return render_template("frontend/paymentlinkerror.html",error=error)
                elif payInType == "button":
                    payment_button_queryset = PaymentButtons.objects(buttonLink=paymentLink,status=1).first()
                    if payment_button_queryset:
                        buttonLinkExpDate = payment_button_queryset.buttonLinkExpDate
                        currentDate = datetime.datetime.now().replace(hour=0,minute=0,second=0,microsecond=0)
                        if buttonLinkExpDate < currentDate:
                            error = "Payment Link Expired!."
                            return render_template("frontend/paymentlinkerror.html",error=error)
                        else:
                            paymentButtonDict = {}
                            paymentButtonDict = {
                            "userId":str(payment_button_queryset.userId.id),
                            "userName":payment_button_queryset.userId.fullName,
                            "userEmail":payment_button_queryset.userId.email,
                            "userPhoneNumber":payment_button_queryset.userId.phoneNumber,
                            "buttonLink":payment_button_queryset.buttonLink,
                            "customerEmail":payment_button_queryset.email,
                            "customerMobileNumber":payment_button_queryset.mobileNumber,
                            'payInType':payInType,
                            'paymentLink':paymentLink,
                            "amount":"{:.2f}".format(float(payment_button_queryset.amount))
                            }
                            return render_template("frontend/paymentpage.html",userDetails=paymentButtonDict)
                    else:
                            error = "Invalid Payment Link"
                            return render_template("frontend/paymentlinkerror.html",error=error)
                elif payInType == "link":
                    payment_link_queryset = PaymentLinks.objects(paymentLink=paymentLink,status=1).first()
                    if payment_link_queryset:
                        linkExpDate = payment_link_queryset.linkExpDate
                        currentDate = datetime.datetime.now().replace(hour=0,minute=0,second=0,microsecond=0)
                        if linkExpDate < currentDate:
                            error = "Payment Link Expired!."
                            return render_template("frontend/paymentlinkerror.html",error=error)
                        else:
                            paymentLinkDict = {}
                            paymentLinkDict = {
                            "userId":str(payment_link_queryset.userId.id),
                            "userName":payment_link_queryset.userId.fullName,
                            "userEmail":payment_link_queryset.userId.email,
                            "userPhoneNumber":payment_link_queryset.userId.phoneNumber,
                            "customerEmail":payment_link_queryset.email,
                            "customerMobileNumber":payment_link_queryset.mobileNumber,
                            "paymentLink":payment_link_queryset.paymentLink,
                            'payInType':payInType,
                            "amount":"{:.2f}".format(float(payment_link_queryset.amount))
                            }
                            return render_template("frontend/paymentpage.html",userDetails=paymentLinkDict)
                    else:
                        error = "Invalid Payment Page."
                        return render_template("frontend/paymentlinkerror.html",error=error)
                else:
                    error = "Invalid Payment Link."
                    return render_template("frontend/paymentlinkerror.html",error=error)
            else:
                error = "Invalid Payment Link."
                return render_template("frontend/paymentlinkerror.html",error=error)
        else:
            error = "Payment Link Expired!."
            return render_template("frontend/paymentlinkerror.html",error=error)
    except Exception as e:
        app.logger.error(traceback.format_exc())
        error = "Unable to connect to server.Please try again after some time!!"
        return render_template("frontend/paymentlinkerror.html",error=error)

@payments.route("/get_payment_intent_url/<payInType>/<paymentLink>", methods=["POST","GET"])
def get_payment_intent_url(payInType,paymentLink):
    data_status = {"responseStatus": 0, "result": ""}
    city = request.form.get("city","")
    description = request.form.get("description","")
    name = request.form.get("fullname","")
    zip_code = request.form.get("pincode","")
    amount=0
    email=''
    phone=''
    userId=''
    order_id=random_digit_generate(16)
    responseDict = {}
    try:
        if paymentLink and (payInType in ["page","button","link","api"]):
            if payInType == "page":
                amount=request.form.get('amount')
                email=request.form.get('email')
                phone=request.form.get('phone_number')
                payment_page_queryset = Users.objects(merchantPaymentLink=paymentLink).first()
                if payment_page_queryset:
                    userId=str(payment_page_queryset.id)
                else:
                    data_status['result']="Invalid Payment Link"
                    return data_status

            elif payInType == "button":
                payment_button_queryset = PaymentButtons.objects(buttonLink=paymentLink,status=1).first()
                if payment_button_queryset:
                    userId=str(payment_button_queryset.userId.id)
                    amount="{:.2f}".format(float(payment_button_queryset.amount))
                    email=payment_button_queryset.email
                    phone=payment_button_queryset.mobileNumber
                else:
                    data_status['result']="Invalid Payment Link"
                    return data_status

            elif payInType == "link":
                payment_link_queryset = PaymentLinks.objects(paymentLink=paymentLink,status=1).first()
                if payment_link_queryset:
                    userId=str(payment_link_queryset.userId.id)
                    amount="{:.2f}".format(float(payment_link_queryset.amount))
                    email=payment_link_queryset.email
                    phone=payment_link_queryset.mobileNumber
                else:
                    data_status['result']="Invalid Payment Link"
                    return data_status
            else:
                data_status['result']="Invalid Payment Link"
                return data_status
            if amount and userId and city and description and name and zip_code and email and phone:
                merchant_queryset = Users.objects(id=userId).first()
                if not merchant_queryset:
                    data_status["result"]="Invalid merchant id!!"
                    return data_status
                if merchant_queryset.payInPaymentGatewayId:
                    payInPaymentGatewayId = str(merchant_queryset.payInPaymentGatewayId.id)
                    payin_gate_way_queryset = TransactionAPI.objects(id=payInPaymentGatewayId).first()
                    paymentGateWayCode = payin_gate_way_queryset.code

                    if paymentGateWayCode == "AccurePay_PayIn":
                        get_api_key = ""
                        get_salt = ""
                        get_base_url=''
                        for each_key in payin_gate_way_queryset.paramsList:
                            
                            get_key = each_key.get("key")
                            
                            if get_key == "api_key":
                                get_api_key = each_key.get("value")
                                
                            if get_key == "salt":
                                get_salt = each_key.get("value")

                            if get_key == "base_url":
                                get_base_url = each_key.get("value")
                                
                        newEncryptedData = {
                        "api_key":get_api_key,
                        "order_id":merchant_queryset.merchantUniqueNumber+"-"+order_id,
                        "mode":'LIVE',
                        "amount":amount,
                        "currency":"INR",
                        "description":description,
                        "name":name,
                        "email":email,
                        "phone":phone,
                        "address_line_1":'',
                        "address_line_2":'',
                        "city":city,
                        "state":'',
                        "country":'IND',
                        "zip_code":zip_code,
                        "udf1":userId,
                        "udf2":paymentLink,
                        "udf3":payInType,
                        "udf4":'',
                        "udf5":'',
                        "return_url":domain+"/api/mobile/payin_call_back_url"
                        }
                        salt = get_salt
                        hashvalue = Sha512Hash(newEncryptedData,salt)
                        
                        newEncryptedData.update({"hash": hashvalue})
                        
                        headers = {'Content-Type': 'application/x-www-form-urlencoded'}
                        payload = "api_key="+get_api_key+"&order_id="+newEncryptedData.get('order_id')+"&mode="+newEncryptedData.get('mode')+"&amount="+str(newEncryptedData.get('amount'))+"&currency="+newEncryptedData.get('currency')+"&description="+newEncryptedData.get('description')+"&name="+newEncryptedData.get('name')+"&email="+newEncryptedData.get('email')+"&phone="+newEncryptedData.get('phone')+"&address_line_1="+newEncryptedData.get('address_line_1')+"&address_line_2="+newEncryptedData.get('address_line_2')+"&city="+newEncryptedData.get('city')+"&state="+newEncryptedData.get('state')+"&country="+newEncryptedData.get('country')+"&zip_code="+newEncryptedData.get('zip_code')+"&udf1="+newEncryptedData.get('udf1')+"&udf2="+newEncryptedData.get('udf2')+"&udf3="+newEncryptedData.get('udf3')+"&udf4="+newEncryptedData.get('udf4')+"&udf5="+newEncryptedData.get('udf5')+"&return_url="+newEncryptedData.get('return_url')+"&hash="+newEncryptedData.get('hash')
                        respnseData = requests.post(get_base_url+"getpaymentrequestintenturl", data=payload, headers=headers)
                        print(respnseData.text,"PAY IN INTENT URL RESPONSE")
                        response_text = respnseData.text
                        # Parse the JSON response
                        response_data = json.loads(response_text)
                        if "data" in response_data:
                            if response_data["data"].get("upi_intent_url")!="null":
                                responseDict={
                                'url':response_data["data"].get("upi_intent_url").strip('\\'),
                                'order_id':order_id,
                                "userId":userId
                                }
                                data_status["responseStatus"]=1
                                data_status["result"]=responseDict
                                return data_status
                            else:
                                data_status['result'] = "Server Connection Error Please Try Again."
                                return data_status
                        else:
                            data_status['result'] = response_data["error"].get("message")
                            return data_status
                else:
                    data_status["result"]="Please contact to admin to enable payin option!!"
                    return data_status
            else:
                data_status["result"]="Required fields are missing!!"
                return data_status
        else:
            data_status["result"]="Invalid Payment page."
            return data_status
    except Exception as e:
        app.logger.error(traceback.format_exc())
        data_status["result"]="Unable to get intent data!!"
        return data_status

@payments.route("/paymentsuccesspage/<orderId>/<ordertype>",methods=["POST","GET"])
def paymentsuccesspage(orderId,ordertype):
    try:
        if request.method == "GET":
            if orderId and ordertype:
                wallet_queryset = WalletTransactions.objects(orderId=orderId).first()
                if wallet_queryset:
                    orderId=wallet_queryset.orderId
                    amount=wallet_queryset.amount
                    error=wallet_queryset.errorMessage
                    status=wallet_queryset.status
                    transactionId=wallet_queryset.transactionId
                    error = "Success."
                    return render_template("frontend/paymentlinksuccess.html",error=error,orderId=orderId,status=1)
                else:
                    error = "Invalid Order Id."
                    status=0
                    if ordertype=="success":
                        status=1
                    return render_template("frontend/paymentlinksuccess.html",error=error,orderId=orderId,status=status)
            else:
                error = "Invalid Order Id."
                return render_template("frontend/paymentlinkerror.html",error=error)
    except Exception as e:
        error = "Server Error.Please Try After Some Time."
        return render_template("frontend/paymentlinkerror.html",error=error)


@payments.route("/get_payin_payment_status", methods=["POST"])
def get_payin_payment_status():
    data_status = {"responseStatus": 0, "result": ""}
    order_id = request.form.get("order_id","")
    user_id = request.form.get("user_id","")
    responseDict = {}
    try:
        if user_id and order_id:
            merchant_queryset = Users.objects(id=user_id).first()
            if not merchant_queryset:
                data_status["result"]="Invalid merchant id!!"
                return data_status
            merchantReferenceNumber = merchant_queryset.merchantUniqueNumber
            orderId = str(merchantReferenceNumber+"-"+order_id)
            order_queryset = WalletTransactions.objects(orderId=orderId).first()
            if order_queryset:
                responseDict={
                'order_id':order_queryset.orderId,
                'amount':order_queryset.amount,
                'status':order_queryset.status,
                'errorMessage':order_queryset.errorMessage
                }
                data_status["responseStatus"]=1
                data_status['result']=responseDict
                return data_status
            else:
                payInPaymentGatewayId = str(merchant_queryset.payInPaymentGatewayId.id)
                paymentGateWayCode = merchant_queryset.payInPaymentGatewayId.code
                paramsList = merchant_queryset.payInPaymentGatewayId.paramsList
                
                if paymentGateWayCode == "AccurePay_PayIn":
                    get_api_key = ""
                    get_salt = ""
                    get_base_url=''
                    for each_key in paramsList:
                        
                        get_key = each_key.get("key")
                        
                        if get_key == "api_key":
                            get_api_key = each_key.get("value")
                            
                        if get_key == "salt":
                            get_salt = each_key.get("value")

                        if get_key == "base_url":
                            get_base_url = each_key.get("value")
                    newEncryptedData={}
                    newEncryptedData={
                    'api_key':get_api_key,
                    'order_id':orderId
                    }
                    salt = get_salt
                    hashvalue = Sha512Hash(newEncryptedData,salt)
                    headers = {'Content-Type': 'application/x-www-form-urlencoded'}
                    payload = "api_key="+get_api_key+"&order_id="+orderId+"&hash="+hashvalue
                    respnseData = requests.post(get_base_url+"paymentstatus", data=payload, headers=headers)
                    response_text = respnseData.text
                    # Parse the JSON response
                    response_data = json.loads(response_text)
                    print(response_data,"********response_data***********")
                    if "data" in response_data:
                        resdata=response_data["data"]
                        status=0
                        if resdata[0].get("response_code")==0 or resdata[0].get("response_code")==1000:
                            status=1
                            if resdata[0].get("response_code")==0:
                                ordertype='success'
                            else:
                                ordertype="failed"
                            successurl=domain+'merchants/paymentsuccesspage/'+orderId+'/'+ordertype
                            responseDict={
                            'order_id':orderId,
                            'amount':resdata[0].get("amount"),
                            'status':status,
                            'successurl':successurl,
                            'errorMessage':resdata[0].get("error_desc")
                            }

                            data_status["responseStatus"]=1
                            data_status['result']=responseDict
                            return data_status
                        else:
                            data_status["result"]="Order id not found!"
                            return data_status
                    else:
                        successurl=domain+'merchants/paymentsuccesspage/'+orderId+'/failed'
                        responseDict={
                        'order_id':orderId,
                        'amount':0,
                        'status':0,
                        'successurl':successurl,
                        'errorMessage':response_data["error"].get("response_message")
                        }

                        data_status["responseStatus"]=1
                        data_status['result']=responseDict
                        return data_status
                else:
                    data_status["result"]="Order id not found!"
                    return data_status
        else:
            data_status["result"]="Required fields are missing!!"
            return data_status
    except Exception as e:
        app.logger.error(traceback.format_exc())
        data_status["result"]="Unable to get intent data!!"
        return data_status


# @payments.route("/check_queue", methods=["POST","GET"])
# def check_queue():
#     try:
#         print('queue1 start')
#         thread=threading.Thread(target=worker,daemon=True)
#         if not thread._started.is_set():
#             thread.start()
#             print("start tread1")
#         print("already start tread1")
#         itemdict={}
#         my_list = ['debit', 'credit']
#         for item in range(1,10000):
#             random_item = random.choice(my_list)
#             itemdict={
#             'itemname':"1item"+str(item),
#             'itemvalue':item,
#             'itemtype':random_item
#             }
#             q.put(itemdict)
#             q.join()
#         print('loop1 done')
#         clear_queue(q)
#         print('All work completed1')
#         return "success"
#     except Exception as e:
#         print(traceback.format_exc())
#         return "failed"

# @payments.route("/check_queue1", methods=["POST","GET"])
# def check_queue1():
#     try:
#         print('queue2 start')
#         thread=threading.Thread(target=worker,daemon=True)
#         if not thread._started.is_set():
#             thread.start()
#             print('tread2 start')
#         print('queue2 already start')
#         itemdict={}
#         my_list = ['debit', 'credit']
#         for item2 in range(10051,10100):
#             random_item = random.choice(my_list)
#             itemdict={
#             'itemname':"2item"+str(item2),
#             'itemvalue':item2,
#             'itemtype':random_item
#             }
#             q.put(itemdict)
#             q.join()
#         clear_queue(q)
#         print('All work completed2')
#         return "success"
#     except Exception as e:
#         print(traceback.format_exc())
#         return "failed"

# def worker():
#     print("worker")
#     while True:
#         try:
#             item = q.get()
#             print("itemvalue",item["itemvalue"])
#             itemname=item["itemname"]
#             itemvalue=item["itemvalue"]
#             itemtype=item["itemtype"]
#             print(itemvalue,"itemvalue")
#             prevamount=0
#             currentamt=0
#             userqry=CheckqueueBalance.objects(userId=1).first()
#             if userqry:
#                 prevamount=float(userqry.amount)
#                 if itemtype=="debit":
#                     currentamt=float(prevamount)-float(itemvalue)
#                 else:
#                     currentamt=float(prevamount)+float(itemvalue)
#                 if currentamt>0:
#                     userqry.update(amount=currentamt,updatedOn=datetime.datetime.now())
#                     checkqry=Checkqueue(
#                         amount=itemvalue,
#                         previousamount=prevamount,
#                         currentamount=currentamt,
#                         itemValue=str(itemvalue),
#                         itemtype=str(itemtype),
#                         createdOn=datetime.datetime.now()
#                         ).save()
#         except Exception as e:
#             print(traceback.format_exc(),'exception Error')
#             continue
#         q.task_done()

# def clear_queue(q):
#     with q.mutex:
#         q.queue.clear()
#         q.all_tasks_done.notify_all()
#         q.unfinished_tasks = 0

def start_worker():
    global worker_thread
    with thread_lock:
        if worker_thread is None or not worker_thread.is_alive():
            worker_thread = threading.Thread(target=worker, daemon=True)
            worker_thread.start()
            print("Worker thread started")

# def worker():
#     print("Worker started")
#     while True:
#         try:
#             item = q.get()
#             if item is None:
#                 break  # Exit the loop if None is received
#             itemname = item["itemname"]
#             itemvalue = item["itemvalue"]
#             itemtype = item["itemtype"]
 
#             print(itemvalue, "itemvalue")
 
#             prevamount = 0
#             currentamt = 0
 
#             userqry = CheckqueueBalance.objects(userId=1).first()
#             if userqry:
#                 if itemtype == "debit":
#                     updated_user = CheckqueueBalance.objects(userId=1,amount__gte=itemvalue).modify(dec__amount=float(itemvalue),new=True)
#                     if updated_user==None:
#                         print("amount Invalid")
#                     print(updated_user.amount,"(((((((((((((((updated_user)))))))))))))))")
#                     currentamt=float(updated_user.amount)
#                     prevamount=float(currentamt)+float(itemvalue)
#                 else:
#                     updated_user = CheckqueueBalance.objects(userId=1).modify(inc__amount=float(itemvalue),new=True)
#                     print(updated_user.amount,"(((((((((((((((updated_user)))))))))))))))")
#                     currentamt=float(updated_user.amount)
#                     prevamount=float(currentamt)-float(itemvalue)
 
#                 if currentamt > 0:
#                     userqry.update(amount=currentamt, updatedOn=datetime.datetime.now())
#                     checkqry = Checkqueue(
#                         amount=itemvalue,
#                         previousamount=prevamount,
#                         currentamount=currentamt,
#                         itemValue=str(itemvalue),
#                         itemtype=str(itemtype),
#                         createdOn=datetime.datetime.now()
#                     ).save()
#         except Exception as e:
#             print(traceback.format_exc(), 'exception Error')
#         finally:
#             q.task_done()
 

 
# @payments.route("/check_queue", methods=["POST", "GET"])
# def check_queue():
#     try:
#         print('queue1 start')
#         start_worker()
 
#         my_list = ['debit', 'credit']
#         with queue_lock:
#             for item in range(1, 10000):
#                 random_item = random.choice(my_list)
#                 itemdict = {
#                     'itemname': "1item" + str(item),
#                     'itemvalue': item,
#                     'itemtype': random_item
#                 }
#                 q.put(itemdict)
#                 q.join()  # Wait for the queue to be processed
#             print('loop1 done')
#             print('All work added to queue1')
 
#         return "success"
#     except Exception as e:
#         print(traceback.format_exc())
#         return "failed"
 
# @payments.route("/check_queue1", methods=["POST", "GET"])
# def check_queue1():
#     try:
#         print('queue2 start')
#         start_worker()
 
#         my_list = ['debit', 'credit']
 
#         with queue_lock:
#             for item2 in range(10051, 10100):
#                 random_item = random.choice(my_list)
#                 itemdict = {
#                     'itemname': "2item" + str(item2),
#                     'itemvalue': item2,
#                     'itemtype': random_item
#                 }
#                 q.put(itemdict)
#                 q.join()  # Wait for the queue to be processed
#         print('loop2 done')
#         print('All work added to queue2')
#         return "success"
#     except Exception as e:
#         print(traceback.format_exc())
#         return "failed"
 


def worker(itemdict):
    print("Worker started")
    while True:
        try:
            if itemdict is None:
                break  # Exit the loop if None is received
            itemname = itemdict["itemname"]
            itemvalue = itemdict["itemvalue"]
            itemtype = itemdict["itemtype"]
 
            print(itemvalue, "itemvalue")
 
            prevamount = 0
            currentamt = 0
 
            userqry = CheckqueueBalance.objects(userId=1).first()
            if userqry:
                if itemtype == "debit":
                    updated_user = CheckqueueBalance.objects(userId=1,amount__gte=itemvalue).modify(dec__amount=float(itemvalue),new=True)
                    if updated_user==None:
                        print("amount Invalid")
                    print(updated_user.amount,"(((((((((((((((updated_user)))))))))))))))")
                    currentamt=float(updated_user.amount)
                    prevamount=float(currentamt)+float(itemvalue)
                    updated_user.update(prevamount=float(prevamount))
                else:
                    updated_user = CheckqueueBalance.objects(userId=1).modify(inc__amount=float(itemvalue),new=True)
                    print(updated_user.amount,"(((((((((((((((updated_user)))))))))))))))")
                    currentamt=float(updated_user.amount)
                    prevamount=float(currentamt)-float(itemvalue)
                checkqry = Checkqueue(
                    amount=itemvalue,
                    previousamount=prevamount,
                    currentamount=currentamt,
                    itemValue=str(itemvalue),
                    itemtype=str(itemtype),
                    createdOn=datetime.datetime.now()
                ).save()
        except Exception as e:
            print(traceback.format_exc(), 'exception Error')
        finally:
            return "success"
 

 
@payments.route("/check_queue", methods=["POST", "GET"])
def check_queue():
    try:
        print('queue1 start')
        my_list = ['debit', 'credit']
        for item in range(1, 2):
            random_item = random.choice(my_list)
            itemdict = {
                'itemname': "1item" + str(item),
                'itemvalue': 150,
                'itemtype': "debit"
            }
            worker1=worker(itemdict)
            print(worker1+ str(item),"***********worker1***********")
        return "success"
    except Exception as e:
        print(traceback.format_exc())
        return "failed"
 
@payments.route("/check_queue1", methods=["POST", "GET"])
def check_queue1():
    try:
        print('queue2 start')
        my_list = ['debit', 'credit']
        for item in range(10050, 10100):
            random_item = random.choice(my_list)
            itemdict = {
                'itemname': "1item" + str(item),
                'itemvalue': item,
                'itemtype': random_item
            }
            worker2=worker(itemdict)
            print(worker2,"***********worker2***********")
        return "success"
    except Exception as e:
        print(traceback.format_exc())
        return "failed"

# def clear_queue(q):
#     with q.mutex:
#         q.queue.clear()
#         q.all_tasks_done.notify_all()
#         q.unfinished_tasks = 0


@payments.route("/check_mail", methods=["POST", "GET"])
def check_mail():
    try:
        print('mail start')
        mail_subject = "Testing Mail"
        recipients_list = ["tirupati@viyonafintech.com","sathyam.yadati@gmail.com"]
        template_name = "emails/emailotpverification.html"
        mail_data = {
        "amount":"1000"
        }
        domainUrl=domain
        send_asynchronous_email(mail_subject, recipients_list, template_name, mail_data)
        return render_template(template_name,domainUrl=domainUrl)
    except Exception as e:
        print(traceback.format_exc())
        return "failed"

def generate_random_name(length):
    first_letter = random.choice(string.ascii_uppercase)
    other_letters = ''.join(random.choice(string.ascii_lowercase) for _ in range(length - 1))
    return first_letter + other_letters

@payments.route("/payout_manual_insert_fundtransfer", methods=["POST","GET"])
def payout_manual_insert_fundtransfer():
    data_status = {"responseStatus": 0, "result":""}
    totalAmount=20000000
    userId = "667d13c17ebce740151a9437"
    while totalAmount>0:
        random_number = str(random_digit_generate(16))
        banksList=["Karur Vysya Bank Ltd","IDBI Bank Ltd","YES Bank Ltd","RBL Bank Ltd","South Indian Bank Ltd"]
        ifsccode=""
        bankName=random.choice(banksList)
        if bankName=="Karur Vysya Bank Ltd":
            ifsccode="KVBL0002118"
        elif bankName=="IDBI Bank Ltd":
            ifsccode="IBKL0000598"
        elif bankName=="YES Bank Ltd":
            ifsccode="YESB0MNSB01"
        elif bankName=="RBL Bank Ltd":
            ifsccode="RATN0000134"
        elif bankName=="South Indian Bank Ltd":
            ifsccode="SIBL0000028"
        else:
            ifsccode="IFSC0001045"
        amount = random.randint(200, 10000)
        if totalAmount<=amount:
            amount=totalAmount
        fundTransferType = "instant"
        accountType = "bank" # "bank","upi"
        beneficiaryName = str(generate_random_name(5))
        accountNumber = str(random_digit_generate(12))
        accountIFSCCode = ifsccode
        uniqueRequestNumber = ""
        bankBranch = ""
        paymentMode = "IMPS"
        beneficiaryMail = beneficiaryName+"@gmail.com"
        beneficiaryPhone = "7896534568"
        narration = "Manual fund transfer"
        reqdata={
        "userId":userId,
        "amount":amount,
        "bankName":bankName,
        "fundTransferType":fundTransferType,
        "accountType":accountType,
        "accountNumber":accountNumber,
        "beneficiaryName":beneficiaryName,
        "beneficiaryMail":beneficiaryMail,
        "accountIFSCCode":accountIFSCCode,
        }

        transactionAPIId = ""
        requestData = [reqdata]
        try:
            if amount and bankName and accountType and fundTransferType and accountNumber and userId and beneficiaryName:
                user_queryset = Users.objects(id=userId).first()
                transactionAPIId = str(user_queryset.payOutPaymentGatewayId.id)
                pgOrderId = str(user_queryset.merchantUniqueNumber)+"-"+str(random_number)
                merchant_reference_number = random_number
                payoutPaymentGatewayId = str(user_queryset.payOutPaymentGatewayId.id)
                payment_mode_queryset = PaymentMode.objects(paymentMode=paymentMode).first()
                paymentModeId = str(payment_mode_queryset.id)
                sub_payment_mode_queryset = SubPaymentModes.objects(paymentModeId=paymentModeId,subPaymentModeType=paymentMode).first()
                subPaymentModeId = str(sub_payment_mode_queryset.id)
                patternId = str(user_queryset.patternId.id)

                transactionAmount = float(amount)
                grandTotal = 0
                startAmount = 0
                endAmount = 0
                chargeType = ""
                chargeValue = 0
                chargeAmount = 0
                gstAmount = 0
                tdsAmount = 0
                gstInclude = ""
                tdsInclude = ""
                priceType = ""
                aggregatorType = ""
                aggregatorValue = 0
                aggregatorAmount = 0
                commissionType = ""
                commissionValue = 0
                commissionAmount = 0

                slab_queryset = SetupChargeCommissions.objects(
                    transactionAPIId=payoutPaymentGatewayId,paymentModeId=paymentModeId,subPaymentModeId=subPaymentModeId,patternId=patternId).first()
                if slab_queryset:
                    priceType = slab_queryset.priceType
                    gstInclude = slab_queryset.gstInclude
                    tdsInclude = slab_queryset.tdsInclude
                    gstValue = float(slab_queryset.gstValue)
                    tdsValue = float(slab_queryset.tdsValue)

                    if priceType == "RANGE":
                        priceRangeList = slab_queryset.priceRangeList
                        for each_record in priceRangeList:
                            startAmount = float(each_record.get("startAmount"))
                            endAmount = float(each_record.get("endAmount"))
                            
                            if (float(startAmount) <= float(amount)) and (float(amount) <= float(endAmount)):
                                chargeType = each_record.get("chargeType")
                                chargeValue = float(each_record.get("chargeValue"))
                                aggregatorType = each_record.get("aggregatorType")
                                aggregatorValue = float(each_record.get("aggregatorValue"))
                                commissionType = each_record.get("commissionType")
                                commissionValue = float(each_record.get("commissionValue"))

                    else:
                        chargeType = slab_queryset.chargeType
                        chargeValue = float(slab_queryset.chargeValue)
                        aggregatorType = slab_queryset.aggregatorType
                        aggregatorValue = float(slab_queryset.aggregatorValue)
                        commissionType = slab_queryset.commissionType
                        commissionValue = float(slab_queryset.commissionValue)


                    if chargeValue > 0:
                        if aggregatorType=="FLAT":
                            aggregatorAmount=float(aggregatorValue)
                        else:
                            aggregatorAmount=(float(aggregatorValue)*float(amount))/100

                        if commissionType=="FLAT":
                            commissionAmount=float(commissionValue)
                        else:
                            commissionAmount=(float(commissionValue)*float(amount))/100

                        if chargeType=="FLAT":
                            chargeAmount=chargeValue
                        else:
                            chargeAmount = (float(amount)*float(chargeValue))/100

                        transactionAmount=float(transactionAmount) + float(chargeAmount)

                        if gstValue>0:   
                            gstAmount = (float(chargeAmount)*float(gstValue))/100

                        if tdsValue>0:
                            tdsAmount = (float(chargeAmount)*float(tdsValue))/100
                            
                        if gstInclude == "Yes":
                            chargeAmount = float(chargeAmount)-float(gstAmount)
                        else:
                            transactionAmount = float(transactionAmount)+float(gstAmount)

                        if tdsInclude == "Yes":
                            chargeAmount = float(chargeAmount)-float(tdsAmount)
                        else:
                            transactionAmount = float(transactionAmount)+float(tdsAmount)
                try:
                    payout_balance_queryset = PayoutBalances.objects(userId=userId,transactionAPIId=transactionAPIId).first()
                    if payout_balance_queryset.currentBalance:
                        userPayoutBalance = payout_balance_queryset.currentBalance
                    else:
                        userPayoutBalance = 0
                except Exception as e:
                    userPayoutBalance = 0

                commissionCharges = {}
                commissionCharges = {
                "aggregatorType":aggregatorType,
                "aggregatorAmount":round(float(aggregatorAmount),2),
                "commissionType":commissionType,
                "commissionAmount":round(float(commissionAmount),2),
                "chargeType":chargeType,
                "chargeAmount":round(float(chargeAmount),2),
                "transactionAmount":round(float(transactionAmount),2),
                "gstInclude":gstInclude,
                "gstAmount":round(float(gstAmount),2),
                "tdsInclude":tdsInclude,
                "tdsAmount":round(float(tdsAmount),2),
                "priceType":priceType,
                }

                transactionData = []
                
                if slab_queryset:
                    slabId = str(slab_queryset.id)
                else:
                    slabId = None

                transactionId=random_digit_generate(16)
                prevuserquery_set=Users.objects(id=str(userId)).first()
                actuallBalance = prevuserquery_set.payoutBalance
                updateAmount = float(actuallBalance)-float(transactionAmount)
                prevuserquery_set.update(payoutBalance=round(float(updateAmount),2))
                prevpayout_balance_queryset = PayoutBalances.objects(userId=userId,transactionAPIId=transactionAPIId).first()
                previousBalance=prevpayout_balance_queryset.currentBalance
                payoutCurrentBalance=float(previousBalance)-float(transactionAmount)
                prevpayout_balance_queryset.update(previousBalance=round(float(previousBalance),2),currentBalance=round(float(payoutCurrentBalance),2),updatedOn=datetime.datetime.now())
                payoutbalancelog_queryset=PayoutBalanceLogs(
                    userId =str(userId),
                    transactionAPIId =str(transactionAPIId),
                    previousBalance = round(float(previousBalance),2),
                    currentBalance = round(float(payoutCurrentBalance),2),
                    amount=round(float(amount),2),
                    grandTotal=round(float(transactionAmount),2),
                    orderId=merchant_reference_number,
                    transferType = "Debit",
                    userType = "user",
                    transactionId = transactionId,
                    createdOn = datetime.datetime.now(),
                    status = 1
                    ).save()

                fund_transfer_table = FundTransfers(
                    userId=userId,
                    slabId=slabId,
                    transactionAPIId=transactionAPIId,
                    bankId=bankId,
                    bankName=str(master_ifsc_queryset.bankName),
                    merchantReferenceNumber=merchant_reference_number,
                    fundTransferType=fundTransferType,
                    accountType=accountType,
                    apiType="manual",
                    bankBranch=bankBranch,
                    accountNumber=accountNumber,
                    accountIFSCCode=accountIFSCCode,
                    beneficiaryName=beneficiaryName,
                    uniqueRequestNumber=uniqueRequestNumber,
                    amount=round(float(amount),2),
                    grandTotal=round(float(transactionAmount),2),
                    transferType="Debit",
                    beneficiaryMail=beneficiaryMail,
                    beneficiaryPhone=beneficiaryPhone,
                    paymentMode=paymentMode,
                    narration=narration,
                    commissionCharges=commissionCharges,
                    createdOn=datetime.datetime.now(),
                    pgOrderId=pgOrderId,
                    userType="user",
                    previousBalance = round(float(actuallBalance),2),
                    currentBalance = round(float(updateAmount),2),
                    status=2
                    )
                save_table = fund_transfer_table.save()
                instantTransferId = str(save_table.id)
                fund_transfer_queryset = FundTransfers.objects(id=instantTransferId).first()

                # Here Routing Switch Table Log Save and Update
                currentDate = datetime.datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
                routing_switch_counts_queryset = RoutingSwitchCounts.objects(userId=userId,transactionAPIId=transactionAPIId,createdOn=currentDate).first()
                if routing_switch_counts_queryset:
                    numberOfFails = routing_switch_counts_queryset.numberOfFails
                    numberOfSuccess = routing_switch_counts_queryset.numberOfSuccess

                else:
                    save_routing_switch_table=save_routing_switch_logs_data(userId,"payout",str(transactionAPIId))


                # check_routing_switch_api = check_transaction_routing(userId, str(transactionAPIId), 0,amount,"payout")
                # if check_routing_switch_api.get("responseStatus") == 1:
                #     routingUserId = check_routing_switch_api.get("userId")
                #     routingApiId = check_routing_switch_api.get("apiId")

                #     currentDate = datetime.datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
                #     routing_switch_counts_queryset = RoutingSwitchCounts.objects(userId=userId,transactionAPIId=transactionAPIId,createdOn=currentDate).first()
                #     if routing_switch_counts_queryset:
                #         numberOfFails = check_routing_switch_api.get("numberOfFails")
                #         numberOfSuccess = check_routing_switch_api.get("numberOfSuccess")
                        
                #     else:
                #         routingUserId = check_routing_switch_api.get("userId")
                #         routingApiId = check_routing_switch_api.get("apiId")

                #         save_routing_switch_table=save_routing_switch_logs_data(userId,"payout",str(apiId))

                # elif check_routing_switch_api.get("responseStatus") == 2:
                #     routingUserId = check_routing_switch_api.get("userId")
                #     routingApiId = check_routing_switch_api.get("apiId")

                #     save_routing_switch_table=save_routing_switch_logs_data(userId,"payout",str(apiId))

                if user_queryset.payOutPaymentGatewayId.code == "AccurePay_Payout":

                    for each_key in user_queryset.payOutPaymentGatewayId.paramsList:
                        get_key = each_key.get("key")        
                        if get_key == "api_key":
                            api_key = each_key.get("value").strip()
                        if get_key == "salt":
                            salt = each_key.get("value").strip()
                        if get_key == "base_url":
                            accurePayOutLiveURL = each_key.get("value").strip()

                    bank_name = master_ifsc_queryset.bankName

                    accurePayoutResponseData = accurepay_payout_request_data_response(userId,transactionAPIId,client_ip,"web",api_key,salt,accurePayOutLiveURL,pgOrderId,amount,paymentMode,beneficiaryName,accountNumber,accountIFSCCode,bank_name,merchant_reference_number)

                    print(accurePayoutResponseData,"((((((((((((((AccurePay Instant Payout))))))))))))))")
                    transactionData = [accurePayoutResponseData]


                    if "data" in accurePayoutResponseData:
                        previousBalance=payout_balance_queryset.currentBalance
                        payoutCurrentBalance=float(previousBalance)-float(transactionAmount)
                        
                        if accurePayoutResponseData["data"].get("transaction_id"):
                            fund_transfer_queryset.update(transactionUniqueId=accurePayoutResponseData["data"].get("transaction_id"),internalId=accurePayoutResponseData["data"].get("internal_id"))
                        else:
                            fund_transfer_queryset.update(transactionUniqueId=accurePayoutResponseData["data"].get("internal_id"),internalId=accurePayoutResponseData["data"].get("internal_id"))

                        if accurePayoutResponseData["data"].get("status") == "SUCCESS":
                            actuallBalance = user_queryset.payoutBalance
                            updateAmount = float(actuallBalance)-float(transactionAmount)
                            fund_transfer_queryset.update(
                                transactionData=transactionData,
                                previousBalance = round(float(actuallBalance),2),
                                currentBalance = round(float(updateAmount),2),
                                errorMessage = accurePayoutResponseData["data"].get("status"),
                                status=1
                                )
                            user_queryset.update(payoutBalance=round(float(updateAmount),2))
                            payout_balance_queryset.update(previousBalance=round(float(previousBalance),2),currentBalance=round(float(payoutCurrentBalance),2),updatedOn=datetime.datetime.now())

                        elif accurePayoutResponseData["data"].get("status") == "PROCESSING":
                            actuallBalance = user_queryset.payoutBalance
                            updateAmount = float(actuallBalance)-float(transactionAmount)
                            fund_transfer_queryset.update(
                                transactionData=transactionData,
                                previousBalance = round(float(actuallBalance),2),
                                currentBalance = round(float(updateAmount),2),
                                errorMessage = accurePayoutResponseData["data"].get("status"),
                                status=2
                                )
                            user_queryset.update(payoutBalance=round(float(updateAmount),2))
                            payout_balance_queryset.update(previousBalance=round(float(previousBalance),2),currentBalance=round(float(payoutCurrentBalance),2),updatedOn=datetime.datetime.now())
                        else:
                            fund_transfer_queryset.update(
                                transactionData=transactionData,
                                errorMessage=accurePayoutResponseData["data"].get("error_message"),
                                status=0
                                )

                        data_status["responseStatus"] = 1
                        data_status["result"] = "Instant transfer was successful!"
                        # For count update number of success in routing switch counts table
                        if routing_switch_counts_queryset:
                            existingSuccessCount = routing_switch_counts_queryset.numberOfSuccess
                            updateSuccessCount = existingSuccessCount+1

                            existingSuccessAmount = float(routing_switch_counts_queryset.maxVolume)
                            updateSuccessAmount = float(existingSuccessAmount)+float(amount)

                            routing_switch_counts_queryset.update(numberOfSuccess=updateSuccessCount,maxVolume=updateSuccessAmount)
                        else:
                            save_routing_switch_table.update(numberOfSuccess=1,maxVolume=amount)
                        return data_status
                    else:
                        fund_transfer_queryset.update(transactionData=transactionData,status=0,errorMessage=accurePayoutResponseData["error"].get("message"))
                        data_status["result"]=accurePayoutResponseData["error"].get("message")
                        save_client_table.update(responseData=[data_status],responseDate=datetime.datetime.now())
                        # For count update number of failures in routing switch counts table
                        if routing_switch_counts_queryset:
                            existingFailuresCount = routing_switch_counts_queryset.numberOfFails
                            updateFailuresCount = existingFailuresCount+1

                            existingSuccessAmount = float(routing_switch_counts_queryset.maxVolume)
                            updateSuccessAmount = float(existingSuccessCount)+float(amount)

                            routing_switch_counts_queryset.update(numberOfFails=updateFailuresCount)
                        else:
                            save_routing_switch_table.update(numberOfFails=1)

                        return data_status

                elif user_queryset.payOutPaymentGatewayId.code == "WowPe_Payout":
                    dynamicClientId = ""
                    dynamicSecret = ""
                    wowpePayoutLiveURL = ""

                    for each_key in user_queryset.payOutPaymentGatewayId.paramsList:
                        get_key = each_key.get("key")        
                        if get_key == "api_key":
                            dynamicClientId = each_key.get("value").strip()
                        if get_key == "secret_key":
                            dynamicSecret = each_key.get("value").strip()
                        if get_key == "base_url":
                            wowpePayoutLiveURL = each_key.get("value").strip()

                    wowpe_request_body = {
                      "clientId": dynamicClientId,
                      "secretKey": dynamicSecret,
                      "number": beneficiaryPhone,
                      "amount": amount,
                      "transferMode": paymentMode,
                      "accountNo": accountNumber,
                      "ifscCode": accountIFSCCode,
                      "beneficiaryName": beneficiaryName,
                      "vpa": "",
                      "clientOrderId": pgOrderId
                    }
                    print(wowpe_request_body,"WOWPE REQUEST BODY")

                    headers = {
                        "accept": "application/json",
                        "content-type": "application/json"
                    }
                    url = wowpePayoutLiveURL+"/payout"
                    # Create API Logs Here
                    requestData=[wowpe_request_body]
                    wowpeResponseData={}
                    save_api_log_table = save_api_logs_data(userId,"payout","fund_transfer",url,transactionAPIId,requestData,client_ip,"web")
                    try:
                        response = requests.post(url,json=wowpe_request_body, headers=headers)
                        print(response.text,"(((((((((((((WOWPE PAYOUT RESPONSE)))))))))))))")
                        wowpeResponseData = json.loads(response.text)
                    except Exception as e:
                        app.logger.error(traceback.format_exc())
                        data_status['result']="Our banking partner server is down please try after sometime!!"
                        return data_status
                    
                    # API Log Update Here
                    save_api_log_table.update(responseData=[wowpeResponseData],responseDate=datetime.datetime.now())

                    # Client Log Update Here
                    save_client_table.update(responseData=[wowpeResponseData],responseDate=datetime.datetime.now())

                    transactionData = [wowpeResponseData]

                    if wowpeResponseData:
                        transactionstatus=1
                        bank_reference_number=''
                        transaction_id=''
                        messages="Success"
                        if wowpeResponseData.get('status')==400:
                            transactionstatus=0
                            errordct=wowpeResponseData.get('errors')
                            traceId=wowpeResponseData.get('traceId')
                            transaction_id=traceId
                            messageslist=[]
                            messages=","
                            for i in errordct:
                                for j in errordct[i]:
                                    messageslist.append(j)

                            messages=','.join([str(elem) for elem in messageslist])
                            print(messages)
                        else:
                            if wowpeResponseData.get("statusCode")==0:
                                transactionstatus=0
                            elif wowpeResponseData.get("statusCode") == 1 and wowpeResponseData.get("status") == 1:
                                transactionstatus=1
                            elif wowpeResponseData.get("statusCode") == 1 and wowpeResponseData.get("status") == 0:
                                transactionstatus=0
                            elif wowpeResponseData.get("statusCode") == 1 and wowpeResponseData.get("status") == 4:
                                transactionstatus=4
                            else:
                                transactionstatus=2

                            messages=wowpeResponseData.get('message')
                            transaction_id=wowpeResponseData.get('orderId')
                            bank_reference_number=wowpeResponseData.get('utr')
                        
                        latest_fund_transfer_queryset = FundTransfers.objects(id=instantTransferId).first()
                        previousstatus=latest_fund_transfer_queryset.status
                        if transactionstatus==0 or transactionstatus==4:
                            if previousstatus==1 or previousstatus==2:
                                userquery_set=Users.objects(id=str(userId)).first()
                                userpreviousbalance=userquery_set.payoutBalance
                                usercurrentbalance=round(float(userpreviousbalance),2)+round(float(transactionAmount))
                                userquery_set.update(payoutBalance=round(float(usercurrentbalance)))
                                updatepayout_balance_queryset = PayoutBalances.objects(userId=str(userId),transactionAPIId=transactionAPIId).first()
                                refundpreviousBalance=updatepayout_balance_queryset.currentBalance
                                refundpayoutCurrentBalance=float(refundpreviousBalance)+float(transactionAmount)
                                updatepayout_balance_queryset.update(previousBalance=round(float(refundpreviousBalance),2),currentBalance=round(float(refundpayoutCurrentBalance),2),updatedOn=datetime.datetime.now())
                                merchantReferenceNumber=random_digit_generate(16)
                                refund_transfer_table = FundTransfers(
                                    userId=str(userId),
                                    transactionAPIId=transactionAPIId,
                                    bankId=bankId,
                                    merchantReferenceNumber=merchantReferenceNumber,
                                    transactionUniqueId=merchant_reference_number,
                                    pgOrderId=transaction_id,
                                    fundTransferType="instant",
                                    accountType="bank",
                                    apiType="api",
                                    slabId=slabId,
                                    transferType="Refund",
                                    bankBranch=bankBranch,
                                    accountNumber=accountNumber,
                                    accountIFSCCode=accountIFSCCode,
                                    beneficiaryName=beneficiaryName,
                                    uniqueRequestNumber=uniqueRequestNumber,
                                    amount=round(float(amount),2),
                                    grandTotal=round(float(transactionAmount),2),
                                    previousBalance = round(float(userpreviousbalance),2),
                                    currentBalance = round(float(usercurrentbalance),2),
                                    beneficiaryMail=beneficiaryMail,
                                    bankReferenceNumber=bank_reference_number,
                                    beneficiaryPhone=beneficiaryPhone,
                                    paymentMode=paymentMode,
                                    narration=narration,
                                    createdOn=datetime.datetime.now(),
                                    userType="user",
                                    status=5,
                                    commissionCharges=commissionCharges
                                    )
                                save_table = refund_transfer_table.save()
                                
                                payoutbalancelog_queryset=PayoutBalanceLogs(
                                    userId =str(userId),
                                    transactionAPIId =str(transactionAPIId),
                                    previousBalance = round(float(refundpreviousBalance),2),
                                    currentBalance = round(float(refundpayoutCurrentBalance),2),
                                    amount=round(float(amount),2),
                                    grandTotal=round(float(transactionAmount),2),
                                    orderId=merchant_reference_number,
                                    transferType = "Refund",
                                    userType = "user",
                                    transactionId = transaction_id,
                                    createdOn = datetime.datetime.now(),
                                    status = 1
                                    ).save()

                        fund_transfer_queryset.update(
                            transactionData=transactionData,
                            errorMessage=messages,
                            transactionUniqueId=transaction_id,
                            bankReferenceNumber=bank_reference_number,
                            status=transactionstatus
                            )

                        try:
                            merchantName = str(user_queryset.fullName)
                            trnsactionDate = datetime.datetime.now().astimezone(ist_timezone).strftime("%d-%m-%Y %I:%M %p")
                            mail_subject = "Instant fund transfer initiated from " + merchantName + "."
                            recipients_list = [admin_recieve_email]
                            template_name = "emails/instant_fundtransfer.html"
                            mail_data = {
                            "merchantName":merchantName,
                            "accountNumber":accountNumber,
                            "bankName":master_ifsc_queryset.bankName,
                            "ifscCode":accountIFSCCode,
                            "amount":formatINR("{:.2f}".format(float(amount))),
                            "transactionId":transaction_id,
                            "orderId":merchant_reference_number,
                            "transactionDate":trnsactionDate,
                            }
                            domainUrl=domain
                            mailoutputData = send_asynchronous_email(mail_subject, recipients_list, template_name, mail_data)
                        except Exception as e:
                            app.logger.error(traceback.format_exc())
                            pass

                        data_status["responseStatus"] = 1
                        data_status["result"] = "Instant transfer was successful!"
                        # For count update number of success in routing switch counts table
                        if routing_switch_counts_queryset:
                            existingSuccessCount = routing_switch_counts_queryset.numberOfSuccess
                            updateSuccessCount = existingSuccessCount+1

                            existingSuccessAmount = float(routing_switch_counts_queryset.maxVolume)
                            updateSuccessAmount = float(existingSuccessAmount)+float(amount)

                            routing_switch_counts_queryset.update(numberOfSuccess=updateSuccessCount,maxVolume=updateSuccessAmount)
                        else:
                            save_routing_switch_table.update(numberOfSuccess=1,maxVolume=amount)
                        return data_status
                    else: 
                        data_status["result"]="Our banking partner server is down please try after sometime!!"
                        save_client_table.update(responseData=[data_status],responseDate=datetime.datetime.now())
                        # For count update number of failures in routing switch counts table
                        if routing_switch_counts_queryset:
                            existingFailuresCount = routing_switch_counts_queryset.numberOfFails
                            updateFailuresCount = existingFailuresCount+1
                            routing_switch_counts_queryset.update(numberOfFails=updateFailuresCount)
                        else:
                            save_routing_switch_table.update(numberOfFails=1)
                        return data_status
            else:
                data_status["result"]="Required fields are missing!!"
                save_client_table.update(responseData=[data_status],responseDate=datetime.datetime.now())
                # For count update number of failures in routing switch counts table
                # routing_switch_counts_queryset = RoutingSwitchCounts.objects(userId=userId,transactionAPIId=transactionAPIId,createdOn=currentDate).first()
                # if routing_switch_counts_queryset:
                #   existingFailuresCount = routing_switch_counts_queryset.numberOfFails
                #   updateFailuresCount = existingFailuresCount+1

                #   existingSuccessAmount = float(routing_switch_counts_queryset.maxVolume)
                #   updateSuccessAmount = float(existingSuccessCount)+float(amount)

                #   routing_switch_counts_queryset.update(numberOfFails=updateFailuresCount)
                # else:
                #   save_routing_switch_table.update(numberOfFails=1)
                return data_status
        except Exception as e:
            app.logger.error(traceback.format_exc())
            data_status["result"] = "Instant transfer was unsuccessful!!"
            exceptionData = [traceback.format_exc()]
            exception_log_table = save_exception_logs_data(userId,"payout","fund_transfer","payout_instant_transfer",exceptionData,client_ip,"web")
            save_client_table.update(responseData=[data_status],responseDate=datetime.datetime.now())
            return data_status   

@payments.route("/wowpe_checksum_check", methods=["POST","GET"])
def wowpe_checksum_check():
    try:
        responseDict={
        "OrderId": "ENAN1000120196830",
        "Date": "7/19/2024 11:01:45 AM",
        "Status": 1,
        "ClientOrderId": "M00000037-JATJH4MNVQ",
        "Currency": "INR",
        "Amount": "1000.0000",
        "OrderAmount": "1000.0000",
        "RefNumber": "ENAN100013363409",
        "BankRRN": "420124941040",
        "MerchantId": "20196830",
        "PayerVPA": "dprasadd6@ybl",
        "PayerMobile": "919409330547",
        "PayerName": "test",
        "PaymentMode": "Intent",
        }


        Checksum="R3ke1RiQL8rksF1XoDoj1u1I0eubIxxS9KZAtEJGo2EdsxLfyoCNTzpFm9PNHKuhecb/vIMkkuPEavcJtg5Xigj8vbhdLe/MXdLgX5HSJDu+kwQyrJCC3hQkcD//R+6A30h28Aq2hyzlKIW5KXdqU2IA9hF80F90WDkFLmcQ1oZ8mr2cAKAyyW1Auh9kzYzVZIgME5TQGeDdc63NBzOUf9fYzeONUH1w4L6oCZhhL8tPzNYOsr0k+rYcvbf2Pge50aahEotjyA3XQKt5HXLm8bvE5TW1Sw8TjRkyVtrUvH+lAcVjXLiNQuSK/Nw6fPhUImR8NFUyE9hIp3i6QSUdOTUOm4tmt45halZc+54xdGSL+5sPMz4zeTHBJ4Tq3QDoL9vAJ4qn4t3h+CCBnfgScchhguNu5rhkje3RLtEWhpLvaUX/bBChhC2fpKbzvdaO"

        secretKey = "ef909009-df0c-9e09-be09-09e090c0"
        key = "1234567890123456"
        encryption_key = bytes(secretKey, 'utf-8')
        iv=base64.b64encode(key.encode('utf-8')).decode('utf-8')
        checkSumData = checksum_encrypt(responseDict, encryption_key)
        jsonData = decrypt(checkSumData.get('encryption_data'),encryption_key,iv)
        decryptResponse = json.loads(jsonData)
        print(decryptResponse,"decryptResponse")
        checkSumData['decryptResponse']=decryptResponse
        return checkSumData
        
         
    except Exception as e:
        raise e

def checksum_encrypt(encryption_data, encryption_key):
    encryptDict = {}
    try:
        # Convert the dictionary to a JSON string
        raw = json.dumps(encryption_data)
        key = "1234567890123456"
        iv=key.encode('utf-8')
        # Create a new AES cipher object in CBC mode
        cipher = AES.new(encryption_key, AES.MODE_CBC, iv)
        
        # Pad the plaintext to be a multiple of the block size
        padded_data = pad(raw.encode(), AES.block_size)
        
        # Encrypt the padded plaintext
        encrypted_data = cipher.encrypt(padded_data)
        
        # Encode the IV and encrypted data with base64
        encryptDict = {
            "iv": base64.b64encode(iv).decode('utf-8'),
            "encryption_data": base64.b64encode(encrypted_data).decode('utf-8')
        }
        return encryptDict
    except Exception as e:
        print(traceback.format_exc())
        return encryptDict

def generateToken_test():
    token_url="https://accounts.payu.in/oauth/token"
    headers = {
        "content-type": "application/json",
        "content-type": "application/x-www-form-urlencoded"
        }
    payload="client_id=35e731ff20e6bc521de9f29618b22c1240a4c69f6350c50e0fcb8e6b7d6c1bec&client_secret=610a66e8fb918d2c6a7ccf5674ea2893cb7cbbd589a0c3d9544e3f3ddf7366f4&scope=read_bills create_transactions read_billers read_biller_categories read_transactions read_circles read_operators read_plans read_operator_circle &grant_type=client_credentials"
    response = requests.post(token_url,data=payload, headers=headers)
    
    if response.status_code == 200:
        token_data = response.json()
        print(f"Token generated successfully: {token_data}")
        return token_data.get("access_token")
    else:
        print(f"Failed to generate token. Status code: {response.status_code}")
        print(f"Error: {response.text}")
        return None
        
@payments.route("/getCircleList", methods=["POST","GET"])
def getCircleList():
    try:
        token = generateToken_test()
        # url = "https://nbc.payu.in/payu-nbc/v3/nbc/getRechargePlans"
        url = "https://nbc.payu.in/payu-nbc/v2/nbc/getOperatorAndCircleInfo"
        getRechargePlans_body={
            "agentId": "VFIPL20240222",
            "operatorId": "AT",
            "circleId": "2" 
        }
        operatorby_circle_body={
            "agentId": "VFIPL20240222",
            "customerPhoneNumber": "9849747800"
            }
        getCustomizedRechargePlans_body={
                "agentId": "VFIPL20240222",
                "refId": "54523652147856945123658565235",
                "mobileNumber": "8639027281",
                "operatorId": "AT"
            }
        
        headers = {'Content-type': 'application/json',"Authorization":f"Bearer {str(token)}"}
        print("((((((((body))))))))", operatorby_circle_body)
        # payuresponse = requests.get(url,headers=headers)
        # payuresponse = requests.post(url, json=getCustomizedRechargePlans_body, headers=headers)
        payuresponse = requests.post(url, json=operatorby_circle_body, headers=headers)

        print(payuresponse.text,"(((((((((((((payuresponse)))))))))))))")
        payuresponsedata = json.loads(payuresponse.text)
        print(payuresponsedata,"(((((((((((((payuresponsedata)))))))))))))")
        return payuresponsedata
    except Exception as e:
        app.logger.error(traceback.format_exc())
        print(traceback.format_exc(),"Error")

@payments.route("/getcategories", methods=["POST","GET"])
def getcategories():
    try:
        url = "https://nbc.payu.in/payu-nbc/v1/nbc/getAllBillerCategory"
        headers = {'Content-type': 'application/json',"Authorization":"Bearer a083e82dd7d7a6ddc6b9038010b8dc51de1168b487941e2b31517c65d9af39f0"}
        payuresponse = requests.get(url,headers=headers)
        print(payuresponse.text,"(((((((((((((payuresponse)))))))))))))")
        payuresponsedata = json.loads(payuresponse.text)
        print(payuresponsedata,"(((((((((((((payuresponsedata)))))))))))))")
        return payuresponsedata
    except Exception as e:
        app.logger.error(traceback.format_exc())
        print(traceback.format_exc(),"Error")

@payments.route("/getBillerByBillerCategory", methods=["POST","GET"])
def getBillerByBillerCategory():
    data_status={'result':""}
    try:
        url = "https://nbc.payu.in/payu-nbc/v1/nbc/getBillerByBillerCategory?billerCategoryName=INSURANCE"
        headers = {'Content-type': 'application/json',"Authorization":"Bearer 5b49f8b55add1735170f12e2cc15050fb7cd99e051c7670d1f6fe072e9c7b6d4"}
        payuresponse = requests.get(url,headers=headers)
        print(payuresponse.text,"(((((((((((((payuresponse)))))))))))))")
        payuresponsedata = json.loads(payuresponse.text)
        data_status['result']=payuresponsedata
        return data_status
    except Exception as e:
        app.logger.error(traceback.format_exc())
        print(traceback.format_exc(),"Error")

@payments.route("/billfetchrequest_test", methods=["POST","GET"])
def billfetchrequest_test():
    try:
        url = "https://nbc.payu.in/payu-nbc/v1/nbc/billfetchrequest"
        print(url,"url")
        payload = {
        "customerParams": {
        "Mobile Number": "8985991588",
        },
        "deviceDetails": {
        "INITIATING_CHANNEL": "MOB",
        "IP": "183.83.216.35",
        "APP": "GraamPay",
        "IMEI": "754249835820316",
        "OS": "ANDROID"
        },
        "agentId": "VFIPL20240222",
        "billerId": "INDI00000NATT5",
        "customerName": "Tirupati Rao",
        "customerPhoneNumber": "9492844021",
        "timeStamp": "2024-08-30 18:49:52",
        "refId": "67575648645435633783746764364552765"
        }
        print(payload,"payload")
        headers = {
        "content-type": "application/json",
        "authorization": "Bearer 4ab8ae1e07aaa28f7df3d0fd4d8ff19e18b854c165f7e52cd8b5451f848513af",
        "clientCode": "610a66e8fb918d2c6a7ccf5674ea2893cb7cbbd589a0c3d9544e3f3ddf7366f4",
        }
        print(headers,"headers")

        payuresponse = requests.post(url, json=payload, headers=headers)
        print(payuresponse.text,"(((((((((((((payuresponse)))))))))))))")
        payuresponsedata = json.loads(payuresponse.text)
    except Exception as e:
        app.logger.error(traceback.format_exc())
        print(traceback.format_exc(),"Error")

# def billfetchrequest(userId,payload,authorizationToken,clientCode):
#     payuresponsedata = {}
#     try:
#         url = "https://nbc.payu.in/payu-nbc/v1/nbc/billfetchrequest"
#         print(payload,"PAYLOAD")
#         headers = {
#         "content-type": "application/json",
#         "authorization": "Bearer "+str(authorizationToken),
#         "clientCode": clientCode,
#         }
#         print(headers,"headers")

#         payuresponse = requests.post(url, json=payload, headers=headers)
#         print(payuresponse.text,"(((((((((((((payuresponse)))))))))))))")
#         payuresponsedata = json.loads(payuresponse.text)
#         return payuresponsedata
#     except Exception as e:
#         app.logger.error(traceback.format_exc())
#     return payuresponsedata



@payments.route("/billpaymentapi_test", methods=["POST","GET"])
def billpaymentapi_test():
    try:
        url = "https://nbc.payu.in/payu-nbc/v1/nbc/billpaymentrequest"
        print(url,"url")

        payload = {
        "paymentDetails": {
            "params": {
            "cardNumber": "4514570020769758",
            "authCode": "ABC123456"
            },
            "paymentMode": "CC"
        },
        "userDetails": {
        "firstName": "Kunabilli Tirupathirao",
        "phone": "9492844021",
        "email": "thirupathirao57@gmail.com"
        },
        "isQuickPay": False,
        "deviceDetails": {
        "INITIATING_CHANNEL": "MOB",
        "IP": "183.83.216.35",
        "APP": "GraamPay",
        "OS": "Android",
        "IMEI": "754249835820316"
        },
        "additionalParams": { 
        "agentTxnID": "agTXN123456" 
        },
        "customerParams": {
        "Policy Number": "619477027",
        "Email ID": "thirupathirao57@gmail.com",
        "Date of Birth (DD/MM/YYYY)": "25/08/1990"
        },
        "agentId": "VFIPL20240222",
        "billerId": "LIFE00000NATL6",
        "paidAmount": 8412.1,
        "paymentName": "TOTAL",
        "timeStamp": "2024-08-30 16:36:37",
        "refId": "67575648668448639639746764364552765",
        "paymentRefID": "AB123456",
        "pgName": "other"
        }
        print(payload,"payload")
        headers = {
        "content-type": "application/json",
        "authorization": "Bearer 35ea0a7170ffbc576292578f5d2800bde37367841d3eb3391e23bbee3431e78a",
        }
        print(headers,"headers")

        payuresponse = requests.post(url, json=payload, headers=headers)
        print(payuresponse.text,"(((((((((((((payuresponse)))))))))))))")
        payuresponsedata = json.loads(payuresponse.text)
    except Exception as e:
        app.logger.error(traceback.format_exc())
        print(traceback.format_exc(),"Error")




@payments.route("/verifyCardholder", methods=["POST","GET"])
def verifyCardholder():
    try:
        url = "https://info.payu.in/merchant/postservice.php/mob1/API/onboarding/v1/verifyCardholder"
        print(url,"url")

        secureCode="543938383838345333627738"
        transactionId=random_digit_generate(14)
        urn=random_digit_generate(11)
        payload = {
        "messageCode": 3020,
        "clientTxnId": transactionId,
        "requestDateTime": str(get_epoch_milli_time()),
        "urn": urn,
        "last4Digits": "9155",
        "cardExpiry": "0429",
        "email": "thirupathirao57@gmail.com",
        "dob": "25-08-1990",
        "customerMobile": "919492844021"
        }
        print(payload,"payload")
        payload1={
        "token":"stOecKbhd+RIyphFlcIrYRHhRoprl+e9arv2hIDA7X8bl3LZgHb+Kp9jyD60fJID6q46houN1EH0lx6jbKWUcir0C20Oq+STDHfOtfqjyDRNLZ7PzRHdj4Hn6MvM3URaOpzZF61NECCSSkK0258jyfXc2bGWS0ghGDCvgi7ykXdOzsfFPZ0zM8+d1+cT+JfT9woZYxsrJceL8EVYYVH/b9vRILOu1cQcDQEt/TNr36jKo+yMYQ4KTMTawxAWNEWKqHmMEdrOBRwQpUw7Mh6kntu7gzVCU59tPpLJUY1BKLlM1GkabDg/Ntw1nMFW7GOm0ci8ce3RtpNDO1mbFpSHuUMP7g0DUlXhDxBuAzM+9Bg="
        }
        print(payload1,"payload")
        headers = {
        "x-api-key": "WI60gT",
        "clientId": "35e731ff20e6bc521de9f29618b22c1240a4c69f6350c50e0fcb8e6b7d6c1bec",
        "bankId": "7000",
        "entityId": "100",
        "content-type": "application/json",
        "secureCode": secureCode,
        }
        print(headers,"headers")

        payuresponse = requests.post(url, json=payload1, headers=headers)
        print(payuresponse.text,"(((((((((((((payuresponse)))))))))))))")
        payuresponsedata = json.dumps(payuresponse.text)
        return payuresponsedata
    except Exception as e:
        app.logger.error(traceback.format_exc())
        print(traceback.format_exc(),"Error")


def jwt_encryption_data(private_key):
    encrypted_jwt_string = ""
    try:
        jwt_headers = {
            "alg": "RS256",
            "typ": "JWT",
            "kid": "be256eed-a767-456e-84b3-cb760e80b13b"
        }
        # print(jwt_headers,"(((((((((((JWT HEADERS)))))))))))")
        jwt_payload = {
            "jti": str(random_digit_generate(14)),
            # "jti": "1623863273200",
            "sub": "b7f93858-d33e-449a-9555-ff6ff428df3b",
            "iss": "b7f93858-d33e-449a-9555-ff6ff428df3b",
            "aud": "https://app.my.idfcfirstbank.com/platform/oauth/oauth2/token",
            "exp": 1726813715
        }
        # print(jwt_payload,"(((((((((((JWT PAYLOAD)))))))))))")
        encrypted_jwt = jwt.encode(jwt_payload, private_key, algorithm='RS256', headers=jwt_headers)
        encrypted_jwt_string=encrypted_jwt.decode('utf-8')
    except Exception as e:
        app.logger.error(traceback.format_exc())
    return encrypted_jwt_string


requestData = {
    "initiateAuthGenericFundTransferAPIReq": {
         "tellerBranch": "",
         "tellerID": "",
         "transactionID":str(random_digit_generate(8)),
         "debitAccountNumber":"10167310372",
         "creditAccountNumber":"152404536784",
         "remitterName":"MERCHANT PAYOUTS-VIYONA ESCROW ACCOUNT",
         "amount":"1.00",
         "currency":"INR",
         "transactionType":"IMPS",
         "paymentDescription":"Narration",
         "beneficiaryIFSC":"INDB0000226",
         "beneficiaryName":"Sai Krishna",
         "beneficiaryAddress": "Hyderabad",
         "emailID": "",
         "mobileNo":"9502971443"
    }
}

# print(requestData,"FT REQUEST DATA")


# def idfc_encrypt(encryption_data, encryption_key):
#     encryptDict = {}
#     try:
#         # Convert the dictionary to a JSON string
#         raw = json.dumps(encryption_data)
        
#         # Generate a random initialization vector (IV)
#         iv = get_random_bytes(AES.block_size)
        
#         # Create a new AES cipher object in CBC mode
#         cipher = AES.new(encryption_key, AES.MODE_CBC, iv)
        
#         # Pad the plaintext to be a multiple of the block size
#         padded_data = pad(raw.encode(), AES.block_size)
        
#         # Encrypt the padded plaintext
#         encrypted_data = cipher.encrypt(padded_data)
        
#         combined = iv + encrypted_data

#         print(combined,"((((((((combined))))))))")

#         # Encode the IV and encrypted data with base64
#         encryptDict = {
#             "combinedEncryption":base64.b64encode(combined).decode('utf-8'),
#             "iv": base64.b64encode(iv).decode('utf-8'),
#             "encryption_data": base64.b64encode(encrypted_data).decode('utf-8')
#         }
#         return encryptDict
#     except Exception as e:
#         print(traceback.format_exc())
#         return encryptDict

import base64
import os
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
from binascii import hexlify, unhexlify

class CustomException(Exception):
    pass

def generate_iv():
    iv_length = 16
    iv = os.urandom(iv_length)
    return iv

def idfc_encrypt(data_to_encrypt, secret_hex_key):
    final_encrypted_payload_base64 = ""
    try:
        raw_data_to_encrypt = json.dumps(data_to_encrypt)
        final_encrypted_payload = b''
        secret_key_hex_bytes = unhexlify(secret_hex_key)
        iv = generate_iv()
        print("Dynamic IV: ", hexlify(iv).decode())
        cipher = AES.new(secret_key_hex_bytes, AES.MODE_CBC, iv=iv)
        encrypted_bytes = cipher.encrypt(pad(raw_data_to_encrypt.encode('utf-8'), AES.block_size))
        final_encrypted_payload = iv + encrypted_bytes
        final_encrypted_payload_base64 = base64.b64encode(final_encrypted_payload).decode()
        return final_encrypted_payload_base64
    except Exception as e:
        print(traceback.format_exc())
        return final_encrypted_payload_base64

def idfc_decrypt(encrypted, secret_key):
    decrypted_text = ''
    try:
        secret_key_hex_bytes = unhexlify(secret_key)
        encrypted_combined_bytes = base64.b64decode(encrypted)
        iv = encrypted_combined_bytes[:16]
        encrypted_payload = encrypted_combined_bytes[16:]
        cipher = AES.new(secret_key_hex_bytes, AES.MODE_CBC, iv=iv)
        decrypted_bytes = unpad(cipher.decrypt(encrypted_payload), AES.block_size)
        decrypted_text = decrypted_bytes.decode()
        return decrypted_text
    except Exception as e:
        print(traceback.format_exc())
        return decrypted_text

@payments.route("/idfc_decrypt_check",methods=["POST"])
def idfc_decrypt_check():
    encrypted = request.json.get("encrypted")
    secret_key = request.json.get("secretKey")
    decrypted_text = idfc_decrypt(encrypted, secret_key)
    return {"decryptedText": decrypted_text}
    
@payments.route("/idfc_fund_transfer",methods=["POST"])
def idfc_fund_transfer():
    data_status={"responseStatus":0,"result":""}
    pem_file_path = os.path.join(app.config["SITE_ROOT"], "static/assets", "graampay.pem")

    private_key = open(pem_file_path, 'rb').read()
    try:
        url = "https://apiext.idfcfirstbank.com/authorization/oauth2/token"

        encrypted_jwt_string = jwt_encryption_data(private_key)

        # print(encrypted_jwt_string,"ENCRYPTED String JWT")

        grant_type = "client_credentials"
        scope = "paymenttxn-v1fundTransfer paymentenq-paymentTransactionStatus cbs-acctenq-accountBalance cbs-acctenq-accountStatement paymentenq-beneValidation"
        # scope = "generic-ovd-verify"
        client_id = "b7f93858-d33e-449a-9555-ff6ff428df3b"
        client_assertion = encrypted_jwt_string
        client_assertion_type= "urn:ietf:params:oauth:client-assertion-type:jwt-bearer"

        token_headers = {'Content-Type': 'application/x-www-form-urlencoded','Accept':'application/json'}
        token_payload = "grant_type="+grant_type+"&scope="+scope+"&client_id="+client_id+"&client_assertion_type="+client_assertion_type+"&client_assertion="+client_assertion
        
        # print(token_headers,"AUTH HEADERS")
        # print(token_payload,"AUTH PAYLOAD")

        token_response = requests.post(url, data=token_payload, headers=token_headers)
        tokenResponseJsonData = json.loads(token_response.text)
        # print(tokenResponseJsonData,"(((((((((((((tokenResponseJsonData)))))))))))))")

        authorizationToken = tokenResponseJsonData.get("access_token")
        # print(authorizationToken,"((((((((authorizationToken))))))))")


        secretKey = "77616d706c65496467134145536b659123616d706c65496468634145536b6614"
        # encryption_key = bytes(secretKey, 'utf-8')
        # Hash the secretKey to ensure it's a valid length (32 bytes for AES-256)
        # encryption_key = SHA256.new(secretKey.encode()).digest()
        responseEncrypt = idfc_encrypt(requestData,secretKey)

        # Retrieve the encrypted data and IV
        # combinedEncryption = responseEncrypt.get('combinedEncryption')
        print(responseEncrypt,"responseEncrypt")
        # encryption_data = responseEncrypt.get('encryption_data')
        # iv = responseEncrypt.get('iv')

        # Output the encrypted data and IV
        # print(encryption_data, "ENCRYPTION DATA")
        # print(iv, "IV")

        # encryptedRequest = {
        # "encrypted":encryption_data,
        # "iv":iv
        # }

        # print(encryptedRequest,"(((((((((((encryptedRequest For fundTransfer)))))))))))")

        ft_headers = {
        "content-type": "application/octet-stream",
        "source": "VYN",
        "correlationId":"IDFDSFDC145982101a0t0w0010000000001",
        # "kid": "be256eed-a767-456e-84b3-cb760e80b13b",
        "Authorization": "Bearer "+str(authorizationToken)
        }
        print(ft_headers,"(((((((((((((ft_headers)))))))))))))")

        ft_url = "https://apiext.payments.idfcfirstbank.com/paymenttxns/v1/fundTransfer"

        ftResponse = requests.post(ft_url,data=responseEncrypt, headers=ft_headers)
        print(ftResponse.text,"ftResponse.text")
        decrypted_text = idfc_decrypt(ftResponse.text, secretKey)
        print(decrypted_text,"(((((((((((((decrypted_text)))))))))))))")

        data_status["responseStatus"]=1
        data_status["result"]="Success"
        return data_status
    except Exception as e:
        app.logger.error(traceback.format_exc())
        data_status["result"]="Unable to payout transfer!!"
        return data_status

# @payments.route("/axis_fund_transfer_test",methods=["POST"])
# def axis_fund_transfer_test():
#     try:
#         client_id="d40ede73538b698c3036a629d09d20dd"
#         client_secret="e695741d8691c1441dcb826aeeece18c"
#         key="A855F0833E6F1B7E2AF7063E01D92F94"
#         channelId="VIYONA"
#         get_base_url="https://sakshamuat.axisbank.co.in/gateway/api/txb/v1"
#         txnPaymode="PA"
#         custUniqRef="GRM900266579"
#         beneCode="beneCode71197d3cbb884e1995b086484"
#         requestUUID="GRM900266574"
#         serviceRequestId="OpenAPI"
#         serviceRequestVersion="1.0"
#         corpCode="VIYONA505"
#         channelId="VIYONA"
#         amount="1"
#         beneficiaryPhone="1234567890"
#         beneficiaryMail="test@test.com"
#         responsedata = axis_payout_fundtransfer(
#             client_id,
#             client_secret,
#             key,
#             channelId,
#             get_base_url,
#             requestUUID,
#             serviceRequestId,
#             serviceRequestVersion,
#             corpCode,
#             amount,
#             beneficiaryName="",  # Make sure this is in the correct position
#             beneficiaryMail=beneficiaryMail,
#             beneficiaryPhone=beneficiaryPhone,
#             userId=''
#         )
#         return responsedata
#     except Exception as e:
#         app.logger.error(traceback.format_exc())


@payments.route("/idfc_benivalidation_test",methods=["POST"])
def idfc_benivalidation_test():
    data_status={"responseStatus":0,"result":""}
    try:
        requestBody ={
            "beneValidationReq": {
                "remitterName": "MERCHANT PAYOUTS-VIYONA ESCROW ACCOUNT",
                "remitterMobileNumber": "918452904575",
                "debtorAccountId": "10167310372",
                "accountType": "00",
                "creditorAccountId": "159502971443",
                "ifscCode": "INDB0000226",
                "paymentDescription": "",
                "transactionReferenceNumber": str(random_digit_generate(15)),
                "merchantCode": "4814",
                "identifier": "IMPS2.0"
            }
        }
        kid = "be256eed-a767-456e-84b3-cb760e80b13b"
        aud_url = "https://app.my.idfcfirstbank.com/platform/oauth/oauth2/token"
        client_id = "b7f93858-d33e-449a-9555-ff6ff428df3b"
        auth_url = "https://apiext.idfcfirstbank.com/authorization/oauth2/token"
        grant_type = "client_credentials"
        scope = "paymentenq-beneValidation"
        client_assertion_type = "urn:ietf:params:oauth:client-assertion-type:jwt-bearer"
        secretKey = "77616d706c65496467134145536b659123616d706c65496468634145536b6614"
        source = "VYN"

        client_assertion =jwt_encryption_token(kid,aud_url,client_id)
        authorizationToken=generate_auth_token(auth_url,grant_type,scope,client_id,client_assertion_type,client_assertion)
        responseEncrypt = idfc_encrypt(requestBody,secretKey)

        headers = {
            "content-type": "application/octet-stream",
            "source":source,
            "correlationId":str(random_digit_generate(15)),
            "Authorization": "Bearer "+str(authorizationToken)
        }

        url = "https://apiext.payments.idfcfirstbank.com/paymentenqs/v1/beneValidation"
        ftResponse = requests.post(url, data=responseEncrypt,headers=headers)
        print(ftResponse.text,"((((((((((RESPONSE TEXT))))))))))")

        decrypted_text = idfc_decrypt(ftResponse.text, secretKey)
        print(decrypted_text,"(((((((((((((decrypted_text)))))))))))))")
        jsondecryptResponse = json.loads(decrypted_text)
        print(jsondecryptResponse,"(((((((((((((jsondecryptResponse)))))))))))))")


        data_status["responseStatus"]=1
        data_status["result"]="Success"
        return data_status
    except Exception as e:
        app.logger.error(traceback.format_exc())
        data_status["result"]="Unable to validate benivalidation!!"
        return data_status


@payments.route("/axis_fund_transfer_test",methods=["POST"])
def axis_fund_transfer_test():
    # x=1 #transfer
    # x=2 #status
    x=3 #bene 
    # x=4 #bene enquiry
    # x=5 #corp account balanace
    responseData={}
    try:
        key="791308CAB9F3972CE7A7899875BDEDF8"

        client_id="0a9a9d489cd5a6cf05ba75589bcf7a98"
        client_secret="3ffc6a24c9cbbe3f834ada2e9e511556"
        channelId="VIYONA"
        get_base_url="https://saksham.axisbank.co.in/gateway/api"
        requestUUID=random_digit_generate(10)
        serviceRequestId="OpenAPI"
        serviceRequestVersion="1.0"
        corpCode="VIYONA505"
        if x == 1 :
            txnPaymode="PA"
            custUniqRef="910266557776564574564"
            beneCode="benecode00008"
            amount = "1"
            beneficiaryName="Sai Krishna"
            beneficiaryMail=""
            beneficiaryPhone=""
            corpAccNum="923020063494505"
            accountIFSCCode="INDB0000226"
            userId=""
            beneBankName="INDUSIND"
            beneAccNum="159502971443"
            responsedata = axis_payout_fundtransfer(
                client_id=client_id,
                client_secret=client_secret,
                key=key,
                channelId=channelId,
                get_base_url=get_base_url,
                requestUUID=requestUUID,
                serviceRequestId=serviceRequestId,
                serviceRequestVersion=serviceRequestVersion,
                corpCode=corpCode,
                txnPaymode=txnPaymode,
                amount=amount,
                beneCode=beneCode,
                beneAccNum=beneAccNum,
                accountIFSCCode=accountIFSCCode,
                custUniqRef=custUniqRef,
                corpAccNum=corpAccNum,
                beneficiaryName=beneficiaryName,  # Make sure this is in the correct position
                # beneficiaryMail=beneficiaryMail,
                # beneficiaryPhone=beneficiaryPhone,
                beneBankName=beneBankName,
                userId=userId
            )
        elif(x==2):
            crn=["91026656564574564"]
            responsedata = axis_payout_fundtransfer_check_status(
                client_id=client_id,
                client_secret=client_secret,
                key=key,
                channelId=channelId,
                get_base_url=get_base_url,
                requestUUID=requestUUID,
                serviceRequestId=serviceRequestId,
                serviceRequestVersion=serviceRequestVersion,
                corpCode=corpCode,
                crn=crn
            )
        elif x == 3 :
            userId="48574358435734"
            apiVersion = "1.0"
            beneLEI = ""
            beneCode = "Bene054985475745"
            beneName = "Tirupati Rao"
            beneAccNum = "31579270218"
            beneBankName="SBI"
            beneIfscCode="SBIN0006636"
            responsedata = axis_beneficiary_registration(
                client_id=client_id,
                client_secret=client_secret,
                get_api_key=key,
                channelId=channelId,
                get_base_url=get_base_url,
                requestUUID=requestUUID,
                serviceRequestId=serviceRequestId,
                serviceRequestVersion=serviceRequestVersion,
                corpCode=corpCode, #meters using existing variables
                userId=userId,
                apiVersion=apiVersion,
                beneCode=beneCode,
                beneName=beneName,
                beneAccNum=beneAccNum,
                beneIfscCode=beneIfscCode,
                beneBankName=beneBankName
                )
        elif x==4:
            fromDate=(datetime.datetime.now() - timedelta(days=5)).strftime("%Y-%m-%d")
            toDate=datetime.datetime.now().strftime("%Y-%m-%d")
            emailId="test@gmail.com"
            status="Active"
            beneCode=["Bene054948339"]
            responsedata = axis_beneficiary_enquiry(
                client_id=client_id,
                client_secret=client_secret,
                key=key,
                channelId=channelId,
                get_base_url=get_base_url,
                requestUUID=requestUUID,
                serviceRequestId=serviceRequestId,
                serviceRequestVersion=serviceRequestVersion,
                corpCode=corpCode, #meters using existing variables
                fromDate=fromDate,
                toDate=toDate,
                emailId=emailId,
                status=status,
                beneCode=beneCode,
                )
        elif(x==5):
            corpaccnum="923020063494505"
            responsedata = axis_payout_get_account_balance(
                client_id=client_id,
                client_secret=client_secret,
                key=key,
                channelId=channelId,
                get_base_url=get_base_url,
                requestUUID=requestUUID,
                serviceRequestId=serviceRequestId,
                serviceRequestVersion=serviceRequestVersion,
                corpCode=corpCode,
                corpaccnum=corpaccnum
            )
        return responsedata
    except Exception as e:
        app.logger.error(traceback.format_exc())


@payments.route("/axis_bulk_apis_test",methods=["POST"])
def axis_bulk_apis_test():
    # x=1 #transfer
    x=2 #bene 
    print("++++++++++++++++++++++++++++","axis_bulk")
    try:
        key="34F461562BFD91DF5AC2C567CFE154B6"
        client_id="514dd5e792716edefcb4872dbaf14ef7"
        client_secret="474acaf77a6caf8ca0cd20d2815bbfad"
        channelId="VIYONA"
        get_base_url="https://sakshamuat.axisbank.co.in/gateway/api"
        requestUUID="REF000006"
        serviceRequestId="OpenAPI"
        serviceRequestVersion="1.0"
        corpCode="DEMOCORP268"
        if x == 1 :
            corpAccNum="429010100102315"
            transfer = [
                {
                    "txnPaymode": "NE",
                    "amount": "1",
                    "beneCode": "BENE001",  
                    "beneficiaryName": "JohnDoe1",
                    "beneAccNum": "123456789012",  
                    "accountIFSCCode": "HDFC0000031",  
                    "custUniqRef": "98745663217426",
                    "beneficiaryMail": "",
                    "beneficiaryPhone": "",
                    "beneBankName": ""
                },
                {
                    "txnPaymode": "NE",
                    "amount": "2",
                    "beneCode": "BENE00",
                    "beneAccNum": "456782913456",
                    "accountIFSCCode": "ICIC0007359",
                    "custUniqRef": "98745663217425",
                    "beneficiaryName": "JohnDoe2",
                    "beneficiaryMail": "",
                    "beneficiaryPhone": "",
                    "beneBankName": ""
                },
                {
                    "txnPaymode": "NE",
                    "amount": "2",
                    "beneCode": "BENE00",
                    "beneAccNum": "456782913456",
                    "accountIFSCCode": "ICIC0007359",
                    "custUniqRef": "98745663217424",
                    "beneficiaryName": "JohnDoe2",
                    "beneficiaryMail": "",
                    "beneficiaryPhone": "",
                    "beneBankName": ""
                }
                # ,
                # {
                #     "txnPaymode": "PA",
                #     "amount": "5",
                #     "beneCode": "BENE003",
                #     "beneAccNum": "456789123456",
                #     "accountIFSCCode": "SBI000782901",
                #     "custUniqRef": "9102665725",
                #     "beneficiaryName": "Alice Johnson",
                #     "beneficiaryMail": "",
                #     "beneficiaryPhone": "",
                #     "beneBankName": ""
                # }
            ]


            responsedata = axis_payout_bulk_fundtransfer(
                client_id=client_id,
                client_secret=client_secret,
                key=key,
                channelId=channelId,
                get_base_url=get_base_url,
                requestUUID=requestUUID,
                serviceRequestId=serviceRequestId,
                serviceRequestVersion=serviceRequestVersion,
                corpCode=corpCode,
                corpAccNum=corpAccNum,
                payment_details_list=transfer
            )
        elif x == 2:
            userId = "userid123455"
            apiVersion = "1.0"
            beneLEI = ""

            # Define the beneficiaries list with minimal fields
            beneficiaries = [
                {   
                    "apiVersion":apiVersion,
                    "beneCode": "BENE001",
                    "beneName": "JohnDoe1",
                    "beneAccNum": "123456789012",
                    "beneIfscCode": "HDFC0000031"
                }
                ,
                {
                    "apiVersion":apiVersion,
                    "beneCode": "BENE00",
                    "beneName": "JohnDoe2",
                    "beneAccNum": "456782913456",
                    "beneIfscCode": "ICIC0007359"
                }
                ,
                {
                    "apiVersion":apiVersion,
                    "beneCode": "BENE003",
                    "beneName": "Alice Johnson",
                    "beneAccNum": "456789123456",
                    "beneIfscCode": "SBI007890"
                }
            ]


            responsedata = axis_bulk_beneficiary_registration(
                client_id=client_id,
                client_secret=client_secret,
                get_api_key=key,
                channelId=channelId,
                serviceRequestId=serviceRequestId,
                serviceRequestVersion=serviceRequestVersion,
                corpCode=corpCode,
                userId=userId,
                get_base_url=get_base_url,
                requestUUID=requestUUID,
                beneficiaries=beneficiaries
            )

        return responsedata
    except Exception as e:
        app.logger.error(traceback.format_exc())


@payments.route("/payu_refund_check",methods=["POST"])
def payu_refund_check():
    print(payu_refund_check,"payu_refund_check")
    url = "https://test.payu.in/merchant/postservice.php"
    payload = {
    "key": "JPM7Fg",
    "command": "cancel_refund_transaction",
    "var1": 403993715524996350,
    "var2": 123321,
    "var3": 500,
    "var5": "https://www.abc.com",
    "hash": "edddd48426cb7c098b97400926b7f860f388e79ce28240adbebda7e7a6426fc2d3acde0fec6cdc21b4bf65394df38bda95bba7fe661650c5ff027b8f967cd8fb"
    }
    headers = {
    "accept": "application/json",
    "content-type": "application/x-www-form-urlencoded"
    }
    response = requests.post(url, data=payload, headers=headers)
    print(response.text,"response.text")
    responsedata=json.loads(response.text)
    return responsedata
    

@payments.route("/commissionChargesCheck",methods=["POST"])
def commissionChargesCheck():
    amount=float(98145)
    paymentModeId="66d34d974daed53977acd698"
    subPaymentModeId="679b566ffb949f2a414c6d3c"
    patternId="67889d27ae14885628c6467f"
    commissionCharges=slab_calculation_for_payin_merchant(amount,paymentModeId,subPaymentModeId,patternId)
    print(commissionCharges,"commissionCharges")
    return commissionCharges
    
@payments.route("/insufficientcheck",methods=["POST"])
def insufficientcheck():
    try:
        sender_mail_query = SenderMails.objects(status=1,mailType="Payout_Insuficiant").first()
        if sender_mail_query:
            apiBalance=1000
            sapiName="idfc_payout"
            savilablebalance=formatINR("{:.2f}".format(float(apiBalance)))
            smobileNumber=','.join(sender_mail_query.mobileNumber)
            if smobileNumber!="":
                smsResponse=send_sms(smobileNumber,"Insufficient_Funds",userName=sapiName,Reason="",amount=savilablebalance)
            if sender_mail_query.mailsList:
                trnsactionDate = datetime.datetime.now().astimezone(ist_timezone).strftime("%d-%m-%Y %I:%M %p")
                mail_subject = "Insufficient fund transfer initiated from " + sapiName + "."
                recipients_list = sender_mail_query.mailsList
                template_name = "emails/insufficientfunds.html" 
                mail_data = {
                "amount":savilablebalance,
                "apiName":sapiName,
                "transactionDate":trnsactionDate
                }
                mailoutputData = send_asynchronous_email(mail_subject, recipients_list, template_name, mail_data)
    except Exception as e:
        app.logger.error(traceback.format_exc())
        pass
    return "success"