<?php 
require_once($_SERVER['DOCUMENT_ROOT']."/merchant/payment_config.php");
require_once($_SERVER['DOCUMENT_ROOT']."/funciones.php");
require($_SERVER['DOCUMENT_ROOT']."/config.php");

function from_request($name){
	return isset($_REQUEST[$name]) ? strip_tags(stripslashes($_REQUEST[$name])) : null;
}

$LMI_PREREQUEST = from_request('LMI_PREREQUEST');
$LMI_SECRET_KEY = from_request('LMI_SECRET_KEY');
$LMI_PAYEE_PURSE = from_request('LMI_PAYEE_PURSE');
$LMI_PAYMENT_AMOUNT = from_request('LMI_PAYMENT_AMOUNT');
$LMI_PAYMENT_NO = from_request('LMI_PAYMENT_NO');
$LMI_MODE = from_request('LMI_MODE');
$LMI_SYS_INVS_NO = from_request('LMI_SYS_INVS_NO');
$LMI_SYS_TRANS_NO = from_request('LMI_SYS_TRANS_NO');
$LMI_SYS_TRANS_DATE = from_request('LMI_SYS_TRANS_DATE');
$LMI_PAYER_PURSE = from_request('LMI_PAYER_PURSE');
$LMI_PAYER_WM = from_request('LMI_PAYER_WM');
$LMI_HASH = from_request('LMI_HASH');
$m_orderid = intval($LMI_PAYMENT_NO );

$allsumme = mysql_result(mysql_query("SELECT SUM(price) FROM `b_basket` WHERE `fromid`='$m_orderid'"),0);
$allsumme = number_format(payment_comis_p(9, $allsumme), 2, '.', "");

$err = FALSE;

if($LMI_PREREQUEST==1) {
	 echo 'YES';
} else {
    if(ZP_ID_SHOP != $LMI_PAYEE_PURSE){
	    $err = TRUE;
	}
	if($LMI_PAYMENT_AMOUNT < $allsumme){
	    $err = TRUE;
	}
	
	if(isset($LMI_SECRET_KEY)) {
        if($LMI_SECRET_KEY != ZP_SECRET_KEY) {
            $err = TRUE;
		}
	} else {
        $CalcHash = md5($LMI_PAYEE_PURSE.$LMI_PAYMENT_AMOUNT.$LMI_PAYMENT_NO.$LMI_MODE.$LMI_SYS_INVS_NO.$LMI_SYS_TRANS_NO.$LMI_SYS_TRANS_DATE.ZP_SECRET_KEY.$LMI_PAYER_PURSE.$LMI_PAYER_WM);
		 if($LMI_HASH != strtoupper($CalcHash)) {
            $err = TRUE;
	    }
    }
}

if (!$err){
include($_SERVER['DOCUMENT_ROOT']."/merchant/success-merch.php");
echo 'YES';
}else{
echo 'NO';}

?>
