<?php
$_POST['username'] = 'Ac@demi@';
$_POST['password'] = 'Ac@d#mi#';
if(isset($_POST['username']) && $_POST['username'] == 'Ac@demi@' && isset($_POST['password']) && $_POST['password'] == 'Ac@d#mi#')
{
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
    require_once("./config/db_config.php");
    require_once("./config/QueryBuilder.php");
    $queryBuilder = new QueryBuilder($conn);

    $curretnDateTime = date('Y-m-d H:i:s');
    $fromDate = date('Y-m-d H:i:s', strtotime($curretnDateTime.' -2 days'));
    $toDate = date('Y-m-d H:i:s', strtotime($curretnDateTime.' -15 minutes'));

    $query = "UPDATE curl_executation SET current_executation = '0' WHERE date_time <= '".$toDate."'";
    $update = mysqli_query($conn, $query);

    $curlExecutationQuery = "SELECT * FROM `curl_executation` where current_executation = 1";
    $select = mysqli_query($conn, $curlExecutationQuery);
    $curlExecutationCount = mysqli_num_rows($select);
    if($curlExecutationCount == 0)
    {
        $curlExecutationQuery = "INSERT INTO `curl_executation` (`remote_ip`, `execute_status`,`current_executation`, `date_time`) VALUES ('".$_SERVER['REMOTE_ADDR']."', '1', '1','".$curretnDateTime."');";
        mysqli_query($conn, $curlExecutationQuery);
        $executationId = mysqli_insert_id($conn);

         ########  Get Order Details according to payment gateway  #######
        function fetchOrderInformation($activeGateway, $orderData)
        {
            if($activeGateway == 'flywire')
            {
                $flywireObj = new FlywirePaymentGateway();
                $orderInfo = $flywireObj->fetchOrder($orderData);
                $orderStatus = 'INVALID-ORDER';
                if(isset($orderInfo['status']))
                {
                    if($orderInfo['status'] == 'guaranteed')
                    {
                        $orderStatus = array("status" => 'TXN-SUCCESS');                           
                      
                    }
                    else
                    {
                        $orderStatus = $orderInfo['status'];
                    }
                }
                return $orderStatus;
            }
            elseif($activeGateway == 'network')
            {
                $networkObj = new NetworkGateway();
                $orderInfo = $networkObj->fetchOrder($orderData);
                $orderStatus = 'INVALID-ORDER';
                if(isset($orderInfo['success']) && $orderInfo['success'] && isset($orderInfo['reference']) && $orderInfo['reference'] == $orderData['txnId'])
                {
                    if($orderInfo['status'] == 'CAPTURED')
                    {
                        $orderStatus = array("status" => 'TXN-SUCCESS');  
                    }
                    else
                    {
                        $orderStatus = $orderInfo['status'];
                    }
                }
                return $orderStatus;
            }elseif($activeGateway == 'adityaBirla')
            {
                $adityaBirlaObj = new AdityaBirlaGateway();
                $orderInfo = $adityaBirlaObj->fetchOrder($orderData);
                $orderStatus = 'INVALID-ORDER';
                if(isset($orderInfo['order_data']['status']) && isset($orderInfo['order_data']['pl_order_id']))
                {
                    if($orderInfo['order_data']['status'] == 'SUCCESS')
                    {
                        $orderStatus = array("status" => 'TXN-SUCCESS');  
                    }
                    else
                    {
                        $orderStatus = $orderInfo['order_data']['status'];
                    }
                }
                return $orderStatus;
            }
            elseif($activeGateway == 'jncb')
            {
                $jncbObj = new JncbPaymentGateway();
                $orderInfo = $jncbObj->fetchOrder($orderData);
                $orderStatus = 'INVALID-ORDER';
                if(isset($orderInfo['IsoResponseCode']) && isset($orderInfo['OrderSummary']['OrderIdentifier']))
                {
                    if($orderInfo['IsoResponseCode'] == '00')
                    {
                        $orderStatus = array("status" => 'TXN-SUCCESS');  
                    }
                    else
                    {
                        $orderStatus = $orderInfo['ResponseMessage'];
                    }
                }
                return $orderStatus;
            }

            elseif($activeGateway == 'payDunya')
            {
                $payDunyaObj = new PayDunyaGateway();
                $orderInfo = $payDunyaObj->fetchOrder($orderData);
                $orderStatus = 'INVALID-ORDER';

                // if (isset($orderInfo['response_code']) && $orderInfo['response_code'] == '00' && $orderInfo['status'] && $orderInfo['invoice']['token'] == $txnId)
                if(isset($orderInfo['response_code']) && $orderInfo['response_code'] == '00')
                {
                    if($orderInfo['response_code'] == '00'  && $orderInfo['status'] == 'completed')
                    {
                        $orderStatus = array("status" => 'TXN-SUCCESS');  
                    }
                    else
                    {
                        $orderStatus = $orderInfo['status'];
                    }
                }
                return $orderStatus;
            }
            elseif($activeGateway == 'stripe')
            {
                $orderStatus = 'INVALID-ORDER';
                if (!empty($orderData['txnId'])) {              
                $stripeObj = new StripePaymentGateway();
                
                $orderInfo = $stripeObj->getOrderStatus($orderData);                
                if(isset($orderInfo['transactionStatus']) && $orderInfo['transactionStatus'] =='Success')
                {
                    $orderStatus = array("status" => 'TXN-SUCCESS');  
                }
                else
                {
                    $orderStatus = $orderInfo['error'];
                }
              }
              return $orderStatus;
            } elseif($activeGateway == 'mpgs')
            {                
                $orderStatus = 'INVALID-ORDER';
                if (!empty($orderData['txnId'])) {              
                $mpgsObj = new MPGSPaymentGateway();                
                $orderInfo = $mpgsObj->fetchOrder($orderData);                
                if(isset($orderInfo['status']) && $orderInfo['status'] =='SUCCESS' && $orderInfo['txnId'] == $orderData['txnId'])
                {
                    $orderStatus = array("status" => 'TXN-SUCCESS');  
                }
                else
                {
                    $orderStatus = $orderInfo['status'];
                }
              }
              return $orderStatus;
            }
            elseif($activeGateway == 'dpo')
            {                
                $orderStatus = 'INVALID-ORDER';
                if (!empty($orderData['txnId'])) {              
                $dpoObj = new DPOPaymentGateway();                
                $orderInfo = $dpoObj->fetchOrder($orderData);             
                if(isset($orderInfo['status']) && $orderInfo['status'] =='SUCCESS' && isset($orderInfo['AccRef']) && $orderInfo['AccRef'] == $orderData['txnId'])
                {
                    $orderStatus = array("status" => 'TXN-SUCCESS');  
                }
                else
                {
                    $orderStatus = $orderInfo['status'];
                }
              }
              return $orderStatus;
            }
            elseif($activeGateway == 'mips')
            {                
                $orderStatus = 'INVALID-ORDER';
                if (!empty($orderData['txnId'])) {              
                $mipsObj = new MIPSPaymentGateway();                
                $orderInfo = $mipsObj->fetchOrderByTxnId($orderData);                
                if(isset($orderInfo['id_order']) == $orderData['txnId']  && $orderInfo['order_status'] =='SUCCESS' && $orderInfo['status'] == 'SUCCESS')
                {
                    $orderStatus = array("status" => 'TXN-SUCCESS');  
                }
                else
                {
                    $orderStatus = $orderInfo['status'];
                }
              }
              return $orderStatus;
            }
            elseif($activeGateway == 'payu')
            {                
                $orderStatus = 'INVALID-ORDER';
                if (!empty($orderData['txnId'])) {              
                $payUObj = new PayUPaymentGateway();                
                $orderInfo = $payUObj->fetchOrder($orderData);                
                if(isset($orderInfo['payUReference']) == $orderData['txnId']  && $orderInfo['status'] =='SUCCESS' && $orderInfo['transactionState'] == 'SUCCESSFUL')
                {
                    $orderStatus = array("status" => 'TXN-SUCCESS');  
                }
                else
                {
                    $orderStatus = $orderInfo['status'];
                }
              }
              return $orderStatus;
            }
            elseif($activeGateway == 'paygate')
            {                
                $orderStatus = 'INVALID-ORDER';
                if (!empty($orderData['gatewayConfig'])) {  
                $paymentJson = json_decode($orderData['paymentRequest'], true);                
                $reference = $paymentJson['REFERENCE']??NULL;          
                $paygateObj = new PayGatePaymentGateway();                
                $orderInfo = $paygateObj->fetchOrder($orderData);                               
                if(isset($orderInfo['reference']) == $reference  && $orderInfo['Status'] =='success' && $orderInfo['txnStatus'] == 'Approved' &&  isset($orderInfo['txnID']) && !empty($orderInfo['txnID']))
                {
                    $orderStatus = array("status" => 'TXN-SUCCESS' , "txnId" => $orderInfo['txnID']);  
                }
                else
                {
                    $orderStatus = $orderInfo['txnStatus'];
                }
              }
              return $orderStatus;
            }
            elseif($activeGateway == 'authorizedotnet')
            {                
                $orderStatus = 'INVALID-ORDER';
                if (!empty($orderData['gatewayConfig'])) {  
                    $authorizeDotNetObj = new AuthorizeDotNetPaymentGateway();                
                    $orderInfo = $authorizeDotNetObj->fetchOrder($orderData);   
                                  
                    if(isset($orderInfo['status']) && $orderInfo['status'] == 'SUCCESS' && isset($orderInfo['txnId']) && !empty($orderInfo['txnId']))
                    {
                        $orderStatus = array("status" => 'TXN-SUCCESS', "txnId" => $orderInfo['txnId']);  
                    }
                    else
                    {
                        $orderStatus = isset($orderInfo['transactionStatus'])?$orderInfo['transactionStatus']: '';
                    }
                }
                return $orderStatus;
            }

            elseif($activeGateway == 'paytm')
            {
                $orderStatus = 'INVALID-ORDER';
                if (!empty($orderData['gatewayConfig'])) {  
                    $paytmObj = new PaytmPaymentGateway();                
                    $orderInfo = $paytmObj->fetchOrder($orderData);                                                  
                    if(isset($orderInfo['status']) && $orderInfo['status'] == 'SUCCESS' && isset($orderInfo['transactionState']) && $orderInfo['transactionState'] == 'TXN_SUCCESS'  && isset($orderInfo['orderId']) && $orderInfo['orderId'] == $orderData['txnId'])
                    {
                        $orderStatus = array("status" => 'TXN-SUCCESS');                       
                    }
                    else
                    {
                        $orderStatus = isset($orderInfo['transactionState'])?$orderInfo['transactionState']: '';                       
                    }
                }
                return $orderStatus;
            }
            elseif($activeGateway == 'icici')
            {
                $orderStatus = 'INVALID-ORDER';
                if (!empty($orderData['gatewayConfig'])) {  
                    $iciciObj = new ICICIPaymentGateway();                
                    $orderInfo = $iciciObj->fetchOrder($orderData);                                                  
                    if(isset($orderInfo['status']) && $orderInfo['status'] == 'Success' && isset($orderInfo['pgreferenceno']) && $orderInfo['pgreferenceno'] == $orderData['txnId'])
                    {
                        $orderStatus = array("status" => 'TXN-SUCCESS');                       
                    }
                    else
                    {
                        $orderStatus = isset($orderInfo['status'])?$orderInfo['status']: '';                       
                    }
                }
                return $orderStatus;
            }
            elseif($activeGateway == 'doku')
            {
                $orderStatus = 'INVALID-ORDER';
                if (!empty($orderData['gatewayConfig'])) {  
                    $dokuObj = new DokuPaymentGateway();                
                    $orderInfo = $dokuObj->fetchOrder($orderData);                                                  
                    if(isset($orderInfo['status']) && $orderInfo['status'] == 'SUCCESS' && isset($orderInfo['jsonResponse']) && $orderInfo['jsonResponse']['order']['invoice_number'] == $orderData['txnId'])
                    {
                        $orderStatus = array("status" => 'TXN-SUCCESS');                       
                    }
                    else
                    {
                        $orderStatus = isset($orderInfo['status'])?$orderInfo['status']: '';                       
                    }
                }
                return $orderStatus;
            }
            elseif($activeGateway == 'ccavenue')
            {
                $orderStatus = 'INVALID-ORDER';
                if (!empty($orderData['gatewayConfig'])) {  
                    $ccavenueObj = new CCAveneuePaymentGateway();                
                    $orderInfo = $ccavenueObj->fetchOrder($orderData);                                                                      
                    if(isset($orderInfo['status']) && $orderInfo['status'] == 'SUCCESS' && isset($orderInfo['txnId']) && $orderInfo['txnId'] == $orderData['txnId'])
                    {
                        $orderStatus = array("status" => 'TXN-SUCCESS');                       
                    }
                    else
                    {
                        $orderStatus = isset($orderInfo['status'])?$orderInfo['status']: '';                       
                    }
                }
                return $orderStatus;
            }

            else
            {
                return 'INACTIVE-PAYMENT';
            }
        }

        ###############  Re-check transaction Start ###################
        $paymentGateways = [
            'network' => "paymentGateway/network/NetworkPaymentGateway.php",
            'flywire' => "paymentGateway/flywire/flywirePaymentGateway.php",
            'adityaBirla' => "paymentGateway/adityabirla/paymentRequest.php",
            'jncb' => "paymentGateway/jncb/paymentRequest.php",
            'payDunya' => "paymentGateway/paydunya/paymentRequest.php",
            'stripe'=> "paymentGateway/stripe/stripePaymentGateway.php",
            'mpgs'=>"paymentGateway/mpgs/mpgsPaymentGateway.php",
            'dpo'=>"paymentGateway/dpo/dpoPaymentGateway.php",
            'mips'=>"paymentGateway/mips/mipsPaymentGateway.php",
            'payu'=>"paymentGateway/payu/payuPaymentGateway.php",
            'paygate'=>"paymentGateway/paygate/payGatePaymentGateway.php",
            'authorizedotnet'=>"paymentGateway/authorizedotnet/authorizeDotNetPaymentGateway.php",
            'paytm'=>"paymentGateway/paytm/paytmPaymentGateway.php",
            'icici'=>"paymentGateway/icici/iciciPaymentGateway.php",
            'doku'=>"paymentGateway/doku/dokuPaymentGateway.php",
            'ccavenue'=>'paymentGateway/ccavenue/ccavenuePaymentGateway.php'
        ];

        ############## Online Payment Verification #############

        $query = "SELECT * FROM `online_payment` where (paymentStatus != 'failure' and paymentStatus != 'FAILED') and (receiptStatus = '' or receiptStatus IS NULL) and paymentDate >= '".$fromDate."' and paymentDate <= '".$toDate."' and paymentOption != '' and paymentType != 'SendPaymentLink-PAYMENT' order by id desc";
        $select = mysqli_query($conn, $query);
        $result = array();
        if (mysqli_num_rows($select) > 0)
        {
            include("academia/onlinePayment.php");
            $onlinePaymentObj = new OnlinePayment();
            $onlinePaymentObj->setConnection($conn, $connMain);
            while ($data = mysqli_fetch_assoc($select))
            {
                $activeGateway = $data['paymentOption'];
                if (array_key_exists($activeGateway, $paymentGateways))
                {
                    require_once($paymentGateways[$activeGateway]);
                    $curlCheckCount = $data['curlCheckCount'];
                    $curlCheckCount++;
                    $txnId = $data['txnId'] ?? NULL;
                    $paymentId = $data['id'];
                    $currency = $data['currency'];
                    $paymentDate = date('d-m-Y', strtotime($data['paymentDate']));
                    $onlinePaymentId = $data['onlinePaymentId'];
                    $data['tableName'] = 'online_payment';
                    $data['conn'] = $conn;
                    $orderInfo= fetchOrderInformation($activeGateway, $data);                    
                    $orderStatus = $orderInfo['status']??$orderInfo;
                    if($txnId == NULL && isset($orderInfo['txnId']))
                    {
                        $query = "UPDATE online_payment  SET  txnId ='".$orderInfo['txnId']."' WHERE id=" . $paymentId;
                        $update = mysqli_query($conn, $query);
                        $txnId = $orderInfo['txnId'];
                    }
                    if($orderStatus == 'INACTIVE-PAYMENT' || $orderStatus == 'INVALID-ORDER')
                    {
                        echo "Invalid - ".$txnId."--".$orderStatus."<br>";
                    }
                    else
                    {
                        if($orderStatus == 'TXN-SUCCESS')
                        {
                            $responseReceipt = $onlinePaymentObj->createReceipt($onlinePaymentId, $txnId, $orderStatus);                           
                            if($responseReceipt == 'SUCCESS')
                            {
                                $reverse_receipt = "INSERT INTO `reverse_transaction_receipt` (`id`, `payment_type`, `transaction_id`, `date_time`, `action`) VALUES (NULL, 'Online Payment', '".$txnId."', '".date('Y-m-d H:i:s')."', '0') ";
                                $reverse_receipt_insert = mysqli_query($conn, $reverse_receipt);
                                $query = "UPDATE online_payment  SET  formSubmissionStatus = '1',paymentStatus='".$orderStatus."', receiptStatus='CRON-SUCCESS', curlCheckCount = '".$curlCheckCount."'  WHERE id=" . $paymentId;
                                $update = mysqli_query($conn, $query);
                                echo "Online Payment - Receipt Generate for TXN ID - ". $txnId."<br/>";
                            }
                            else if($responseReceipt == 'RECEIPT_ALREADY_GENERATE')
                            {
                                $query = "UPDATE online_payment SET formSubmissionStatus = '1',paymentStatus='".$orderStatus."', receiptStatus='Already-Generated', curlCheckCount = '".$curlCheckCount."'  WHERE id=". $paymentId;
                                $update = mysqli_query($conn, $query);
                                echo "Online Payment - Receipt Already Generate for TXN ID - ". $txnId."<br/>";
                            }
                            else
                            {
                                $query = "UPDATE online_payment SET formSubmissionStatus = '1',paymentStatus='".$orderStatus."', curlCheckCount = '".$curlCheckCount."' WHERE id=" . $paymentId;
                                $update = mysqli_query($conn, $query);
                                echo "Online Payment - Receipt Not Generate for TXN ID - ". $txnId."<br/>";
                            }
                        }
                        else
                        {
                            $query = "UPDATE online_payment SET formSubmissionStatus = '1',paymentStatus='".$orderStatus."', curlCheckCount = '".$curlCheckCount."' WHERE id=" . $paymentId;
                            $update = mysqli_query($conn, $query);
                            echo "Online Payment - Receipt Not Generate for TXN ID - ". $txnId." and order status - ".$orderStatus."<br/>";
                        }
                    }
                }
            }
        }
        else
        {
                echo "No data found for online payment";
                echo "<br>";
        }

        ############## Mobile Payment Verification #############

        $query = "SELECT * FROM `mobile_payment` where (paymentStatus != 'failure' and paymentStatus != 'FAILED') and (receiptStatus = '' or receiptStatus IS NULL) and paymentDate >= '".$fromDate."' and paymentDate <= '".$toDate."' and paymentOption != '' order by id desc";
        $select = mysqli_query($conn, $query);
        $result = array();
        if (mysqli_num_rows($select) > 0)
        {
            include("./academia/mobilePayment.php");
            $mobilePaymentObj = new MobilePayment();
            $mobilePaymentObj->setConnection($conn, $connMain);
            while ($data = mysqli_fetch_assoc($select))
            {
                $activeGateway = $data['paymentOption'];
                if (array_key_exists($activeGateway, $paymentGateways))
                {
                    require_once($paymentGateways[$activeGateway]);
                    $curlCheckCount = $data['curlCheckCount'];
                    $curlCheckCount++;
                    $paymentId = $data['id'];
                    $currency = $data['currency'];
                    $paymentDate = date('d-m-Y', strtotime($data['paymentDate']));
                    $onlinePaymentId = $data['onlinePaymentId'];
                    $txnId = $data['txnId'] ?? NULL;
                    $data['tableName'] = 'mobile_payment';
                    $data['conn'] = $conn;
                    $orderInfo= fetchOrderInformation($activeGateway, $data);
                    $orderStatus = $orderInfo['status']??$orderInfo;
                    if($txnId == NULL && isset($orderInfo['txnId']))
                    {
                        $query = "UPDATE mobile_payment  SET  txnId ='".$orderInfo['txnId']."' WHERE id=" . $paymentId;
                        $update = mysqli_query($conn, $query);
                        $txnId = $orderInfo['txnId'];
                    }
                    if($orderStatus == 'INACTIVE-PAYMENT' || $orderStatus == 'INVALID-ORDER')
                    {
                        echo "Invalid - ".$txnId."--".$orderStatus."<br>";
                    }
                    else
                    {
                        if($orderStatus == 'TXN-SUCCESS')
                        {
                            $responseReceipt = $mobilePaymentObj->createReceipt($onlinePaymentId, $txnId, $orderStatus);
                            if($responseReceipt == 'SUCCESS')
                            {
                                $reverse_receipt = "INSERT INTO `reverse_transaction_receipt` (`id`, `payment_type`, `transaction_id`, `date_time`, `action`) VALUES (NULL, 'Mobile Payment', '".$txnId."', '".date('Y-m-d H:i:s')."', '0') ";
                                $reverse_receipt_insert = mysqli_query($conn, $reverse_receipt);
                                $query = "UPDATE mobile_payment SET formSubmissionStatus = '1',paymentStatus='".$orderStatus."', receiptStatus='CRON-SUCCESS', curlCheckCount = '".$curlCheckCount."' WHERE id=" . $paymentId;
                                $update = mysqli_query($conn, $query);
                                echo "Mobile Payment - Receipt Generate for TXN ID - ". $txnId."<br/>";
                            }
                            else if($responseReceipt == 'RECEIPT_ALREADY_GENERATE')
                            {
                                $query = "UPDATE mobile_payment SET formSubmissionStatus = '1',paymentStatus='".$orderStatus."', receiptStatus='Already-Generated', curlCheckCount = '".$curlCheckCount."' WHERE id=" . $paymentId;
                                $update = mysqli_query($conn, $query);
                                echo "Mobile Payment - Receipt Already Generate for TXN ID - ". $txnId."<br/>";
                            }
                            else
                            {
                                $query = "UPDATE mobile_payment SET formSubmissionStatus = '1',paymentStatus='".$orderStatus."', curlCheckCount = '".$curlCheckCount."' WHERE id=" . $paymentId;
                                $update = mysqli_query($conn, $query);
                                echo "Mobile Payment - Receipt Not Generate for TXN ID - ". $txnId."--".$responseReceipt."<br/>";
                            }
                        }
                        else
                        {
                            $query = "UPDATE mobile_payment SET formSubmissionStatus = '1',paymentStatus='".$orderStatus."', curlCheckCount = '".$curlCheckCount."' WHERE id=" . $paymentId;
                            $update = mysqli_query($conn, $query);
                            echo "Mobile Payment - Receipt Not Generate for TXN ID - ". $txnId." and order status - ".$orderStatus."<br/>";
                        }
                    }
                }
            }
        }
        else
        {
            echo "No data found for Mobile payment";
            echo "<br>";
        }

        ############## Declaration Payment Verification #############
        $query = "SELECT * FROM `declaration_payment` where (paymentStatus != 'failure' and paymentStatus != 'FAILED') and (receiptStatus = '' or receiptStatus IS NULL) and paymentDate >= '".$fromDate."' and paymentDate <= '".$toDate."' and paymentOption != '' order by id desc";
        $select = mysqli_query($conn, $query);
        $result = array();

        if (mysqli_num_rows($select) > 0)
        {
            include("academia/declarationPayment.php");
            $declarationPaymentObj = new DeclarationPayment();
            $declarationPaymentObj->setConnection($conn, $connMain);
            while ($data = mysqli_fetch_assoc($select))
            {
                $activeGateway = $data['paymentOption'];
                if (array_key_exists($activeGateway, $paymentGateways))
                {
                    require_once($paymentGateways[$activeGateway]);
                    $curlCheckCount = $data['curlCheckCount'];
                    $curlCheckCount++;
                    $paymentId = $data['id'];
                    $currency = $data['currency'];
                    $paymentDate = date('d-m-Y', strtotime($data['paymentDate']));
                    $txnId = $data['txnId'] ?? NULL;
                    $onlinePaymentId = $data['onlinePaymentId'];
                    $data['tableName'] = 'declaration_payment';
                    $data['conn'] = $conn;
                    $orderInfo= fetchOrderInformation($activeGateway, $data);
                    $orderStatus = $orderInfo['status']??$orderInfo;
                    if($txnId == NULL && isset($orderInfo['txnId']))
                    {
                        $query = "UPDATE declaration_payment  SET  txnId ='".$orderInfo['txnId']."' WHERE id=" . $paymentId;
                        $update = mysqli_query($conn, $query);
                        $txnId = $orderInfo['txnId'];
                    }
                    if($orderStatus == 'INACTIVE-PAYMENT' || $orderStatus == 'INVALID-ORDER')
                    {
                        echo "Invalid - ".$txnId."--".$orderStatus."<br>";
                    }
                    else
                    {
                        if($orderStatus == 'TXN-SUCCESS')
                        {
                            $responseReceipt = $declarationPaymentObj->createReceipt($data);
                            if(isset($responseReceipt['status']) && $responseReceipt['status'] == 'SUCCESS' && $responseReceipt['receiptId'])
                            {
                                $reverse_receipt = "INSERT INTO `reverse_transaction_receipt` (`id`, `payment_type`, `transaction_id`, `date_time`, `action`) VALUES (NULL, 'Declaration Payment', '".$txnId."', '".date('Y-m-d H:i:s')."', '0') ";
                                $reverse_receipt_insert = mysqli_query($conn, $reverse_receipt);
                                $query = "UPDATE declaration_payment SET formSubmissionStatus = '1',paymentStatus='".$orderStatus."',receiptStatus='CRON-SUCCESS', receiptId= '".$responseReceipt['receiptId']."' WHERE id=" . $paymentId;
                                $update = mysqli_query($conn, $query);
                                echo "Declaration Payment - Receipt Generate for TXN ID - ". $txnId."<br/>";
                            }
                            else if($responseReceipt == 'RECEIPT_ALREADY_GENERATE')
                            {
                                $query = "UPDATE declaration_payment SET formSubmissionStatus = '1',paymentStatus='".$orderStatus."', receiptStatus='Already-Generated', curlCheckCount = '".$curlCheckCount."' WHERE id=" . $paymentId;
                                $update = mysqli_query($conn, $query);
                                echo "Declaration Payment - Receipt Already Generate for TXN ID - ". $txnId."<br/>";
                            }
                            else
                            {
                                $query = "UPDATE declaration_payment SET formSubmissionStatus = '1',paymentStatus='".$orderStatus."', curlCheckCount = '".$curlCheckCount."' WHERE id=" . $paymentId;
                                $update = mysqli_query($conn, $query);
                                echo "Declaration Payment - Receipt Not Generate for TXN ID - ". $txnId."<br/>";
                            }
                        }
                        else
                        {
                            $query = "UPDATE declaration_payment SET formSubmissionStatus = '1',paymentStatus='".$orderStatus."', curlCheckCount = '".$curlCheckCount."' WHERE id=" . $paymentId;
                            $update = mysqli_query($conn, $query);
                            echo "Declaration Payment - Receipt Not Generate for TXN ID - ". $txnId." and order status - ".$orderStatus."<br/>";
                        }
                    }
                }
            }
        }
        else
        {
            echo "No data found for Declaration Payment "."</br>";
        }

        ############## Course Enrollement From Core ERP #############
        $query = "SELECT * FROM `enrollment_payment` where (paymentStatus != 'failure' and paymentStatus != 'FAILED') and (receiptStatus = '' or receiptStatus IS NULL) and paymentDate >= '".$fromDate."' and paymentDate <= '".$toDate."' and paymentOption != '' order by id desc";

        $select = mysqli_query($conn, $query);
        $result = array();

        if (mysqli_num_rows($select) > 0)
        {
            include("academia/courseEnrollement.php");
            $courseEnrollPaymentObj = new CourseEnrollement();
            $courseEnrollPaymentObj->setConnection($conn, $connMain);
            while ($data = mysqli_fetch_assoc($select))
            {
                $activeGateway = $data['paymentOption'];
                if (array_key_exists($activeGateway, $paymentGateways))
                {
                    require_once($paymentGateways[$activeGateway]);
                    $curlCheckCount = $data['curlCheckCount'];
                    $curlCheckCount++;
                    $paymentId = $data['id'];
                    $paymentDate = date('d-m-Y', strtotime($data['paymentDate']));
                    $onlinePaymentId = $data['onlinePaymentId'];
                    $currency = $data['currency'];
                    $txnId = $data['txnId'] ?? NULL;
                    $data['tableName'] = 'enrollment_payment';
                    $data['conn'] = $conn;
                    $orderInfo= fetchOrderInformation($activeGateway, $data);
                    $orderStatus = $orderInfo['status']??$orderInfo;
                    if($txnId == NULL && isset($orderInfo['txnId']))
                    {
                        $query = "UPDATE enrollment_payment  SET  txnId ='".$orderInfo['txnId']."' WHERE id=" . $paymentId;
                        $update = mysqli_query($conn, $query);
                        $txnId = $orderInfo['txnId'];
                    }
                    if($orderStatus == 'INACTIVE-PAYMENT' || $orderStatus == 'INVALID-ORDER')
                    {
                        echo "Invalid - ".$txnId."--".$orderStatus."<br>";
                    }
                    else
                    {
                        $query = "UPDATE enrollment_payment SET paymentStatus='" . $orderStatus . "',curlCheckCount = '".$curlCheckCount."'  WHERE id=" . $paymentId;
                        $update = mysqli_query($conn, $query);
                        if($orderStatus == 'TXN-SUCCESS')
                        {
                            $responseReceipt = $courseEnrollPaymentObj->createReceipt($onlinePaymentId, $txnId, $orderStatus);
                            if($responseReceipt == 'SUCCESS')
                            {
                                $reverse_receipt = "INSERT INTO `reverse_transaction_receipt` (`id`, `payment_type`, `transaction_id`, `date_time`, `action`) VALUES (NULL, 'Course Enrollment Payment', '".$txnId."', '".date('Y-m-d H:i:s')."', '0') ";
                                $reverse_receipt_insert = mysqli_query($conn, $reverse_receipt);
                                $query = "UPDATE enrollment_payment SET formSubmissionStatus = '1',receiptStatus='CRON-SUCCESS' WHERE id=" . $paymentId;
                                $update = mysqli_query($conn, $query);
                                echo "Course Enroll Payment - Receipt Generate for TXN ID - ". $txnId."<br/>";
                            }
                            else if($responseReceipt == 'RECEIPT_ALREADY_GENERATE')
                            {
                                $query = "UPDATE enrollment_payment SET formSubmissionStatus = '1',receiptStatus='Already-Generated'  WHERE id=" . $paymentId;
                                $update = mysqli_query($conn, $query);
                                echo "Course Enroll - Receipt Already Generate for TXN ID - ". $txnId."<br/>";
                            }
                            else
                            {
                                echo "Course Enroll - Receipt Not Generate for TXN ID - ". $txnId."<br/>";
                            }
                        }
                        else
                        {
                            echo "Course Enroll - Receipt Not Generate for TXN ID - ". $txnId." and order status - ".$orderStatus."<br/>";
                        }
                    }
                }
            }
        }
        else
        {
            echo "No data found for Course Enrollement Payment "."</br>";
        }

        ############## Examination #############
        $query = "SELECT * FROM `assessment_payment` where (paymentStatus != 'failure' and paymentStatus != 'FAILED') and (receiptStatus = '' or receiptStatus IS NULL) and paymentDate >= '".$fromDate."' and paymentDate <= '".$toDate."' and paymentOption != '' order by id desc";

        $select = mysqli_query($conn, $query);
        $result = array();

        if (mysqli_num_rows($select) > 0)
        {
            include("academia/examinationPayment.php");
            $examinationPaymentObj = new ExaminationPayment();
            $examinationPaymentObj->setConnection($conn, $connMain);

            while ($data = mysqli_fetch_assoc($select))
            {
                $activeGateway = $data['paymentOption'];
                if (array_key_exists($activeGateway, $paymentGateways))
                {
                    require_once($paymentGateways[$activeGateway]);
                    $curlCheckCount = $data['curlCheckCount'];
                    $curlCheckCount++;
                    $paymentId = $data['id'];
                    $amount = $data['amount'];
                    $currency = $data['currency'];
                    $academyLocationId = $data['academyLocationId'];
                    $paymentDate = date('d-m-Y', strtotime($data['paymentDate']));
                    // $onlinePaymentId = $data['onlinePaymentId'];
                    $txnId = $data['txnId'] ?? NULL;
                    $data['tableName'] = 'assessment_payment';
                    $data['conn'] = $conn;
                    $orderInfo= fetchOrderInformation($activeGateway, $data);
                    $orderStatus = $orderInfo['status']??$orderInfo;
                    if($txnId == NULL && isset($orderInfo['txnId']))
                    {
                        $query = "UPDATE assessment_payment  SET  txnId ='".$orderInfo['txnId']."' WHERE id=" . $paymentId;
                        $update = mysqli_query($conn, $query);
                        $txnId = $orderInfo['txnId'];
                    }
                    if($orderStatus == 'INACTIVE-PAYMENT' || $orderStatus == 'INVALID-ORDER')
                    {
                        echo "Invalid - ".$txnId."--".$orderStatus."<br>";
                    }
                    else
                    {
                        $query = "UPDATE assessment_payment SET formSubmissionStatus = '1',paymentStatus='" . $orderStatus . "',curlCheckCount = '".$curlCheckCount."'  WHERE id=" . $paymentId;
                        $update = mysqli_query($conn, $query);
                        if($orderStatus == 'TXN-SUCCESS')
                        {
                            $responseReceipt = $examinationPaymentObj->createReceipt($paymentId , $academyLocationId , $txnId, $amount);
                            if(isset($responseReceipt['status']) && $responseReceipt['status'] == 'SUCCESS')
                            {
                                $reverse_receipt = "INSERT INTO `reverse_transaction_receipt` (`id`, `payment_type`, `transaction_id`, `date_time`, `action`) VALUES (NULL, 'Examination Payment', '".$txnId."', '".date('Y-m-d H:i:s')."', '0') ";
                                $reverse_receipt_insert = mysqli_query($conn, $reverse_receipt);

                                $query = "UPDATE assessment_payment_detail SET  receiptId='" . $responseReceipt['receiptId'] . "' WHERE examAssessmentPaymentId=" . $paymentId;
                                $update = mysqli_query($conn, $query);

                                $query = "UPDATE assessment_payment SET formSubmissionStatus = '1',receiptStatus='CRON-SUCCESS'  WHERE id=" . $paymentId;
                                $update = mysqli_query($conn, $query);

                                echo "Examination Payment - Receipt Generate for TXN ID - ". $txnId."<br/>";
                            }
                            else if($responseReceipt == 'RECEIPT_ALREADY_GENERATE')
                            {
                                $query = "UPDATE assessment_payment SET formSubmissionStatus = '1',receiptStatus='Already-Generated'  WHERE id=" . $paymentId;
                                $update = mysqli_query($conn, $query);
                                echo "Examination - Receipt Already Generate for TXN ID - ". $txnId."<br/>";
                            }
                            else
                            {
                                echo "Examination - Receipt Not Generate for TXN ID - ". $txnId."<br/>";
                            }
                        }
                        else
                        {
                            echo "Examination - Receipt Not Generate for TXN ID - ". $txnId." and order status - ".$orderStatus."<br/>";
                        }
                    }
                }
            }
        }
        else
        {
            echo "No data found for Examination Payment "."</br>";
        }




        ############## Revaluation #############
        $query = "SELECT * FROM `reval_payment` where (paymentStatus != 'failure' and paymentStatus != 'FAILED') and (receiptStatus = '' or receiptStatus IS NULL) and paymentDate >= '".$fromDate."' and paymentDate <= '".$toDate."' and paymentOption != '' order by id desc";

        $select = mysqli_query($conn, $query);
        $result = array();

        if (mysqli_num_rows($select) > 0)
        {
            include("academia/revaluationPayment.php");
            $revalPaymentObj = new RevaluationPayment();
            $revalPaymentObj->setConnection($conn, $connMain);
            while ($data = mysqli_fetch_assoc($select))
            {
                $activeGateway = $data['paymentOption'];
                if (array_key_exists($activeGateway, $paymentGateways))
                {
                    require_once($paymentGateways[$activeGateway]);
                    $curlCheckCount = $data['curlCheckCount'];
                    $curlCheckCount++;
                    $paymentId = $data['id'];
                    $amount = $data['amount'];
                    $currency = $data['currency'];
                    $paymentDate = date('d-m-Y', strtotime($data['paymentDate']));
                    // $onlinePaymentId = $data['onlinePaymentId'];
                    $txnId = $data['txnId'] ?? NULL;
                    $data['tableName'] = 'reval_payment';
                    $data['conn'] = $conn;
                    $orderInfo= fetchOrderInformation($activeGateway, $data);
                    $orderStatus = $orderInfo['status']??$orderInfo;
                    if($txnId == NULL && isset($orderInfo['txnId']))
                    {
                        $query = "UPDATE reval_payment  SET  txnId ='".$orderInfo['txnId']."' WHERE id=" . $paymentId;
                        $update = mysqli_query($conn, $query);
                        $txnId = $orderInfo['txnId'];
                    }
                    if($orderStatus == 'INACTIVE-PAYMENT' || $orderStatus == 'INVALID-ORDER')
                    {
                        echo "Invalid - ".$txnId."--".$orderStatus."<br>";
                    }
                    else
                    {
                        $query = "UPDATE reval_payment SET formSubmissionStatus = '1',paymentStatus='" . $orderStatus . "',curlCheckCount = '".$curlCheckCount."'  WHERE id=" . $paymentId;
                        $update = mysqli_query($conn, $query);
                        if($orderStatus == 'TXN-SUCCESS')
                        {
                            $responseReceipt = $revalPaymentObj->createReceipt($paymentId,$amount, $txnId, $orderStatus);
                            if(isset($responseReceipt['status']) && $responseReceipt['status'] == 'SUCCESS')
                            {
                                $reverse_receipt = "INSERT INTO `reverse_transaction_receipt` (`id`, `payment_type`, `transaction_id`, `date_time`, `action`) VALUES (NULL, 'Reval Payment', '".$txnId."', '".date('Y-m-d H:i:s')."', '0') ";
                                $reverse_receipt_insert = mysqli_query($conn, $reverse_receipt);

                                $query = "UPDATE reval_payment SET receiptStatus='CRON-SUCCESS' WHERE id=" . $paymentId;
                                $update = mysqli_query($conn, $query);
                                echo "Revaluation Payment - Receipt Generate for TXN ID - ". $txnId."<br/>";
                            }
                            else if($responseReceipt == 'RECEIPT_ALREADY_GENERATE')
                            {
                                $query = "UPDATE reval_payment SET receiptStatus='Already-Generated'  WHERE id=" . $paymentId;
                                $update = mysqli_query($conn, $query);
                                echo "Revaluation - Receipt Already Generate for TXN ID - ". $txnId."<br/>";
                            }
                            else
                            {
                                echo "Revaluation - Receipt Not Generate for TXN ID - ". $txnId."<br/>";
                            }
                        }
                        else
                        {
                            echo "Revaluation - Receipt Not Generate for TXN ID - ". $txnId." and order status - ".$orderStatus."<br/>";
                        }
                    }

                }
            }
        }
        else
        {
            echo "No data found for Revaluation Payment "."</br>";
        }

         ############## SendPaymentLink Payment Verification #############

         $query = "SELECT * FROM `online_payment` where (paymentStatus != 'failure' and paymentStatus != 'FAILED') and (receiptStatus = '' or receiptStatus IS NULL) and paymentDate >= '".$fromDate."' and paymentDate <= '".$toDate."' and paymentOption != '' and paymentType = 'SendPaymentLink-PAYMENT' order by id desc";
         $select = mysqli_query($conn, $query);
         $result = array();
         if (mysqli_num_rows($select) > 0)
         {
             include("academia/sendPaymentLinkPayment.php");
             $sendPaymentLinkObj = new SendPaymentLinkPayment();
             $sendPaymentLinkObj->setConnection($conn, $connMain);
             while ($data = mysqli_fetch_assoc($select))
             {
                 $activeGateway = $data['paymentOption'];
                 if (array_key_exists($activeGateway, $paymentGateways))
                 {
                     require_once($paymentGateways[$activeGateway]);
                     $curlCheckCount = $data['curlCheckCount'];
                     $curlCheckCount++;
                     $txnId = $data['txnId'] ?? NULL;
                     $paymentId = $data['id'];
                     $currency = $data['currency'];
                     $paymentDate = date('d-m-Y', strtotime($data['paymentDate']));
                     $sendPaymentLinkId = $data['sendPaymentLinkId'];
                     $onlinePaymentId = $data['onlinePaymentId'];
                     $data['tableName'] = 'online_payment';
                     $data['conn'] = $conn;
                     $orderInfo= fetchOrderInformation($activeGateway, $data);
                     $orderStatus = $orderInfo['status']??$orderInfo;
                    if($txnId == NULL && isset($orderInfo['txnId']))
                    {
                        $query = "UPDATE online_payment  SET  txnId ='".$orderInfo['txnId']."' WHERE id=" . $paymentId ." AND sendPaymentLinkId = ".$sendPaymentLinkId;
                        $update = mysqli_query($conn, $query);
                        $txnId = $orderInfo['txnId'];
                    }
                     if($orderStatus == 'INACTIVE-PAYMENT' || $orderStatus == 'INVALID-ORDER')
                     {
                         echo "Invalid - ".$txnId."--".$orderStatus."<br>";
                     }
                     else
                     {
                         if($orderStatus == 'TXN-SUCCESS')
                         {
                             $responseReceipt = $sendPaymentLinkObj->createReceipt($sendPaymentLinkId,$onlinePaymentId, $txnId, $orderStatus);

                             if($responseReceipt == 'SUCCESS')
                             {
                                 $reverse_receipt = "INSERT INTO `reverse_transaction_receipt` (`id`, `payment_type`, `transaction_id`, `date_time`, `action`) VALUES (NULL, 'Online Payment', '".$txnId."', '".date('Y-m-d H:i:s')."', '0') ";
                                 $reverse_receipt_insert = mysqli_query($conn, $reverse_receipt);
                                 $query = "UPDATE online_payment  SET formSubmissionStatus = '1',paymentStatus='".$orderStatus."', receiptStatus='CRON-SUCCESS', curlCheckCount = '".$curlCheckCount."'  WHERE id=" . $paymentId;
                                 $update = mysqli_query($conn, $query);
                                 echo "Send Payment Link Payment - Receipt Generate for TXN ID - ". $txnId."<br/>";
                             }
                             else if($responseReceipt == 'RECEIPT_ALREADY_GENERATE')
                             {
                                 $query = "UPDATE online_payment SET formSubmissionStatus = '1',paymentStatus='".$orderStatus."', receiptStatus='Already-Generated', curlCheckCount = '".$curlCheckCount."'  WHERE id=". $paymentId;
                                 $update = mysqli_query($conn, $query);
                                 echo "Send Payment Link Payment - Receipt Already Generate for TXN ID - ". $txnId."<br/>";
                             }
                             else
                             {
                                 $query = "UPDATE online_payment SET formSubmissionStatus = '1',paymentStatus='".$orderStatus."', curlCheckCount = '".$curlCheckCount."' WHERE id=" . $paymentId;
                                 $update = mysqli_query($conn, $query);
                                 echo "Send Payment Link Payment - Receipt Not Generate for TXN ID - ". $txnId."<br/>";
                             }
                         }
                         else
                         {
                             $query = "UPDATE online_payment SET formSubmissionStatus = '1',paymentStatus='".$orderStatus."', curlCheckCount = '".$curlCheckCount."' WHERE id=" . $paymentId;
                             $update = mysqli_query($conn, $query);
                             echo "Send Payment Link Payment - Receipt Not Generate for TXN ID - ". $txnId." and order status - ".$orderStatus."<br/>";
                         }
                     }
                 }
             }
         }
         else
         {
                 echo "No data found for SendPaymentLink payment";
                 echo "<br>";
         }



         ####### Advance Payment ########################
        $query = "SELECT * FROM `advance_payment` where (paymentStatus != 'failure' and paymentStatus != 'FAILED') and (receiptStatus = '' or receiptStatus IS NULL) and paymentDate >= '".$fromDate."' and paymentDate <= '".$toDate."' and paymentOption != '' order by id desc";
        $select = mysqli_query($conn, $query);
        $result = array();
        if (mysqli_num_rows($select) > 0)
        {
            include("academia/advancePayment.php");
            $advancePaymentObj = new AdvancePayment();
            $advancePaymentObj->setConnection($conn, $connMain);
            while ($data = mysqli_fetch_assoc($select))
            {
                $activeGateway = $data['paymentOption'];
                if (array_key_exists($activeGateway, $paymentGateways))
                {
                    require_once($paymentGateways[$activeGateway]);
                    $curlCheckCount = $data['curlCheckCount'];
                    $curlCheckCount++;
                    $txnId = $data['txnId'] ?? NULL;
                    $paymentId = $data['id'];
                    $currency = $data['currency'];
                    $paymentDate = date('d-m-Y', strtotime($data['paymentDate']));
                    $onlinePaymentId = $data['onlinePaymentId'];
                    $data['conn'] = $conn;
                    $data['tableName'] = 'advance_payment';
                    $orderInfo= fetchOrderInformation($activeGateway, $data);
                    $orderStatus = $orderInfo['status']??$orderInfo;
                    if($txnId == NULL && isset($orderInfo['txnId']))
                    {
                        $query = "UPDATE advance_payment  SET  txnId ='".$orderInfo['txnId']."' WHERE id=" . $paymentId;
                        $update = mysqli_query($conn, $query);
                        $txnId = $orderInfo['txnId'];
                    }
                    if($orderStatus == 'INACTIVE-PAYMENT' || $orderStatus == 'INVALID-ORDER')
                    {
                        echo "Invalid - ".$txnId."--".$orderStatus."<br>";
                    }
                    else
                    {
                        if($orderStatus == 'TXN-SUCCESS')
                        {
                            $responseReceipt = $advancePaymentObj->createReceipt($data);
                            if(isset($responseReceipt['status']) && $responseReceipt['status'] == 'SUCCESS' && isset($responseReceipt['receiptId']))
                            {
                                $reverse_receipt = "INSERT INTO `reverse_transaction_receipt` (`id`, `payment_type`, `transaction_id`, `date_time`, `action`) VALUES (NULL, 'Advance Payment', '".$txnId."', '".date('Y-m-d H:i:s')."', '0') ";
								$reverse_receipt_insert = mysqli_query($conn, $reverse_receipt);
								$query = "UPDATE advance_payment SET formSubmissionStatus = '1',paymentStatus='".$orderStatus."',receiptStatus='CRON-SUCCESS', receiptId= '".$responseReceipt['receiptId']."' WHERE id=" . $paymentId;
								$update = mysqli_query($conn, $query);
								echo "Advance Payment - Receipt Generate for TXN ID - ". $txnId."<br/>";
                            }
                            else if($responseReceipt == 'RECEIPT_ALREADY_GENERATE')
                            {
                                $query = "UPDATE advance_payment SET formSubmissionStatus = '1',formSubmissionStatus = '1',paymentStatus='".$orderStatus."', receiptStatus='Already-Generate', curlCheckCount = '".$curlCheckCount."' WHERE id=" . $paymentId;
								$update = mysqli_query($conn, $query);
								echo "Advance Payment - Receipt Already Generate for TXN ID - ". $txnId."<br/>";
                            }
                            else
                            {
                                $query = "UPDATE advance_payment SET formSubmissionStatus = '1',paymentStatus='".$orderStatus."', curlCheckCount = '".$curlCheckCount."' WHERE id=" . $paymentId;
								$update = mysqli_query($conn, $query);
								echo "Advance Payment - Receipt Not Generate for TXN ID - ". $txnId."<br/>";
                            }
                        }
                        else
                        {
                            $query = "UPDATE advance_payment SET formSubmissionStatus = '1',paymentStatus='".$orderStatus."', curlCheckCount = '".$curlCheckCount."' WHERE id=" . $paymentId;
                            $update = mysqli_query($conn, $query);
                            echo "Advance Payment - Receipt Not Generate for TXN ID - ". $txnId." and order status - ".$orderStatus."<br/>";
                        }
                    }
                }
            }
        }
        else
        {
                echo "No data found for Advance payment";
                echo "<br>";
        }

          ### Course Registration Pending Payment ###
          $tableExistsQuery = "SHOW TABLES LIKE 'course_registration_pending_payment'";
          $result = mysqli_query($conn, $tableExistsQuery);
          
          if (mysqli_num_rows($result) > 0) {
              $query = "SELECT * FROM `course_registration_pending_payment` where (paymentStatus != 'failure' and paymentStatus != 'FAILED') and (receiptStatus = '' or receiptStatus IS NULL) and paymentDate >= '".$fromDate."' and paymentDate <= '".$toDate."' and paymentOption != '' order by id desc";
              $select = mysqli_query($conn, $query);
              $result = array();
              if (mysqli_num_rows($select) > 0)
              {
                  include("academia/courseRegistrationPendingPayment.php");
                  $pendingPaymentObj = new CourseRegistrationPendingPayment();
                  $pendingPaymentObj->setConnection($conn, $connMain);
                  while ($data = mysqli_fetch_assoc($select))
                  {
                      $activeGateway = $data['paymentOption'];
                      if (array_key_exists($activeGateway, $paymentGateways))
                      {
                          require_once($paymentGateways[$activeGateway]);
                          $curlCheckCount = $data['curlCheckCount'];
                          $curlCheckCount++;
                          $txnId = $data['txnId'] ?? NULL;
                          $paymentId = $data['id'];
                          $currency = $data['currency'];
                          $paymentDate = date('d-m-Y', strtotime($data['paymentDate']));
                          $onlinePaymentIds = $data['onlinePaymentIds'];
                          $data['conn'] = $conn;
                          $data['tableName'] = 'course_registration_pending_payment';
                          $orderInfo= fetchOrderInformation($activeGateway, $data);
                          $orderStatus = $orderInfo['status']??$orderInfo;
                            if($txnId == NULL && isset($orderInfo['txnId']))
                            {
                                $query = "UPDATE course_registration_pending_payment  SET  txnId ='".$orderInfo['txnId']."' WHERE id=" . $paymentId;
                                $update = mysqli_query($conn, $query);
                                $txnId = $orderInfo['txnId'];
                            }
                          if($orderStatus == 'INACTIVE-PAYMENT' || $orderStatus == 'INVALID-ORDER')
                          {
                              echo "Invalid - ".$txnId."--".$orderStatus."<br>";
                          }
                          else
                          {
                              if($orderStatus == 'TXN-SUCCESS')
                              {
                                  $responseReceipt = $pendingPaymentObj->createReceipt($data);
                                  if(isset($responseReceipt) && $responseReceipt == 'SUCCESS')
                                  {
                                      $reverse_receipt = "INSERT INTO `reverse_transaction_receipt` (`id`, `payment_type`, `transaction_id`, `date_time`, `action`) VALUES (NULL, 'Registration Pending Payment', '".$txnId."', '".date('Y-m-d H:i:s')."', '0') ";
                                      $reverse_receipt_insert = mysqli_query($conn, $reverse_receipt);
                                      $query = "UPDATE course_registration_pending_payment SET formSubmissionStatus = '1',paymentStatus='".$orderStatus."',receiptStatus='CRON-SUCCESS' WHERE id=" . $paymentId;
                                      $update = mysqli_query($conn, $query);
                                      echo "Registration Pending Payment - Receipt Generate for TXN ID - ". $txnId."<br/>";
                                  }
                                  else if($responseReceipt == 'RECEIPT_ALREADY_GENERATE')
                                  {
                                      $query = "UPDATE course_registration_pending_payment SET formSubmissionStatus = '1',formSubmissionStatus = '1',paymentStatus='".$orderStatus."', receiptStatus='Already-Generate', curlCheckCount = '".$curlCheckCount."' WHERE id=" . $paymentId;
                                      $update = mysqli_query($conn, $query);
                                      echo "Registration Pending Payment - Receipt Already Generate for TXN ID - ". $txnId."<br/>";
                                  }
                                  else
                                  {
                                      $query = "UPDATE course_registration_pending_payment SET formSubmissionStatus = '1',paymentStatus='".$orderStatus."', curlCheckCount = '".$curlCheckCount."' WHERE id=" . $paymentId;
                                      $update = mysqli_query($conn, $query);
                                      echo "Registration Pending Payment - Receipt Not Generate for TXN ID - ". $txnId."<br/>";
                                  }
                              }
                              else
                              {
                                  $query = "UPDATE course_registration_pending_payment SET formSubmissionStatus = '1',paymentStatus='".$orderStatus."', curlCheckCount = '".$curlCheckCount."' WHERE id=" . $paymentId;
                                  $update = mysqli_query($conn, $query);
                                  echo "Registration Pending Payment - Receipt Not Generate for TXN ID - ". $txnId." and order status - ".$orderStatus."<br/>";
                              }
                          }
                      }
                  }
              }
              else
              {
                      echo "No data found for Registration Pending Payment";
                      echo "<br>";
              }
          }else
          {
              echo "Table Doesnot Exist Course Registration Pending Payment";
          }
  
          ### Course Registration Payment ###
          
          $tableExistsQuery = "SHOW TABLES LIKE 'course_registration_payment'";
          $result = mysqli_query($conn, $tableExistsQuery);
          
          if (mysqli_num_rows($result) > 0) {
              $query = "SELECT * FROM `course_registration_payment` where (paymentStatus != 'failure' and paymentStatus != 'FAILED') and (receiptStatus = '' or receiptStatus IS NULL) and paymentDate >= '".$fromDate."' and paymentDate <= '".$toDate."' and paymentOption != '' order by id desc";
              $select = mysqli_query($conn, $query);
              $result = array();
              if (mysqli_num_rows($select) > 0)
              {
                  include("academia/courseRegistrationPayment.php");
                  $pendingPaymentObj = new CourseRegistrationPayment();
                  $pendingPaymentObj->setConnection($conn, $connMain);
                  while ($data = mysqli_fetch_assoc($select))
                  {
                      $activeGateway = $data['paymentOption'];
                      if (array_key_exists($activeGateway, $paymentGateways))
                      {
                          require_once($paymentGateways[$activeGateway]);
                          $curlCheckCount = $data['curlCheckCount'];
                          $curlCheckCount++;
                          $txnId = $data['txnId'] ?? NULL;
                          $paymentId = $data['id'];
                          $currency = $data['currency'];
                          $paymentDate = date('d-m-Y', strtotime($data['paymentDate']));
                          $onlinePaymentId = $data['onlinePaymentId'];
                          $data['conn'] = $conn;
                          $data['tableName'] = 'course_registration_payment';
                          $orderInfo= fetchOrderInformation($activeGateway, $data);
                          $orderStatus = $orderInfo['status']??$orderInfo;
                            if($txnId == NULL && isset($orderInfo['txnId']))
                            {
                                $query = "UPDATE course_registration_payment  SET  txnId ='".$orderInfo['txnId']."' WHERE id=" . $paymentId;
                                $update = mysqli_query($conn, $query);
                                $txnId = $orderInfo['txnId'];
                            }
                          if($orderStatus == 'INACTIVE-PAYMENT' || $orderStatus == 'INVALID-ORDER')
                          {
                              echo "Invalid - ".$txnId."--".$orderStatus."<br>";
                          }
                          else
                          {
                              if($orderStatus == 'TXN-SUCCESS')
                              {
                                  $responseReceipt = $pendingPaymentObj->createReceipt($data);
                                  if(isset($responseReceipt) && $responseReceipt == 'SUCCESS')
                                  {
                                      $reverse_receipt = "INSERT INTO `reverse_transaction_receipt` (`id`, `payment_type`, `transaction_id`, `date_time`, `action`) VALUES (NULL, 'Registration Payment', '".$txnId."', '".date('Y-m-d H:i:s')."', '0') ";
                                      $reverse_receipt_insert = mysqli_query($conn, $reverse_receipt);
                                      $query = "UPDATE course_registration_payment SET formSubmissionStatus = '1',paymentStatus='".$orderStatus."',receiptStatus='CRON-SUCCESS' WHERE id=" . $paymentId;
                                      $update = mysqli_query($conn, $query);
                                      echo "Registration Payment - Receipt Generate for TXN ID - ". $txnId."<br/>";
                                  }
                                  else if($responseReceipt == 'RECEIPT_ALREADY_GENERATE')
                                  {
                                      $query = "UPDATE course_registration_payment SET formSubmissionStatus = '1',formSubmissionStatus = '1',paymentStatus='".$orderStatus."', receiptStatus='Already-Generate', curlCheckCount = '".$curlCheckCount."' WHERE id=" . $paymentId;
                                      $update = mysqli_query($conn, $query);
                                      echo "Registration Payment - Receipt Already Generate for TXN ID - ". $txnId."<br/>";
                                  }
                                  else
                                  {
                                      $query = "UPDATE course_registration_payment SET formSubmissionStatus = '1',paymentStatus='".$orderStatus."', curlCheckCount = '".$curlCheckCount."' WHERE id=" . $paymentId;
                                      $update = mysqli_query($conn, $query);
                                      echo "Registration Payment - Receipt Not Generate for TXN ID - ". $txnId."<br/>";
                                  }
                              }
                              else
                              {
                                  $query = "UPDATE course_registration_payment SET formSubmissionStatus = '1',paymentStatus='".$orderStatus."', curlCheckCount = '".$curlCheckCount."' WHERE id=" . $paymentId;
                                  $update = mysqli_query($conn, $query);
                                  echo "Registration Payment - Receipt Not Generate for TXN ID - ". $txnId." and order status - ".$orderStatus."<br/>";
                              }
                          }
                      }
                  }
              }
              else
              {
                      echo "No data found for Registration Payment";
                      echo "<br>";
              }
          }else
          {
              echo "Table Doesnot Exist Course Registration Payment";
          }




        ##############    Curl Searching Finish  ###############


        $query = "UPDATE curl_executation SET current_executation='0' WHERE id=" . $executationId;
        $update = mysqli_query($conn, $query);
    }
    else
    {
        $curlExecutationQuery = "INSERT INTO `curl_executation` (`remote_ip`, `execute_status`,`current_executation`, `date_time`) VALUES ('".$_SERVER['REMOTE_ADDR']."', '0', '0','".$curretnDateTime."')";
        mysqli_query($conn, $curlExecutationQuery);
        echo "Request Already Running";
    }
}
else
{
    echo "Authentication Failed";
}