<?php
include '../config.php';
define('API_KEY',$API_KC);
//----------------------------------------------
function factweb($method,$datas=[]){
    $url = "https://api.telegram.org/bot".API_KEY."/".$method;
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$datas);
    $res = curl_exec($ch);
    if(curl_error($ch)){
        var_dump(curl_error($ch));
    }else{ 
        return json_decode($res);
    }
} 
$settings = mysqli_fetch_assoc(mysqli_query($connect,"SELECT * FROM settings WHERE botid = '$botid' LIMIT 1"));
$sub1 = $settings["sub1"];
$sub1_parts = explode('^', $sub1);
$subp1=$sub1_parts[3];
$subday1=$sub1_parts[2];

$sub2 = $settings["sub2"];
$sub2_parts = explode('^', $sub2);
$subp2=$sub2_parts[3];
$subday2=$sub2_parts[2];

$sub3 = $settings["sub3"];
$sub3_parts = explode('^', $sub3);
$subp3=$sub3_parts[3];
$subday3=$sub3_parts[2];

// ------- Get -------
$Authority = $_GET['Authority'];
$user = $_GET['id'];
$amount = $_GET['amount'];
$data = array("merchant_id" => "$MerchantID", "authority" => $Authority, "amount" => $amount);
$jsonData = json_encode($data);
$ch = curl_init('https://api.zarinpal.com/pg/v4/payment/verify.json');
curl_setopt($ch, CURLOPT_USERAGENT, 'ZarinPal Rest Api v4');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($jsonData)
));

$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result, true);
if ($err) {
    echo "cURL Error #:" . $err;
} else {
    if ($result['data']['code'] == 100) {
$datenoww=date('Y-m-d');   
if($amount == $subp1){
$new_expiry_date = date('Y-m-d', strtotime('+' . $subday1 . ' days'));
$sql_new = "UPDATE `user` SET vip='yes', viptime='$new_expiry_date' WHERE id=$user";    
mysqli_query($connect,$sql_new);      
}  
if($amount == $subp2){
$new_expiry_date = date('Y-m-d', strtotime('+' . $subday2 . ' days'));
$sql_new = "UPDATE `user` SET vip='yes', viptime='$new_expiry_date' WHERE id=$user";    
mysqli_query($connect,$sql_new);      
}  
if($amount == $subp3){
$new_expiry_date = date('Y-m-d', strtotime('+' . $subday3 . ' days'));
$sql_new = "UPDATE `user` SET vip='yes', viptime='$new_expiry_date' WHERE id=$user";    
mysqli_query($connect,$sql_new);      
}  
factweb('sendMessage',[
                        'chat_id'=>$user,
                        'text'=>"✅ پرداخت شما با موفقیت انجام و اشتراک فعال شد",
                        'parse_mode'=>"HTML",
                        'reply_markup'=>json_encode([
                        'inline_keyboard'=>[
                        [
                            [ 'text' => " 💳 مبلغ پرداختی"   , 'callback_data' => "none" ] ,
                            [ 'text' => "$amount تومان"   , 'callback_data' => "none" ]
                        ],
                        ]
                        ])
                        ]);
                        
    factweb('sendMessage', [
        'chat_id' => $admins[0],
        'text' => "✅ پرداخت جدید در ربات انجام شد",
        'parse_mode' => "HTML",
        'reply_markup' => json_encode([
            'inline_keyboard' => [
                [
                    ['text' => "💳 مبلغ پرداختی", 'callback_data' => "none"],
                    ['text' => "$amount تومان", 'callback_data' => "none"]
                ],
                [
                    ['text' => "👤 شناسه کاربر", 'callback_data' => "none"],
                    [ 'text' => "$user"   , 'url' => "tg://openmessage?user_id=$user" ]

                ]
            ]
        ])
    ]);
    
echo 'پرداخت انجام شد';
exit();	         
   } else {
        
      factweb('sendMessage',[
                        'chat_id'=>$user,
                        'text'=>"❌ پرداخت شما انجام نشد",
                        'parse_mode'=>"HTML",
                        ]);
    echo 'پرداخت انجام نشد';
die();   
        
    }
}

?>