<?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("5842954272:AAEQHABFRsKIyH2icWrA9hQp4yROF0XHoww", '');

$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"];
$a_back = $_GET["back"];
$MerchantID = '3c0abf78-5f32-4c6f-8986-de2298f0addd';




$sql = "select * from users where chatid=\"$a_id\"  ";
$stmt = $db->conn->prepare($sql);
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$getter = $stmt->fetchAll();
$numrowfusers = $stmt->rowCount();

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;
}




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


        if ($getter[0]['type'] == 'VIP 1 month') {
            $expireTime = time() + (3600 * 24 * 30);
        } elseif ($getter[0]['type'] == 'VIP 3 month') {
            $expireTime = time() + (3600 * 24 * 90);
        } elseif ($getter[0]['type'] == 'VIP 6 month') {
            $expireTime = time() + (3600 * 24 * 180);
        }



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


        $channelId = '-1001601998241';
        $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");

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



    } else {
        var_dump($result);

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


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


    }
}

?>