<?php 

// ------- Include -------
include '../../config.php';
$peyment = mysqli_fetch_assoc(mysqli_query($connect,"SELECT * FROM peyment LIMIT 1"));
$merichentziball = $peyment['merichentzib'];
// ------- Get -------
function postToZibal($path, $parameters)
{
    $url = 'https://gateway.zibal.ir/v1/'.$path;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($parameters));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response  = curl_exec($ch);
    curl_close($ch);
    return json_decode($response);
}
$user = $_GET['id'];
$amount = $_GET['amount'];
$sqlCdd    = "SELECT `id` FROM `user` WHERE `id`=$user";
$resultCdd = mysqli_query($connect,$sqlCdd);

$resCdd = mysqli_fetch_assoc($resultCdd);
if(isset($resCdd)){
$parameters = array(
    "merchant"=> $merichentziball,//required
    "callbackUrl"=> "$web/pay/ziball/back.php?amount=$amount&id=$user",//required
    "amount"=> $amount,//required
);

$response = postToZibal('request', $parameters);
if ($response->result == 100)
{
    $startGateWayUrl = "https://gateway.zibal.ir/start/".$response->trackId;
    header('location: '.$startGateWayUrl);
}
else{
    echo "errorCode: ".$response->result."<br>";
    echo "message: ".$response->message;
}
}
?>