<?php
//ini_set('display_errors',1);
//ini_set('display_startup_erros',1);
//error_reporting(E_ALL);

include("db_config.php");

$agora = date("j/n/Y H:i:ss");

use OpenPix\PhpSdk\Client;



$identificador = substr(strtoupper(hash('md2', $agora)), 0, 16);

$token = $_POST["token"];
$email = $_POST["user"];
$name=$_POST["nome"];
$phone=$_POST["telefone"];
$cpf=$_POST["cpf"];
$valorvenda=$_POST["valor"];

$vamount = str_replace(",", ".", $valorvenda);


$busca = mysqli_query($conexao,"SELECT * FROM users where UUIDN = '$token' and email = '$email';");
  $num_results = mysqli_num_rows($busca);
  if ($num_results > 0){
      while ($anu = mysqli_fetch_array($busca)){
          $id = $anu[id];
          $username= $anu[username];
          $fname = $anu[fname];
          $lname = $anu[lname];
          $pais = $anu[pais];
          $gateway_charge = $anu[gateway_charge];
      };
      
$vcharge = (($vamount * $gateway_charge) / 100);
$final_amount = $vamount + (($vamount * $gateway_charge) / 100);
$valorf = number_format($final_amount, 2, '.', '.');
$valor = str_replace(".", "", $valorf);

require_once('../vendor/autoload.php');

$client = Client::create("Q2xpZW50X0lkX2UzZTNhOTE5LThkZjUtNGNlMS1iNWQ5LWE5YzhhYjY1YzY0NzpDbGllbnRfU2VjcmV0X3NPbnd2ZG9FUGV0bTZhWnlLb0xmc0RoZmRuM0FNbm5ubWpaRUNUYURHMVk9");
//$client = Client::create("Q2xpZW50X0lkXzJmN2ZlZTVkLWExODQtNDE3NC1hZmRjLTJiZTBlMzM2MzM5MTpDbGllbnRfU2VjcmV0XzdKdzc1MEtuWDB6QSs2TkorckZBOGs3dGRLRkp5a05NS1BYc2dYVGFiaEU9"); //SACliente

// Create a customer.
$customer = [
    "name" => $name,
    "taxID" => $cpf, // CPF
    //"email" => "alinemdst@gmail.com",
    "phone" => $phone,
    //"correlationID" => "TMFAL8AZMWHU67P5-" . mt_rand(1, 10000),
    //"correlationID" => "b807235d-e535-11ee-8f95-fa163e594a80",
];

//$client->customers()->create($customer);

// Create a charge using above customer.
$charge = [
    // Charge value.
    "value" => $valor, // (R$ 1,00)

    // Your correlation ID to keep track of this charge.
    //"correlationID" => "TMFAL8AZMWHU67P5-" . mt_rand(1, 10000),
    "correlationID" => $identificador,
    "expiresIn" => "300",

    // Charge customer.
    "customer" => $customer,
];

$result = $client->charges()->create($charge);

// Get the generated dynamic BR code to be rendered as a QR Code.
//echo $result["brCode"] . "\n";
//echo "<br><br>";

$json01 = json_encode($result);
//echo $json01;


$json02 = json_decode($json01);

$identifier = $json02->charge->identifier;
$value = $json02->charge->value;
$status = $json02->charge->status;
$expiresDate = $json02->charge->expiresDate;
$brCode = $json02->charge->brCode;
$paymentLinkUrl = $json02->charge->paymentLinkUrl;
$qrCodeImage = $json02->charge->qrCodeImage;

$consult1 = '{"identifier":"' . $identifier . '","value":"' . $value . '","status":"' . $status . '","expiresDate":"' . $expiresDate . '","brCode":"' . $brCode . '","paymentLinkUrl":"' . $paymentLinkUrl . '","qrCodeImage":"' . $qrCodeImage . '"}';

$consult2 = json_encode($consult1);
$consult3 = json_decode($consult2);

echo $consult3;

mysqli_query($conexao, "INSERT INTO deposits(user_id, gateway_id, transaction_id, amount, rate, charge, final_amount, payment_status, payment_type, payment_proof, nome, cpf, telefone, identifier, status, expiresDate, paymentLinkUrl, pix, qrcode) VALUES ('$id','3', '$identificador', '$vamount', '0', '$vcharge', '$valorf', '2', '0', '', '$name', '$cpf', '$phone', '$identifier', '$status', '$expiresDate', '$paymentLinkUrl', '$brCode', '$qrCodeImage')");

}else{
     echo "Usuário não encontrado"; 
};




?>