<?php
session_start();
include("db_modal.php");
include('../smtp/PHPMailerAutoload.php');

// ini_set('display_errors', 1);
// error_reporting(E_ALL);

if (isset($_SESSION['username']) || isset($_SESSION['appuser'])) {
    $mobile = $_SESSION['username'];
    if($mobile == ''){
        
    $mobile = $_SESSION['appuser'];
    }
    $user = $crud->read("users","mobile = '$mobile'")[0];
}




class merchantAuthClass extends CrudOperation{
    
  
    public function sendEmail($to, $subject, $message) {  
        
    $mail = new PHPMailer(); 
    	$mail->IsSMTP(); 
    	$mail->SMTPAuth = true; 
    	$mail->SMTPSecure = 'tls'; 
    	$mail->Host = "localhost";
    	$mail->Port = 587; 
    	$mail->IsHTML(true);
    	$mail->CharSet = 'UTF-8';
    	//$mail->SMTPDebug = 2; 
        $mail->Username = "no-reply@localhost";
    	$mail->Password = "P5&XExDcm";
    	$mail->SetFrom("no-reply@localhost","imb Pay");
    	$mail->Subject = $subject;
    	$mail->Body =$message;
    	$mail->AddAddress($to);
    	$mail->SMTPOptions=array('ssl'=>array(
    		'verify_peer'=>false,
    		'verify_peer_name'=>false,
    		'allow_self_signed'=>false
    	));
    	if(!$mail->Send()){
    		return $mail->ErrorInfo;
    	}else{
    	    return true;
    	}

}
    
// for sms msg send
public function send_smsmsg($mobile,$pagename,$otp){
    
    $message = "$otp is the OTP for $pagename. Please do not share this OTP with anyone. This SMS has been sent from GuestRAR";
    
    $username = "your username";
    $senderName = "your sendername"; 
    $smsType = "type"; 
    $apiKey = "your api key";
    
    $url = "http://sms.hspsms.com/sendSMS?username=$username&message=" . urlencode($message) . 
           "&sendername=$senderName&smstype=$smsType&numbers=$mobile&apikey=$apiKey";
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    $response = curl_exec($ch);
    if (curl_errno($ch)) {
        echo "cURL Error: " . curl_error($ch);
    }
    curl_close($ch);
                
}
     // Send OTP via sms end
     

                
// for create cashfree token
public function send_msg($email,$mobile,$pagename,$otp){
    
             $mailmsg = '<html>
<head>
  <title>'.$pagename.' OTP - imb Pay Gateway</title>
  
</head>
<body>
<p>Your One-Time Password (OTP): ' . htmlspecialchars($otp) . '</p>
</body>
</html>'
;
        $mailsend = $this->sendEmail($email, "OTP Verfication For $pagename", $mailmsg);
    	if(!$mailsend){
    return false;
    	}else{
    	    
$this->update("users",["otp" => $otp],"mobile = '$mobile'");

return true;
} 

}

}

$merchantVAuth = new merchantAuthClass("localhost", "root", "", "paycodex_codex");




// send otp code here
if(isset($_POST["sendotp"]) && $_POST["sendotp"] == true){
    $otp = mt_rand(100000,999999);
    $pagename = $_POST["page"];
    $email = $user["email"];
    $mobile = $user["mobile"];
    if($email == ''){
        $email = $_POST["email"];
    }
    $encryptotp = password_hash($otp, PASSWORD_BCRYPT);
    if($merchantVAuth->send_msg($email,$mobile,$pagename,$otp)){
        
        echo json_encode(["rescode" => 200,"status"=>true,"msg"=>"OTP Sent Successfully.","data" => $encryptotp,"type" => "email"]);
        exit;
    }else{
        echo json_encode(["rescode" => 403,"status"=>false,"msg"=>"Falied to send OTP ! try agian later."]);
        exit;
    }
}

