<?
/* WebMoney orders verifier */
include("../../includes/common.php");



/* CHECK IP HERE */
/*if($_SERVER["REMOTE_ADDR"]!=$WM['server_ip']){
	@mail($SUPPORT_EMAIL,"Ошибка в WM скрипте. IP неверен!", "IP ".$_SERVER["REMOTE_ADDR"],"From:ExChanger System<$SUPPORT_EMAIL>");
	exit;
}*/

$get = "";
$post = "";
foreach($_GET as $key=>$value){
	$get .= "$key -> $value\n";
}
foreach($_POST as $key=>$value){
	$post .= "$key -> $value\n";
}

@mail($SUPPORT_EMAIL,"Проверка", "GET:\n $get\n\nPOST:\n$post","From:".$SETTINGS['site_name']." System<$SUPPORT_EMAIL>");

/* PRE REQUEST */
if(isset($_POST['LMI_PREREQUEST'])){
	echo "YES";
	exit;
}

if(!isset($_POST['LMI_PAYMENT_NO'])){
	exit;
}

reset ($_POST);
$test="";
while (list($key, $value) = each ($_POST)) {
	$$key = trim($value);
}

$control = $LMI_PAYEE_PURSE.$LMI_PAYMENT_AMOUNT.$LMI_PAYMENT_NO.$LMI_MODE.$LMI_SYS_INVS_NO.$LMI_SYS_TRANS_NO.$LMI_SYS_TRANS_DATE.$WM['secret_key'].$LMI_PAYER_PURSE.$LMI_PAYER_WM;

@mail($SUPPORT_EMAIL,"Control", "$control","From:".$SETTINGS['site_name']." System<$SUPPORT_EMAIL>");

if(strtoupper(md5($control))!= strtoupper($LMI_HASH)){
	$msg = "Здравствуйте!\n\nОшибка при проверке подписи в WM скрипте.\nЗаказ # ".$LMI_PAYMENT_NO;
	@mail($SUPPORT_EMAIL,"Ошибка в WM скрипте. Подпись неверна!", $msg,"From:".$SETTINGS['site_name']." System<$SUPPORT_EMAIL>");
	exit;
}

/* updating order */
if(!is_numeric($LMI_PAYMENT_NO)){
	exit;
}
$row = @mysql_fetch_object(@mysql_query("select * from wm_orders where id=$LMI_PAYMENT_NO and status=1"));
if($row->id==""){
	exit;
}

/* double checking order */
if(!WMCheckInv($LMI_PAYMENT_NO,$LMI_SYS_INVS_NO,$row->payee_purse,$LMI_PAYER_WM)){
	$msg = "Здравствуйте!\n\nОшибка при повторной проверке оплаты в WM скрипте.\nЗаказ # ".$LMI_PAYMENT_NO;
	@mail($SUPPORT_EMAIL,"Ошибка в WM скрипте. Повторная проверка не прошла!", $msg,"From:".$SETTINGS['site_name']." System<$SUPPORT_EMAIL>");
	exit;
}

@mysql_query("update wm_orders set status = 0, payer_purse = '$LMI_PAYER_PURSE', payer_wm = '$LMI_PAYER_WM', sys_invs_no = '$LMI_SYS_INVS_NO', sys_trans_no = '$LMI_SYS_TRANS_NO', sys_trans_date = '$LMI_SYS_TRANS_DATE' where id=$LMI_PAYMENT_NO");

/* exchange order */
$row = @mysql_fetch_object(@mysql_query("select orders.* from orders left outer join ex_currencies on orders.ex_currency_from_id = ex_currencies.id where (orders.amount_from - orders.ex_currency_from_comission) =  $LMI_PAYMENT_AMOUNT and orders.from_order_id=$LMI_PAYMENT_NO and orders.status=1 and orders.to_status=1 and (ex_currencies.code='WMR' or ex_currencies.code='WMZ' or ex_currencies.code='WME' or ex_currencies.code='WMU')"));

if($row->id==""){
	exit;
}
@mysql_query("update orders set from_status = 0, batch_from ='$LMI_SYS_TRANS_NO' where id = $row->id");


/* tranfering money */

/* changing balance */
$comments = "Зачисление средств из $LMI_PAYER_PURSE, WM ID $LMI_PAYER_WM в систему. Операция обмена # $row->id.";
ChangeBalance($row->ex_currency_from_id,$LMI_PAYMENT_AMOUNT,$comments);

if($row->id!="")
{
	if (isset($_POST['IS_MERCHANT']) && (1 == $_POST['IS_MERCHANT']))
	{
		if (file_exists("../../includes/merchant/merchant_user_class.php")) //merchant module installed
		{
			include_once("../../includes/merchant/common.php");
			require_once("../../includes/merchant/merchant_user_class.php");
			complete_merchant_exchange($row->id);
			/*
			$merchant_user = new MerchantUser($db);
			$merchant_user->notify($row->id);*/
		}	
	}
	else
	{
		CompleteExchange($row->id);		
	}
}

exit;
?>