<?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($_SERVER['DOCUMENT_ROOT']."/config.php");
define('PATH_TO_LOG', dirname(__FILE__).'/');

if(!isset($_REQUEST["signature"])) {
exit("ERROR");
}
$private_key = LP_PRIVAT_KEY;

$signature_post = (isset($_REQUEST["signature"])) ? trim(strip_tags($_REQUEST["signature"])) : false;
$merch_amount = (isset($_REQUEST["amount"])) ? floatval(trim($_REQUEST["amount"])) : false;
$currency = (isset($_REQUEST["currency"])) ? trim(strip_tags($_REQUEST["currency"])) : false;
$public_key = (isset($_REQUEST["public_key"])) ? trim(strip_tags($_REQUEST["public_key"])) : false;
$order_id = (isset($_REQUEST["order_id"])) ? trim(strip_tags($_REQUEST["order_id"])) : false;
$m_type = (isset($_REQUEST["type"])) ? trim(strip_tags($_REQUEST["type"])) : false;
$desc_en = (isset($_REQUEST["description"])) ? strip_tags($_REQUEST["description"]) : false;
$status = (isset($_REQUEST["status"])) ? trim(strip_tags($_REQUEST["status"])) : false;
$merchant_id = ( isset($_REQUEST["transaction_id"]) && preg_match("|^[\d]{1,11}$|", strip_tags(trim($_REQUEST["transaction_id"]))) ) ? intval(strip_tags(trim($_REQUEST["transaction_id"]))) : false;
$sender_phone = (isset($_REQUEST["sender_phone"])) ? trim(strip_tags($_REQUEST["sender_phone"])) : false;

$signature = base64_encode( sha1( 
  private_key . 
  merch_amount . 
  currency . 
  public_key . 
  order_id . 
  m_type . 
  desc_en . 
  status .
  merchant_id .
  sender_phone 
 , 1 ));

if($order_id!=false) {
	$exp = explode("~", $order_id);
	$m_orderid = $exp[1];
}else{
	$m_orderid = false;
}

#запись в файл информации о проведенной операции
$f=@fopen(PATH_TO_LOG."lp_order.txt","a+") or die("error");
@fputs($f,"0: $m_orderid 1: $merch_amount; 2: $currency; 3: $merchant_id; 33: $public_key 4: $order_id; 5: $m_type; 55: $desc_en 555: $status 5555: $merchant_id 6: $signature_post; 7:$signature\n");
@fclose($f);


/*if($signature!=$signature_post) {
exit("ERROR");
}else
*/

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