<?php 

/**
* Входящий платеж
*/

if (isset($_POST['LMI_PREREQUEST'])) {
  
  // Получение настроек системы
  $set_paytype = $payment->getSettings('webmoney');
  
  // Подпись с данными
  $sign = md5($_POST['LMI_PAYMENT_AMOUNT'] .':'. $_POST['LMI_PAYEE_PURSE'] .':'. $_POST['LMI_PAYMENT_NO'] .':'. $set_paytype['PAYMENT']['SECRET_KEY']);
  
  $msg = '';
  foreach($_POST AS $key => $value) {
    $msg .= $key . '=>' . $value . "\n";
  }
  file_put_contents(H.'sys/files/webmoney-' . time() . '.txt', $msg);
  
  if ($_POST['hash'] != $sign) {
    die(__('Ошибка(1): Проверка подписи не прошла'));
  } else {
    die('YES');
  }
  exit;
  
  
} 

elseif (isset($_POST['LMI_HASH'])) {
  
  // Получение настроек системы
  $set_paytype = $payment->getSettings('webmoney');
  
  $common_string = $_POST['LMI_PAYEE_PURSE'].$_POST['LMI_PAYMENT_AMOUNT'].$_POST['LMI_PAYMENT_NO'].$_POST['LMI_MODE'].$_POST['LMI_SYS_INVS_NO'].$_POST['LMI_SYS_TRANS_NO'].$_POST['LMI_SYS_TRANS_DATE'].$set_paytype['PAYMENT']['SECRET_KEY'].$_POST['LMI_PAYER_PURSE'].$_POST['LMI_PAYER_WM'];
  
  $hash = strtoupper(hash('sha256', $common_string));
  
  if ($hash != $_POST['LMI_HASH']) {
    die(__('Ошибка доступа'));
  }

  $checkPOST = array(
                  'id' => (int) $_POST['LMI_PAYMENT_NO'], 
                  'service' => $_POST['service'], 
                  'service_id' => $_POST['service_id'], 
                  'wmr' => round($_POST['LMI_PAYMENT_AMOUNT'], 2), 
                  'array' => serialize($_POST),
                  'paytype' => 'webmoney',
                  'method' => $_POST['LMI_PAYER_PURSE'],
                  'resp' => ''
                  );
  
  $pay_result = 'result';
} 

elseif (isset($_POST['LMI_PAYMENT_NO'])) {
  
  $checkPOST = array(
                  'id' => (int) $_POST['LMI_PAYMENT_NO'], 
                  'service' => $_POST['service'], 
                  'service_id' => $_POST['service_id'], 
                  'paytype' => 'webmoney', 
                  );
                  
  $pay_result = (is_numeric($_POST['LMI_SYS_INVS_NO']) ? 'success' : 'fail');
}