<?php 

// ------- Include -------
include '../../config.php';
$peyment = mysqli_fetch_assoc(mysqli_query($connect,"SELECT * FROM peyment LIMIT 1"));
$merichentzarin = $peyment['merichentzarin'];
// ------- Get -------
$user = $_GET['id'];
$amount = $_GET['amount'] * 10;

$sqlCdd    = "SELECT `id` FROM `user` WHERE `id`=$user";
$resultCdd = mysqli_query($connect,$sqlCdd);

$resCdd = mysqli_fetch_assoc($resultCdd);
if(isset($resCdd)){

$data = array("merchant_id" => "$merichentzarin",
    "amount" => $amount,
    "callback_url" => "$web/pay/zarin/back2.php?amount=$amount&id=$user",
    "description" => "خرید از ربات",
    "metadata" => [ "email" => "NOMAIL",null],
    );
$jsonData = json_encode($data);
$ch = curl_init('https://api.zarinpal.com/pg/v4/payment/request.json');
curl_setopt($ch, CURLOPT_USERAGENT, 'ZarinPal Rest Api v1');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($jsonData)
));

$result = curl_exec($ch);
$err = curl_error($ch);
$result = json_decode($result, true, JSON_PRETTY_PRINT);
curl_close($ch);



if ($err) {
    echo "cURL Error #:" . $err;
} else {
    if (empty($result['errors'])) {
        if ($result['data']['code'] == 100) {
            header('Location: https://www.zarinpal.com/pg/StartPay/' . $result['data']["authority"]);
        }
    } else {
         echo'Error Code: ' . $result['errors']['code'];
         echo'message: ' .  $result['errors']['message'];

    }
}}
else{
    echo"ایدی عددی وارد شده اشتباه است";
}
?>