<?php
/*
برای اولین بار اپن شد در کانال @IRA_Team

ذکر منبع اجباری منبع سورس کد های مختلف @IRA_Team
*/
include '../../config.php';
define('API_KEY', $API_KC);
//-----------------------------------------------------------------------------------------------
function factweb($method, $datas = [])
{
    $url = "https://api.telegram.org/bot" . API_KEY . "/" . $method;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $datas);
    $res = curl_exec($ch);
    if (curl_error($ch)) {
        var_dump(curl_error($ch));
    } else {
        return json_decode($res);
    }
}
function postToZibal($path, $parameters)
{
    $url = 'https://gateway.zibal.ir/v1/' . $path;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($parameters));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response  = curl_exec($ch);
    curl_close($ch);
    return json_decode($response);
}
$payments = mysqli_fetch_assoc(mysqli_query($connect, "SELECT * FROM peyment LIMIT 1"));
$sub1 = $payments["sub1"];
$sub1_parts = explode('^', $sub1);
$subp1 = $sub1_parts[3];
$subday1 = $sub1_parts[2];

$sub2 = $payments["sub2"];
$sub2_parts = explode('^', $sub2);
$subp2 = $sub2_parts[3];
$subday2 = $sub2_parts[2];

$sub3 = $payments["sub3"];
$sub3_parts = explode('^', $sub3);
$subp3 = $sub3_parts[3];
$subday3 = $sub3_parts[2];

$sub4 = $payments["sub4"];
$sub4_parts = explode('^', $sub4);
$subp4 = $sub4_parts[3];
$subday4 = $sub4_parts[2];

$sub5 = $payments["sub5"];
$sub5_parts = explode('^', $sub5);
$subp5 = $sub5_parts[3];
$subday5 = $sub5_parts[2];

$sub6 = $payments["sub6"];
$sub6_parts = explode('^', $sub6);
$subp6 = $sub6_parts[3];
$subday6 = $sub6_parts[2];


$user = $_GET['id'];
$amount = $_GET['amount'];

if ($_GET['success'] == 1) {

    //start verfication
    $parameters = array(
        "merchant" => $merichentziball, //required
        "amount" => $amount, //required
        "trackId" => $_GET['trackId'], //required
    );

    $response = postToZibal('verify', $parameters);
    if ($response->result == 100) {

        $datenoww = date('Y-m-d');
        if ($amount == $subp1) {
            $new_expiry_date = date('Y-m-d', strtotime('+' . $subday1 . ' days'));
            $sql_new = "UPDATE `user` SET vip='yes', viptime='$new_expiry_date' WHERE id=$user";
            mysqli_query($connect, $sql_new);
        }
        if ($amount == $subp2) {
            $new_expiry_date = date('Y-m-d', strtotime('+' . $subday2 . ' days'));
            $sql_new = "UPDATE `user` SET vip='yes', viptime='$new_expiry_date' WHERE id=$user";
            mysqli_query($connect, $sql_new);
        }
        if ($amount == $subp3) {
            $new_expiry_date = date('Y-m-d', strtotime('+' . $subday3 . ' days'));
            $sql_new = "UPDATE `user` SET vip='yes', viptime='$new_expiry_date' WHERE id=$user";
            mysqli_query($connect, $sql_new);
        }
        if ($amount == $subp4) {
            $new_expiry_date = date('Y-m-d', strtotime('+' . $subday4 . ' days'));
            $sql_new = "UPDATE `user` SET vip='yes', viptime='$new_expiry_date' WHERE id=$user";
            mysqli_query($connect, $sql_new);
        }
        if ($amount == $subp5) {
            $new_expiry_date = date('Y-m-d', strtotime('+' . $subday5 . ' days'));
            $sql_new = "UPDATE `user` SET vip='yes', viptime='$new_expiry_date' WHERE id=$user";
            mysqli_query($connect, $sql_new);
        }
        if ($amount == $subp6) {
            $new_expiry_date = date('Y-m-d', strtotime('+' . $subday6 . ' days'));
            $sql_new = "UPDATE `user` SET vip='yes', viptime='$new_expiry_date' WHERE id=$user";
            mysqli_query($connect, $sql_new);
        }
        factweb('sendMessage', [
            'chat_id' => $user,
            'text' => "✅ پرداخت شما با موفقیت انجام و اشتراک فعال شد",
            'parse_mode' => "HTML",
            'reply_markup' => json_encode([
                'inline_keyboard' => [
                    [
                        ['text' => " 💳 مبلغ پرداختی", 'callback_data' => "none"],
                        ['text' => "$amount تومان", 'callback_data' => "none"]
                    ],
                ]
            ])
        ]);

        factweb('sendMessage', [
            'chat_id' => $admins[0],
            'text' => "✅ پرداخت جدید در ربات انجام شد",
            'parse_mode' => "HTML",
            'reply_markup' => json_encode([
                'inline_keyboard' => [
                    [
                        ['text' => "💳 مبلغ پرداختی", 'callback_data' => "none"],
                        ['text' => "$amount تومان", 'callback_data' => "none"]
                    ],
                    [
                        ['text' => "👤 شناسه کاربر", 'callback_data' => "none"],
                        ['text' => "$user", 'url' => "tg://openmessage?user_id=$user"]

                    ]
                ]
            ])
        ]);

        echo '
    <!DOCTYPE html>
    <html lang="fa">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>پرداخت موفق</title>
        <style>
            body {
                display: flex;
                justify-content: center;
                align-items: center;
                height: 100vh;
                background-color: #f4f4f4;
                font-family: Arial, sans-serif;
            }
            .message {
                text-align: center;
                background-color: #ffffff;
                padding: 20px;
                border-radius: 10px;
                box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            }
            .success {
                color: green;
                font-size: 24px;
                font-weight: bold;
            }
            .link {
                display: inline-block;
                margin-top: 20px;
                text-decoration: none;
                color: #007BFF;
            }
        </style>
    </head>
    <body>
        <div class="message">
            <div class="success">پرداخت با موفقیت انجام شد</div>
            <a href="https://t.me/$bottag" class="link">لطفا به ربات مراجعه کنید</a>
        </div>
    </body>
    </html>
    ';
        die();
    } else {

        echo "result: " . $response->result . "<br>";
        echo "message: " . $response->message;
    }
} else {
    factweb('sendMessage', [
        'chat_id' => $user,
        'text' => "❌ پرداخت شما انجام نشد",
        'parse_mode' => "HTML",
    ]);
    echo 'پرداخت انجام نشد';
    die();
}
/*
برای اولین بار اپن شد در کانال @IRA_Team

ذکر منبع اجباری منبع سورس کد های مختلف @IRA_Team
*/