<?php
require_once('../classes/body.class.php');
require_once('../classes/db.class.php');
require_once('../classes/system.class.php');

if (isset($_POST['LMI_PAYMENT_NO'])) {
$result = DB::$dbs->query("SELECT * FROM `payment` WHERE `id` = ? AND `yes` = ?", array(intval($_POST['LMI_PAYMENT_NO']),0));
} else {
exit("Нет данных!");
}

$rows = $result -> rowCount();
if ($rows != 1) {
exit('Такого платежа нет');
} else {

$pay = $result -> fetch();
$LMI_PAYEE_PURSE = $_POST['LMI_PAYEE_PURSE'];
$LMI_PAYMENT_AMOUNT = $_POST['LMI_PAYMENT_AMOUNT'];
$LMI_PAYMENT_NO = $_POST['LMI_PAYMENT_NO'];
$LMI_MODE = $_POST['LMI_MODE'];
$LMI_SYS_INVS_NO = $_POST['LMI_SYS_INVS_NO'];
$LMI_SYS_TRANS_NO = $_POST['LMI_SYS_TRANS_NO'];
$LMI_SYS_TRANS_DATE = $_POST['LMI_SYS_TRANS_DATE'];
$LMI_PAYER_PURSE = $_POST['LMI_PAYER_PURSE'];
$LMI_PAYER_WM = $_POST['LMI_PAYER_WM'];
$lmihash = $_POST['LMI_HASH'];
$LMI_SECRET_KEY = "16phpage16"; // secret key

$chkstring = $LMI_PAYEE_PURSE.$LMI_PAYMENT_AMOUNT.$LMI_PAYMENT_NO.$LMI_MODE.$LMI_SYS_INVS_NO.$LMI_SYS_TRANS_NO.$LMI_SYS_TRANS_DATE.$LMI_SECRET_KEY.$LMI_PAYER_PURSE.$LMI_PAYER_WM;
$hash = hash('sha256', $chkstring);
$md5sum = strtoupper($hash);
if ($_POST['LMI_HASH'] == $md5sum) {

DB::$dbs->query("UPDATE `payment` SET `LMI_PAYMENT_AMOUNT` = '".$LMI_PAYMENT_AMOUNT."', `LMI_SYS_INVS_NO` = '".$LMI_SYS_INVS_NO."', `LMI_SYS_TRANS_NO` = '".$LMI_SYS_TRANS_NO."', `LMI_SYS_TRANS_DATE` = '".$LMI_SYS_TRANS_DATE."', `LMI_PAYER_PURSE` = '".$LMI_PAYER_PURSE."', `LMI_PAYER_WM` = '".$LMI_PAYER_WM."', `time` = '".time()."', `yes` = '1' WHERE `id` = '".intval($_POST['LMI_PAYMENT_NO'])."'");

$w = DB::$dbs->queryFetch("SELECT *  FROM `payment` WHERE `id` = ?", array(intval($_POST['LMI_PAYMENT_NO'])));

DB::$dbs->query("INSERT INTO `bill_hist` (`user_id`,`to_id`,`rur`,`mod`,`komm`,`time`,`yes`,`kod`) VALUES (?,?,?,?,?,?,?,?)", array($w['user_id'],$w['user_id'],$LMI_PAYMENT_AMOUNT,'in','Пополнение баланса на  <b>".$LMI_PAYMENT_AMOUNT."</b> рублей (№ ".$LMI_PAYMENT_NO.")',time(),'1',$LMI_PAYMENT_NO));

$rur = $w['LMI_PAYMENT_AMOUNT'];
DB::$dbs->query("UPDATE `blogger` SET `rur`= `rur` + ? WHERE `id` = ?", [$rur,$w['user_id']]);

$info = DB::$dbs->queryFetch("SELECT * FROM `blogger` where `id` = ? LIMIT 1", [$w['user_id']]);
if (!empty($info['wmid']) AND !empty($info['wmr'])) {
$info['wmr'] = 'R'.$info['wmr'];
if ($info['wmid']==$LMI_PAYER_WM AND $info['wmr']==$LMI_PAYER_PURSE) {
DB::$dbs->query("UPDATE `blogger` SET `wm` = ? WHERE `id` = ?", [1,$w['user_id']]);
}
}
bill(Nick($w['user_id']).' Пополнил счет на <b>'.$LMI_PAYMENT_AMOUNT.'</b> рублей и подтвердил Webmoney реквизиты!');
DB::$dbs->query("UPDATE blogger SET wm = ? WHERE id = ?", [1,$w['user_id']]);

}
}
?>