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

include("db_config.php");

cors();
function cors() {
    
    // Allow from any origin
    if (isset($_SERVER['HTTP_ORIGIN'])) {
        // Decide if the origin in $_SERVER['HTTP_ORIGIN'] is one
        // you want to allow, and if so:
        header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Max-Age: 86400');    // cache for 1 day
    }
    
    // Access-Control headers are received during OPTIONS requests
    if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
        
        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
            // may also be using PUT, PATCH, HEAD etc
            header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
        
        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
            header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
    
        exit(0);
    }
    
    //echo "You have CORS!";
}

$identificador=$_GET["identificador"];

$str = strtoupper($identificador);

$url = 'https://api.woovi.com/api/v1/charge/' . $identificador; 
//echo $url;
//echo '<br>';

$curl = curl_init();

curl_setopt_array($curl, [
  CURLOPT_URL => $url,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => [
    "Authorization:Q2xpZW50X0lkXzJmN2ZlZTVkLWExODQtNDE3NC1hZmRjLTJiZTBlMzM2MzM5MTpDbGllbnRfU2VjcmV0X0E2U2N1Qyt4TEZkci9VdXdkVEI3VmVzZHZRbHQyRlFkZ0duU1JkbnJKb2M9"
  ],
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

//if ($err) {
//  echo "cURL Error #:" . $err;
//} else {
//  echo $response;
//}

$result = $response;

$json02 = json_decode($result);
//$json02 = json_decode($json01);

//echo $json01->charge->identifier;
//var_dump($json01);





$identifier = $json02->charge->identifier;
$status = $json02->charge->status;

$nome = $json02->charge->customer->name;
$telefone = $json02->charge->customer->phone;
$cpf = $json02->charge->customer->taxID->taxID;

//COMPLETED

//$identifier = $identificador;
//$status = "COMPLETED";

$consult1 = '{"identifier":"' . $identifier . '","status":"' . $status . '"}';
//$consult1 = '{"identifier":"' . $identifier . '","status":"EXPIRED"}';

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

echo $consult3;

if ($status == "COMPLETED"){
    $busca = mysqli_query($conexao,"SELECT * FROM deposits WHERE identifier = '$identifier'");
      while ($anu = mysqli_fetch_array($busca)){
      $id = $anu[id];
      $user_id = $anu[user_id];
      $gateway_id = $anu[gateway_id];
      $trx = $anu[transaction_id];
      $amount = $anu[amount];
      $dep_payment_status = $anu[payment_status];
      $charge = $anu[charge];
      $final_amount = $anu[final_amount]; 
      $currency = "BRL"; 
      $details = "PIX";
      $type = "+";
      $payment_status = "1";
      $gateway_transaction = $anu[identifier];

    };

 $busca2 = mysqli_query($conexao,"SELECT * FROM users WHERE id = '$user_id'");
      while ($anu2 = mysqli_fetch_array($busca2)){
      $balance = $anu2[balance];
    };
    $newbalance = ($balance + $amount);
   
    if ($dep_payment_status != 1) {
        mysqli_query($conexao, "INSERT INTO transactions(trx, gateway_transaction,user_id,gateway_id,amount,currency,charge,details,type,payment_status) VALUES('$trx','$gateway_transaction','$user_id','$gateway_id','$amount','$currency','$charge','$details','$type','$payment_status')");
        mysqli_query($conexao, "UPDATE deposits SET payment_status = '1', status = 'COMPLETED', nome = '$nome', cpf = '$cpf', telefone = '$telefone' WHERE id = $id");
        mysqli_query($conexao, "UPDATE users SET balance = $newbalance WHERE id = '$user_id'");
    };


} elseif ($status == "EXPIRED") {
mysqli_query($conexao, "UPDATE deposits SET payment_status ='3' WHERE id = $id");
    
};

?>