I need to set up a query where I will only have 5 fields ( STATUS
, ID_USUARIO
, PACKED
, ID_TRANSACAO
and ENTREGUE
).
I need when I change from STATUS
change in another table, and update in the current field the ENTREGUE
from "N" to "S" and do not carry out any query in the one that is already delivered.
At the same time, I need the PACKED
to identify how much the amount will be delivered because it is being ID, that is, id 1 corresponds to 30 Chips ...
Below my status code; I have no idea how to make this logic work.
<?php
require_once("core/config/config.php");
if (isset($_POST['notificationType']) && $_POST['notificationType'] == 'transaction') {
//CÓDIGO DA NOTIFICAÇÃO
$code = $_POST['notificationCode'];
//MONTAMOS A URL PARA PEGAR O STATUS
$url = 'https://ws.sandbox.pagseguro.uol.com.br/v2/transactions/notifications/' . $code . '?email=' . $email . '&token=' . $token;
//PEGAMOS O STATUS VIA CURL
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$transaction= curl_exec($curl);
curl_close($curl);
//SE DER ALGUM ERRO NO NOSSO CURL VAI VIM COMO Unauthorized
if($transaction == 'Unauthorized'){
exit;//Mantenha essa linha
}
$pdo = conectaDB();
$get = $pdo->prepare("SELECT * FROM pagseguro");
$get->execute();
$query = $get->fetchAll(PDO::FETCH_ASSOC);
foreach ($query as $row) {
$user = $row['id_user'];
$codigo_transacao = $row['id_transacao'];
$codigo = $row['packed'];
}
$entregue = 'S';
switch ($query->$codigo) {
case '1':
$chips = '30.00';
break;
case '2':
$chips = '50.00';
break;
case '3':
$chips = '100.00';
break;
case '4':
$chips = '250.00';
break;
}
//COMVERTE O RERTORNO EM STRING
$transaction = simplexml_load_string($transaction);
/*
* STATUS RETORNO
* 1 - Aguardando pagamento
* 2 - Em análise
* 3 - Paga
* 4 - Disponível
* 5 - Em disputa
* 6 - Devolvida
* 7 - Cancelada
*/
//PEGAMOS O CÓDIGO DO STATUS
switch ($transaction->status) {
case '1':
$status = 'Aguardando pagamento';
break;
case '2':
$status = 'Em análise';
break;
case '3':
$status = 'Paga';
$edit2 = $pdo->prepare("UPDATE pagseguro SET entregue=:entregue WHERE id_transacao=:id_transacao");
$edit2->bindValue(':entregue', $entregue);
$edit2->bindValue(':id_transacao', $transaction->code);
$edit2->execute();
// SECOND CASE
$update = $pdo->prepare("UPDATE users SET credits=:credits WHERE username=:user_pagseguro");
$update->bindValue(':credits', $chips);
$update->bindValue(':user_pagseguro', $user);
$update->execute();
break;
case '4':
$status = 'Disponível';
break;
case '5':
$status = 'Em disputa';
break;
case '6':
$status = 'Devolvida';
break;
case '7':
$status = 'Cancelada';
break;
}
//ATUALIZAMOS O STATUS NO BANCO DADOS.
$pdo = conectaDB();
$edit = $pdo->prepare("UPDATE pagseguro SET status=:status WHERE id_transacao=:id_transacao");
$edit->bindValue(':status', $status);
$edit->bindValue(':id_transacao', $transaction->code);
$edit->execute();
}
?>