<?php
    $auto_depozit = true;
  	//session_start();
  	require_once(__DIR__."/../../conf.php");

	$ip = check_ip();

  	$system = "Payeer";
  	if (is_post()) {
  	    if ($payeer_secret_key === '*****************') {
            die($_POST['m_orderid'].'|error');
        }

    	$ar_hash = array(
      		post('m_operation_id'),
	      	post('m_operation_ps'),
			post('m_operation_date'),
			post('m_operation_pay_date'),
			post('m_shop'),
			post('m_orderid'),
			post('m_amount'),
			post('m_curr'),
			post('m_desc'),
			post('m_status'),
      		$payeer_secret_key
      	);

      	$sign_hash = strtoupper(hash('sha256', implode(':', $ar_hash)));
     	if (post('m_sign') === $sign_hash && post('m_status') === 'success') {
        	$id = (int)post("m_orderid");
        	$m_amount = post("m_amount");
        	$m_amount = format_money($m_amount);
        	$transaction = post('m_operation_id');

            $sql = "SELECT * FROM `invest` WHERE `id` = ".escape_db($id)." AND `summa` = ".escape_db($m_amount)." AND `status` = ".escape_db(0).condition_delete()." LIMIT 1;";
	        $result = $mysqli->query($sql);
	        if ($result->num_rows !== 1) {
	            die($_POST['m_orderid'].'|error');
	        }
	        $row = $result->fetch_assoc();
	        if (!$row) {
	            die($_POST['m_orderid'].'|error');
	        }
            if ($row["status"]) {
                die($_POST['m_orderid'].'|error');
            }
            if ($row["delete"] == 0) {
                die($_POST['m_orderid'].'|error');
            }
            //исправление безопасности
			if ($row["system"] !== $system) {
               log_write(['Подмена системы', $_POST, $_GET, $_SESSION, $_SERVER]);
               die('Подмена системы');
            }

            $set = get_row_by_field('payment_system', 'name', $system);
            if (!$set) {
                log_write("Ошибка оплаты {$system}. Настройки по системе не получены.", $_POST);
                die($_POST['m_orderid'].'|error');
            }

            if ($set['access'] !== 'payeer') {
                die($_POST['m_orderid'].'|error');
            }

	        $login = $row['login'];//получаем логин
      		$invest_id = $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($_POST['m_orderid'].'|error');
            }

	        $sql = "UPDATE `invest` SET `status`= '1', `transaction` = ".escape_db($transaction)." WHERE `id` = ".escape_db($id)." LIMIT 1;";
	        if (!$mysqli->query($sql)) {
	            die($_POST['m_orderid'].'|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 $_POST['m_orderid'].'|success';
       		exit(0);
     	} else {
       		die($_POST['m_orderid'].'|error');
     	}
   	} else {
     	die($_POST['m_orderid'].'|error');
   	}
