<?php
require_once "mpgsPaymentGateway.php";
require_once "./config/QueryBuilder.php";

class PaymentRequest
{
    private $conn;
    private $queryBuilder;
    private $mpgsObj;

    public function __construct($conn)
    {
        $this->conn         = $conn;
        $this->queryBuilder = new QueryBuilder($conn);
        $this->mpgsObj      = new MPGSPaymentGateway();
    }

    public function paymentRequest($data, $screenName, $tableName, $merchantDetails)
    {
        if ($data['gatewayConfig']) {
            echo '<div class="col-md-12 text-center alert alert-warning">
                        <h4>Please try again later because Transaction ID already generated for this order.</h4>
                    </div>';
        } else {
            $activeGateway = $data['paymentOption'];
            $gatewayJSON   = <<<GATEWAYJSON
            {
                "academyLocationName": "{$merchantDetails['academyLocation']['value']}",
                "merchantID": "{$merchantDetails['merchantID']}",
                "merchantKey": "{$merchantDetails['merchantKey']}",
                "password": "{$merchantDetails['password']}",
                "currency": "{$merchantDetails['currency']}",
                "environment": "{$merchantDetails['environment']}"
            }
            GATEWAYJSON;
            $txnId      = 'ORD' . time() . mt_rand(100, 999);
            $updateData = ['gatewayConfig' => $gatewayJSON, 'academyLocationId' => $data['academyLocationId'], 'paymentOption' => $activeGateway, 'txnId' => $txnId];
            $where      = ['id' => $data['id']];
            $result     = $this->queryBuilder->update($tableName, $updateData, $where);
            if ($result == 1) {
                $paymentRequestColumnData = ['screen' => $screenName, 'txnId' => $txnId, 'paymentDate' => date('Y-m-d H:i:s'), 'onlinePaymentId' => ! empty($data['onlinePaymentId']) ? $data['onlinePaymentId'] : (isset($data['onlinePaymentId']) ? 0 : null), 'paymentId' => $data['id'], 'payment_gateway' => $activeGateway];
                $this->queryBuilder->insert('payment_request', $paymentRequestColumnData);
                $where          = ['id' => $data['id']];
                $paymentDetails = $this->queryBuilder->selectById($tableName, '*', $where);
                $gatewayInfo    = '';
                if ($paymentDetails && $paymentDetails['gatewayConfig']) {
                    $gatewayInfoArr = json_decode($paymentDetails['gatewayConfig'], true);

                    if (isset($gatewayInfoArr['merchantID']) && $gatewayInfoArr['merchantID'] && isset($gatewayInfoArr['merchantKey']) && $gatewayInfoArr['merchantKey'] && isset($gatewayInfoArr['password']) && $gatewayInfoArr['password'] && isset($gatewayInfoArr['currency']) && $gatewayInfoArr['currency'] && isset($gatewayInfoArr['environment']) && $gatewayInfoArr['environment']) {
                        $gatewayInfo = 'EXIST';
                    }
                }
                if (! $paymentDetails || $paymentDetails == 0 || $gatewayInfo = '') {

                    echo '<div class="col-md-12 text-center alert alert-warning">
                            <h4>Unfortunately, Your transaction could not be processed. Please try again.</h4>
                          </div>';
                } else {

                    $paymentRequest = $this->mpgsObj->createOrder($paymentDetails);
                    if (isset($paymentRequest['status']) && isset($paymentRequest['jsonResponse']) && isset($paymentRequest['sessionId']) && $paymentRequest['status'] == 'SUCCESS' && $paymentRequest['sessionId']) {
                        $updateData = ['paymentRequest' => $paymentRequest['jsonResponse']];
                        $where      = ['id' => $data['id']];
                        $result     = $this->queryBuilder->update($tableName, $updateData, $where);
                        echo '<html>
                                <head>
                                    <script src="'.$gatewayInfoArr['environment'].'static/checkout/checkout.min.js" data-error="errorCallback" data-cancel="cancelCallback"></script>
                                    <script type="text/javascript">
                                        var sessionId = "' . $paymentRequest['sessionId'] . '";
                                        function errorCallback(error) {
                                            console.log(JSON.stringify(error));
                                        }
                                        function cancelCallback() {
                                            console.log("Payment cancelled");
                                        }
                                        Checkout.configure({
                                            session: {
                                                id: sessionId
                                            }
                                        });

                                        // Automatically call the showPaymentPage() function
                                        window.onload = function() {
                                            Checkout.showPaymentPage();
                                        };
                                    </script>
                                </head>
                                <body>
                                    ...
                                    <div id="embed-target"> </div>
                                    <!-- No button is needed here for showPaymentPage() -->
                                    ...
                                </body>
                            </html>';

                    } else {
                        echo '<div class="col-md-12 text-center alert alert-warning">
                            <h4>Unfortunately, Your transaction could not be processed. No Response came from the Gateway Server.</h4>
                          </div>';
                    }

                }
            } else {
                echo '<div class="col-md-12 text-center alert alert-warning">
                      <h4>Unfortunately, Your transaction could not be processed. Please try again.</h4>
                      </div>';
            }

        }
        
    }

