I can’t save the information in the bank

Asked

Viewed 442 times

0

I wonder if there’s something wrong:

if(isset($_POST['notificationType']) && $_POST['notificationType'] == 'transaction'){
//Todo resto do código iremos inserir aqui.

$email = 'douglas@...';
$token = '95112...';

$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'){
    //Insira seu código avisando que o sistema está com problemas, sugiro enviar um e-mail avisando para alguém fazer a manutenção
    $resultado = "Não autorizado!";
    exit;//Mantenha essa linha
}
$transaction = simplexml_load_string($transaction);

$transactionStatus = $transaction->status;  
if($transactionStatus > 0) {


    $TransacaoID = $transaction->code;
    $Referencia = $transaction->reference;        
    $mpresult = $transaction->paymentMethod->type;
    if($mpresult == 1){ 
        $MetodoPagamento = "Cartão de crédito";
    } elseif($mpresult == 2){ 
        $MetodoPagamento = "Boleto";
    } elseif($mpresult == 3){ 
        $MetodoPagamento = "Débito online (TEF)"; 
    } else { 
        $MetodoPagamento = "Outro"; 
    }
    $DataTransacao = date('d/m/Y', strtotime($transaction->date));
    if($transactionStatus == 1){
        $transactionStatus = 'Aguardando pagamento';
    } elseif($transactionStatus == 2){
        $transactionStatus = 'Em análise';
    } elseif($transactionStatus == 3){ // :)
        $transactionStatus = 'Paga';
    } elseif($transactionStatus == 4){ // :D
        $transactionStatus = 'Disponível';
    } elseif($transactionStatus == 5){
        $transactionStatus = 'Em disputa';
    } elseif($transactionStatus == 6){
        $transactionStatus = 'Devolvida';
    } elseif($transactionStatus == 7){
        $transactionStatus = 'Cancelada';
    }   
    $CliNome = $transaction->sender->name;


    $sql = mysql_query("INSERT INTO pedidos (TransacaoID,Referencia,MetodoPagamento,StatusTransacao,CliNome) VALUES ('".$TransacaoID."', '".$Referencia."', '".$MetodoPagamento."', '".$transactionStatus."', '".$CliNome."')");

} } else {
echo "Transação concluida com sucesso!"; }
  • I made the connection to the bank externally.

  • 1

    What is the error? Are you sure the script will reach the query execution? Note: avoid using the functions mysql_*...

  • problem solved..

  • 1

    Share the solution by answering your own question, or feel free to delete the question...

1 answer

4

You need to make an appointment with the transaction code to save the information in the database, the problem is that it was using a notifying. Then I took the first condition that checked if it was a notification and changed the URL.

Look at the code:

header("access-control-allow-origin: https://sandbox.pagseguro.uol.com.br");

$email = 'douglas...';
$token = '4360...';

$codigotransicao = $_GET['pagamento'];
$url = 'https://ws.sandbox.pagseguro.uol.com.br/v2/transactions/'. $codigotransicao .'?email=' .$email. '&token=' .$token;

// Errado $url = 'https://ws.sandbox.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'){
    //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 = 'log.txt';
    $text = " A transação não foi validada!" . "\r\n";
    $file = fopen($name, 'a+');
    fwrite($file, $text);
    fclose($file);
    exit;//Mantenha essa linha
}
$transaction = simplexml_load_string($transaction);

if($transaction->code > 0) {
$TransacaoID = $transaction->code;
$CompradorID = $transaction->reference;        
    $mpresult = $transaction->paymentMethod->type;
    if($mpresult == 1){ 
        $MetodoPagamento = "Cartão de crédito";
    } elseif($mpresult == 2){ 
        $MetodoPagamento = "Boleto";
    } elseif($mpresult == 3){ 
        $MetodoPagamento = "Débito online (TEF)"; 
    } else { 
        $MetodoPagamento = "Outro"; 
    }
    $nParcelas = $transaction->installmentCount;
    $TaxaPacelas = $transaction->installmentFeeAmount;
    $DataTransacao = date('d/m/Y', strtotime($transaction->date));
    if($transaction->status == 1){
        $transactionStatus = 'Aguardando pagamento';
    } elseif($transaction->status == 2){
        $transactionStatus = 'Em análise';
    } elseif($transaction->status == 3){ // :)
        $transactionStatus = 'Paga';
    } elseif($transaction->status == 4){ // :D
        $transactionStatus = 'Disponível';
    } elseif($transaction->status == 5){
        $transactionStatus = 'Em disputa';
    } elseif($transaction->status == 6){
        $transactionStatus = 'Devolvida';
    } elseif($transaction->status == 7){
        $transactionStatus = 'Cancelada';
    }   
    $CliNome = $transaction->sender->name;

    $sql = mysql_query("INSERT INTO pedidos (TransacaoID,CompradorID,MetodoPagamento,nParcelas,TaxaParcelas,StatusTransacao,CliNome) VALUES ('".$TransacaoID."', '".$CompradorID."', '".$MetodoPagamento."', '".$nParcelas."', '".$TaxaParcelas."', '".$transactionStatus."', '".$CliNome."')") or die(mysql_error());

}

Browser other questions tagged

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