<?php
function rus_lat($str,$to = true)
{
$rus = $lat = array('¸','æ','ö','÷','ø','ù','þ','ÿ','¨','Æ',
'Ö','×','Ø','Ù','Þ','ß','ý','û','Ý','Û','ü','ú','Ü','Ú');

$temp = array('yo','zh','tc','ch','sh','sh','yu','ya',
'YO','ZH','TC','CH','SH','SH','YU','YA','ye','ue','YE',
'UE','mz','tz','MZ','TZ');

$rus_str = $lat_str = "ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÐÑÒÓÔÕàáâãäåçèéêëìíîïðñòóôõ";
$temp_str = "ABVGDEZIJKLMNOPRSTUFHabvgdezijklmnoprstufh";

if ($to == true) {$lat = $temp; $lat_str = $temp_str;}
else {$rus = $temp; $rus_str = $temp_str;}

$str = str_replace($rus,$lat,$str);
$str = strtr($str,$rus_str,$lat_str);
return($str);
 }

 class still_liq{
	var $url = "https://www.liqpay.com/?do=api_xml";
	var $pass = " ";
	var $merchant_id = " ";
	var $valuta = " ";

	function zaprosXML($to, $amount, $descr, $order_id){
	  $xmlzap = "<request>
            <version>1.2</version>
	        <action>send_money</action>
	        <kind>phone</kind>
	        <merchant_id>".$this->merchant_id."</merchant_id>
	        <order_id>".$order_id."</order_id>
	        <to>".$to."</to>
	        <amount>".$amount."</amount>
	        <currency>".$this->valuta."</currency>
	        <description>".$descr."</description>
      </request>";
	  return $xmlzap;}

	function Signature($xmlzap){
		$signat = base64_encode(sha1($this->pass.$xmlzap.$this->pass,1));
		return $signat;}

	function Request($xmlzap){
		$result = base64_encode($xmlzap);
		return $result;}

	function createXML($request, $signature){
		$result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
		<request>
		<liqpay>
		<operation_envelope><operation_xml>".$request."</operation_xml>
		<signature>".$signature."</signature></operation_envelope>
		</liqpay>
		</request>";
		return $result;}

   function head($fullXML){
		$result = array("POST /?do=api_xml HTTP/1.0",
                         "Content-type: text/xml;charset=\"utf-8\"",
                         "Accept: text/xml",
                         "Content-length: ".strlen($fullXML));
		return $result;}

   function sendXML($xmlzap, $headers){
   	    $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL, $this->url);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
          curl_setopt($ch, CURLOPT_TIMEOUT, 60);
          curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
          curl_setopt($ch, CURLOPT_POST, 1);
          curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlzap);
        $result = curl_exec($ch);
	return $result;}

	function obrabotca($to, $amount, $descr, $order_id){
		$xml_request = $this->zaprosXML($to, $amount, $descr, $order_id);
		$signat = $this->Signature($xml_request);
		$xmlzap = $this->Request($xml_request);
		$fullXML = $this->createXML($xmlzap, $signat);
		$headers = $this->head($fullXML);
		$response = $this->sendXML($fullXML,$headers);

		$xmlres = simplexml_load_string($response);

		$operation_xml = strval($xmlres->liqpay->operation_envelope->operation_xml);
        $result['signprix'] = strval($xmlres->liqpay->operation_envelope->signature);

        $xmlot = base64_decode($operation_xml);
        $xmlr = simplexml_load_string($xmlot);

        $result['status'] = strval($xmlr->status);
        $result['code'] = strval($xmlr->code);
        $result['transaction_id'] = strval($xmlr->transaction_id);

        $result['signprov'] = base64_encode(sha1($this->pass.$xmlot.$this->pass, 1));

		return $result;}

}?>
