<?php
defined('COT_CODE') or die('Wrong URL');

$return['success'] = false;
$orderHash = cot_import('orderHash', 'P', 'INT');

if($orderHash > 0 && $usr['id'] > 0 && cot_plugin_active('marketorders')) {
  require_once cot_incfile('marketorders', 'plug');

  $mcart = $db->query("SELECT * FROM $db_market_cart WHERE cart_userid=".$usr['id'].' AND cart_paid=0 AND (cart_status="incart" OR cart_status="inpay") LIMIT 1')->fetch();
  if($mcart['cart_id'] > 0) {
    if($mcart['cart_status'] == 'inpay') {
      $db->delete($db_payments, "pay_userid=".$usr['id']." AND pay_status='new' AND pay_area='market_paycart'");
      $db->update($db_market_cart, array('cart_status' => 'incart'), "cart_id=".$mcart['cart_id']);

      $ciarr = $db->query("SELECT * FROM $db_market_citems WHERE ci_userid=".$usr['id'].' AND ci_cid='.$mcart['cart_id'])->fetchAll();
      foreach($ciarr as $ci) {
        $db->delete($db_market_orders, "order_userid=".$usr['id']." AND order_status='new' AND order_paid=0 AND order_cid=".$ci['ci_id']);
      }
    }

    $ci = $db->query("SELECT ci_id FROM $db_market_citems WHERE ci_userid=".$usr['id'].' AND ci_cid='.$mcart['cart_id'].' AND ci_id='.$orderHash.' LIMIT 1')->fetchColumn();
    if($ci > 0 && $ci == $orderHash) {
      require_once cot_incfile('payments', 'module');
      $db->delete($db_payments, "pay_userid=".$usr['id']." AND pay_status='new' AND pay_area='market_payorder'");
      $db->delete($db_market_orders, "order_userid=".$usr['id']." AND order_status='new' AND order_paid=0 AND order_cid=".$ci);

      $db->delete($db_market_citems, 'ci_id='.$ci);
    }
  }
}

?>