    public function onlinePaymentReceipt($paymentRequest , $connMain)
    {
        require_once("academia/onlinePayment.php");
        $tableName = 'online_payment';
        $txnId =  $paymentRequest['txnId'];
        $onlinePaymentObj = new OnlinePayment();
        $onlinePaymentObj->setConnection($this->conn, $connMain);
        $where = ['id' => $paymentRequest['paymentId'], 'txnId' => $txnId];
        $onlinePaymentInfo = $this->queryBuilder->selectById($tableName, '*', $where);
        if ($onlinePaymentInfo) {
            if ($onlinePaymentInfo['formSubmissionStatus'] != 1) {
                $orderInfo = $this->mpgsObj->fetchOrder($onlinePaymentInfo);
                if ($orderInfo['status'] == 'SUCCESS' && $orderInfo['txnId'] == $txnId) {                    
                    $updateData = ['notificationResponse' => json_encode($orderInfo['response']), 'formSubmissionStatus' => 1, 'paymentStatus' => $orderInfo['status']];
                    $where = ['id' => $paymentRequest['paymentId']];
                    $this->queryBuilder->update($tableName, $updateData, $where);                   
                    $response = $onlinePaymentObj->createReceipt($onlinePaymentInfo['onlinePaymentId'], $txnId, $orderInfo['status']);
                    if ($response == 'SUCCESS') {
                        $updateData = ['receiptStatus' => $response];
                        $where = ['id' => $paymentRequest['paymentId']];
                        $result = $this->queryBuilder->update($tableName, $updateData, $where);
                        echo '<div class="col-md-12 text-center alert alert-success">
                                    <h4>Fee Payment is successful !!!.<br /><br />Thank you for the payment. Your Transaction id is '.$txnId.'.<br /> Please close the window.</h4>
                            </div>';

                    } else {
                        echo '<div class="col-md-12 text-center alert alert-success">
                                    <h4>Fee Payment is successful !!!.<br /><br />Thank you for the payment. Your Transaction id is '.$txnId.'.<br /> However, the receipt could not be generated. Please contact the administrator for assistance. You may now close this window.</h4>
                                </div>';
                    }
                    
                } else {
                    $updateData = ['notificationResponse' => json_encode($orderInfo['response']), 'formSubmissionStatus' => 1 , 'paymentStatus' => $orderInfo['status']];
                    $where = ['id' => $paymentRequest['paymentId']];
                    $this->queryBuilder->update($tableName, $updateData, $where);
                    echo '<div class="col-md-12 text-center alert alert-warning">
                            <h4>Payment failed. Please check your card details and try again. If the issue persists, contact the administrator.</h4>
                            <p>Transaction ID: <strong>'.$orderInfo['txnId'].'</strong></p>                          
                        </div>';
                }
            } else {
                echo '<div class="col-md-12 text-center alert alert-warning">
                        <h4>Unfortunately, Your transaction could not be processed. Please try again.</h4>
                    </div>';
            }
        } else {
            echo '<div class="col-md-12 text-center alert alert-warning">
              <h4>Unfortunately, Your transaction could not be processed becuase your order details not found. Please contact administrator.</h4>
          </div>';
        }
    }

