<?php

/***********************************************************************
 EcshopRUS 2.5.1.build0229 
 (C)2008 DarkWeb Studio - http://darkweb.ru
 -----------------------------------------------------------------------
Данный материал является объектом  интеллектуальной собственности
студии DARKWEB.RU Воспроизведение (распространение, копирование, использование)
данного материала в целом или его фрагмента возможно только с разрешения
представителя студии DARKWEB.RU
Модуль оплаты по ситеме Webmoney для портала EcshopRus.ru
************************************************************************/

if (!defined('IN_ECS'))
{
    die('Hacking attempt');
}

$payment_lang = ROOT_PATH . 'languages/' .$GLOBALS['_CFG']['lang']. '/payment/wm.php';

if (file_exists($payment_lang))
{
    global $_LANG;

    include_once($payment_lang);
}

/* The basic modules of information */
if (isset($set_modules) && $set_modules == TRUE)
{
    $i = isset($modules) ? count($modules) : 0;

    /* Code */
    $modules[$i]['code']    = basename(__FILE__, '.php');

    /* Description of the corresponding language */
    $modules[$i]['desc']    = 'wm_desc';

    /* Whether to support */
    $modules[$i]['is_cod']  = '0';

    /* Whether to support the on-line payment */
    $modules[$i]['is_online']  = '1';

    /* Pay */
    $modules[$i]['pay_fee'] = '0.8%';

    /* Author */
    $modules[$i]['author']  = 'ECSHOPRU';

    /* Website */
    $modules[$i]['website'] = 'http://www.ecshoprus.ru';

    /* Version */
    $modules[$i]['version'] = '1.0.0';

    /* Configuration information */
    $modules[$i]['config'] = array(
        array('name' => 'wm_account', 'type' => 'text', 'value' => ''),
        array('name' => 'wm_key',     'type' => 'text', 'value' => ''),
    );

    return;
}

/**
 * Class
 */
class wm
{
    /**
     * Constructors
     *
     * @access  public
     * @param
     *
     * @return void
     */
    function wm()
    {
    }

    function __construct()
    {
        $this->wm();
    }

    /**
     * Generated to pay code
     * @param   array   $order      Order Information
     * @param   array   $payment    Payment Information
     */
    function get_code($order, $payment)
    {
        $data_vid           = trim($payment['wm_account']);
        $data_orderid       = $order['order_id'];
        $data_vamount       = $order['order_amount'];
        $mch_name = $GLOBALS['_CFG']['shop_name'];  //магазина название

        $def_url  = '<br /><form style="text-align:center;" method=post action="https://merchant.webmoney.ru/lmi/payment.asp" target="_blank">';
        $def_url .= "<input type=HIDDEN name='LMI_PAYEE_PURSE' value='".$data_vid."'>";
        $def_url .= "<input type=HIDDEN name='LMI_PAYMENT_NO' value='".$data_orderid."'>";
        $def_url .= "<input type=HIDDEN name='LMI_PAYMENT_AMOUNT' value='".$data_vamount."'>";
        $def_url .= "<input type=HIDDEN name='LMI_PAYMENT_DESC' value='\"$mch_name\" -> PAY ID: \"$order[order_sn]\"'>";  //PAY ID: 
        $def_url .= "<input type=submit value='" .$GLOBALS['_LANG']['pay_button']. "'>";
        $def_url .= "</form>";

        return $def_url;
    }

    /**
     * Response operation
     */


    function respond()
    {
        $payment        = get_payment(basename(__FILE__, '.php'));            

if (!isset ($_POST['LMI_PAYMENT_NO']) ||
    !isset ($_POST['LMI_PAYMENT_AMOUNT']) ||
    !isset ($_POST['LMI_MODE']) ||
    !isset ($_POST['LMI_PAYEE_PURSE']) ||
    !isset ($_POST['LMI_PAYER_WM'])) { exit; }

        // идентифицируем переменные
        $LMI_PAYEE_PURSE = $_POST['LMI_PAYEE_PURSE'];
        $LMI_MODE = $_POST['LMI_MODE'];
        $WM_SIM_MODE = $_POST['WM_SIM_MODE'];
        $LMI_PREREQUEST = $_POST['LMI_PREREQUEST'];
        $LMI_HASH = $_POST['LMI_HASH'];
        $LMI_PAYMENT_AMOUNT = $_POST['LMI_PAYMENT_AMOUNT'];
        $LMI_PAYMENT_NO = $_POST['LMI_PAYMENT_NO'];
        $LMI_SYS_INVS_NO = $_POST['LMI_SYS_INVS_NO'];
        $LMI_SYS_TRANS_NO = $_POST['LMI_SYS_TRANS_NO'];
        $LMI_SYS_TRANS_DATE = $_POST['LMI_SYS_TRANS_DATE'];
        $LMI_PAYER_PURSE = $_POST['LMI_PAYER_PURSE'];
        $LMI_PAYER_WM = $_POST['LMI_PAYER_WM'];

$sql = "SELECT pay_config FROM " . $GLOBALS['ecs']->table('payment') ."WHERE pay_code = 'wm'";
$get_pay_config = $GLOBALS['db']->getOne($sql);


   $store = unserialize($get_pay_config);
        $code_list = array();
        foreach ($store as $key=>$value)
        {
         $code_list[$value['name']] = $value['value'];
        }

$purse = $code_list[wm_account];
$secure = $code_list[wm_key];

if ($LMI_PAYEE_PURSE != $purse) { exit; }

$am = $order['order_amount'];

//if ($LMI_MODE && !isset($WM_SIM_MODE)) { exit; }

if (isset($LMI_PREREQUEST) && $LMI_PREREQUEST == 1)
{
    print "YES";
    exit;
}

if (!isset($LMI_PAYER_PURSE) || !isset($LMI_HASH)) { exit;}

if (strtolower($LMI_HASH) != strtolower(md5($purse.$LMI_PAYMENT_AMOUNT.$LMI_PAYMENT_NO.$LMI_MODE.$LMI_SYS_INVS_NO.$LMI_SYS_TRANS_NO.$LMI_SYS_TRANS_DATE.$secure.$LMI_PAYER_PURSE.$LMI_PAYER_WM))) { exit; }

$time = time();

$sql = 'UPDATE' . $GLOBALS['ecs']->table('order_info') . "SET `pay_status` = '2', `money_paid` = '$LMI_PAYMENT_AMOUNT', `order_amount` = order_amount-'$LMI_PAYMENT_AMOUNT', `pay_time` = '$time' WHERE order_id = '$LMI_PAYMENT_NO'";
$result = $GLOBALS['db']->query($sql);

    }
}

?>