<?php

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

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


function sendmessage($text, $chatid, $keys)
{
    $send = "https://api.telegram.org/bot7048752048:AAF1ULWXCcQGPeBsj5sUb9vF5pRus4WkbuA/sendmessage?chat_id=$chatid&text=$text&parse_mode=Html&reply_markup=$keys";
    $c = curl_init($send);
    curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
    //curl_setopt(... other options you want...)
    curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
    //curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
    $sended = curl_exec($c);
    if (curl_error($c)) {
        die(curl_error($c));
    }
    // Get the status code
    $status = curl_getinfo($c, CURLINFO_HTTP_CODE);
    curl_close($c);
    $decodeData = json_decode($sended, true);
    $id_message = $decodeData['result']['message_id'];
    print $sended;
    return $id_message;
}



function connectd()
{
    include("asl.php");
    $conn = new PDO("mysql:host=$servername;dbname=$mydb", $username, $password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8mb4"));
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    return $conn;
}
$conn = connectd();



$a_chid = $_GET["chid"];
$a_am = $_GET["am"];

$MerchantID = 'ea1e96f2-3fbe-11e6-b6c1-000c295eb8fc';


$sql = "select * from user where chatid ='" . $a_chid . "' ";
$stmt = $conn->prepare($sql);
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$getter = $stmt->fetchAll();
$numrowfusers = $stmt->rowCount();





$Amount = $a_am;


$sql = "select * from packages";
$stmt = $conn->prepare($sql);
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$getterPackage = $stmt->fetch();
$numrowfusers = $stmt->rowCount();


$one_month = $getterPackage['1_month'];
$six_month = $getterPackage['6_month'];
$twelve_month = $getterPackage['12_month'];

if ($Amount == $one_month) {
    $temp_package = '1_month';
} elseif ($Amount == $six_month) {
    $temp_package = '6_month';
} elseif ($Amount == $twelve_month) {
    $temp_package = '12_month';
}





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



// use 1_month keyword for three month package
        if ($temp_package == '1_month') {
            $expire_time = time() + (91 * 24 * 3600);
        } elseif ($temp_package == '6_month') {
            $expire_time = time() + (180 * 24 * 3600);
        } elseif ($temp_package == '12_month') {
            $expire_time = time() + (365 * 24 * 3600);
        }

        $sql = "update user set expire_time=\"$expire_time\" , price=\"$Amount\" , package=\"$temp_package\"  where chatid='" . $a_chid . "' ";
        $conn->exec($sql);

        $sql = "insert into payments (chatid,amount,refid,time) values (\"$a_chid\",\"$Amount\",\"$refid\",'".time()."')";
        $conn->exec($sql);





        sendmessage(urlencode("
✅ پرداخت شما با موفقت انجام شد

کد پیگیری : $refid


"), $a_chid, '');



        redirect("https://telegram.me/zaeimtestbot");
    } else {
        var_dump($result);

        sendmessage(urlencode("
خطا در پرداخت
 "), $a_chid, "");


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