    public function RevaluationReceipt($paymentRequest, $connMain)
    {
        require_once("academia/revaluationPayment.php");
        $tableName = 'reval_payment';
        $txnId = $paymentRequest['txnId'];
        $revalObj = new RevaluationPayment();
        $revalObj->setConnection($this->conn, $connMain);
        $where = ['id' => $paymentRequest['paymentId'], 'txnId' => $txnId];
        $revalInfo = $this->queryBuilder->selectById($tableName, '*', $where);
        if($revalInfo){
            if ($revalInfo['formSubmissionStatus'] != 1) {
                $orderInfo = $this->mpgsObj->fetchOrder($revalInfo);
                if ($orderInfo['status'] == 'SUCCESS' && $orderInfo['txnId'] == $txnId) {
                    $updateData = ['notificationResponse' => json_encode($orderInfo['response']), 'formSubmissionStatus' => 1, 'paymentStatus' => $orderInfo['status']];
                    $where = ['id' => $paymentRequest['paymentId']];
                    $this->queryBuilder->update($tableName, $updateData, $where); 
                    $response = $revalObj->createReceipt($revalInfo['id'], $revalInfo['amount'], $txnId, $orderInfo['status']);
                    if (isset($response) && isset($response['status']) == 'SUCCESS') {
                        $updateData = ['receiptStatus' => $response['status']];
                        $where = ['id' => $paymentRequest['paymentId']];
                        $result = $this->queryBuilder->update($tableName, $updateData, $where);
                        echo '<div class="col-md-12 text-center alert alert-success">
                                    <h4>Fee Payment is successful !!!.<br /><br />Thank you for the payment. Your Transaction id is ' . $txnId . '.<br /> Please close the window.</h4>
                         </div>';
                    } else {
                        echo '<div class="col-md-12 text-center alert alert-success">
                                    <h4>Fee Payment is successful !!!.<br /><br />Thank you for the payment. Your Transaction id is '.$txnId.'.<br /> However, the receipt could not be generated. Please contact the administrator for assistance. You may now close this window.</h4>
                                </div>';
                    }
                } else {
                    $updateData = ['notificationResponse' => json_encode($orderInfo['response']), 'formSubmissionStatus' => 1 , 'paymentStatus' => $orderInfo['status']];
                    $where = ['id' => $paymentRequest['paymentId']];
                    $this->queryBuilder->update($tableName, $updateData, $where);
                    echo '<div class="col-md-12 text-center alert alert-warning">
                            <h4>Payment failed. Please check your card details and try again. If the issue persists, contact the administrator.</h4>
                            <p>Transaction ID: <strong>'.$orderInfo['txnId'].'</strong></p>                          
                        </div>';
                }
            } else {
                echo '<div class="col-md-12 text-center alert alert-warning">
                        <h4>Unfortunately, Your transaction could not be processed. Please try again.</h4>
                    </div>';
            }

        } else {
            echo '<div class="col-md-12 text-center alert alert-warning">
              <h4>Unfortunately, Your transaction could not be processed becuase your order details not found. Please contact administrator.</h4>
          </div>';
        }
    }