// send otp code here
if(isset($_POST["sendmobileotp"]) && $_POST["sendmobileotp"] == true){
    $otp = mt_rand(100000,999999);
    $pagename = $_POST["page"];
    $mobile = $_POST["mobile"];
    if($mobile == ''){
        $mobile = $user["mobile"];
    }
    $encryptotp = password_hash($otp, PASSWORD_BCRYPT);
  
    $merchantVAuth->send_smsmsg($mobile,$pagename,$otp);
        
        echo json_encode(["rescode" => 200,"status"=>true,"msg"=>"OTP Sent Successfully.","data" => $encryptotp,"type" => "mobile"]);
        exit;
}
// send otp code here
if(isset($_POST["type"]) && $_POST["type"] == 'sendCallbackMsg'){
    
    $message = $_POST["message"];
   
    $merchantVAuth->send_whatsappcallback($message);
        
    echo json_encode(["rescode" => 200,"status"=>true,"msg"=>"Callback Request Sent Successfully."]);
    exit;
}

// check otp verify code here
if(isset($_POST["checkotp"])){
        $otp = $_POST["otp"];
        $otptype = $_POST["otptype"];
        $decryptotp = $_POST["otpdata"];
        
        if(password_verify($otp, $decryptotp)){
            echo json_encode(["rescode" => 200,"status"=>true,"msg"=>"OTP Verified Successfully.","type" => $otptype]);
            exit;
        }else{
            echo json_encode(["rescode" => 402,"status"=>false,"msg"=>"Invalid OTP."]);
            exit;
            
        }
   
}

// user register code here
if(isset($_POST["userRegister"])){
      
    $mobile =  $_POST['mobile'];
    $email = $_POST['email'];

    $checkMobileQuery = $crud->read("users","`mobile` = '$mobile'")[0];
    $checkEmailQuery = $crud->read("users","`email` = '$email'")[0];
    
    if (!empty($checkMobileQuery)) {
        
       echo json_encode(["rescode" => 402,"status"=>false,"msg"=>"Mobile Number already exist."]);
       exit;
       
    } elseif (!empty($checkEmailQuery)) {
        
       echo json_encode(["rescode" => 403,"status"=>false,"msg"=>"Email id already exists."]);
      exit;

    } else {
        // Proceed with user registration
        $password = $_POST['password'];
        $name = $_POST['name'];
        $company = $_POST['company'];
        $pin = $_POST['pin'];
        $pan = $_POST['pan'];
       
    $checkpan = $crud->read("users", "`pan` = '$pan'")[0];

    if (!empty($checkpan)) {
        echo json_encode(["rescode" => 405,"status"=>false,"msg"=>"Pan Number already exists."]);
        exit;
   
    }else{  
     
        $sponser_id = $_POST['sponser_id'];
        $location = $_POST['location'];
        $usertoken = md5(rand(00000000, 99999999));
        $pass = password_hash($password, PASSWORD_BCRYPT);
        $today = date("Y-m-d", strtotime("+3 days"));

function generateRandomInstanceId($length = 16) {
  $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  $randomString = 'I'; // Fixed 'I' as the first character

  // Generate a random string with the specified length - 7 (for the time part and additional digit)
  for ($i = 1; $i < $length - 6; $i++) {
      $randomString .= $characters[rand(0, strlen($characters) - 1)];
  }

  // Get the current time in seconds since the epoch
  $currentTime = time();

  // Take the last 6 digits from the current time and append them to the random string
  $lastSixDigits = substr(strval($currentTime), -6);
  $randint = rand(100, 900);
  
  return $randomString . $randint . $lastSixDigits;
}


$instanceId = generateRandomInstanceId();

$insertdata = 
  [
       'name' => $name,
       'mobile' => $mobile,
       'role' => 'User',
       'password' => $pass,
       'email' => $email,
       'company' => $company,
       'pin' => $pin,
       'pan' => $pan,
       'location' => $location,
       'user_token' => $usertoken,
       'expiry' => $today,
       'sponser_by' => $sponser_id,
       'instance_id' => $instanceId
    ];

$register = $crud->create("users",$insertdata);

        if ($register) {
            
            $usid = $register;
            $sponserid = "IMBRFL00$usid";
            
            $crud->update("users", ["sponser_id" => $sponserid], "id = '$usid'");
            
            echo json_encode(["rescode" => 200,"status"=>true,"msg"=>"Account created successfully."]);
            exit;
        } else {
             echo json_encode(["rescode" => 406,"status"=>false,"msg"=>"Failed to create account ! try again later."]);
            exit;
        }
    }
}
       
}

