<?php ob_start();
require_once '../lib/nusoap.php';
require_once '../config.php';
require_once '../telegram.php';
$telegram = new telegram(TOKEN,HOST,USERNAME,PASSWORD,DBNAME);
if(!isset($_GET['token'])){
    header('location:https://t.me/'.botid);
    exit;
}

$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));

if(isset($input[3])){
    $dcode = htmlspecialchars(strip_tags($input[3]));
    $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'");
	}
}

$token = base64_encode($token); 
$CallbackURL = baseURI."/renew/verify.php?token=".$token;

$MerchantID = ZARINPALMID;  //Required
$Amount = $price; //Amount will be based on Toman  - Required
$Description = DESC. " u$userid |w$woid";  // Required
$Email = ''; // Optional
$Mobile = ''; // Optional


$client = new nusoap_client('https://www.zarinpal.com/pg/services/WebGate/wsdl', 'wsdl');
$client->soap_defencoding = 'UTF-8';
$result = $client->call('PaymentRequest', [
    [
        'MerchantID'     => $MerchantID,
        'Amount'         => $Amount,
        'Description'    => $Description,
        'Email'          => $Email,
        'Mobile'         => $Mobile,
        'CallbackURL'    => $CallbackURL,
    ],
]);

//Redirect to URL You can do it also by creating a form
if ($result['Status'] == 100) {
    $authority = $result['Authority'];
    $url = "https://www.zarinpal.com/pg/StartPay/$authority";
    echo zp_redirect($url);
} else { 
	echo file_get_contents('../html/failed.html');
    //echo'تراکنش با خطا مواجه شد';
}
