<?php ob_start();
require_once 'lib/nusoap.php';
require_once 'config.php';
require_once 'telegram.php';
$telegram = new telegram(TOKEN,HOST,USERNAME,PASSWORD,DBNAME);

$TokenCode = ZIBALID;

if(!isset($_GET['type'])) exit;


$action = isset($_GET['action']) ? $_GET['action'] : 'pay';
$type = $_GET['type'];
$callback_url = baseURI."zibal.php?type=$type&action=verify"; 

$time = time();



if($type == 'code'){
    if($action == 'pay'){
        $token = base64_decode($_GET['token']);  
        $input = explode('.',$token); 
        $userid = intval($input[0]); 
        $fid = intval($input[1]);

        $res = $telegram->db->query("select * from fl_1file where id='$fid'")->fetch(2); 
        $price = $res['price'];
        $seller = $telegram->db->query("select * from fl_sellers where userid=$userid")->fetch(2);
		if(!empty($seller)) $price = $price - (($price) * ($seller['percent'] / 100));

        if(isset($input[2])){
            $dcode = $input[2];
            $dcount = $telegram->db->query("select * from fl_discount WHERE code='$dcode' and active=1 and count > 0 ");
            if($dcount->rowCount() > 0){
                $dres = $dcount->fetch(2);
                $amount = $dres['amount'];
                if($amount <= 100) {
                    $price = $price * (100-$amount)/100;
                }else {
                    $price = $price - $amount ;
                }
                //if($dres['count'] != '') $telegram->db->query("update fl_discount set count= count - 1 WHERE code='$dcode'");
            }
        }
        
        $tkid = time();
        file_put_contents("state/$tkid-token-code.txt", base64_encode($token));
        
        zibal_payment($price, $tkid, $callback_url);
    }elseif($action == 'verify'){
        $reference = intval($_GET['clientrefid']);
        $token = file_get_contents("state/$reference-token-code.txt");
        if(!$token) exit;
        
        $input = explode('.',base64_decode($token)); 
        $userid = intval($input[0]); 
        $fid = intval($input[1]);

        $res = $telegram->db->query("select * from fl_1file where id='$fid'")->fetch(2); 
        $title = $res['title'];
        $price = $res['price'];
        $descr = $res['descr'];
        $seller = $telegram->db->query("select * from fl_sellers where userid=$userid")->fetch(2);
		if(!empty($seller)) $price = $price - (($price) * ($seller['percent'] / 100));
        
        if(isset($input[2])){
            $dcode = $input[2];
            $dcount = $telegram->db->query("select * from fl_discount WHERE code='$dcode' and active=1");
            if($dcount->rowCount() > 0){
                $dres = $dcount->fetch(2);
                $amount = $dres['amount'];
                if($amount <= 100) {
                    $price = $price * (100-$amount)/100;
                }else {
                    $price = $price - $amount ;
                }
                if($dres['count'] != '') $telegram->db->query("update fl_discount set count= count - 1 WHERE code='$dcode'");
            }
        }

        $time = time();
        
        $pay_result = zibal_verify($price, $TokenCode);
        if($pay_result == 'success'){
            unlink("state/$reference-token-code.txt");
            
            $sndmsg = "
خرید کد با درگاه زیبال 
قیمت : $amount تومان
آیدی کاربر : $userid

کد ارسالی به کاربر :
".$respd['text'];
            $telegram->sendMessage($sendchnl,$sndmsg);
            
            $telegram->sendMessage($userid,"
✅پرداخت شما با موفقیت تکمیل شد

Userid: $userid
Plan: $title
Price: $price TOMAN

$descr");
            //echo 'تراکنش با موفقیت انجام شد';
			echo file_get_contents('html/success.html');
        }
        
        
    }
}

if($type == 'renew'){
    if($action == 'pay'){
        $token = base64_decode($_GET['token']);
        $input = explode('#',$token); 
        $userid = intval($input[0]);
        $fid = intval($input[1]);
        $oid = intval($input[2]);

        $order = $telegram->db->query("select * from fl_order where id=$oid")->fetch(2);
        //$fid = $order['fileid'];
        $remark = $order['remark'];
        $server_id = $order['server_id'];
        $inbound_id = $order['inbound_id'];
        $respd = $telegram->db->query("select * from fl_file WHERE id='$fid' and active=1")->fetch(2);
        $name = $respd['title'];
        $days = $respd['days'];
        $volume = $respd['volume'];
        $price = $respd['price'];
        $seller = $telegram->db->query("select * from fl_sellers where userid=$userid")->fetch(2);
		if(!empty($seller)) $price = $price - (($price) * ($seller['percent'] / 100));
  
        $tkid = time();
        file_put_contents("state/$tkid-token-renew.txt", base64_encode($token));
        
        zibal_payment($price, $tkid, $callback_url);
    }elseif($action == 'verify'){
        $reference = intval($_GET['clientrefid']);
        $token = file_get_contents("state/$reference-token-renew.txt");
        if(!$token) exit;
        

        $input = explode('#',base64_decode($token));
        $userid = intval($input[0]);
        $fid = intval($input[1]);
        $oid = intval($input[2]);
        $order = $telegram->db->query("select * from fl_order where id=$oid")->fetch(2); 
        //$fid = $order['fileid'];
        $remark = $order['remark'];
        $server_id = $order['server_id'];
        $inbound_id = $order['inbound_id'];
        $expire_date = $order['expire_date'];
        $expire_date = ($expire_date > $time) ? $expire_date : $time;
        $respd = $telegram->db->query("select * from fl_file WHERE id='$fid' and active=1")->fetch(2);
        $name = $respd['title'];
        $days = $respd['days'];
        $volume = $respd['volume'];
        $price = $respd['price'];
        $seller = $telegram->db->query("select * from fl_sellers where userid=$userid")->fetch(2);
		if(!empty($seller)) $price = $price - (($price) * ($seller['percent'] / 100));
        
        $pay_result = zibal_verify($price, $TokenCode);
        if($pay_result == 'success'){
            unlink("state/$reference-token-renew.txt");
    
            require_once('vray.php');
            if($inbound_id > 0)
                $response = renew_client($server_id, $inbound_id, $remark, $volume, $days);
            else
                $response = renew_inbound($server_id, $remark, $volume, $days);
            if($response->success){
                $telegram->db->query("update fl_order set expire_date= $expire_date + $days * 86400,notif=0 where id='$oid'"); 
                $telegram->db->query("INSERT INTO `fl_extra_order` VALUES (NULL, '$userid', '$server_id', '$inbound_id', '$remark', '$price', '$time');");
                //echo 'تراکنش با موفقیت انجام شد';
				echo file_get_contents('html/success.html');
                //$msg = "✅ سفارش شما با موفقیت ثبت شد";
                //$telegram->sendMessage($userid, $msg);
                $telegram->sendMessage($userid, "✅سرویس $remark با موفقیت تمدید شد");
                $sndmsg = "
تمدید سرویس $remark با زیبال
قیمت : $price
آیدی کاربر : $userid
آیدی سرور : $server_id
تعداد روز  $days
تعداد گیگ $volume
";
            $telegram->sendMessage($sendchnl,$sndmsg);
                // pay referer
                $userReferer = $telegram->db->query("select * from fl_subuser where userid=$userid");
                if($userReferer->rowCount() ){
					$ures = $userReferer->fetch(2);
					$userToplevel = $ures['toplevel_userid'];
					$ufname = $ures['fname'];
					$amount = $pursant > 100 ? $pursant : ($price) * ($pursant / 100);
					$telegram->db->query("update fl_user set wallet= wallet + $amount WHERE userid=$userToplevel");
					if($pursant > 100) $msg = "💟کاربر {$ufname} یک خرید به مبلغ  $price تومان انجام داد و $amount تومان به عنوان هدیه به کیف پول شما اضافه شد👍";
					else $msg = "💟کاربر {$ufname} یک خرید به مبلغ  $price تومان انجام داد و $pursant درصد آن یعنی $amount تومان به کیف پول شما اضافه شد👍";
					$telegram->sendMessage($userToplevel, $msg); 
				}
                

            }else {
                die("مشکل فنی در ارتباط با سرور. لطفا به مدیریت اطلاع بدید");
            }
        }
        
        
    }
}

if($type == 'day'){
    if($action == 'pay'){
        $token = base64_decode($_GET['token']);
        $input = explode('#',$token); 
        $server_id = intval($input[0]);
        $inbound_id = intval($input[1]);
        $remark = htmlspecialchars(strip_tags($input[2]));
        $planid = intval($input[3]);
        $userid = intval($input[4]); 
        $res = $telegram->db->query("select * from extra_day where id=$planid")->fetch(2);  
        $price = $res['price'];
        $seller = $telegram->db->query("select * from fl_sellers where userid=$userid")->fetch(2);
		if(!empty($seller)) $price = $price - (($price) * ($seller['percent'] / 100));

        $volume = $res['volume'];
        
        $tkid = time();
        file_put_contents("state/$tkid-token-day.txt", base64_encode($token));
        
        zibal_payment($price, $tkid, $callback_url);
    }elseif($action == 'verify'){
        $reference = intval($_GET['clientrefid']);
        $token = file_get_contents("state/$reference-token-day.txt");
        if(!$token) exit;
        
        $input = explode('#',base64_decode($token));
        $server_id = intval($input[0]);
        $inbound_id = intval($input[1]);
        $remark = htmlspecialchars(strip_tags($input[2]));
        $planid = intval($input[3]);
        $userid = intval($input[4]); 
        $res = $telegram->db->query("select * from extra_day where id=$planid")->fetch(2);  
        $volume = $res['volume'];
        $price = $res['price'];
        $seller = $telegram->db->query("select * from fl_sellers where userid=$userid")->fetch(2);
		if(!empty($seller)) $price = $price - (($price) * ($seller['percent'] / 100));

        $time = time();
        
        $pay_result = zibal_verify($price, $TokenCode);
        if($pay_result == 'success'){
            unlink("state/$reference-token-day.txt");
    
            require_once('vray.php');
            if($inbound_id > 0)
                $response = update_client_traffic($server_id, $inbound_id, $remark, 0, $volume);
            else
                $response = update_inbound_traffic($server_id, $remark, 0, $volume);
            if($response->success){
                $telegram->db->query("update fl_order set expire_date= expire_date + $volume * 86400,notif=0 where remark='$remark'");
                $telegram->db->query("INSERT INTO `fl_extra_order` VALUES (NULL, '$userid', '$server_id', '$inbound_id', '$remark', '$price', '$time');");
                $telegram->sendMessage($userid, "✅$volume روز به مدت زمان سرویس شما اضافه شد");
                //echo 'تراکنش با موفقیت انجام شد';
				
				echo file_get_contents('html/success.html');
                //$msg = "✅ سفارش شما با موفقیت ثبت شد";
                //$telegram->sendMessage($userid, $msg);
                $sndmsg = "
خرید $volume روز با درگاه زیبال
قیمت : $price
آیدی کاربر : $userid
آیدی سرور : $server_id
ریمارک : $remark
";
        $telegram->sendMessage($sendchnl,$sndmsg);

            }else {
                die("مشکل فنی در ارتباط با سرور. لطفا به مدیریت اطلاع بدید");
            }
        }
        
        
    }
}

if($type == 'volume'){
    if($action == 'pay'){
        $token = base64_decode($_GET['token']);
        $input = explode('#',$token); 
        $server_id = intval($input[0]);
        $inbound_id = intval($input[1]);
        $remark = htmlspecialchars(strip_tags($input[2]));
        $planid = intval($input[3]);
        $userid = intval($input[4]); 
        $res = $telegram->db->query("select * from extra_plan where id=$planid")->fetch(2);  
        $price = $res['price'];
        $volume = $res['volume'];
        $seller = $telegram->db->query("select * from fl_sellers where userid=$userid")->fetch(2);
		if(!empty($seller)) $price = $price - (($price) * ($seller['percent'] / 100));

        $tkid = time();
        file_put_contents("state/$tkid-token-volume.txt", base64_encode($token));
        
        
        zibal_payment($price, $tkid, $callback_url);
    }elseif($action == 'verify'){
        $reference = intval($_GET['clientrefid']);
        $token = file_get_contents("state/$reference-token-volume.txt"); 
        if(!$token) exit;
        
        $input = explode('#',base64_decode($token));
        $server_id = intval($input[0]);
        $inbound_id = intval($input[1]);
        $remark = htmlspecialchars(strip_tags($input[2]));
        $planid = intval($input[3]);
        $userid = intval($input[4]); 
        $res = $telegram->db->query("select * from extra_plan where id=$planid")->fetch(2);  
        $price = $res['price'];
        $seller = $telegram->db->query("select * from fl_sellers where userid=$userid")->fetch(2);
		if(!empty($seller)) $price = $price - (($price) * ($seller['percent'] / 100));
    
        $volume = $res['volume'];
        $time = time();

        $pay_result = zibal_verify($price, $TokenCode);
        if($pay_result == 'success'){
            unlink("state/$reference-token-volume.txt");
    
            require_once('vray.php');
            if($inbound_id > 0)
                $response = update_client_traffic($server_id, $inbound_id, $remark, $volume, 0);
            else
                $response = update_inbound_traffic($server_id, $remark, $volume, 0);
            if($response->success){
                $telegram->db->query("update fl_order set notif=0 where remark='$remark'");
                $telegram->db->query("INSERT INTO `fl_extra_order` VALUES (NULL, '$userid', '$server_id', '$inbound_id', '$remark', '$price', '$time');");
                $telegram->sendMessage($userid, "✅$volume گیگ به حجم سرویس شما اضافه شد");
                //echo 'تراکنش با موفقیت انجام شد';
				
				echo file_get_contents('html/success.html');
                //$msg = "✅ سفارش شما با موفقیت ثبت شد";
                //$telegram->sendMessage($userid, $msg);
                $sndmsg = "
خرید $volume گیگ با درگاه زیبال
قیمت : $price
آیدی کاربر : $userid
آیدی سرور : $server_id
ریمارک : $remark
";
          $telegram->sendMessage($sendchnl,$sndmsg);
                
            }else {
                die("مشکل فنی در ارتباط با سرور. لطفا به مدیریت اطلاع بدید");
            }
        }
        
        
    }
}


if($type == 'buy'){
    if($action == 'pay'){
        if(!isset($_GET['token'])){
            header('location:https://t.me/'.botid);
            exit;
        }
        
        $token = base64_decode($_GET['token']); 
        $token = explode('.',$token);
        $userid = intval($token[0]);
        $fid = intval($token[1]);
        if($token[2]) $dcode = htmlspecialchars(strip_tags($token[2]));
        
        $file_detail = $telegram->db->query("select * from fl_file WHERE id=$fid")->fetch(2); 
        $date = time();
        $days = $file_detail['days'];
        $expire_date = $date + (86400 * $days);
        $protocol = $file_detail['protocol'];
        $price = $file_detail['price'];
        
        $seller = $telegram->db->query("select * from fl_sellers where userid=$userid")->fetch(2);
		if(!empty($seller)) $price = $price - (($price) * ($seller['percent'] / 100));
		
        $server_id = $file_detail['server_id'];
        $acount = $file_detail['acount'];
        $inbound_id = $file_detail['inbound_id'];
        $ctype = $file_detail['ctype'];
        
        if($dcode){
            $dcount = $telegram->db->query("select * from fl_discount WHERE code='$dcode' and active=1");
            if($dcount->rowCount() > 0){
                $amount = $dcount->fetch(2)['amount'];
                if($amount <= 100) {
                    $price = $price * (100-$amount)/100;
                }else {
                    $price = $price - $amount ;
                }
            }
        }
            
        if($acount == 0 and $inbound_id != 0){
            echo 'ظرفیت این کانکشن پر شده است';
            exit;
        }
        $order = $telegram->db->query("INSERT INTO `fl_order` VALUES (NULL,  {$userid}, '', {$fid}, $server_id, $inbound_id, '', '$protocol', $expire_date, '', '', $price,0, '$date', 0);");
        $order_id = $telegram->db->lastInsertId();
        zibal_payment($price, $order_id, $callback_url);
    }elseif($action == 'verify'){
        
        $order_id = intval($_GET['clientrefid']);
        if(!$order_id) exit;
        
        $orderDetails = $telegram->db->query("select * from fl_order where id={$order_id} and status=0")->fetch(2);
        if(empty($orderDetails)) die('این سفارش قبلا تایید شده است');
        $userid = $orderDetails['userid'];
        $fid = $orderDetails['fileid'];
        $file_detail = $telegram->db->query("select * from fl_file WHERE id=$fid")->fetch(2);
        $days = $file_detail['days'];
        $date = time();
        $expire_microdate = intval(floor(microtime(true) * 1000) + (864000 * $days * 100));
        $expire_date = $date + (86400 * $days);
        $type = $file_detail['type'];
        $volume = $file_detail['volume'];
        $protocol = $file_detail['protocol'];
        $price = $file_detail['price'];
        $seller = $telegram->db->query("select * from fl_sellers where userid=$userid")->fetch(2);
		if(!empty($seller)) $price = $price - (($price) * ($seller['percent'] / 100));
        $server_id = $file_detail['server_id'];
        $netType = $file_detail['type'];
        $acount = $file_detail['acount'];
        $inbound_id = $file_detail['inbound_id'];
        $limitip = $file_detail['limitip'];
        
        $price = $orderDetails['amount'];
        
        $pay_result = zibal_verify($price, $TokenCode);
        if($pay_result == 'success'){
            
            // V2ray Api
            require_once('vray.php');
            $uniqid = generateRandomString(42,$protocol); 
        
            $savedinfo = file_get_contents('savedinfo.txt');
            $savedinfo = explode('-',$savedinfo);
            $port = $savedinfo[0] + 1;
            $last_num = $savedinfo[1] + 1;
        
            $srv_remark = $telegram->db->query("select * from fl_server WHERE id=$server_id")->fetch(2)['remark'];
			$uremark = $telegram->db->query("select * from fl_remark where userid='$userid'")->fetch(2);
			if($uremark) $uremark = $uremark['remark']; else $uremark = $srv_remark;
			$remark = "{$uremark}-{$last_num}";
        
            file_put_contents('savedinfo.txt',$port.'-'.$last_num);
            //$telegram->sendMessage($cuserid,"$server_id, $uniqid, $inbound_id, $expire_microdate, $remark, $volume");exit;
            
            if($inbound_id == 0){    
                $response = add_inbound($server_id, $uniqid, $protocol, $port, $expire_microdate, $remark, $volume, $netType); 
                if(! $response->success){
                    // run again
                    $response = add_inbound($server_id, $uniqid, $protocol, $port, $expire_microdate, $remark, $volume, $netType);
                } 
            }else {
                $response = add_inbount_client($server_id, $uniqid, $inbound_id, $expire_microdate, $remark, $volume, $limitip); 
                if(! $response->success){
                    // run again
                    $response = add_inbount_client($server_id, $uniqid, $inbound_id, $expire_microdate, $remark, $volume, $limitip);
                } 
            }
            
            //$telegram->sendMessage($cuserid,json_encode($response));exit;
             
            $vray_link = genLink($server_id, $uniqid, $protocol, $remark, $port, $netType, $inbound_id);
            $addsub = "";
            /*if($gateways['sublink'] == 1){
            	$subid = getsubid($server_id, $inbound_id, $remark);
            	if($subid != '' and !is_null($subid) and $subid != '0'){
            		$server_detail = $telegram->db->query("SELECT * FROM server_info WHERE id=$server_id")->fetch(2);
            		$pnlurl = $server_detail['sublink'];
            		$sublink = $pnlurl."/sub/$subid";
            		$addsub = "\n\n (لینک سابسکریپشن با امکان نمایش حجم و تاریخ انقضا و بروز رسانی لینک ) \n $sublink \n";
            	}
            }*/
        	$token = generateRandomString2(16);
        	$getLink_url = baseURI."sub.php?token=".$token;
        	if($gateways['sublink'] == 1){
        		$addsub = "\n\n 🔗 لینک هوشمند سرویس شما: \n <code>$getLink_url</code>";
        	}
            $acc_text = "🔗 $remark \n <code>$vray_link</code> $addsub" . " \n  برای کپی کردن لینک روی آن کلیک کنید \n";

            include 'phpqrcode/qrlib.php';
            $path = 'images/';
            $file = $path.$userid.".png"; //unlink($file);
            $ecc = 'L';
            $pixel_Size = 10;
            $frame_Size = 10;
            QRcode::png($vray_link, $file, $ecc, $pixel_Size, $frame_size);
        	addBorderImage($file);
            //$fileLink = "<a href='".baseURI.$file."'>&#8194;</a>";
        	//$telegram->sendHTML($cuserid,$acc_text.$fileLink,$finalop);
        	$telegram->sendPhoto($userid,$acc_text,$file);
        
        	$order = $telegram->db->query("update `fl_order` set link='$vray_link',remark='$remark',status=1,token='$token' where id=$order_id");
        	
        	if($inbound_id == 0) {
                $server_info = $telegram->db->query("SELECT * FROM fl_server WHERE id=$server_id")->fetch(2);
                if($server_info['ucount'] != 0)
                    $telegram->db->query("UPDATE `fl_server` SET `ucount` = ucount - 1 WHERE id=$server_id");
            }else{
                if($acount != 0) 
                    $telegram->db->query("UPDATE `fl_file` SET `acount` = acount - 1 WHERE id=$fid");
            }
            
            $msg = "✅سفارش شما با موفقیت ثبت شد";
            echo $msg;
            $telegram->db->query("update fl_order set status=1 where id='$order_id'");
            $telegram->sendMessage($userid, $msg);
            // pay referer
            $userReferer = $telegram->db->query("select * from fl_subuser where userid=$userid");
            if($userReferer->rowCount() ){
                $ures = $userReferer->fetch(2);
                $userToplevel = $ures['toplevel_userid'];
                $ufname = $ures['fname'];
                $amount = ($price) * ($pursant / 100);
                $telegram->db->query("update fl_user set wallet= wallet + $amount WHERE userid=$userToplevel");
                $telegram->sendMessage($userToplevel, "💟کاربر {$ufname} یک خرید به مبلغ  $price تومان انجام داد و $pursant درصد آن یعنی $amount تومان به کیف پول شما اضافه شد👍"); 
            }
    $sndmsg = "
خرید درگاه زیبال 
قیمت : $price
آیدی کاربر : $userid
آیدی سرور : $server_id
لینک دریافتی :

$vray_link
";
    $telegram->sendMessage($sendchnl,$sndmsg);
   

        }
    } // verify
}


if($type == 'wallet'){
    if($action == 'pay'){
        $amount = intval($_GET['amount']);// echo $amount;die;
        //if($amount < $min_wallet_charge) die('مبلغ کمتر از 2هزار تومان است');
        $userid = intval($_GET['userid']);
        $result = $telegram->db->query("select * from fl_user WHERE userid='$userid'")->rowCount();
        if($result == 0) die('اطلاعات ارسال اشتباه است');
        $telegram->db->query("INSERT INTO `fl_wallet` VALUES (NULL, '',$amount, $userid, '','0','$time');");
        $woid = $telegram->db->lastInsertId();
        zibal_payment($amount, $woid, $callback_url."&woid=$woid");
    }elseif($action == 'verify'){
        $woid = intval($_GET['clientrefid']);
        if(!$woid) exit;
        
        $res = $telegram->db->query("select * from fl_wallet where id='$woid'")->fetch(2);
        $amount = $res['amount'];
        $userid = $res['userid'];
        $transid = $res['trans_id'];
        if($_GET['trackId'] != $transid) exit;
        
        $pay_result = zibal_verify($amount);
        if($pay_result == 'success'){
            //echo '<h1 style="text-align:right;color:green;">کیف پول شما با موفقیت شارژ شد</h1>' ; 
			echo file_get_contents('html/success.html');
            $telegram->db->query("update fl_user set wallet= wallet + $amount where userid='$userid'");
            $telegram->db->query("update fl_wallet set status=1 where id=$woid");
            $msg = "✅ کیف پول شما به مبلغ $amount تومان شارژ شد ";
            $telegram->sendMessage($userid, $msg);
            $sndmsg = "
افزایش موجودی با درگاه زیبال
مقدار : $amount تومان
آیدی کاربر : $userid
";
    $telegram->sendMessage($sendchnl,$sndmsg);
        }else{
            //echo 'خطا در تایید تراکنش';
			echo file_get_contents('html/failed.html');
        }
    }
}


function zibal_payment($price, $woid, $callback_url){
    global $TokenCode, $telegram;
    $parameters = array(
        "merchant"=> $TokenCode,//required
        "callbackUrl"=> $callback_url."&clientrefid=$woid",//required
        "amount"=> $price * 10,//required
        "description" => DESC,
        //"orderId"=> time(),//optional
        //"mobile"=> "09120000000",//optional for mpg
    );
    //var_dump($parameters);die;
    $response = postToZibal('request', $parameters);
    //var_dump($response);
    if ($response->result == 100)
    {
        $trackid = $response->trackId;
        $telegram->db->query("UPDATE `fl_wallet` set trans_id='$trackid' where id=$woid");
        $startGateWayUrl = "https://gateway.zibal.ir/start/$trackid";
        header('location: '.$startGateWayUrl);die;
    }
    else{
		echo file_get_contents('html/failed.html');
        echo "errorCode: ".$response->result."<br>";
        echo "message: ".$response->message;
    }

}

function zibal_verify($price){
    global $TokenCode;
    if($_GET['success']==1) {
       // echo "شناسه سفارش: ".$_GET['orderId']."<br>";
    
        //start verfication
        $parameters = array(
            "merchant" => $TokenCode,//required
            "trackId" => $_GET['trackId'],//required
    
        );
    
        $response = postToZibal('verify', $parameters);
    
        if ($response->result == 100) { return 'success';
            //echo "<pre>";//for pretty view :)
            //var_dump($response);
            //update database or something else
        } else {
			echo file_get_contents('html/failed.html');
            echo "result: " . $response->result . "<br>";
            echo "message: " . $response->message;
        }
    }else{
		echo file_get_contents('html/failed.html');
        //echo "پرداخت با شکست مواجه شد.";
    }

}

function postToZibal($path, $parameters)
{
    $url = 'https://gateway.zibal.ir/v1/'.$path;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($parameters));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response  = curl_exec($ch);
    curl_close($ch);
    return json_decode($response);
}


