<?php
if(isset($message->successful_payment)) {
    $invoice       = $message->successful_payment;
    $transactionId = $invoice->invoice_payload;
    $current_time  = date('Y-m-d H:i:s');
    $date = jdate("Y/m/d");
    $time = jdate("H:i:s");

if(strpos($transactionId, 'VIP-') === 0) {
        $stmt = $pdo->prepare("SELECT * FROM `vip_transactions` WHERE `order_number` = ?");
        $stmt->execute([$transactionId]);
        $transaction = $stmt->fetch();

if(!$transaction || $transaction->status == 'success') {
            die;
}

        $user_id = $transaction->user_id;
        $days    = $transaction->vip_days;
        $amount  = $transaction->price_toman;
        $stmt = $pdo->prepare("UPDATE `vip_transactions` SET `status` = 'success' WHERE `order_number` = ?");
        $stmt->execute([$transactionId]);
        $expireDate = date('Y-m-d H:i:s', strtotime("+$days days"));
        $stmt = $pdo->prepare("UPDATE `users` SET `is_vip` = 1, `vip_expire` = ? WHERE `user_id` = ?");
        $stmt->execute([$expireDate, $user_id]);
        $stmt = $pdo->prepare("UPDATE `orders` SET `is_vip` = 1 WHERE `user_id` = ? AND `status` IN ('pending','running')");
        $stmt->execute([$user_id]);
        $expireJalali = jdate("Y/m/d", strtotime($expireDate));
        $responseText = "🎉 *تبریک! حساب شما به ویژه ارتقا یافت.*\n\n" .
            "⏳ مدت اعتبار: $days روز\n" .
            "📅 تاریخ انقضا: $expireJalali\n\n" .
            "از امکانات ویژه لذت ببرید! 💎";
        sendMessage($user_id, $responseText, $userKeyboard);
        $logChannel = settings('channel_purchase_log', 'none');

if($logChannel && $logChannel !== 'none' && is_numeric($logChannel)) {
            $logMsg = "💎 خرید اشتراک ویژه\n\n" .
                "👤 کاربر: [$user_id](uid:$user_id)\n" .
                "⏳ مدت: $days روز\n" .
                "💵 مبلغ: " . number_format($amount) . " تومان\n" .
                "🧾 کد سفارش: $transactionId\n" .
                "📅 تاریخ: $date | ⏰ $time";
            sendMessage($logChannel, $logMsg);
}

        die;
} else {
        $query = "SELECT * FROM `coin_transactions` WHERE `order_number` = ?";
        $stmt  = $pdo->prepare($query);
        $stmt->execute([$transactionId]);
        $transaction = $stmt->fetch();

if(!$transaction || $transaction->status == 'success') {
            die;
}

        $user_id = $transaction->user_id;
        $amount  = $transaction->price_toman;
        $multiplier = (float) settings('coin_purchase_multiplier', 1);
        $coins      = $transaction->amount_coins * $multiplier;
        $query = "UPDATE `coin_transactions` SET `status` = 'success' WHERE `order_number` = ?";
        $stmt  = $pdo->prepare($query);
        $stmt->execute([$transactionId]);
        $query = "UPDATE `users` SET `coins` = `coins` + ? WHERE `user_id` = ?";
        $stmt  = $pdo->prepare($query);
        $stmt->execute([$coins, $user_id]);
        $commissionPercent = (float) settings('referral_commission_percent', 0);

if($commissionPercent > 0) {
            $stmtRef = $pdo->prepare("SELECT `inviter_id` FROM `referrals` WHERE `invited_id` = ? LIMIT 1");
            $stmtRef->execute([$user_id]);
            $inviterId = $stmtRef->fetchColumn();

if($inviterId) {
                $commissionCoins = intval(($coins * $commissionPercent) / 100);

if($commissionCoins > 0) {
                    $stmtInv = $pdo->prepare("UPDATE `users` SET `coins` = `coins` + ? WHERE `user_id` = ?");
                    $stmtInv->execute([$commissionCoins, $inviterId]);
                    $stmtRefUpd = $pdo->prepare("UPDATE `referrals` SET `reward_coins` = `reward_coins` + ? WHERE `inviter_id` = ? AND `invited_id` = ?");
                    $stmtRefUpd->execute([$commissionCoins, $inviterId, $user_id]);
                    $inviterMsg = "💰 *پورسانت زیرمجموعه*\n\n" .
                                  "یکی از زیرمجموعه‌های شما خرید سکه انجام داد.\n" .
                                  "🎁 سهم شما ($commissionPercent٪): *" . number_format($commissionCoins) . "* سکه\n" .
                                  "✅ به حساب شما افزوده شد.";
                    sendMessage($inviterId, $inviterMsg);
}

}

}

        $responseText = "🎉 *خرید سکه با موفقیت انجام شد*\n\n💰 تعداد سکه: " . number_format($coins) . "\n💵 مبلغ پرداختی: " . number_format($amount) . " تومان\n🔘 کد سفارش: $transactionId\n\n📅 تاریخ: $date\n⏰ ساعت: $time";
        sendMessage($user_id, $responseText, $userKeyboard);
        $logChannel = settings('channel_purchase_log', 'none');

if($logChannel && $logChannel !== 'none' && is_numeric($logChannel)) {
            $logMsg = "🪙 خرید سکه\n\n" .
                "👤 کاربر: [$user_id](uid:$user_id)\n" .
                "💎 مقدار: " . number_format($coins) . " سکه\n" .
                "💵 مبلغ: " . number_format($amount) . " تومان\n" .
                "🧾 کد سفارش: $transactionId\n" .
                "📅 تاریخ: $date | ⏰ $time";
            sendMessage($logChannel, $logMsg);
}

        die;
}

}

