prepare("SELECT prix FROM Prix"); if (!$requete) { retour("erreur_bdd_preparee", ["message" => $db->error]); } $requete->bind_result($prixItem); if (!$requete->execute()) { retour("erreur_bdd", ["message" => $requete->error]); } $requete->fetch(); $requete->close(); $montant = $prixItem * $quantite; } else { $montant = floatval($_POST["montant"]); $quantite = 0; } if ($montant <= 0) { retour("paiement_negatif"); } $requete = $db->prepare("SELECT solde, decouvert FROM Clients WHERE idCarte=?"); if (!$requete) { retour("erreur_bdd_preparee", ["message" => $db->error]); } $requete->bind_param("s", $_POST["idCarte"]); $requete->bind_result($soldeAncien, $decouvert); if (!$requete->execute()) { retour("erreur_bdd", ["message" => $requete->error]); } $requete->fetch(); $requete->close(); $soldeNouveau = $soldeAncien - $montant; if ($soldeNouveau < 0 && !$decouvert) { retour("solde_insuffisant", ["solde" => $soldeAncien, "manque" => abs($soldeNouveau)]); } $requete = $db->prepare("UPDATE Clients SET solde=? WHERE idCarte=?"); if (!$requete) { retour("erreur_bdd_preparee", ["message" => $db->error]); } $requete->bind_param("ss", $soldeNouveau, $_POST["idCarte"]); if (!$requete->execute()) { retour("erreur_bdd", ["message" => $requete->error]); } $requete->close(); $idTransaction = transaction(TRANSACTION_PAIEMENT, $_POST["idCarte"], $montant, $quantite); retour("ok", ["idTransaction" => $idTransaction, "soldeAncien" => $soldeAncien, "soldeNouveau" => $soldeNouveau]); ?>