Commit b79afcea41c5fc91999cec98d3f6dfe596d92735
1 parent
207a98d5
api/annuler
Showing
2 changed files
with
80 additions
and
2 deletions
Show diff stats
... | ... | @@ -0,0 +1,76 @@ |
1 | +<?php | |
2 | + | |
3 | +require_once("commun.php"); | |
4 | + | |
5 | +verifierJeton(donne("jeton")); | |
6 | + | |
7 | +if (!donne("idTransaction")) { | |
8 | + retour("requete_malformee"); | |
9 | +} | |
10 | + | |
11 | +$requete = $db->prepare("SELECT type, client, UNIX_TIMESTAMP(date), montant, utilisateur, valide FROM Transactions WHERE id=?"); | |
12 | +$requete->bind_param("s", $_POST['idTransaction']); | |
13 | +if (!$requete->execute()) { | |
14 | + retour("erreur_bdd", ["message" => $requete->error]); | |
15 | +} | |
16 | +$requete->bind_result($type, $client, $date, $montant, $utilisateur, $valide); | |
17 | +if (!$requete->fetch()) { | |
18 | + retour("transaction_inconnue"); | |
19 | +} | |
20 | +$requete->close(); | |
21 | + | |
22 | +if (!$valide) { | |
23 | + retour("transaction_deja_annulee"); | |
24 | +} | |
25 | + | |
26 | +if ($utilisateur != $login) { | |
27 | + verifierDroit(3, "transaction_autre"); | |
28 | +} | |
29 | + | |
30 | +if (time() > $date + TRANSACTION_DUREE) { | |
31 | + verifierDroit(3, "transaction_expire"); | |
32 | +} | |
33 | + | |
34 | +$requete = $db->prepare("SELECT solde FROM Clients WHERE idCarte=?"); | |
35 | +$requete->bind_param("s", $client); | |
36 | +$requete->bind_result($soldeAncien); | |
37 | +if (!$requete->execute()) { | |
38 | + retour("erreur_bdd", ["message" => $requete->error]); | |
39 | +} | |
40 | +$requete->fetch(); | |
41 | +$requete->close(); | |
42 | + | |
43 | +switch ($type) { | |
44 | +case TRANSACTION_CREATION: | |
45 | +case TRANSACTION_RECHARGEMENT: | |
46 | + $soldeNouveau = $soldeAncien - $montant; | |
47 | + break; | |
48 | + | |
49 | +case TRANSACTION_PAIEMENT: | |
50 | +case TRANSACTION_VIDANGE: | |
51 | + $soldeNouveau = $soldeAncien + $montant; | |
52 | + break; | |
53 | + | |
54 | +default: | |
55 | + retour("erreur_interne"); | |
56 | + break; | |
57 | +} | |
58 | + | |
59 | +$requete = $db->prepare("UPDATE Clients SET solde=? WHERE idCarte=?"); | |
60 | +$requete->bind_param("ss", $soldeNouveau, $client); | |
61 | +if (!$requete->execute()) { | |
62 | + retour("erreur_bdd", ["message" => $requete->error]); | |
63 | +} | |
64 | +$requete->close(); | |
65 | + | |
66 | + | |
67 | +$requete = $db->prepare("UPDATE Transactions SET valide=0 WHERE id=?"); | |
68 | +$requete->bind_param("s", $_POST["idTransaction"]); | |
69 | +if (!$requete->execute()) { | |
70 | + retour("erreur_bdd", ["message" => $requete->error]); | |
71 | +} | |
72 | +$requete->close(); | |
73 | + | |
74 | +retour("ok", ["soldeAncien" => $soldeAncien, "soldeNouveau" => $soldeNouveau]); | |
75 | + | |
76 | +?> | ... | ... |
api/commun.php
... | ... | @@ -16,6 +16,8 @@ define("TRANSACTION_RECHARGEMENT", 2); |
16 | 16 | define("TRANSACTION_PAIEMENT", 3); |
17 | 17 | define("TRANSACTION_VIDANGE", 4); |
18 | 18 | |
19 | +define("TRANSACTION_DUREE", 60); | |
20 | + | |
19 | 21 | // Fonctions utiles |
20 | 22 | function retour($status, $donnees = array()) { // Renvoie les données passées |
21 | 23 | $donnees['status'] = $status; |
... | ... | @@ -63,12 +65,12 @@ function verifierJeton($jeton) { |
63 | 65 | $requete->close(); |
64 | 66 | } |
65 | 67 | |
66 | -function verifierDroit($droitMinimum) { | |
68 | +function verifierDroit($droitMinimum, $retour = "droits_insuffisants") { | |
67 | 69 | global $droit; |
68 | 70 | if (donne("jeton")) { |
69 | 71 | verifierJeton(donne("jeton")); |
70 | 72 | if ($droit < $droitMinimum) { |
71 | - retour("droits_insuffisants"); | |
73 | + retour($retour); | |
72 | 74 | } |
73 | 75 | } else { |
74 | 76 | retour("jeton_vide"); | ... | ... |