    public function declarationPaymentReceipt($paymentRequest , $connMain)
    {
        require_once("academia/declarationPayment.php");
        $tableName = 'declaration_payment';
        $txnId =  $paymentRequest['txnId'];
        $declarationPaymentObj = new DeclarationPayment();
        $declarationPaymentObj->setConnection($this->conn, $connMain);
        $where = ['id' => $paymentRequest['paymentId'], 'txnId' => $txnId];
        $declarationPaymentInfo = $this->queryBuilder->selectById($tableName, '*', $where);
        if ($declarationPaymentInfo) {
            if ($declarationPaymentInfo['formSubmissionStatus'] != 1) {
                $orderInfo = $this->mpgsObj->fetchOrder($declarationPaymentInfo);
                if ($orderInfo['status'] == 'SUCCESS' && $orderInfo['txnId'] == $txnId) {                    
                    $updateData = ['notificationResponse' => json_encode($orderInfo['response']), 'formSubmissionStatus' => 1, 'paymentStatus' => $orderInfo['status']];
                    $where = ['id' => $paymentRequest['paymentId']];
                    $this->queryBuilder->update($tableName, $updateData, $where);                   
                    $response = $declarationPaymentObj->createReceipt($declarationPaymentInfo);
                    if (isset($response['status']) && $response['status'] == 'SUCCESS') {
                        $updateData = ['receiptStatus' => $response['status'], 'receiptId' => $response['receiptId']];
                        $where = ['id' => $paymentRequest['paymentId']];
                        $result = $this->queryBuilder->update($tableName, $updateData, $where);
                        echo '<div class="col-md-12 text-center alert alert-success">
                                    <h4>Fee Payment is successful !!!.<br /><br />Thank you for the payment. Your Transaction id is '.$txnId.'.<br /> Please close the window.</h4>
                            </div>';

                    } else {
                        echo '<div class="col-md-12 text-center alert alert-success">
                                    <h4>Fee Payment is successful !!!.<br /><br />Thank you for the payment. Your Transaction id is '.$txnId.'.<br /> However, the receipt could not be generated. Please contact the administrator for assistance. You may now close this window..</h4>
                                </div>';
                    }
                    
                } else {
                    $updateData = ['notificationResponse' => json_encode($orderInfo['response']), 'formSubmissionStatus' => 1 , 'paymentStatus' => $orderInfo['status']];
                    $where = ['id' => $paymentRequest['paymentId']];
                    $this->queryBuilder->update($tableName, $updateData, $where);
                    echo '<div class="col-md-12 text-center alert alert-warning">
                            <h4>Unfortunately, Your transaction could not be processed becuase order details not found from payment gateway server.</h4>
                        </div>';
                }
            } else {
                echo '<div class="col-md-12 text-center alert alert-warning">
                        <h4>Unfortunately, Your transaction could not be processed. Please try again.</h4>
                    </div>';
            }
        } else {
            echo '<div class="col-md-12 text-center alert alert-warning">
              <h4>Unfortunately, Your transaction could not be processed becuase your order details not found. Please contact to administrator.</h4>
          </div>';
        }
    }

