<?php
header("Content-type: text/html; charset=windows-1251");
require_once($_SERVER['DOCUMENT_ROOT']."/merchant/payment_config.php");
require_once($_SERVER['DOCUMENT_ROOT']."/funciones.php");
require_once($_SERVER['DOCUMENT_ROOT']."/config.php");
@require($_SERVER['DOCUMENT_ROOT']."/curs/curs.php");
define('PATH_TO_LOG', dirname(__FILE__).'/');

$m_orderid = (isset($_REQUEST["PAYMENT_ID"])) ? intval(strip_tags(trim($_REQUEST["PAYMENT_ID"]))) : false;
$PAYEE_ACCOUNT = (isset($_REQUEST["PAYEE_ACCOUNT"])) ? strip_tags(trim($_REQUEST["PAYEE_ACCOUNT"])) : false;
$merch_amount_usd = (isset($_REQUEST["PAYMENT_AMOUNT"])) ? strip_tags(trim($_REQUEST["PAYMENT_AMOUNT"])) : false;
$PAYMENT_UNITS = (isset($_REQUEST["PAYMENT_UNITS"])) ? strip_tags(trim($_REQUEST["PAYMENT_UNITS"])) : false;

$string = $_POST['PAYMENT_ID'].':'.$_POST['PAYEE_ACCOUNT'].':'.$_POST['PAYMENT_AMOUNT'].':'.$_POST['PAYMENT_UNITS'].':'.strtoupper(md5(PM_PHRASE_HASH)).':'.$_POST['TIMESTAMPGMT'];
$hash = strtoupper(md5($string));

if($hash == $_POST['V2_HASH']) {
	if($PAYEE_ACCOUNT==PM_MEMBER_USD && $PAYMENT_UNITS==PM_PAYMENT_UNITS) {
$allsumme = mysql_result(mysql_query("SELECT SUM(price) FROM `b_basket` WHERE `fromid`='$m_orderid'"),0);
$allsumme = number_format(payment_comis_p(5, $allsumme), 2, '.', "");
if($merch_amount_usd >= floatval(round(($allsumme/$CURS_USD),2))){
require_once($_SERVER['DOCUMENT_ROOT']."/merchant/success-merch.php");
}else{
$f = fopen(PATH_TO_LOG."pm_bad.log", "ab+");
fwrite($f, date("d.m.Y H:i")."; REASON: summa; PAYMENT_AMOUNT: $merch_amount_usd; summa_basket: ".floatval(round(($allsumme/$CURS_USD),2))."\n");
fclose($f);
}
	}else{
		$f = fopen(PATH_TO_LOG."pm_bad.log", "ab+");
		fwrite($f, date("d.m.Y H:i")."; REASON: purse; PAYEE_ACCOUNT: $PAYEE_ACCOUNT; PAYMENT_UNITS: $PAYMENT_UNITS;\n");
		fclose($f);
	}
}else{
	$f=fopen(PATH_TO_LOG."pm_bad.log", "ab+");
	fwrite($f, date("d.m.Y H:i")."; REASON: bad hash; POST: ".serialize($_POST)."; STRING: $string; HASH: $hash\n");
	fclose($f);
}
?>