<?php
$BOT_TOKEN = "7679448689:AAEbXyy-G_YL90X-JGXr8mo5yNlPCLtxadE";
define("BOT_TOKEN",$BOT_TOKEN);
require_once __DIR__.'/conn.php';
require_once __DIR__.'/functions.php';
$data = $_POST['data'] ?? null;
if (!$data) {

    exit("No data received\n");
}
$payload = json_decode($data, true);
if (!$payload || !isset($payload['order_id'], $payload['phone'])) {

}

$orderId = $payload['order_id'];
$userId  = $payload['phone'];
$amount = $payload['amount'];
$x = handle_deposit_paid($pdo,$userId,$orderId,"w","w","w",$amount);
$i =1;

echo "SUCCESS";
function sendAlert($text, $id)
{
    $url ="https://api.telegram.org/bot" .BOT_TOKEN ."/answerCallbackQuery?callback_query_id={$id}&text=" .
        urlencode($text) .
        "&show_alert=true";
    $ch = curl_init($url);
    

    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

function sendMessage($chat_id, $text, $inline = null, $parse = "Markdown")
{
    $params = [
        "chat_id" => $chat_id,
        "text" => $text,
        "disable_web_page_preview" => true
    ];

    if (!empty($inline)) {
        $params["reply_markup"] = json_encode($inline, JSON_UNESCAPED_UNICODE);
    }

    if (!empty($parse)) {
        $params["parse_mode"] = $parse; // HTML or MarkdownV2
    }

    $url = "https://api.telegram.org/bot" .BOT_TOKEN . "/sendMessage";

    $ch = curl_init();
    curl_setopt_array($ch, [
        CURLOPT_URL => $url,
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => $params,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_CONNECTTIMEOUT => 5,
        CURLOPT_TIMEOUT => 10
    ]);

    $result = curl_exec($ch);

    if (curl_errno($ch)) {
        error_log("Telegram CURL Error: " . curl_error($ch));
    }

    curl_close($ch);
    return $result;
}