    public function sendPaymentLinkReceipt($paymentRequest, $connMain)
    {
        require_once("academia/sendPaymentLinkPayment.php");
        $tableName = 'online_payment';
        $txnId = $paymentRequest['txnId'];
        $sendPaymentLinkObj = new SendPaymentLinkPayment();
        $sendPaymentLinkObj->setConnection($this->conn, $connMain);
        $where = ['id' => $paymentRequest['paymentId'], 'txnId' => $txnId];
        $sendPaymentLinkInfo = $this->queryBuilder->selectById($tableName,'*',$where);
        if($sendPaymentLinkInfo){
            if ($sendPaymentLinkInfo['formSubmissionStatus'] != 1) {
                $orderInfo = $this->mpgsObj->fetchOrder($sendPaymentLinkInfo);
                if ($orderInfo['status'] == 'SUCCESS' && $orderInfo['txnId'] == $txnId) {
                    $updateData = ['notificationResponse' => json_encode($orderInfo['response']), 'formSubmissionStatus' => 1, 'paymentStatus' => $orderInfo['status']];
                    $where = ['id' => $paymentRequest['paymentId']];
                    $this->queryBuilder->update($tableName, $updateData, $where); 
                    $response = $sendPaymentLinkObj->createReceipt($sendPaymentLinkInfo['sendPaymentLinkId'],$sendPaymentLinkInfo['onlinePaymentId'], $txnId, $orderInfo['status']);
                    if ($response == 'SUCCESS') {
                        $updateData = ['receiptStatus' => $response];
                        $where = ['id' => $paymentRequest['paymentId']];
                        $result = $this->queryBuilder->update($tableName, $updateData, $where);
                        echo '<div class="col-md-12 text-center alert alert-success">
                                    <h4>Fee Payment is successful !!!.<br /><br />Thank you for the payment. Your Transaction id is '.$txnId.'.<br /> Please close the window.</h4>
                            </div>';

                    } else {
                        echo '<div class="col-md-12 text-center alert alert-success">
                                   <h4>Fee Payment is successful !!!.<br /><br />Thank you for the payment. Your Transaction id is '.$txnId.'.<br /> However, the receipt could not be generated. Please contact the administrator for assistance. You may now close this window..</h4>
                                </div>';
                    }
                } else {
                    $updateData = ['notificationResponse' => json_encode($orderInfo['response']), 'formSubmissionStatus' => 1 , 'paymentStatus' => $orderInfo['status']];
                    $where = ['id' => $paymentRequest['paymentId']];
                    $this->queryBuilder->update($tableName, $updateData, $where);
                    echo '<div class="col-md-12 text-center alert alert-warning">
                            <h4>Payment failed. Please check your card details and try again. If the issue persists, contact the administrator.</h4>
                            <p>Transaction ID: <strong>'.$orderInfo['txnId'].'</strong></p>                          
                        </div>';
                }
            } else {
                echo '<div class="col-md-12 text-center alert alert-warning">
                        <h4>Unfortunately, Your transaction could not be processed. Please try again.</h4>
                    </div>';
            }

        } else {
            echo '<div class="col-md-12 text-center alert alert-warning">
              <h4>Unfortunately, Your transaction could not be processed becuase your order details not found. Please contact administrator.</h4>
          </div>';
        }
    }

    public function examAssessmentReceipt($paymentRequest, $connMain)
    {
        require_once "academia/examinationPayment.php";
        $tableName = 'assessment_payment';
        $txnId =  $paymentRequest['txnId'];
        $examObj = new ExaminationPayment();
        $examObj->setConnection($this->conn, $connMain);
        $where = ['id' => $paymentRequest['paymentId'], 'txnId' => $txnId];
        $examInfo = $this->queryBuilder->selectById($tableName, '*', $where);
        if($examInfo){
            if ($examInfo['formSubmissionStatus'] != 1) {
                $orderInfo = $this->mpgsObj->fetchOrder($examInfo);
                if ($orderInfo['status'] == 'SUCCESS' && $orderInfo['txnId'] == $txnId) {
                    $updateData = ['notificationResponse' => json_encode($orderInfo['response']), 'formSubmissionStatus' => 1, 'paymentStatus' => $orderInfo['status']];
                    $where = ['id' => $paymentRequest['paymentId']];
                    $this->queryBuilder->update($tableName, $updateData, $where); 
                    $response = $examObj->createReceipt($examInfo['id'], $examInfo['academyLocationId'], $txnId, $examInfo['amount'],$examInfo['currency']);
                    if (isset($response['status']) && $response['status'] == 'SUCCESS') {
                        $updateData = ['receiptStatus' => $response['status']];
                        $where = ['id' => $paymentRequest['paymentId']];
                        $result = $this->queryBuilder->update($tableName, $updateData, $where);
                        $tableName2 = 'assessment_payment_detail';
                        $updateDataDetail = ['receiptId' => $response['receiptId']];
                        $updateWhere = ['examAssessmentPaymentId' => $paymentRequest['paymentId']];
                        $result2 = $this->queryBuilder->update($tableName2, $updateDataDetail, $updateWhere);
                        echo '<div class="col-md-12 text-center alert alert-success">
                                        <h4>Exam Payment is successful !!!.<br /><br />Thank you for the payment. Your Transaction id is '.$txnId.'.<br /> Please close the window.</h4>
                                </div>';
                    } else {
                        echo '<div class="col-md-12 text-center alert alert-success">
                            <h4>Exam Payment is successful !!!.<br /><br />Thank you for the payment. Your Transaction id is '.$txnId.'.<br /> However, the receipt could not be generated. Please contact the administrator for assistance. You may now close this window..</h4>
                         </div>';
                    }
                } else {
                    $updateData = ['notificationResponse' => json_encode($orderInfo['response']), 'formSubmissionStatus' => 1 , 'paymentStatus' => $orderInfo['status']];
                    $where = ['id' => $paymentRequest['paymentId']];
                    $this->queryBuilder->update($tableName, $updateData, $where);
                    echo '<div class="col-md-12 text-center alert alert-warning">
                            <h4>Payment failed. Please check your card details and try again. If the issue persists, contact the administrator.</h4>
                            <p>Transaction ID: <strong>'.$orderInfo['txnId'].'</strong></p>                          
                        </div>';
                }
            } else {
                echo '<div class="col-md-12 text-center alert alert-warning">
                        <h4>Unfortunately, Your transaction could not be processed. Please try again.</h4>
                    </div>';
            }
        } else {
            echo '<div class="col-md-12 text-center alert alert-warning">
              <h4>Unfortunately, Your transaction could not be processed becuase your order details not found. Please contact administrator.</h4>
          </div>';
        }
    }

