<?php
//echo "<pre>";
//print_r($_POST);
//die();

require_once('config.php');
require_once('lib/AhkWeb_Config.php');
require_once('lib/AhkWebCheckSum.php');

if(isset($_POST['status']) && $_POST['status']!=NULL){
    $verifySignature = '';
    $array = array();
    $paramList = array();
    $secret = 'N6JYbgmzrb'; // Your Secret Key.
    $status = $_POST['status']; // Its Payment Status Only, Not Txn Status.
    $hash = $_POST['hash']; // Encrypted Hash / Generated Only SUCCESS Status.
    $checksum = $_POST['checksum'];  // Checksum verifySignature / Generated Only SUCCESS Status.
    
    // Payment Status.
    if($status=="SUCCESS"){
    	
    $paramList = hash_decrypt($hash,$secret);
    $verifySignature = AhkWebCheckSum::verifySignature($paramList, $secret, $checksum);
    
    // Checksum verify.
    if($verifySignature){
        $resdata = json_decode($paramList,true);
        $orderid = $resdata['orderId'];
        if($resdata['txnStatus']=="TXN_SUCCESS"){
                $bankTxnId = $resdata['bankTxnId'];
                $dbdata = mysqli_fetch_assoc(mysqli_query($connection,"SELECT * FROM transactions WHERE txn='$orderid' LIMIT 1"));
                $userid= $dbdata['oid'];
                $amount= $dbdata['amount'];
                $updateuser = mysqli_query($connection,"UPDATE usertable SET walletamount=walletamount+$amount WHERE userid='$userid'");
                if($updateuser){
                     ?>
                    <form action="index.php" method="POST" name="f1">
                        <input type="hidden" name="success" value="true">
                    </form>
                    <script>
                        document.f1.submit();
                    </script>
                    <?php 
                }else{
                     ?>
                    <form action="index.php" method="POST" name="f1">
                        <input type="hidden" name="failed" value="true">
                    </form>
                    <script>
                        document.f1.submit();
                    </script>
                    <?php 
                }
    
        
    }else{
        ?>
           <form action="index.php" method="POST" name="f1">
                        <input type="hidden" name="payment cancel" value="true">
                    </form>
                    <script>
                        document.f1.submit();
                    </script>
            <?php 
    }	
    	
    
    }else{
         ?>
           <form action="index.php" method="POST" name="f1">
                        <input type="hidden" name="failed" value="true">
                    </form>
                    <script>
                        document.f1.submit();
                    </script>
            <?php 
    }
}else{
    ?>
          <form action="index.php" method="POST" name="f1">
                        <input type="hidden" name="failed" value="true">
                    </form>
                    <script>
                        document.f1.submit();
                    </script>
            <?php 
}


}
?>