<?php

// require 
require_once  'class/Base.php';
require_once  'config.php';

//===================== VARIABLES =====================\\
$MahdyarEn = new Base($token);

$update = json_decode(file_get_contents('php://input'), true);



if ($update['type'] == "invoicePay") {

    @$factor = $db->query("SELECT * FROM pay WHERE factor = '{$update['data']['payload']}'")->fetch_assoc();

    @$user = $db->query("SELECT * FROM user WHERE id = '{$factor["id"]}'")->fetch_assoc();
    @$lang = $user["lang"];

    function __($key, $vars = [])
    {
        global $lang;
        if (!$lang) $lang = "fa";
        static $translations = null;
        if (!$translations) {
            $translations = include "languages/{$lang}.php";
        }

        $text = $translations[$key] ?? $key;

        foreach ($vars as $k => $v) {
            $text = str_replace("{" . $k . "}", $v, $text);
        }

        return $text;
    }

    $MahdyarEn->sendMessage([
        'chat_id' => $factor['id'],
        'text' => __('crypto_payment_success', [
            'amount' => $update['data']['amount'],
            'currency' => $update['data']['currency'],
            'toman' => number_format($factor['price']),
            'usd' => $factor['usd'],
        ]),
        'reply_markup' => $backInline,
        'parse_mode' => 'html'
    ]);

    $db->multi_query("UPDATE user SET stock = stock+{$factor['price']} WHERE id = '{$factor['id']}';UPDATE pay SET stats = 1 WHERE `factor` = '{$update['data']['payload']}';");

    $MahdyarEn->sendMessage(['chat_id' => $reportChannel, 'text' => "\n\n#پرداخت_جدید\n👤 کاربر: <a href='tg://user?id={$factor['id']}'>{$factor['id']}</a>\n📂 شماره فاکتور:{$update['data']['payload']}💰 مبلغ واریزی: {$update['data']['amount']} {$update['data']['currency']}\n🏦 معادل: " . number_format($factor['price']) . " تومان ", 'parse_mode' => 'html']);
}