    public function courseEnrollementPaymentReceipt($paymentRequest,$connMain)
    {
        require_once("academia/courseEnrollement.php");
        $tableName = 'enrollment_payment';
        $txnId = $paymentRequest['txnId'];
        $courseEnrollementPaymentObj = new CourseEnrollement();
        $courseEnrollementPaymentObj->setConnection($this->conn, $connMain);
        $where = ['id' => $paymentRequest['paymentId'], 'txnId' => $txnId];
        $courseEnrollementInfo = $this->queryBuilder->selectById($tableName, '*', $where);
        if ($courseEnrollementInfo) {
            if ($courseEnrollementInfo['formSubmissionStatus'] != 1) {
                $orderInfo = $this->mpgsObj->fetchOrder($courseEnrollementInfo);
                if ($orderInfo['status'] == 'SUCCESS' && $orderInfo['txnId'] == $txnId) {
                    $updateData = ['notificationResponse' => json_encode($orderInfo['response']), 'formSubmissionStatus' => 1, 'paymentStatus' => $orderInfo['status']];
                    $where = ['id' => $paymentRequest['paymentId']];
                    $this->queryBuilder->update($tableName, $updateData, $where); 
                    $response = $courseEnrollementPaymentObj->createReceipt($courseEnrollementInfo['onlinePaymentId'], $txnId, $orderInfo['status']);
                    if ($response == 'SUCCESS') {
                        $updateData = ['receiptStatus' => $response];
                        $where = ['id' => $paymentRequest['paymentId']];
                        $result = $this->queryBuilder->update($tableName, $updateData, $where);
                        echo '<div class="col-md-12 text-center alert alert-success">
                                    <h4>Fee Payment is successful !!!.<br /><br />Thank you for the payment. Your Transaction id is '.$txnId.'.<br /> Please close the window.</h4>
                            </div>';

                    } else {
                        echo '<div class="col-md-12 text-center alert alert-success">
                                   <h4>Fee Payment is successful !!!.<br /><br />Thank you for the payment. Your Transaction id is '.$txnId.'.<br /> However, the receipt could not be generated. Please contact the administrator for assistance. You may now close this window..</h4>
                                </div>';
                    } 
                } else {
                    $updateData = ['notificationResponse' => json_encode($orderInfo['response']), 'formSubmissionStatus' => 1 , 'paymentStatus' => $orderInfo['status']];
                    $where = ['id' => $paymentRequest['paymentId']];
                    $this->queryBuilder->update($tableName, $updateData, $where);
                    echo '<div class="col-md-12 text-center alert alert-warning">
                            <h4>Payment failed. Please check your card details and try again. If the issue persists, contact the administrator.</h4>
                            <p>Transaction ID: <strong>'.$orderInfo['txnId'].'</strong></p>                          
                        </div>';
                }
            } else {
                echo '<div class="col-md-12 text-center alert alert-warning">
                        <h4>Unfortunately, Your transaction could not be processed. Please try again.</h4>
                    </div>';
            }
        } else {
            echo '<div class="col-md-12 text-center alert alert-warning">
              <h4>Unfortunately, Your transaction could not be processed becuase your order details not found. Please contact administrator.</h4>
          </div>';
        }
    }

