<?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");
define('PATH_TO_LOG', dirname(__FILE__).'/');

$ik_co_id = (isset($_REQUEST["ik_co_id"])) ? strip_tags($_REQUEST["ik_co_id"]) : false;
$ik_inv_id = (isset($_REQUEST["ik_inv_id"])) ? strip_tags($_REQUEST["ik_inv_id"]) : false;
$ik_inv_st = (isset($_REQUEST["ik_inv_st"])) ? strip_tags($_REQUEST["ik_inv_st"]) : false;
$ik_trn_id = (isset($_REQUEST["ik_trn_id"])) ? strip_tags($_REQUEST["ik_trn_id"]) : false;
$m_orderid = (isset($_REQUEST["ik_pm_no"])) ? intval(strip_tags($_REQUEST["ik_pm_no"])) : false;
$ik_desc = (isset($_REQUEST["ik_desc"])) ? strip_tags($_REQUEST["ik_desc"]) : false;
$merch_amount = (isset($_REQUEST["ik_am"])) ? strip_tags($_REQUEST["ik_am"]) : false;
$ik_cur = (isset($_REQUEST["ik_cur"])) ? floatval(strip_tags($_REQUEST["ik_cur"])) : false;
$sign_hash_post = (isset($_REQUEST["ik_sign"])) ? strip_tags($_REQUEST["ik_sign"]) : false;

$dataSet = $_REQUEST;
$key_arr = array_key_exists('ik_sign', $dataSet);
if($key_arr != false) {unset($dataSet['ik_sign']);}
ksort($dataSet, SORT_STRING);
array_push($dataSet, IK_SECRET_KEY);
$signString = implode(':', $dataSet);
$sign = base64_encode(md5($signString, true));

#запись в файл информации о проведенной операции
$f=@fopen(PATH_TO_LOG."ik_order.txt","a+") or die("error");
@fputs($f,"Summa: $merch_amount; Tran_id: $m_orderid; Tran_id_ik: $ik_inv_id; Method: ".$_REQUEST["ik_pw_via"]."; Status: $ik_inv_st; Date: ".DATE("d.m.Y H:i")."; SIGN1: $sign_hash_post; SIGN2:$sign\n");
@fclose($f);

if($sign_hash_post!=$sign) {
exit("ERROR");
}elseif($ik_inv_st=="success") {

$allsumme = mysql_result(mysql_query("SELECT SUM(price) FROM `b_basket` WHERE `fromid`='$m_orderid'"),0);
$allsumme = number_format(payment_comis_p(8, $allsumme), 2, '.', "");
if($merch_amount >= $allsumme){
include($_SERVER['DOCUMENT_ROOT']."/merchant/success-merch.php");
}else{
exit("ERROR");
}

}else{
exit("ERROR");
}
?>