<?php
@header("Content-type: text/html; charset=windows-1251");
require_once($_SERVER['DOCUMENT_ROOT']."/merchant/payment_config.php");
$global_merch_id = LP_MERCH_ID;
$global_sign = LP_SIGN1;
$XML_addr[1]="https://www.liqpay.com/?do=api_xml";

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

function _GetAnswer_LP($address, $xml){
	$ch = curl_init($address);
	curl_setopt($ch, CURLOPT_HEADER, FALSE);
	//curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: text/xml;charset=UTF-8;"));
	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_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
	curl_setopt($ch, CURLOPT_TIMEOUT, 60);

	$result=curl_exec($ch);
	return $result;
}


// ÎÒÏÐÀÂÊÀ ÏÅÐÅÂÎÄÀ.
function LP_XML_SEND_MONEY ($phone,$amount,$currency,$description) {
	global $global_merch_id, $global_sign, $XML_addr;
	$reqn = _GetReqn_LP();
	$description = strip_tags(trim($description), ENT_QUOTES);
	$description = iconv("CP1251", "UTF-8", $description);
	$amount = floatval($amount);
	$operation_xml = "
	<request>
		<version>1.2</version>
		<action>send_money</action>
		<kind>phone</kind>
		<merchant_id>$global_merch_id</merchant_id>
		<order_id>ORDER_$reqn</order_id>
		<to>$phone</to>
		<amount>$amount</amount>
		<currency>$currency</currency>
		<description>$description</description>
	</request>";
	$signature = base64_encode(sha1($global_sign.$operation_xml.$global_sign, 1));
	$operation_xml = base64_encode($operation_xml);

	$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
	<request>
		<liqpay>
			<operation_envelope>
				<operation_xml>$operation_xml</operation_xml>
				<signature>$signature</signature>
			</operation_envelope>
		</liqpay>
	</request>";

	//echo base64_decode($operation_xml)."<br>";
	$resxml = _GetAnswer_LP($XML_addr[1], $xml);
	$xmlres = simplexml_load_string($resxml);
	$decode_xml = base64_decode($xmlres->liqpay->operation_envelope->operation_xml);
	$xml_arr = simplexml_load_string($decode_xml);

	$get_sign = ($xmlres->liqpay->operation_envelope->signature);
	$test_sign = base64_encode(sha1($global_sign.$decode_xml.$global_sign,1));

	if(!$xmlres | $get_sign != $test_sign) {
		$xml_arr["status"] = "No XML-answer";
		return $xml_arr;
	}

	$xml_arr["status"] = strval($xml_arr->status);
	$xml_arr["code"] = iconv("UTF-8", "CP1251", strval($xml_arr->code));
	$xml_arr["description"] = iconv("UTF-8", "CP1251", strval($xml_arr->description));
	$xml_arr["response_description"] = iconv("UTF-8", "CP1251", strval($xml_arr->response_description));

	//echo iconv("UTF-8", "CP1251", $decode_xml)."<br>";
	return $xml_arr;
}


?>