<?php
function doAction($payRowId, $gateType)
{
    global $connection, $admin, $botUrl, $mainKeys, $botState;
    include '../phpqrcode/qrlib.php';
    $time = time();
    $stmt = $connection->prepare("SELECT * FROM `pays` WHERE `id` = ? AND (`state` = 'pending' OR `state` = 'send')");
    $stmt->bind_param("i", $payRowId);
    $stmt->execute();
    $payInfo = $stmt->get_result();
    $stmt->close();

    $payParam = $payInfo->fetch_assoc();
    $rowId = $payParam['id'];
    $amount = $payParam['price'];
    $user_id = $payParam['user_id'];
    $payType = $payParam['type'];
    $description = $payParam['description'];

    $from_id = $user_id;
    $plan_id = $payParam['plan_id'];
    $volume = $payParam['volume'];
    $days = $payParam['day'];
    $agentBought = $payParam['agent_bought'];

    if ($payType == "BUY_SUB")
        $payDescription = "خرید اشتراک";
    elseif ($payType == "RENEW_ACCOUNT")
        $payDescription = "تمدید اکانت";
    elseif ($payType == "INCREASE_WALLET")
        $payDescription = "شارژ کیف پول";
    elseif (preg_match('/^INCREASE_DAY_(\d+)_(\d+)/', $payType))
        $payDescription = "افزایش زمان اکانت";
    elseif (preg_match('/^INCREASE_VOLUME_(\d+)_(\d+)/', $payType))
        $payDescription = "افزایش حجم اکانت";

    if ($gateType == "zarinpal" || $gateType == "nextpay")
        $payDescription = "خرید اشتراک";

    $stmt = $connection->prepare("UPDATE `pays` SET `state` = 'paid' WHERE `id` =?");
    $stmt->bind_param("i", $payRowId);
    $stmt->execute();
    $stmt->close();



    if ($payType == "BUY_SUB") {
        include 'action_buy_sub.php';
    }
    elseif ($payType == "INCREASE_WALLET") {
        include 'action_increase_wallet.php';
    }
    elseif ($payType == "RENEW_ACCOUNT") {
        include 'action_renew_account.php';
    }
    elseif (preg_match('/^INCREASE_DAY_(\d+)_(\d+)/', $payType, $match)) {
        include 'action_increase_day.php';
    }
    elseif (preg_match('/^INCREASE_VOLUME_(\d+)_(\d+)/', $payType, $match)) {
        include 'action_increase_volume.php';
    }
    elseif ($payType == "RENEW_SCONFIG") {
        include 'action_renew_sconfig.php';
    }
    
    // پیام تایید نهایی و دکمه بازگشت به منوی اصلی
    sendMessage("پرداخت شما با موفقیت انجام شد", json_encode(['inline_keyboard' => [[['text' => "صفحه اصلی 🏘", 'callback_data' => "mainMenu"]]]]), null, $user_id);
}
