<?php
require ($_SERVER['DOCUMENT_ROOT'].'/core/sys.php');
/*Получаем значения*/
$filter = array(
    'platform' => trim(filter_input(INPUT_POST, 'WP_PAYMENT_SITE', FILTER_UNSAFE_RAW)),
    'summa' => trim(filter_input(INPUT_POST, 'WP_PAYMENT_AMOUNT', FILTER_UNSAFE_RAW)),
    'var' => trim(filter_input(INPUT_POST, 'WP_PAYMENT_VAR', FILTER_UNSAFE_RAW)),
    'comm' => trim(htmlspecialchars(base64_decode($_POST['WP_PAYMENT_COMM']), ENT_QUOTES, 'UTF-8')),
    'hash' => trim(filter_input(INPUT_POST, 'WP_PAYMENT_HASH', FILTER_UNSAFE_RAW)),
    );
$hash = strtoupper(hash('sha256', implode(":", $filter['summa'].$client['id'])));
$com = base64_encode($filter['comm']);
if(isset($_GET['comm'])){
$com = trim(htmlspecialchars($com_no_hash, ENT_QUOTES, 'UTF-8'));
$filter['hash'] == $hash;
}
/*SQL*/
$client = $db->prepare('SELECT * FROM `platform` WHERE `id` = ?');
$client -> execute([$filter['platform']]);
$client = $client -> fetch(PDO :: FETCH_ASSOC);
/*error*/
if(!isset($client['id'])){
header('Location: /api/payment/error.php');
exit;
}
if($client['type'] == 2){
header('Location: /api/payment/error.php?msg='.urlencode('Данная площадка была заблокирована из-за нарушения правил сервиса.'));
exit; 
}
if(intval($filter['summa']) == 0){
header('Location: /api/payment/error.php?msg='.urlencode('Не верно указана сумма платежа'));
exit; 
}
if($filter['hash'] != $hash){
header('Location: /api/payment/error.php?msg='.urlencode('Не верно указан HASH площадки'));
exit; 
}
if(!isset($filter['summa']) || $filter['summa'] == 0 || $filter['platform'] == 0 || !isset($filter['platform'])){
header('Location: /api/payment/error.php?msg='.urlencode('Не верно указан один из обязательных пунктов.'));
exit; 
}
/*END*/
$hash = md5($client['key']);
$check = md5($summa.$client['id'].time());
$sql = $db->prepare('INSERT INTO `pay` (`platform`,`summa`,`var`,`com`,`time`,`hash`,`check`,`type`) VALUES (?,?,?,?,?,?,?,?)');
$sql -> execute([$client['id'],$filter['summa'],$filter['var'],$com,time(),$hash,$check,$client['type']]);
$id = $db->lastInsertId();
$_SESSION['payment'] = $id;
$curr_timestamp = date('Ymd H:i:s');
$data = $db->prepare("UPDATE `pay` SET `date` = NOW() WHERE `id`= ?");
$data -> execute([$id]);
header('Location: /api/payment/merchant/');
exit;