<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

include('class/bot.php');
include('class/db.php');
include('price.php');
$bot = new bot("7531529065:AAGzkEfsl-VcW-dX-sObJnWkEvXdbgU3xT0", '');

$db = new db();
function redirect($url)
{


    if (!headers_sent()) {
        header('Location: ' . $url);
        exit;
    } else {
        echo '<script type="text/javascript">';
        echo 'window.location.href="' . $url . '";';
        echo '</script>';
        echo '<noscript>';
        echo '<meta http-equiv="refresh" content="0;url=' . $url . '" />';
        echo '</noscript>';
        exit;
    }


}



$a_id = $_GET["id"];
print('$a_id : ' . $a_id);
print("<br />");
$Amount = $_GET["amount"];
print('$Amount : ' . $Amount);
print("<br />");

$package = $_GET["package"];
print('$package : ' . $package);
print("<br />");




//$MerchantID = '3c0abf78-5f32-4c6f-8986-de2298f0addd';
$MerchantID = '9eca98ba-543b-458c-9cdb-6c7969352ffa';




// $sql = "select * from users  where chatid=\"$a_id\"  ";
// $stmt = $db->conn->prepare($sql);
// $stmt->execute();
// $stmt->setFetchMode(PDO::FETCH_ASSOC);
// $getterAdmin = $stmt->fetchAll();
// $numrowfusers = $stmt->rowCount();
// $Amount = $getterAdmin[0]['tmp_price'];

/* if ($getter[0]['type'] == 'VIP 1 month') {
    $Amount = $month_1_toman;
} elseif ($getter[0]['type'] == 'VIP 3 month') {
    $Amount =$month_3_toman;
} else {
    $Amount = $month_12_toman;
} */


// $res = $bot->copyMessage(5803067539, 5803067539, $messageId, $bot->backKey);



$Authority = $_GET['Authority'];

if ($_GET['Status'] == 'OK') {


    $zara["encoding"] = "UTF-8";
    $client = new SoapClient('https://www.zarinpal.com/pg/services/WebGate/wsdl', $zara);


    $zara2["MerchantID"] = $MerchantID;
    $zara2["Authority"] = $Authority;
    $zara2["Amount"] = $Amount;

    $result = $client->PaymentVerification($zara2);

    if ($result->Status == 100) {
        $refid = $result->RefID;
        $nowtime = time();
        // $pkg_type = $getter[0]['type'];


   

//         if($getter[0]['tmp_type'] == 'package'){

//             $sql="select * from package where title='$pkg_type'";
//             $stmt = $db->conn->prepare($sql);
//             $stmt->execute();
//             $stmt->setFetchMode(PDO::FETCH_ASSOC);
//             $getter = $stmt->fetch();
    
//             $days=$getter[0]['time'];
//             if($getterAdmin[0]['expire_time'] > time()){
//                 $previousExTime = intval($getterAdmin[0]['expire_time']);
//                 $expireTime = $previousExTime + (3600 * 24 * $days);
//             }else{
//                 $expireTime = time() + (3600 * 24 * $days);
//             }
            

//             $nowtime = time();
//             $sql = "update users set status=\"active\" ,oneday_alert='no',pay_time=\"$nowtime\",expire_time=\"$expireTime\" where chatid='" . $getter[0]['chatid'] . "' ";
//             $db->conn->exec($sql);

//             // it must be checked 
//             $sql = "insert into payments (chatid, refid, time, crypto, rial) 
//             values ('$a_id', '$refid', '$nowtime', '', '$Amount')";
//             $db->conn->exec($sql);

            
//             $channelId = '-1001979150494';

//             $bot->unBanMember($getter[0]['chatid'], $channelId);

//             print("Getting link");
//             $vipChannel = $bot->getChannelLink($channelId);
//             print("Sending message");

//             $bot->sendmessage(urlencode("
// ✅ پرداخت شما با موفقیت انجام شد
// کد پیگیری : $refid

// لینک ورود به کانال :
// " . $vipChannel . "
// "), $a_id, '');

// print("Done");


//         }
        
       
        

$bot->sendmessage(urlencode("
✅ پرداخت شما با موفقیت انجام شد
کد پیگیری : $refid
"), $a_id, '');


// if($finalPrice == 0){

    switch ($package) {
        case "ayannahan":
            $messageId = 145;
            break;
        case "batobarto":
            $messageId = 146;
            break;
        case "darkhodayam":
            $messageId = 147;
            break;
        case "pardebadr":
            $messageId = 148;
            break;
        case "benametoo":
            $messageId = 149;
            break;
        case "khodabin":
            $messageId = 150;
            break;
        case "aztotato":
            $messageId = 151;
            break;
        case "kolealbum":
            $messageId = 386;
            break;
        case "masihian":
            $messageId =345;
            break;
        case "asharat":
            $messageId =346;
            break;
        case "mashlool":
            $messageId =347;
            break;
        case "ghamoos":
            $messageId =348;
            break;
        case "boodayi":
            $messageId =349;
            break;
        case "yahoodian":
            $messageId =350;
            break;
        case "emam":
            $messageId =351;
            break;
        case "sariejabe":
            $messageId =352;
            break;
        case "sabah":
            $messageId =353;
            break;
        case "roozane":
            $messageId =354;
            break;
        case "sorkhpoostan":
            $messageId =355;
            break;
        case "african":
            $messageId =356;
            break;
        case "sahar":
            $messageId =357;
            break;
        case "shafaeslami":
            $messageId =358;
            break;
        case "shafasorkhpoosti":
            $messageId =359;
            break;
        case "shafamasihi":
            $messageId =360;
            break;
        case "shoghl":
            $messageId =384;
            break;
        case "ezdevaj":
            $messageId =361;
                break; 
        case "yasteshir":
            $messageId =362;
                break; 
        case "safar":
            $messageId =363;
                break;                                                                                             
}


$res = $bot->copyMessage($a_id, 5803067539, $messageId, $bot->backKey);



// }
		
        redirect("https://telegram.me/K1MALEK_bot");
        



    } else {
        var_dump($result);

        $bot->sendmessage(urlencode("
خطا در پرداخت
 "), $a_id, "");


        redirect("https://telegram.me/K1MALEK_bot");


    }
}

?>