Commit 9f8246fd201d13168417222eb9d2b9b52710d0df
1 parent
e00627b4
api/client/recharger
Showing
3 changed files
with
45 additions
and
1 deletions
Show diff stats
api/client/ajouter.php
... | ... | @@ -32,7 +32,7 @@ if (!$requete->execute()) { |
32 | 32 | retour("erreur_bdd", ["message" => $requete->error]); |
33 | 33 | } |
34 | 34 | $requete->close(); |
35 | -$id = transaction(1, $_POST["idCarte"], $solde); | |
35 | +$id = transaction(TRANSACTION_CREATION, $_POST["idCarte"], $solde); | |
36 | 36 | |
37 | 37 | |
38 | 38 | retour("ok", ["id" => $id]); | ... | ... |
... | ... | @@ -0,0 +1,39 @@ |
1 | +<?php | |
2 | + | |
3 | +require_once("../commun.php"); | |
4 | + | |
5 | +verifierDroit(2); | |
6 | + | |
7 | +if (!(donne("idCarte") && donne("montant"))) { | |
8 | + retour("requete_malformee"); | |
9 | +} | |
10 | + | |
11 | +if (!clientExiste(donne("idCarte"))) { | |
12 | + retour("client_inconnu"); | |
13 | +} | |
14 | + | |
15 | +$montant = floatval($_POST["montant"]); | |
16 | + | |
17 | +$requete = $db->prepare("SELECT solde FROM Clients WHERE idCarte=?"); | |
18 | +$requete->bind_param("s", $_POST["idCarte"]); | |
19 | +$requete->bind_results($soldeAncien); | |
20 | +if (!$requete->execute()) { | |
21 | + retour("erreur_bdd", ["message" => $requete->error]); | |
22 | +} | |
23 | +$requete->fetch(); | |
24 | +$requete->close(); | |
25 | + | |
26 | +$soldeNouveau = $soldeAncien + $montant; | |
27 | + | |
28 | +$requete = $db->prepare("UPDATE Clients SET solde=? WHERE idCarte=?"); | |
29 | +$requete->bind_param("ss", $soldeNouveau, $_POST["idCarte"]); | |
30 | +if (!$requete->execute()) { | |
31 | + retour("erreur_bdd", ["message" => $requete->error]); | |
32 | +} | |
33 | +$requete->close(); | |
34 | +$id = transaction(TRANSACTION_RECHARGEMENT, $_POST["idCarte"], $montant); | |
35 | + | |
36 | + | |
37 | +retour("ok", ["id" => $id, "montantAncien" => $montantAncien, "montantNouveau" => $montantNouveau]); | |
38 | + | |
39 | +?> | ... | ... |
api/commun.php
... | ... | @@ -11,6 +11,11 @@ header('Content-type: application/json'); // Histoire de faire comprendre au cli |
11 | 11 | define("JETON_TAILLE", 30); // Taille d'un jeton |
12 | 12 | define("JETON_DUREE", 10*60); // Temps de validité du jeton en secondes |
13 | 13 | |
14 | +define("TRANSACTION_CREATION", 1); | |
15 | +define("TRANSACTION_RECHARGEMENT", 2); | |
16 | +define("TRANSACTION_PAIEMENT", 3); | |
17 | +define("TRANSACTION_VIDANGE", 4); | |
18 | + | |
14 | 19 | // Fonctions utiles |
15 | 20 | function retour($status, $donnees = array()) { // Renvoie les données passées |
16 | 21 | $donnees['status'] = $status; | ... | ... |