<?php

/*
* @package BANANA WARS
* @copyright: (c) 2017 G.P. aka Mvcshka (mvcshka@yandex.ru)
* @license: GNU General Public License version 2; see LICENSE.txt
* @files: payeer.php
*
*/

session_start();
include($_SERVER['DOCUMENT_ROOT'].'/lib/connect.php');
include($_SERVER['DOCUMENT_ROOT'].'/lib/func.php');
include($_SERVER['DOCUMENT_ROOT'].'/lib/CofigFarm.php');


$m_key = $PassShopPayeer;
if(isset($_POST["m_operation_id"]) && isset($_POST["m_sign"])) {

	$arHash = 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'],
			$m_key);

	$sign_hash = strtoupper(hash('sha256', implode(":", $arHash)));

	if($_POST["m_sign"] == $sign_hash && $_POST['m_status'] == "success") {
		
	$q = $mysql->prepare("SELECT * FROM db_enter WHERE Uid = ?");
	$q->execute(array($_POST['m_orderid']));
	
	if($q->rowCount() == 1)
	{
		$w = $q->fetch();
		if($w['Status'] == 0)
		{
			if(sprintf("%01.2f", $_POST['m_amount'])==$w['Summa'])
			{
				$sumRub = $_POST['m_amount'];
				$Serebro = (int)$sumRub * $SerebroPrice;
				$refId = GetUser('RefId', $w['UserId']);
				$lvl = GetUser('Level', $refId);
				$SumRef = intval($Serebro / 100 * (8 + $RefPercent[$lvl]));
				
				 //Мой  бонусный код;
				$UserMoneyIn = GetUser('MoneyIn', $refId);
				
				if( $UserMoneyIn < 1 ) {
		        $Serebro = intval ($Serebro + $Serebro*0.1);}
				
				elseif($Serebro >= 10000 and $Serebro < 50000 ) {
		        $Serebro = intval ($Serebro + $Serebro*0.03);} // Бонус 3%;
				
				elseif($Serebro >= 50000 and $Serebro < 100000 ) {
		        $Serebro = intval ($Serebro + $Serebro*0.05);} // Бонус 5%;
				
				elseif($Serebro >= 100000 and $Serebro < 250000 ) {
		        $Serebro = intval ($Serebro + $Serebro*0.07);} // Бонус 7%;
				
				elseif($Serebro >= 250000 and $Serebro < 500000 ) {
		        $Serebro = intval ($Serebro + $Serebro*0.1);} // Бонус 10%;
				
				elseif($Serebro >= 500000 ) {
		        $Serebro = intval ($Serebro + $Serebro*0.15);} // Бонус 15%;
				
				
				
				$e = $mysql->prepare("UPDATE db_users SET MoneyIn = MoneyIn + ?, ReferMoney = ReferMoney + ?, MoneyEnter = MoneyEnter + ? WHERE Uid = ?");
				$e->execute(array($Serebro, $SumRef, $sumRub, $w['UserId']));
				
				//$refId = GetUser('RefId', $w['UserId']);
				//$lvl = GetUser('Level', $refId);
				//$SumRef = intval($Serebro / 100 * (5 + $RefPercent[$lvl]);
				$r = $mysql->prepare("UPDATE db_users SET MoneyOut = MoneyOut + ?, RefMoney = RefMoney + ? WHERE Uid = ?");
				$r->execute(array($SumRef, $SumRef, $refId));
				
				$Reut = GetUser('Reuting', $refId);
				$LevelAcc = GetUser('Level', $refId);
				$UserMoney = GetUser('MoneyOut', $refId);
				$UserMoneyIn = GetUser('MoneyIn', $refId);
				$FullOpyt = $Lev[$LevelAcc];
				ReutingUser($refId, $LevelAcc, $Reut, $FullOpyt, $Serebro * 0.5);
				
				$UserMoney1 = GetUser('MoneyOut', $w['UserId']);
				$UserMoneyIn1 = GetUser('MoneyIn', $w['UserId']);
				$gg1 = $mysql->prepare("INSERT INTO db_log SET UserId = ?, DateAdd = ?, `Text` = ?, MoneyIn = ?, MoneyOut = ?");
				$gg1->execute(array($w['UserId'], time(), 'Пополнение баланса <img src="/images/icons/buy.gif"/><span class="title fs14">+ '.$Serebro.'</span>', $UserMoney1, $UserMoneyIn1));
				
				
				$gg = $mysql->prepare("INSERT INTO db_log SET UserId = ?, DateAdd = ?, `Text` = ?, MoneyIn = ?, MoneyOut = ?");
				$gg->execute(array($refId, time(), 'Пополнение рефералом <img src="/images/icons/level.png"/><span class="title fs14">+ '. $Serebro * 0.5 .'</span><img src="/images/moneyout.png"/><span class="title fs14">+ '.$SumRef.'</span>', $UserMoney, $UserMoneyIn));
				
				$t = $mysql->prepare("UPDATE db_enter SET Status = ? WHERE Uid = ?");
				$t->execute(array('1', $_POST['m_orderid']));
				
				//Конкурс рефералов
				include("lib/cref.php");
				$c = new competition($mysql);
				$c->UpdatePoints($w['UserId'], $sumRub);
				//Конкурс инвесторов
				include("lib/cinv.php");
				$v = new competition($mysql);
				$v->UpdatePoints($w['UserId'], $sumRub);
			}
		}
		else
		{
			echo $_POST['m_orderid']."|error";
		}
	}
	else
	{
		echo $_POST['m_orderid']."|error";
	}
		

		echo $_POST['m_orderid']."|success";
		exit();

	} else {
		echo $_POST['m_orderid']."|error";
	}
}


?>