// change upi to pg mode
if(isset($_POST["changepgmode"]) && $_POST["changepgmode"] == true){
    $mode = $_POST["mode"];
    $chnagpg = $_POST["upitopg"];
   
    if(!empty($user)){
        
        $updatemode = $crud->update("users",["pg_mode" => $mode],"mobile = '$mobile'");
        if($updatemode){
            if($mode == 2){
            echo json_encode(["rescode" => 200,"status"=>true,"msg"=>"Success."]);
            exit;
            }else if($mode == 3){
            echo json_encode(["rescode" => 111,"status"=>true,"msg"=>"Success."]);
            exit;
            }else{
            echo json_encode(["rescode" => 222,"status"=>true,"msg"=>"Success."]);
            exit;
                
            }
        }
    }else{
            echo json_encode(["rescode" => 404,"status"=>false,"msg"=>"User Data Not Found | Try Again Later!."]);
            exit;
    }
    
}



// accept terms and condition code here
if(isset($_POST["tandc_accept"]) && $_POST["tandc_accept"] == true){
        $userid = $_POST["usid"];
        
        $updatemode = $crud->update("users",["term_and_condition" => 1],"id = '$userid'");
        if($updatemode){
            echo json_encode(["rescode" => 200,"status"=>true,"msg"=>"Success."]);
            exit;
        }else{
            echo json_encode(["rescode" => 400,"status"=>false,"msg"=>"Failure."]);
            exit;
            
        }
   
}


// apply coupon code code here
if(isset($_POST["type"]) && $_POST["type"] == 'getPlanAmount'){
    
        $plan_id = $_POST["planid"];
        $s_months = $_POST["s_months"].'month_amount';
       
        $getPlanAmount = $crud->read("plan_details","plan_id = '$plan_id'","`$s_months` as amount")[0];
        if(!empty($getPlanAmount)){
        $planamount = $getPlanAmount["amount"]*$s_months;
        $permamount = $getPlanAmount["amount"];
        echo json_encode(["rescode" => 200,"msg"=>"Success.","planamount" => $planamount,"permamount" => $permamount],JSON_NUMERIC_CHECK);
        exit;
        }else{
        echo json_encode(["rescode" => 404,"msg"=>"erorr."]);
        exit;
        }
   
}



// getmerchant upi deatils code here
if(isset($_POST["mno"])){
        $mno = $_POST["mno"];
        $mname = $_POST["mname"];
        $usertoken = $user["user_token"];
      
        $upiid = $user["upi_id"];
        
        if($mname == 'phonepe'){
            
        echo json_encode(["status" => 1,"msg"=>"Success.","upiid" => $upiid]);
        exit;
        }else{
        $getmerchant = $crud->read("merchant","user_token = '$usertoken'")[0];
        echo json_encode(["status" => 1,"msg"=>"Success.","upiid" => $getmerchant["merchant_upi"]]);
        exit;
        }
   
}



// update merchant upi details code here
if(isset($_POST["upi_id"]) && $_POST["upi_id"] != ''){
        $upi_id = $_POST["upi_id"];
        $mname = $_POST["mname"];
        $usertoken = $user["user_token"];
        
        if($mname == 'phonepe'){
        $updateupiid = $crud->update("users",["upi_id" => "$upi_id"],"mobile = '$mobile'");
        }else{
        $updateupiid = $crud->update("merchant",["merchant_upi" => "$upi_id"],"user_token = '$usertoken'");
        }
        if($updateupiid){
            echo json_encode(["status" => 1,"msg"=>"Success."]);
            exit;
        }else{
            echo json_encode(["status" => 5,"msg"=>"Failed to update UPI id."]);
            exit;
            
        }
  
   
}


