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

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

if(!isset($_POST)){
	exit;
}



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

	$test .= "$key - $value\n";
}

mail("dima@businessmedia.ru","E-Gold",$test);

$control ="$PAYMENT_ID:$PAYEE_ACCOUNT:$PAYMENT_AMOUNT:$PAYMENT_UNITS:$PAYMENT_METAL_ID:$PAYMENT_BATCH_NUM:$PAYER_ACCOUNT:".strtoupper(md5($EGOLD['alt_pass'])).":$ACTUAL_PAYMENT_OUNCES:$USD_PER_OUNCE:$FEEWEIGHT:$TIMESTAMPGMT";

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

/* updating order */
if(!is_numeric($PAYMENT_ID)){
	exit;
}
$query = "select egold_orders.*, orders.amount_from from egold_orders inner join (orders left outer join ex_currencies on orders.ex_currency_from_id = ex_currencies.id) on orders.from_order_id = egold_orders.id where egold_orders.id=$PAYMENT_ID and egold_orders.status=1 and (ex_currencies.code='EGOLD' or ex_currencies.code='ESILVER' or ex_currencies.code='EPLATINUM' or ex_currencies.code='EPALLADIUM')";

$row = @mysql_fetch_object(@mysql_query($query));
if($row->id==""){
	$msg = "Здравствуйте!\n\nОшибка в запросе в EGOLD скрипте.\nЗапрос ".$query;
	@mail($SUPPORT_EMAIL,"Ошибка в EGOLD скрипте. Повторная проверка не прошла!", $msg,"From:".$SETTINGS['site_name']." System<$SUPPORT_EMAIL>");
	exit;

}

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

@mail("support@ExChanger.ru","scsds","sdsds");

@mysql_query("update egold_orders set status = 0, payment_batch_num = '$PAYMENT_BATCH_NUM', payer_account = '$PAYER_ACCOUNT', actual_payment_ounces = '$ACTUAL_PAYMENT_OUNCES', usd_per_ounce = '$USD_PER_OUNCE',  feeweight  = '$FEEWEIGHT', timestampgmt = '$TIMESTAMPGMT' where id=".$row->id);


/* tranfering money */
$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.from_order_id=$PAYMENT_ID and orders.status=1 and orders.to_status=1 and (ex_currencies.code='EGOLD' or ex_currencies.code='ESILVER' or ex_currencies.code='EPLATINUM' or ex_currencies.code='EPALLADIUM')"));

if($row->id==""){
	exit;
}

switch($PAYMENT_METAL_ID){
	case 1:
		$metal_price = GetMetalPriceByName("GOLD");
		break;
	case 2:
		$metal_price = GetMetalPriceByName("SILVER");
		break;
	case 3:
		$metal_price = GetMetalPriceByName("PLATINUM");
		break;
	case 4:
		$metal_price = GetMetalPriceByName("PALLADIUM");
		break;
}

$sys_com = round(($ACTUAL_PAYMENT_OUNCES-$FEEWEIGHT)*$USD_PER_OUNCE - $row->amount_to*GetCourseByExID($row->ex_currency_to_id,$row->ex_currency_from_id),2);

$sys_metal_com = round((($ACTUAL_PAYMENT_OUNCES-$FEEWEIGHT) - $row->amount_to*GetCourseByExID($row->ex_currency_to_id,$row->ex_currency_from_id)/$USD_PER_OUNCE)*31.1034768,6);


@mysql_query("update orders set from_status = 0, batch_from ='$PAYMENT_BATCH_NUM', metal_amount_from = '".(round($ACTUAL_PAYMENT_OUNCES*31.1034768,6))."', metal_ex_currency_from_comission = '".(round($FEEWEIGHT*31.1034768,6))."', sys_comission='$sys_com', metal_sys_comission ='$sys_metal_com', ex_currency_from_comission='".(round($FEEWEIGHT*$USD_PER_OUNCE,2))."', sys_comission = (amount_from-".(round($FEEWEIGHT*$USD_PER_OUNCE,2))."-((amount_to+ex_currency_to_comission)/real_course)) where id = $row->id");

/* tranfering money */

/* changing balance */
$comments = "Зачисление средств из EGold # $PAYER_ACCOUNT в систему. Операция обмена # $row->id.";
ChangeBalance($row->ex_currency_from_id,(round(($ACTUAL_PAYMENT_OUNCES-$FEEWEIGHT)*31.1034768,6)),$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);
		}	
	}
	else
	{
		CompleteExchange($row->id);		
	}
}

exit;
?>