<?php
require_once($_SERVER['DOCUMENT_ROOT']."/merchant/payment_config.php");
require_once($_SERVER['DOCUMENT_ROOT']."/funciones.php");
require_once($_SERVER['DOCUMENT_ROOT']."/config.php");

$skey = WO_SECRET_KEY;
 
function print_answer($result, $description)
{
  print "WMI_RESULT=" . strtoupper($result) . "&";
  print "WMI_DESCRIPTION=" .urlencode($description);
  exit();
}
 
if (!isset($_POST["WMI_SIGNATURE"]))
  print_answer("Retry", "error WMI_SIGNATURE");
 
if (!isset($_POST["WMI_PAYMENT_NO"]))
  print_answer("Retry", "error WMI_PAYMENT_NO");
 
if (!isset($_POST["WMI_ORDER_STATE"]))
  print_answer("Retry", "error WMI_ORDER_STATE");
 
foreach($_POST as $name => $value){
  if ($name !== "WMI_SIGNATURE") $params[$name] = $value;
}
 
uksort($params, "strcasecmp"); $values = "";
 
foreach($params as $name => $value){
  //Конвертація з поточного кодування (UTF-8)
  //необхідна тільки якщо кодування магазину відмінна від Windows-1251
  //$value = iconv("utf-8", "windows-1251", $value);
  $values .= $value;
}
 
$signature = base64_encode(pack("H*", md5($values . $skey)));

$m_orderid = (isset($_POST["WMI_PAYMENT_NO"])) ? intval(trim($_POST["WMI_PAYMENT_NO"])) : false;

if ($signature == $_POST["WMI_SIGNATURE"]){

if (strtoupper($_POST["WMI_ORDER_STATE"]) == "ACCEPTED"){

$allsumme = mysql_result(mysql_query("SELECT SUM(price) FROM `b_basket` WHERE `fromid`='$m_orderid'"),0);
$allsumme = number_format(payment_comis_p(1, $allsumme), 2, '.', "");

if($_POST["WMI_PAYMENT_AMOUNT"] >= $allsumme){
include($_SERVER['DOCUMENT_ROOT']."/merchant/success-merch.php");
print_answer ("Ok", "Заказ #".$m_orderid." оплачен!");
}else{
print_answer("Retry", "error summ ". $_POST["WMI_PAYMENT_AMOUNT"]);
}
}else{
print_answer("Retry", "error stat ". $_POST["WMI_ORDER_STATE"]);
}
}else{
print_answer("Retry", "error signature ". $_POST["WMI_SIGNATURE"]);
}
 
?>