<?php
require($_SERVER['DOCUMENT_ROOT']."/merchant/payment_config.php");
$global_login = QW_LOGIN;
$global_passw = QW_PASSWORD;
$XML_addr[1]="https://w.qiwi.com/xml/xmlutf.jsp";

function _GetReqn_QW(){
	$time=microtime();
	$int=substr($time,11);
	$flo=substr($time,2,5);
	return $int.$flo;
};

function _GetAnswer_QW($address, $xml){
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $address);
	curl_setopt($ch, CURLOPT_HTTPHEADER, array("POST","HTTP/1.0","Content-type: text/xml;charset=UTF-8", "Accept: text/xml", "Content-length: ".strlen($xml)));
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
	curl_setopt($ch, CURLOPT_POST, 1);
    
	$result = curl_exec($ch);
	curl_close($ch);
	return $result;
}


// ÎÒÏÐÀÂÊÀ ÏÅÐÅÂÎÄÀ.
function QW_XML_SEND_MONEY ($tranid,$phone,$amount,$coment) {
	global $global_login, $global_passw, $XML_addr;
	$amount = number_format(floatval($amount),2,'.','');
	//$reqn = _GetReqn_QW();
	$reqn = $tranid;

	$operation_xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
		<request>
			<request-type>pay</request-type>
			<terminal-id>$global_login</terminal-id>
			<extra name=\"password\">$global_passw</extra>
			<extra name=\"ALARM_SMS\">0</extra>
			<auth count=\"1\" to-amount=\"$amount\">
				<payment>
					<transaction-number>$reqn</transaction-number>
					<from>
						<ccy>643</ccy>
					</from>
					<to>
						<ccy>643</ccy>
						<amount>$amount</amount>
						<service-id>99</service-id>
						<account-number>$phone</account-number>
					</to>
					<extra name=\"comment\">$coment</extra>
				</payment>
			</auth>
		</request>";

	$resxml = _GetAnswer_QW($XML_addr[1], $operation_xml);
	$xml_arr = simplexml_load_string($resxml);

	//print_r($resxml);
	//print_r($xml_arr);

	$xml_arr["status"] = strval($xml_arr->payment->attributes()->status);
	$xml_arr["txn_id"] = strval($xml_arr->payment->attributes()->txn_id);
	//$xml_arr["transaction-number"] = strval($xml_arr->payment->attributes()->transaction-number);
	//$xml_arr["result-code"] = strval($xml_arr->payment->attributes()->result-code);
	//$xml_arr["final-status"] = strval($xml_arr->payment->attributes()->final-status);
	//$xml_arr["fatal-error"] = strval($xml_arr->payment->attributes()->fatal-error);
	$xml_arr["amount"] = strval($xml_arr->payment->to->amount);
	//$xml_arr["account-number"] = strval($xml_arr->payment->to->account-number);
	$xml_arr["balance"] = strval($xml_arr->balances->balance);

	return $xml_arr;
}
?>