Commit 67f77f33286bc0c22e43a99ad4f6895b18ef556b
1 parent
5ded5781
api/client/payer
Showing
2 changed files
with
66 additions
and
2 deletions
Show diff stats
@@ -0,0 +1,60 @@ | @@ -0,0 +1,60 @@ | ||
1 | +<?php | ||
2 | + | ||
3 | +require_once("../commun.php"); | ||
4 | + | ||
5 | +verifierDroit(2); | ||
6 | + | ||
7 | +if (!(donne("idCarte") && (donne("montant") XOR donne("quantite")))) { | ||
8 | + retour("requete_malformee"); | ||
9 | +} | ||
10 | + | ||
11 | +if (!clientExiste(donne("idCarte"))) { | ||
12 | + retour("client_inconnu"); | ||
13 | +} | ||
14 | + | ||
15 | +if (donne("quantite")) { | ||
16 | + $quantite = intval($_POST["quantite"]); | ||
17 | + $requete = $db->prepare("SELECT prix FROM Prix"); | ||
18 | + $requete->bind_result($prixItem); | ||
19 | + if (!$requete->execute()) { | ||
20 | + retour("erreur_bdd", ["message" => $requete->error]); | ||
21 | + } | ||
22 | + $requete->fetch(); | ||
23 | + $requete->close(); | ||
24 | + | ||
25 | + $montant = $prixItem * $quantite; | ||
26 | +} else { | ||
27 | + $montant = floatval($_POST["montant"]); | ||
28 | +} | ||
29 | + | ||
30 | +if ($montant <= 0) { | ||
31 | + retour("paiement_negatif"); | ||
32 | +} | ||
33 | + | ||
34 | +$requete = $db->prepare("SELECT solde, decouvert FROM Clients WHERE idCarte=?"); | ||
35 | +$requete->bind_param("s", $_POST["idCarte"]); | ||
36 | +$requete->bind_result($soldeAncien, $decouvert); | ||
37 | +if (!$requete->execute()) { | ||
38 | + retour("erreur_bdd", ["message" => $requete->error]); | ||
39 | +} | ||
40 | +$requete->fetch(); | ||
41 | +$requete->close(); | ||
42 | + | ||
43 | +$soldeNouveau = $soldeAncien - $montant; | ||
44 | + | ||
45 | +if ($soldeNouveau < 0 && !$decouvert) { | ||
46 | + retour("solde_insuffisant", ["solde" => $soldeAncien, "manque" => abs($soldeNouveau)]); | ||
47 | +} | ||
48 | + | ||
49 | +$requete = $db->prepare("UPDATE Clients SET solde=? WHERE idCarte=?"); | ||
50 | +$requete->bind_param("ss", $soldeNouveau, $_POST["idCarte"]); | ||
51 | +if (!$requete->execute()) { | ||
52 | + retour("erreur_bdd", ["message" => $requete->error]); | ||
53 | +} | ||
54 | +$requete->close(); | ||
55 | +$id = transaction(TRANSACTION_PAIEMENT, $_POST["idCarte"], $montant); | ||
56 | + | ||
57 | + | ||
58 | +retour("ok", ["id" => $id, "soldeAncien" => $soldeAncien, "soldeNouveau" => $soldeNouveau]); | ||
59 | + | ||
60 | +?> |
api/client/recharger.php
@@ -14,9 +14,13 @@ if (!clientExiste(donne("idCarte"))) { | @@ -14,9 +14,13 @@ if (!clientExiste(donne("idCarte"))) { | ||
14 | 14 | ||
15 | $montant = floatval($_POST["montant"]); | 15 | $montant = floatval($_POST["montant"]); |
16 | 16 | ||
17 | +if ($montant <= 0) { | ||
18 | + retour("rechargement_negatif"); | ||
19 | +} | ||
20 | + | ||
17 | $requete = $db->prepare("SELECT solde FROM Clients WHERE idCarte=?"); | 21 | $requete = $db->prepare("SELECT solde FROM Clients WHERE idCarte=?"); |
18 | $requete->bind_param("s", $_POST["idCarte"]); | 22 | $requete->bind_param("s", $_POST["idCarte"]); |
19 | -$requete->bind_results($soldeAncien); | 23 | +$requete->bind_result($soldeAncien); |
20 | if (!$requete->execute()) { | 24 | if (!$requete->execute()) { |
21 | retour("erreur_bdd", ["message" => $requete->error]); | 25 | retour("erreur_bdd", ["message" => $requete->error]); |
22 | } | 26 | } |
@@ -34,6 +38,6 @@ $requete->close(); | @@ -34,6 +38,6 @@ $requete->close(); | ||
34 | $id = transaction(TRANSACTION_RECHARGEMENT, $_POST["idCarte"], $montant); | 38 | $id = transaction(TRANSACTION_RECHARGEMENT, $_POST["idCarte"], $montant); |
35 | 39 | ||
36 | 40 | ||
37 | -retour("ok", ["id" => $id, "montantAncien" => $montantAncien, "montantNouveau" => $montantNouveau]); | 41 | +retour("ok", ["id" => $id, "soldeAncien" => $soldeAncien, "soldeNouveau" => $soldeNouveau]); |
38 | 42 | ||
39 | ?> | 43 | ?> |