<?php

require_once '../../wcore/core.php';
$config_kassa = kassa_config;
$key = $config_kassa['yandex']['private_key'];

// $yandexSecretKey = 'Skm6eEZrtxr45MP1YnaxxJcm';
$yandexSecretKey = $key;

if (empty($_POST['sha1_hash']))
{
	// header('Location:/');
	logPayment('[Yandex] Error: Пустые данные');
}
else
{
	$httpData = (object) $_POST;
	$sha1 = sha1( $httpData->notification_type . '&'. $httpData->operation_id . '&' . $httpData->amount . '&643&' . $httpData->datetime . '&'. $httpData->sender . '&' . $httpData->codepro . '&' . $yandexSecretKey. '&' . $httpData->label );
 
	if ($sha1 != $httpData->sha1_hash ) {
		logPayment('[Yandex] Error: Hash не совпал');
	}
	else
	{
		$bill_id = (int) $httpData->label;
		
		$checkBill = mysqli_fetch_object(mysqli_query($mysqli, 'SELECT * FROM `bill_pays` WHERE `id` = '.$bill_id.' LIMIT 1'));
		
		if (!$checkBill)
		{
			logPayment('[Yandex] Error: Счет #'.$bill_id.' не найден');
		}
		else
		{
			if (mysqli_query($mysqli, 'UPDATE `bill_pays` SET `time_pay` = '.time().', `payment` = "yandex" WHERE `user_id` = '.$checkBill->user_id) && mysqli_query($mysqli, 'UPDATE `users` SET `bits` = `bits` +'.$checkBill->amount.' WHERE `id` = '.$checkBill->user_id))
			{
				logPayment('[Yandex] Success: Счет найден и оплачен, user_id: '.$checkBill->user_id);
			}
			else
			{
				logPayment('[Yandex] Error: '.mysqli_error($mysqli));
			}
		}
	}
	$logData = json_encode($checkBill);
	logPayment($logData);
}
