Pagseguro notification API does not return data

0

I am having a problem with the pagseguro notification API, it is not returning the XML in any way, I'm testing on HTTP , because in HTTPS also did not work ... Does anyone know how to solve this?

Code:

<?php
require("includes/connection.php"); //meu banco de dados
require("includes/pagSeguro.php");

header("access-control-allow-origin: https://ws.pagseguro.uol.com.br");
$email     = '[email protected]';
$token     = 'token';
$pagamento = $_GET['transaction_id'];

$url = 'https://ws.pagseguro.uol.com.br/v3/transactions/notifications/' . $pagamento . '?email=' . $email . '&token=' . $token;



$curl = curl_init($url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$transaction = curl_exec($curl);
curl_close($curl);

$dominio = $_SERVER['HTTP_HOST'];

if ($transaction == 'Unauthorized') {
    $name = 'unauthorizeds.txt';
    $text = " A transação não foi validada!" . "\r\n";
    $file = fopen($name, 'a+');
    fwrite($file, $text);
    fclose($file);
    exit;
}
$transaction = simplexml_load_string($transaction);
date_default_timezone_set('America/Sao_Paulo');
$date = date('d-m-Y H:i');
$name = 'transaction.txt';
$text = $url = $date . " " . $transaction . "\r\n";
$file = fopen($name, 'a+');
fwrite($file, $text);
fclose($file);

if ($transaction->status >= 1) {
    $transaction_id = $transaction->code;
    $client_id      = $transaction->reference;
    $payment_type   = $transaction->paymentMethod->type;
    if ($payment_type == 1) {
        $payment_method = "Cartão de crédito";
    } elseif ($payment_type == 2) {
        $payment_method = "Boleto";
    } elseif ($payment_type == 3) {
        $payment_method = "Débito online (TEF)";
    } else {
        $payment_method = "Outro";
    }
    $payment_type_method = $transaction->type;
    if ($payment_type_method == 1) {
        $payment_method_transaction = "Pagamento";
    } elseif ($payment_type_method == 11) {
        $payment_method_transaction = "Assinatura";
    } else {
        $payment_method_transaction = "Outro";
    }
    $client_email     = $transaction->sender->email;
    $parceled         = $transaction->installmentCount;
    $parceled_value   = $transaction->installmentFeeAmount;
    $product          = $transaction->items->item->id;
    $product_value    = $transaction->items->item->amount;
    $transaction_date = date('Y-m-d H:i:s', strtotime($transaction->date));

    $transaction_date_last = date('Y-m-d H:i:s', strtotime($transaction->lastEventDate));
    if ($transaction->status == 1) {
        $transaction_status = 'Aguardando pagamento';
    } elseif ($transaction->status == 2) {
        $transaction_status = 'Em análise';
    } elseif ($transaction->status == 3) {
        $transaction_status = 'Paga';
    } elseif ($transaction->status == 4) {
        $transaction_status = 'Disponível';
    } elseif ($transaction->status == 5) {
        $transaction_status = 'Em disputa';
    } elseif ($transaction->status == 6) {
        $transaction_status = 'Devolvida';
    } elseif ($transaction->status == 7) {
        $transaction_status = 'Cancelada';
    }
    $client_name = $transaction->sender->name;
    $select      = $mysqli->query("select * from payments where transaction_id = '$transaction->code' ");
    $row         = $select->num_rows;
    $get         = $select->fetch_array();
    if ($row >= 1) {
        $update = $mysqli->query("UPDATE payments SET transaction_id = '$transaction_id', client_email = '$client_email', payment_method = '$payment_method', payment_method_transaction = '$payment_method_transaction', transaction_status = '$transaction_status', transaction_date_last = '$transaction_date_last', product = '$product', product_value = '$product_value', client_name = '$client_name' WHERE transaction_id = '$transaction->code'");
        if ($update) {
            $select       = $mysqli->query("select * from payments where transaction_id = '$transaction->code' ");
            $row          = $select->num_rows;
            $get          = $select->fetch_array();
            $foiPago      = $get['transaction_status'];
            $status       = $get['status'];
            $product_cash = $get['product'];
            if ($foiPago == 'Paga' or $foiPago == 'Disponivel' and $status == '0') {
                $select = $mysqli->query("select * from data where email = '$client_email' ");
                $row    = $select->num_rows;
                $get    = $select->fetch_array();
                $cash   = $get['cash'];
                if ($product_cash == '50KP') {
                    $cash_update = $cash + 50;
                    $update      = $mysqli->query("UPDATE data SET cash = '$cash_update' WHERE email = '$client_email'");
                    $update      = $mysqli->query("UPDATE payments SET status = '1' WHERE transaction_id = '$transaction->code'");
                }
                if ($product_cash == '200KP') {
                    $cash_update = $cash + 200;
                    $update      = $mysqli->query("UPDATE data SET cash = '$cash_update' WHERE email = '$client_email'");
                    $update      = $mysqli->query("UPDATE payments SET status = '1' WHERE transaction_id = '$transaction->code'");
                }
                if ($product_cash == '1000KP') {
                    $cash_update = $cash + 1000;
                    $update      = $mysqli->query("UPDATE data SET cash = '$cash_update' WHERE email = '$client_email'");
                    $update      = $mysqli->query("UPDATE payments SET status = '1' WHERE transaction_id = '$transaction->code'");
                }
                if ($product_cash == '2000KP') {
                    $cash_update = $cash + 2000;
                    $update      = $mysqli->query("UPDATE data SET cash = '$cash_update' WHERE email = '$client_email'");
                    $update      = $mysqli->query("UPDATE payments SET status = '1' WHERE transaction_id = '$transaction->code'");
                }

            }
        } else {
            printf("Erro do BD de atualização : %s\n", $mysqli->error);
        }
    } else {

        $insert = $mysqli->query("INSERT INTO 'payments'('transaction_id', 'client_email', 'payment_method', 'payment_method_transaction', 'transaction_status', 'transaction_date', 'transaction_date_last', 'product', 'product_value', 'client_name') VALUES ('$transaction_id', '$client_email', '$payment_method', '$payment_method_transaction', '$transaction_status', '$transaction_date', '$transaction_date_last', '$product', '$product_value', '$client_name')");
        if ($insert) {
            $select       = $mysqli->query("select * from payments where transaction_id = '$transaction->code' ");
            $row          = $select->num_rows;
            $get          = $select->fetch_array();
            $foiPago      = $get['transaction_status'];
            $status       = $get['status'];
            $product_cash = $get['product'];
            if ($foiPago == 'Paga' or $foiPago == 'Disponivel' and $status == '0') {
                $select = $mysqli->query("select * from data where email = '$client_email' ");
                $row    = $select->num_rows;
                $get    = $select->fetch_array();
                $cash   = $get['cash'];
                if ($product_cash == '50KP') {
                    $cash_update = $cash + 50;
                    $update      = $mysqli->query("UPDATE data SET cash = '$cash_update' WHERE email = '$client_email'");
                    $update      = $mysqli->query("UPDATE payments SET status = '1' WHERE transaction_id = '$transaction->code'");
                }
                if ($product_cash == '200KP') {
                    $cash_update = $cash + 200;
                    $update      = $mysqli->query("UPDATE data SET cash = '$cash_update' WHERE email = '$client_email'");
                    $update      = $mysqli->query("UPDATE payments SET status = '1' WHERE transaction_id = '$transaction->code'");
                }
                if ($product_cash == '1000KP') {
                    $cash_update = $cash + 1000;
                    $update      = $mysqli->query("UPDATE data SET cash = '$cash_update' WHERE email = '$client_email'");
                    $update      = $mysqli->query("UPDATE payments SET status = '1' WHERE transaction_id = '$transaction->code'");
                }
                if ($product_cash == '2000KP') {
                    $cash_update = $cash + 2000;
                    $update      = $mysqli->query("UPDATE data SET cash = '$cash_update' WHERE email = '$client_email'");
                    $update      = $mysqli->query("UPDATE payments SET status = '1' WHERE transaction_id = '$transaction->code'");
                }

            }
        } else {
            printf("Erro do BD : %s\n", $mysqli->error);
        }
    }
} else {
    date_default_timezone_set('America/Sao_Paulo');
    $date = date('d-m-Y H:i');
    $name = 'invalids.txt';
    $text = $url = $date . " https://" . $dominio . $_SERVER['REQUEST_URI'] . "\r\n";
    $file = fopen($name, 'a+');
    fwrite($file, $text);
    fclose($file);
    exit;
}
?>

I have tried to contact them, but I had a bad experience, so I would like to see you, my code is wrong or would it be a problem with the pagseguro platform? I noticed that I am not receiving the XML , in the pagseguro site they send the notification with status 200, and the communication with my page exists however it is as if they did not send anything, just access it, transition code in the logs ... when I get the notification code in the pagseguro and I enter in the url manually, everything works perfectly ... so I'm thinking they are not sending the code, I just do not know who the error is. / p>     

asked by anonymous 07.08.2016 / 20:50

1 answer

2

The error was in the way that I was trying to get the data, because I did not really understand how the notification api worked. Instead of the variable being $pagamento = $_GET['transaction_id']; was $pagamento = $_POST['notificationCode']; .

In the case of transaction_id is for when the PagSeguro redirects the client to a page pre-defined by the user, then the information would be taken that way

$pagamento = $_GET['transaction_id'];

In the case of notification api I want the notification code, not the transaction code ... for this I get it using the notificationCode parameter.

    
14.08.2016 / 22:05