<?php include('ApiConfig.php');
require_once("../conexao.php");

$id = $_GET['id'];

$nome_aluno = "Teste";
$email_aluno = "emailteste@hotmail.com";


$query2 = $pdo->query("SELECT * FROM pgto_matriculas where id = '$id'");
$res2 = $query2->fetchAll(PDO::FETCH_ASSOC);
if(@count($res2) > 0){
     $matricula = $res2[0]['matricula'];
     $valor = $res2[0]['valor'];
     $valor_pgto = number_format($valor, 2);

}

$query2 = $pdo->query("SELECT * FROM matriculas where id = '$matricula'");
$res2 = $query2->fetchAll(PDO::FETCH_ASSOC);
if(@count($res2) > 0){
     $aluno = $res2[0]['aluno'];
     
}



$query2 = $pdo->query("SELECT * FROM alunos where id = '$aluno'");
$res2 = $query2->fetchAll(PDO::FETCH_ASSOC);
if(@count($res2) > 0){
    $nome_aluno = $res2[0]['nome'];
    $email_aluno = $res2[0]['email'];
    $cpf_aluno = $res2[0]['cpf'];

    if($cpf_aluno == ""){
           $cpf_aluno = '34152426764';
    }

    $cpf_aluno = str_replace('.', '', $cpf_aluno);
    $cpf_aluno = str_replace('-', '', $cpf_aluno);

}


?>

<?php
$curl = curl_init();
    
    $dados["external_reference"]                    = $id;
    $dados["transaction_amount"]                    = (float)$valor_pgto;
    $dados["description"]                           = 'Pagamento Mensalidade';
    $dados["payment_method_id"]                     = "bolbradesco";
    $dados["notification_url"]                      = "https://google.com";
    $dados["payer"]["email"]                        = $email_aluno;
    $dados["payer"]["first_name"]                   = "Aluno ";
    $dados["payer"]["last_name"]                    = $nome_aluno;
    
    $dados["payer"]["identification"]["type"]       = "CPF";
    $dados["payer"]["identification"]["number"]     = '34152426764';
    
    $dados["payer"]["address"]["zip_code"]          = "06233200";
    $dados["payer"]["address"]["street_name"]       = "Av. das Nações Unidas";
    $dados["payer"]["address"]["street_number"]     = "3003";
    $dados["payer"]["address"]["neighborhood"]      = "Bonfim";
    $dados["payer"]["address"]["city"]              = "Osasco";
    $dados["payer"]["address"]["federal_unit"]      = "SP";

    curl_setopt_array($curl, array(
        CURLOPT_URL => 'https://api.mercadopago.com/v1/payments',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS => json_encode($dados),
    CURLOPT_HTTPHEADER => array(
        'accept: application/json',
        'content-type: application/json',
        'X-Idempotency-Key: '.date('Y-m-d-H:i:s-').rand(0, 1500),
        'Authorization: Bearer '.$access_token
    ),
    ));
    $response = curl_exec($curl);
    $resultado = json_decode($response);
    //var_dump($response);
curl_close($curl);
?>



<?php 

//echo $resultado->transaction_details->external_resource_url;

$pdo->query("UPDATE pgto_matriculas SET boleto = '$resultado->id' where id = '$id' ");

echo "<script>window.location='".$resultado->transaction_details->external_resource_url."'</script>";


?>   
    
      
       









       
