<?php
require ($_SERVER['DOCUMENT_ROOT'].'/core/sys.php');
require ('wp.php');
define('WK_ID', 4399); //id площадки
define('WK_SECRET', 'Uuv5gcpJMVwb6oCj'); //секретный код

try {
    // Инициализация класса с id сайта и секретным ключом
    $wapkassa = new WapkassaClass(WK_ID, WK_SECRET);

    // Проверка обработчика (PING)
    if ($wapkassa->ping($_POST)) {
        // возврат успешной проверки
        echo $wapkassa->successPing();
    } else {
        // Парсинг входящих параметров
        $params = $wapkassa->parseRequest($_POST);
        
$pay = $db->prepare('SELECT * FROM `pay` WHERE `id` = ?');
$pay -> execute([$params['add']['user_id']]);
$pay = $pay -> fetch(PDO :: FETCH_ASSOC);
$client = $db->prepare('SELECT * FROM `platform` WHERE `id` = ?');
$client -> execute([$pay['platform']]);
$client = $client -> fetch(PDO :: FETCH_ASSOC);
$id = $params['add']['user_id'];
$moneys = $pay['summa'];
$time = vremja($pay['time']);
/*Присваиваем статус */
$sql = $db->prepare('UPDATE `pay` SET `status` = ?,`ot` = ? WHERE `id` = ?');
$sql -> execute(['yes',$out,$id]);

if( $curl = curl_init() ) {
    curl_setopt($curl, CURLOPT_URL, $client['success']);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, "MNM_PAY_ID={$params['add']['user_id']}&MNM_PRICE={$moneys}&MNM_VAR={$pay['var']}&MNM_DATE_PAY={$time}&MNM_PLATFORM={$client['id']}&MNM_COM={$pay['com']}");
    $out = curl_exec($curl);
    curl_close($curl);
}
if($out == 'NO'){
#header('location :'.$client['error']);
$sql = $db->prepare('UPDATE `pay` SET `status` = ?,`ot` = ? WHERE `id` = ?');
$sql -> execute(['bad',$out,$id]);
exit(print_r($out));

}else{
$sql = $db->prepare('UPDATE `pay` SET `status` = ?,`ot` = ? WHERE `id` = ?');
$sql -> execute(['yes',$out,$id]);
}
$money = $db->prepare('UPDATE `platform` SET `money` = ? WHERE `id` = ?');
$money -> execute([$client['money']+$moneys,$client['id']]);
echo $wapkassa->successPayment();

    }
} catch (Exception $e) {
    // вывод ошибки
    echo 'Ошибка: ' . $e->getMessage() . PHP_EOL;
}
