<?php
    $auto_depozit = true;
    require_once(__DIR__ . "/../../conf.php");
    $ip = check_ip();
    $paykassa = new PayKassaSCI($paykassa_shop_id, $paykassa_shop_password);
    $res = $paykassa->sci_confirm_order();
    if ($res["error"]) {
        die("ERROR");
    }
    $id = (int) $res["data"]["order_id"];
    $transaction = $res["data"]["transaction"];
    $hash = $res["data"]["hash"];
    $currency = $res["data"]["currency"];
    $m_amount = $res["data"]["amount"];
    $m_amount = format_currency($m_amount, $currency);
    $system = $res["data"]["system"];
    $sql = "SELECT * FROM `invest` WHERE `id` = " . escape_db($id) . " AND `status` = " . escape_db(0) . condition_delete() . " LIMIT 1;";
    $result = $mysqli->query($sql);
    if ($result->num_rows !== 1) {
        die($id . '|error');
    }
    $row = $result->fetch_assoc();
    if (!$row) {
        die($id . '|error');
    }
    if ($row["status"]) {
        die($id . '|error');
    }
    if ($row["delete"] == 0) {
        die($id . '|error');
    }
    if (strtolower($row["system"]) !== strtolower($system)) {
        die('Подмена системы');
    }
    $login = $row['login'];
    $invest_id = $id;
    $summa = $row['summa'];
    $exch_rate = $row['exch_rate'];
    $currency = $val;
    $summa_check = $summa;
    //если USD тогда высчитываем сумму пополнения в рублях и сравниваем.
    $summa_check = $summa_check * $exch_rate;
    if (format_money($m_amount) !== format_money($summa_check)) {
        die($id . '|error');
    }
    $sql = "UPDATE `invest` SET `status`= '1', `transaction` = " . escape_db($transaction) . " WHERE `id` = " . escape_db($id) . " LIMIT 1;";
    if (!$mysqli->query($sql)) {
        die($id . '|error');
    }
    $res = active_invest($invest_id);
    if ($res['error']) {
        log_write("ERROR: Пополнение на {$system}; MESSAGE: {$res['message']}", $res['data']);
    }
    $mysqli->query("UPDATE `cache` SET balans_invest = balans_invest + '$summa'");
    echo $id . '|success';
