Notification API does not return data

Asked

Viewed 1,041 times

0

I’m having a problem with the Pagseguro Notification API, it’s not returning the XML in no way am I testing in HTTP, for in HTTPS also did not work.

Someone knows how to fix 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 tried to contact their support but I had a bad experience, then I would like to see with you, my code is wrong or would it be a problem with the pagseguro platform? I realized I’m not getting the XML, on the site of pagseguro they send the notification with status 200, and the communication with my page exists but it is as if they do not send anything, just access it, because it does not come the transition code in the logs... when I take the notification code in the pagseguro and enter the url manually, everything works perfectly... so I’m thinking that they are not sending the code, I just do not know whose the error is.

  • 1

    Inserts a var_dump($transaction);exit(2); after $transaction= curl_exec($curl); and put there what he returns.

  • 1

    @Jacksonantunes to save a "custom" log can do so: error_log(var_dump($transaction,true)). The function error_log() will save the content of var_dump(), can be print_r, string or anything else, in the same php log file, it makes it easy not to have to do file_put_contents() and similar

1 answer

2


The error was in the way I was trying to get the data because I didn’t quite understand how the notification api worked. Instead of the variable being $pagamento = $_GET['transaction_id']; era $pagamento = $_POST['notificationCode'];.

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

$pagamento = $_GET['transaction_id'];

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.