<?php
require ($_SERVER['DOCUMENT_ROOT'].'/core/sys.php');
$all = $db->prepare("SELECT * FROM `pay` WHERE `status` != 'no' AND `status` != 'yes' ORDER BY `id` DESC LIMIT 10");
$all -> execute();
$all = $all -> fetchAll();
foreach($all as $pay){
$client = $db->prepare('SELECT * FROM `platform` WHERE `id` = ?');
$client -> execute([$pay['platform']]);
$client = $client -> fetch(PDO :: FETCH_ASSOC);

if( $curl = curl_init() ) {
$time = $pay['date'];
$hash = strtoupper(hash('sha256', implode(":", $client['key'].$client['id'])));
$params = [
'WP_PAYMENT_SITE' => $client['id'],
'WP_PAYMENT_AMOUNT' => $pay['summa'],
'WP_PAYMENT_VAR' => $pay['var'],
'WP_PAYMENT_DATE' => $time,
'WP_PAYMENT_ID' => $pay['id'],
'WP_PAYMENT_COM' => $pay['com'],
'WP_PAYMENT_HASH' => $hash,
];

curl_setopt($curl, CURLOPT_URL, $client['success']);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params));
#curl_setopt($curl, CURLOPT_HEADER, 1);
$out = curl_exec($curl);
curl_close($curl);
}


if($out == 'YES|'.$pay['id']){
$sql = $db->prepare('UPDATE `pay` SET `status` = ? WHERE `id` = ?');
$sql -> execute(['yes',$pay['id']]);
if($client['type'] == 3){
$money = $db->prepare('UPDATE `platform` SET `type` = ? WHERE `id` = ?');
$money -> execute([1,$client['id']]);
}
if($client['type'] == 1){
$money = $db->prepare('UPDATE `platform` SET `money` = `money` + ? WHERE `id` = ?');
$money -> execute([$pay['summa'],$client['id']]);
}
}else{
$sql = $db->prepare('UPDATE `pay` SET `status` = ? WHERE `id` = ?');
$sql -> execute(['bad',$pay['id']]);
}
$out = ($out);
$sql = $db->prepare('UPDATE `pay` SET `ot` = ? WHERE `id` = ?');
$sql -> execute([$out,$pay['id']]);
print_r($out);
}
