Blame view

api/annuler.php 1.86 KB
b79afcea   Geoffrey PREUD'HOMME   api/annuler
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  <?php
  
  require_once("commun.php");
  
  verifierJeton(donne("jeton"));
  
  if (!donne("idTransaction")) {
      retour("requete_malformee");
  }
  
  $requete = $db->prepare("SELECT type, client, UNIX_TIMESTAMP(date), montant, utilisateur, valide FROM Transactions WHERE id=?");
  $requete->bind_param("s", $_POST['idTransaction']);
  if (!$requete->execute()) {
      retour("erreur_bdd", ["message" => $requete->error]);
  }
  $requete->bind_result($type, $client, $date, $montant, $utilisateur, $valide);
  if (!$requete->fetch()) {
      retour("transaction_inconnue");
  }
  $requete->close();
  
  if (!$valide) {
      retour("transaction_deja_annulee");
  }
  
  if ($utilisateur != $login) {
      verifierDroit(3, "transaction_autre");
  }
  
  if (time() > $date + TRANSACTION_DUREE) {
      verifierDroit(3, "transaction_expire");
  }
  
  $requete = $db->prepare("SELECT solde FROM Clients WHERE idCarte=?");
  $requete->bind_param("s", $client);
  $requete->bind_result($soldeAncien);
  if (!$requete->execute()) {
      retour("erreur_bdd", ["message" => $requete->error]);
  }
  $requete->fetch();
  $requete->close();
  
  switch ($type) {
  case TRANSACTION_CREATION:
  case TRANSACTION_RECHARGEMENT:
      $soldeNouveau = $soldeAncien - $montant;
      break;
  
  case TRANSACTION_PAIEMENT:
  case TRANSACTION_VIDANGE:
      $soldeNouveau = $soldeAncien + $montant;
      break;
  
  default:
      retour("erreur_interne");
      break;
  }
  
  $requete = $db->prepare("UPDATE Clients SET solde=? WHERE idCarte=?");
  $requete->bind_param("ss", $soldeNouveau, $client);
  if (!$requete->execute()) {
      retour("erreur_bdd", ["message" => $requete->error]);
  }
  $requete->close();
  
  
  $requete = $db->prepare("UPDATE Transactions SET valide=0 WHERE id=?");
  $requete->bind_param("s", $_POST["idTransaction"]);
  if (!$requete->execute()) {
      retour("erreur_bdd", ["message" => $requete->error]);
  }
  $requete->close();
  
  retour("ok", ["soldeAncien" => $soldeAncien, "soldeNouveau" => $soldeNouveau]);
  
  ?>