// update txn status code here
if(isset($_POST["type"]) && $_POST["type"] == 'updateTxnOrder'){
        $orderid = $_POST["orderid"];
        $status = $_POST["status"];
        $utrno = $_POST["utrno"];
        $remark = $_POST["remark"];
        $usertoken = $user["user_token"];
        
        $gettxndata = $crud->read("orders","order_id = '$orderid'")[0];
        
        if(!empty($gettxndata)){
            
        if($gettxndata["status"] == 'PENDING'){
            
    if($status == 1){    

$callback_url =  $user["callback_url"];          
$mcq = $crud->read("callback_report","order_id = '$orderid'")[0];

if (empty($mcq)) {
    
// Data to be sent
$postData = array(
    'status' => 'SUCCESS',
    'order_id' => $orderid,
    'message' => 'Transaction Successfully',
    'result' => array(
            "txnStatus" => "COMPLETED",
            "resultInfo" => "Transaction Success",
            "orderId" => $orderid,
            'amount' => $gettxndata["amount"],
            'date' => $gettxndata['create_date'],
            'utr' => $utrno,
            'customer_mobile' => $gettxndata['customer_mobile'],
            'remark1' => $remark,
            'remark2' => $gettxndata["remark2"]
        )
);

$url = $callback_url;

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));

curl_exec($ch);

curl_close($ch);


}

}
        $st = ($status == 1) ? 'SUCCESS' : 'FAILURE';
    $updatetxn = $crud->update("orders",["status" => "$st","utr" => "$utrno","remark1" => "$remark"],"order_id = '$orderid'");
        
         if($updatetxn){
            echo json_encode(["status" => 1,"msg"=>"Transaction status updated successfully."]);
            exit;
        }else{
            echo json_encode(["status" => 5,"msg"=>"Failed to update status."]);
            exit;
            
        }
        
        }else{
            echo json_encode(["status" => 409,"msg"=>"Transaction already updated."]);
            exit;
        }
        
        }else{
            echo json_encode(["status" => 9,"msg"=>"transaction not found !."]);
            exit;
            
        }
   
}

// update merchant upi details code here
if(isset($_POST["type"]) && $_POST["type"] == 'updateMerchantSt'){
        $mid = $_POST["mid"];
        $mtype = $_POST["mtype"];
        $status = $_POST["status"];
        $usertoken = $user["user_token"];
        $mrtTable = '';
        $mrtTableidName = 'id';
        
        switch ($mtype) {
            case 'HDFC':
                $mrtTable = 'hdfc';
                break;
            case 'PhonePe':
                $mrtTable = 'phonepe_tokens';
                $mrtTableidName = 'sl';
                break;
            case 'Paytm':
                $mrtTable = 'paytm_tokens';
                break;
            case 'Freecharge':
                $mrtTable = 'freecharge';
                break;
            case 'SBI':
                $mrtTable = 'merchant';
                $mrtTableidName = 'merchant_id';
                break;
            case 'Bharatpe':
                $mrtTable = 'bharatpe_tokens';
                break;
            case 'MOBIKWIK':
                $mrtTable = 'mobikwik_token';
                break;
            case 'Amazonpay':
                $mrtTable = 'amazon_pay';
                break;
            
            default:
                $mrtTable = '';
                break;
        }
        
        $mst = ($status == 1) ? 'Active' : 'Off';
    
        $updatemertst = $crud->update("$mrtTable",["status" => "$mst"],"$mrtTableidName = '$mid' AND user_token = '$usertoken'");
       
        if($updatemertst){
            echo json_encode(["status" => 1,"msg"=>"Merchant $mst Successfully."]);
            exit;
        }else{
            echo json_encode(["status" => 5,"msg"=>"Failed to update merchant."]);
            exit;
            
        }
   
}

// update merchant upi details code here
if(isset($_POST["type"]) && $_POST["type"] == 'seennotif'){
        $nid = $_POST["nid"];
        $usertoken = $user["user_token"];
    
        $updatenotif = $crud->update("users",["notif_seen" => "$nid"],"mobile = '$mobile'");
       
        if($updatenotif){
            echo json_encode(["status" => 1,"msg"=>"Success."]);
            exit;
        }else{
            echo json_encode(["status" => 5,"msg"=>"Failed to update UPI id."]);
            exit;
            
        }
   
}


?>

