PagSeguro API does not return purchase status

-1

Previously I had opened an error question in the pagseguro API, well, I managed to solve it. Now my problem is another ... I can not receive the notifications of the pagseguro. I already configured the URL in the pagseguro panel, but I do not receive anything after the purchases. I tested the direct notification file in the browser and it sent me an email (which is what I want to do), but for the sake it does not return anything. My code is this:

<?php
if(isset($_POST['notificationType']) && $_POST['notificationType'] == 'transaction'){



    $email = '[email protected]';
    $token = 'MEU-TOKEN-PAGSEGURO';

    $url = 'https://ws.pagseguro.uol.com.br/v2/transactions/notifications/' . $_POST['notificationCode'] . '?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);

    if($transaction == 'Unauthorized'){


        exit;
    }

    $transaction = new SimpleXMLElement($transaction);

    $Ref    = $transaction->{'reference'};
    $Status = $transaction->{'status'};

    $headers  = "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html;charset=utf-8\r\n";
    $headers .= "From: ABC <[email protected]>\r\n";

   mail('[email protected]', 'Teste', 'Testando', $headers);


}


?>
    
asked by anonymous 18.09.2014 / 06:39

5 answers

1

You have not fully understood how payback is working so you're having trouble.

At the end of the purchase the payer sends to the return link but does not send any data as POST, just identify this and make the appropriate treatment:

<?php
if(isset($_POST['notificationType']) && $_POST['notificationType'] == 'transaction'){

// Post recebido, confirmar o código de notificação e processar o retorno para obter o status atual da transação.


    $email = '[email protected]';
    $token = 'MEU-TOKEN-PAGSEGURO';
...   
...   
...   

} else {

// POST não recebido, indica que a requisição é o retorno do Checkout PagSeguro.
// No término do checkout o usuário é redirecionado para este bloco.

    // redirecione para uma pagina de confirmação por ex.:
    header("Location: index.php?pag=fatura&retorno=pagseguro");

    // ou inclua aqui mesmo a mensagem, por ex:

    echo '<h4>Pagamento em Processamento</h4>
    <p>Seu pagamento será processado pelo PagSeguro e o recibo será emitido automaticamente.</p>
    <p>Obrigado.</p>
    <p><strong>Empresa XYz.</strong></p>';
}
?>
POST is sent by automatic returns, which are triggered when a customer makes a ticket payment (the other day), or when the purchase on the card is effected and confirmed by the carrier (a few minutes later), and these events do not occur at the exact moment the customer completes the purchase at the gateway ...

    
19.09.2014 / 16:36
1

It's good to check that email and tokens are correct:

As you can not print anything on the screen because PagSeguro sends this data via POST in another session, create a txt file (error.txt) and place an alert code within the authorization condition:

if($transaction == 'Unauthorized'){
    //Insira seu código avisando que o sistema está com problemas, sugiro enviar um e-mail avisando para alguém fazer a manutenção
    $name = 'erro.txt';
    $text = "Transação não foi validada!" . "\r\n";
    $file = fopen($name, 'a+');
    fwrite($file, $text);
    fclose($file);
    exit;//Mantenha essa linha
}

You can still check what you are getting from the payout with:

$name = 'erro.txt';
$text = var_export($transaction, true) . "\r\n";
$file = fopen($name, 'a+');
fwrite($file, $text);
fclose($file);
    
04.03.2016 / 16:10
0

To receive the automatic return of the Pagseguro, it is necessary that the server where your application is located is with ModSecurity turned off.

ModSecurity is a web application firewall that acts as an intrusion detection and prevention tool. Turning it off may be a risk to the application . But it is the only way to receive the data from Pagseguro.

Following is an example of setting up a Kinghost hosting environment link

    
19.09.2014 / 16:44
0

look what I saw should be what your friend William said because I'm using a code similar to yours and mine worked. more is simple only works if you receive the post of uol. has the test environment of uol also in it has a return in a log file there to see what the error is giving.

    
30.07.2015 / 00:31
0

Verify that Mod_Security is enabled and blocking the replies from the pagseguro. On my site this was the problem and solved by doing the following:

  • Go to the logs of your apacha, usually in the folder /usr/local/apache/logs/error_log and search for pagseguro .
  • When it finds, it searches the same line for the id of this transaction, usually [id=xxxxxx] (a random number).
  • Then go to the Mod_Security settings (if you have access) that usually stays in /usr/local/apache/modsecurity-xxx/xxx.conf and add a line with it in the configuration: SecRuleRemoveById xxxxxx

Save, restart the httpd and test. The sites may be different because I use the Centos Web Panel, but from what I saw the location of the error_log is usually the same, but of mod_security not ... then you have to find it.

    
05.04.2017 / 09:44