<?php
require('site_config.php');
require('bd.php');
$merchant_id = $fk_id;
$merchant_secret = $fk_secret_1;
 if(isset($_POST['MERCHANT_ORDER_ID'])){
 function getIP() {
                        if(isset($_SERVER['HTTP_X_REAL_IP'])) return $_SERVER['HTTP_X_REAL_IP'];
                        return $_SERVER['REMOTE_ADDR'];
                        }
                        if (!in_array(getIP(), array('136.243.38.147', '136.243.38.149', '136.243.38.150', '136.243.38.151', '136.243.38.189', '136.243.38.108'))) {
                        die("hacking attempt!");
                        }
getIP();

    $sign = md5($merchant_id.':'.$_REQUEST['AMOUNT'].':'.$merchant_secret.':'.$_REQUEST['MERCHANT_ORDER_ID']);

    if ($sign != $_REQUEST['SIGN']) {
    die('wrong sign');
    }

$label = $_POST['intid'];
$idmoney = $_POST['MERCHANT_ORDER_ID'];
$data = date('d-m-Y H:i:s');
$yandex = '';
$suma = $_POST['AMOUNT'];
		if (is_numeric($idmoney))
		{
		$sql_select = "SELECT * FROM kot_user WHERE id='$idmoney'";
$result = mysql_query($sql_select);
$row = mysql_fetch_array($result);
if($row)
{	
$balance = $row['balance'];
$ref = $row['ref_id'];
}
$before = $balance;
$balancenew = $row['balance'] + $suma;
$after = $balancenew;
$update_sql1 = "Update kot_user set balance='$balancenew' WHERE id='$idmoney'";
    mysql_query($update_sql1);
			$insert_sql1 = "
			INSERT INTO `kot_payments` (`user_id`, `suma`, `data`, `qiwi`, `transaction`, `beforepay`, `afterpay`) 
			VALUES ('{$idmoney}', '{$suma}', '{$data}', '{$yandex}', '{$label}', '{$before}', '{$after}')
			";
mysql_query($insert_sql1);
	$sumaref = ($suma / 100) * 10;
if($ref >= 1)
{	
			$sql_select1 = "SELECT * FROM kot_user WHERE id='$ref'";
$result1 = mysql_query($sql_select1);
$row1 = mysql_fetch_array($result1);
if($row1)
{	
$balanceref = $row1['balance'];
$balancerefs = $balanceref + $sumaref;
$update_sql12 = "Update kot_user set balance='$balancerefs' WHERE id='$ref'";
    mysql_query($update_sql12);
}
} 


} 

    die('YES');
 }
?>