<?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");
include("".$_SERVER['DOCUMENT_ROOT']."/merchant/qiwi/IShopServerWSService.php");

define('TRACE', 1);
$service = new IShopServerWSService($_SERVER['DOCUMENT_ROOT'].'/merchant/qiwi/IShopServerWS.wsdl', array('location' => 'http://ishop.qiwi.ru/services/ishop', 'trace' => TRACE));

function checkBill($txn_id) {
	global $service;
	$params = new checkBill();
	$params->login = QW_LOGIN;
	$params->password = QW_PASSWORD;
	$params->txn = $txn_id;
	
	$res = $service->checkBill($params);

	$check["user"] = trim($res->user);
	$check["amount"] = trim($res->amount);
	$check["date"] = trim($res->date);
	$check["lifetime"] = trim($res->lifetime);
	$check["status"] = trim($res->status);

	return $check;
}

$s = new SoapServer($_SERVER['DOCUMENT_ROOT'].'/merchant/qiwi/IShopClientWS.wsdl', array('classmap' => array('tns:updateBill' => 'Param', 'tns:updateBillResponse' => 'Response')));
$s->setClass('TestServer');
$s->handle();

class Response {
	public $updateBillResult;
}

class Param {
	public $login;
	public $password;
	public $txn;      
	public $status;
}

class TestServer {

function updateBill($param) {
$pay_tran_id = trim($param->txn);
$pay_status = intval(trim($param->status));
$pay_login = intval(trim($param->login));
$pay_password = trim($param->password);
$pay_hash = strtoupper(md5($pay_tran_id.strtoupper(md5(QW_PASSWORD))));

if($pay_login == QW_LOGIN && $pay_password == $pay_hash) {

$m_orderid = ( isset($pay_tran_id) && preg_match("|^[\d]{1,11}$|", strip_tags(trim($pay_tran_id))) ) ? intval(strip_tags(trim($pay_tran_id))) : false;
$check = checkBill($m_orderid);
$check = $check["amount"];

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

$temp = new Response();
$temp->updateBillResult = 0;
return $temp;

}else{
$temp = new Response();
$temp->updateBillResult = 150;
return $temp;
}
}
}
?>