    public function donationPaymentReceipt($paymentRequest , $connMain)
    {
        require_once("academia/donationPayment.php");
        $tableName = 'donation_payment';
        $txnId =  $paymentRequest['txnId'];
        $donationObj = new DonationPayment();
        $donationObj->setConnection($this->conn, $connMain);
        $where = ['id' => $paymentRequest['paymentId'], 'txnId' => $txnId];
        $donationInfo = $this->queryBuilder->selectById($tableName, '*', $where);
        if ($donationInfo) {
            if ($donationInfo['formSubmissionStatus'] != 1) {
                $orderInfo = $this->mpgsObj->fetchOrder($donationInfo);
                if ($orderInfo['status'] == 'SUCCESS' && $orderInfo['txnId'] == $txnId) {
                    $updateData = ['notificationResponse' => json_encode($orderInfo['response']), 'formSubmissionStatus' => 1, 'paymentStatus' => $orderInfo['status']];
                    $where = ['id' => $paymentRequest['paymentId']];
                    $this->queryBuilder->update($tableName, $updateData, $where); 
                    $response = $donationObj->createReceipt($donationInfo, 'TXN_SUCCESS');
                    if ($response == 'SUCCESS') {
                        $updateData = ['receiptStatus' => $response];
                        $where = ['id' => $paymentRequest['paymentId']];
                        $result = $this->queryBuilder->update($tableName, $updateData, $where);
                        echo '<div class="col-md-12 text-center alert alert-success">
                                    <h4>Fee Payment is successful !!!.<br /><br />Thank you for the payment. Your Transaction id is '.$txnId.'.<br /> Please close the window.</h4>
                            </div>';

                    } else {
                        echo '<div class="col-md-12 text-center alert alert-success">
                                    <h4>Fee Payment is successful !!!.<br /><br />Thank you for the payment. Your Transaction id is '.$txnId.'.<br /> However, the receipt could not be generated. Please contact the administrator for assistance. You may now close this window..</h4>
                                </div>';
                    }
                } else {
                    $updateData = ['notificationResponse' => json_encode($orderInfo['response']), 'formSubmissionStatus' => 1 , 'paymentStatus' => $orderInfo['status']];
                    $where = ['id' => $paymentRequest['paymentId']];
                    $this->queryBuilder->update($tableName, $updateData, $where);
                    echo '<div class="col-md-12 text-center alert alert-warning">
                            <h4>Payment failed. Please check your card details and try again. If the issue persists, contact the administrator.</h4>
                            <p>Transaction ID: <strong>'.$orderInfo['txnId'].'</strong></p>                          
                        </div>';
                }
            } else {
                echo '<div class="col-md-12 text-center alert alert-warning">
                        <h4>Unfortunately, Your transaction could not be processed. Please try again.</h4>
                    </div>';
            }
        } else {
            echo '<div class="col-md-12 text-center alert alert-warning">
              <h4>Unfortunately, Your transaction could not be processed becuase your order details not found. Please contact administrator.</h4>
          </div>';
        }
    }
}
