<?php

if(!count($_POST))
  die("Request doesn\'t contain POST elements.");

$request = $_POST;

require_once('../../../manager/includes/config.inc.php');
require_once(MODX_MANAGER_PATH . "includes/document.parser.class.inc.php");

define('TMGCO_PATH', MODX_BASE_PATH ."assets/snippets/tmgco");

//load tmgco config or return
$tmgco_config_file = TMGCO_PATH . "/config.php";
$tmgco_config = array();
if(file_exists($tmgco_config_file)) {
    $tmgco_config = require($tmgco_config_file);
} else {
    die('no config file found');
}

$modx = new DocumentParser;
$modx->db->connect();

//System configuration
$modx_dbname = $modx->db->config['dbase'];
$modx_tb_prefix = $modx->db->config['table_prefix'];

$modx_tab_payments = $modx_tb_prefix."tmgco_payments";
$modx_SHK_table = $modx_tb_prefix."manager_shopkeeper";

$the_Id		=  $request['id'];
$the_Sum	=  $request['sum'];
$the_Clientid	=  $request['clientid'];
$the_Orderid	=  intval($request['orderid']);
$the_Key	=  $request['key'];

if(!$modx->db->getValue($modx->db->select('COUNT(*)',$modx_SHK_table,'id=' . $the_Orderid)))
  die('Order not found');

    // load client for further validation
//if($our_customerId != $the_Clientid)
//    die('Client not found');

$our_key = $tmgco_config['tmgco_secret_key'];
$our_signature = md5($the_Id . number_format((float)$the_Sum, 2, '.', '') . $the_Clientid . $the_Orderid . $our_key);

    // check transaction signature
if($the_Key != $our_signature)
  die('Message digest incorrect');

  //change order status
  if (mysql_num_rows(mysql_query("show tables from $modx_dbname like '$modx_SHK_table'"))>0){
      $modx->db->update(array("status" => 6), $modx_SHK_table, "id = '" . $the_Orderid . "'");
      $modx->invokeEvent('OnSHKChangeStatus',array('order_id'=>$the_Orderid,'status'=>6));
  }

$our_hash = md5($the_Id . $our_key);

die("OK $our_hash");
?>