<?php
date_Default_timezone_set('Asia/Tashkent');
define('API_KEY','Bot_tokeni');

$admin = 123456789; //admin IDsi
$api_key = 'kalit'; //Do'kon kaliti

$sana = date("d.m.Y");
$soat = date("H:i");

define("DB_SERVER", "localhost"); 
define("DB_USERNAME", "example_user"); //mysql login
define("DB_PASSWORD", 'example_pass'); //mysql parol
define("DB_NAME", "example_user"); //mysql login

$connect = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
mysqli_set_charset($connect,"utf8mb4");

if (!$connect) {
    die("DB ulanishda xato: " . mysqli_connect_error());
}

mysqli_query($connect,"CREATE TABLE IF NOT EXISTS `users` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`user_id` TEXT,
`balance` TEXT DEFAULT 0,
`date` TEXT DEFAULT '$soat $sana'
);");

mysqli_query($connect,"CREATE TABLE IF NOT EXISTS `funds` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`user_id` TEXT,
`payment_id` TEXT,
`amount` TEXT,
`fee` TEXT,
`status` TEXT DEFAULT 'pending',
`time` TEXT DEFAULT 15,
`date` TEXT DEFAULT '$soat $sana'
);");

function bot($method,$datas=[]){
$ch = curl_init("https://api.telegram.org/bot". API_KEY ."/". $method);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$datas);
$res = curl_exec($ch);
return json_decode($res);
}

function send($id, $tx, $m=null){
return bot('sendMessage', [
'chat_id' => $id,
'text' => "<b>$tx</b>",
'parse_mode' => "HTML",
'reply_markup' => $m,
'disable_web_page_preview' => true
]);
}

$update = json_decode(file_get_contents('php://input'));
$message = $update->message;
$callback = $update->callback_query;
$data = $callback->data;
$bot = bot('getme',['bot'])->result->username;

$cid = $message->chat->id;
$text = $message->text;
$name = $message->from->first_name;
$mid = $message->message_id;
$cid2 = $update->callback_query->message->chat->id;
$mid2 = $update->callback_query->message->message_id;
$qid  = $callback->id;

$step = file_get_contents("step/$cid.step");

$menu=json_encode([
'resize_keyboard'=>true,
'keyboard'=>[
[['text'=>"🤖 Bot"],['text'=>"💳 Hisobni to'ldirish"]],
]]);

if($text == "/start" or $text == "⏪ Orqaga"){
unlink("step/$cid.step");
send($cid,"🔥 @$bot ga xush kelibsiz!

🤝 @GoldHamyonBot orqali avtomatik to'lov tekshirilmoqda.",$m);
exit();
}

if($text == "💳 Hisobni to'ldirish"){
send($cid,"💵 Hisobingizni necha so'mga to'ldirmoqchisiz?
📰 Minimal miqdor: 1000 so'm",$back);
file_put_contents("step/$cid.step","add_funds");
}

if ($step == "add_funds") {
if(!is_numeric($text)){
send($cid,"🔢 Faqat raqamlardan foydalaning!",$back);
exit;
}
$amount = intval($text);
if ($amount >= 1000 && $amount <= 1000000) {
$fee = rand(1,100); //Komissiya
$amount = $amount + $fee;
$ch = curl_init("https://payment.api-link.uz/create");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(['api_key' => $payment_key,'amount' => $amount]));
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response, true);
if ($response['success'] === 'false') {
send($cid,"⚠️ Qaytadan urinib ko'ring");
exit;
}
$payment_id = $response['payment_id'];
$card = $response['card'];
$card_holder = $response['card_holder'];
$cancel = json_encode([
'inline_keyboard' => [
[['text' => "❌ Bekor qilish", 'callback_data' => "cancel-$payment_id"]],
]
]);
mysqli_query($connect,"INSERT INTO funds (user_id,payment_id,amount,fee) VALUES ('$cid','$payment_id','$amount','$fee')");
send($cid,"💳 To'lov ma'lumotlari:\n
➡️ To'lov kartasi: <code>$card_number</code>
👤 Karta egasi: $card_holder
💵 Miqdor: <code>$amount</code> so'm

👉 <code>$amount</code> so'mdan ortiq yoki kam to'lov qilmang!",$cancel);
}else{
send($cid,"⚠️ Minimal: 1000 so'm, Maksimal: 1000000 so'm",$back);
}
}

if (mb_stripos($data, "cancel-") !== false) {
$id = explode("-", $data)[1];
mysqli_query($connect, "UPDATE funds SET status='cancelled' WHERE payment_id='$id'");
del();
send($cid2,"❌ To'lov bekor qilindi!");
}

if (!isset($_GET['update']) || $_GET['update'] !== 'fund') {
die('Ruxsat yo\'q');
} else {

$stmt = $connect->prepare("SELECT id, user_id, payment_id, amount, fee, time FROM funds WHERE status = 'pending'");
$stmt->execute();
$result = $stmt->get_result();

while ($row = $result->fetch_assoc()) {
$id         = $row['id'];
$user_id    = $row['user_id'];
$payment_id   = $row['payment_id'];
$amount     = (int)$row['amount'];
$fee     = (int)$row['fee'];
$time  = (int)$row['time'];
$api_url = "https://payment.api-link.uz/check?payment_id=$payment_id";
$response = @file_get_contents($api_url);
if ($response === false) {
continue;
}
$data = json_decode($response, true);
if (!$data || !isset($data['status'])) {
continue;
}
if ($data['success'] == 'true' && isset($data['status'])) {
$payment_status = $data['status'];
if ($payment_status === 'paid') {
$plus = $amount-$fee;
$update_balance = $connect->prepare("UPDATE users SET balance = balance + ? WHERE user_id = ?");
$update_balance->bind_param("is", $plus, $user_id);
$update_balance->execute();
$update_balance->close();
$update_fund = $connect->prepare("UPDATE funds SET status = 'paid' WHERE id = ?");
$update_fund->bind_param("i", $id);
$update_fund->execute();
$update_fund->close();
send($user_id, "<b><tg-emoji emoji-id='5823396554345549784'>✔️</tg-emoji> To'lov qabul qilindi\n
<tg-emoji emoji-id='5854776233950188167'>🏷</tg-emoji> +$plus so'm</b>");
$user = json_encode([
'inline_keyboard' => [
[['text' =>"👤 Foydalanuvchi", 'url' => "tg://user?id=$user_id"]],
]]);
send($admin, "<b><tg-emoji emoji-id='5823396554345549784'>✔️</tg-emoji> To'lov qabul qilindi\n
<tg-emoji emoji-id='5854776233950188167'>🏷</tg-emoji> +$plus so'm</b>",$user);
} else {
$new_over_time = $time - 1;
if ($new_over_time <= 0) {
$update_cancel = $connect->prepare("UPDATE funds SET status = 'cancelled', time = 0 WHERE id = ?");
$update_cancel->bind_param("i", $id);
$update_cancel->execute();
$update_cancel->close();
} else {
$update_time = $connect->prepare("UPDATE funds SET time = ? WHERE id = ?");
$update_time->bind_param("ii", $new_over_time, $id);
$update_time->execute();
$update_time->close();
}}}}
$stmt->close();
}

