Comus Party 1.0.0
Application web de mini-jeux en ligne
Chargement...
Recherche...
Aucune correspondance
ControllerShop.class.php
Aller à la documentation de ce fichier.
1<?php
2
10
11namespace ComusParty\Controllers;
12
13use ComusParty\App\Exceptions\NotFoundException;
14use ComusParty\App\Mailer;
15use ComusParty\App\MessageHandler;
16use ComusParty\Models\ArticleDAO;
17use ComusParty\Models\InvoiceDAO;
18use ComusParty\Models\PlayerDAO;
19use ComusParty\Models\UserDAO;
20use DateMalformedStringException;
21use DateTime;
22use Twig\Environment;
23use Twig\Error\LoaderError;
24use Twig\Error\RuntimeError;
25use Twig\Error\SyntaxError;
26use Twig\Loader\FilesystemLoader;
27
33{
39 public function __construct(FilesystemLoader $loader, Environment $twig)
40 {
41 parent::__construct($loader, $twig);
42 }
43
44
55 public function show()
56 {
57 $managerArticle = new ArticleDAO($this->getPdo());
58 $articles = $managerArticle->findAll();
59 $pfps = $managerArticle->findAllPfps();
60 $banners = $managerArticle->findAllBanners();
61 $pfpsOwned = $managerArticle->findAllPfpsOwnedByPlayer($_SESSION['uuid']);
62 $idsPfpsOwned = [];
63 foreach ($pfpsOwned as $pfpOwned) {
64 $idsPfpsOwned[] = $pfpOwned->getId();
65 }
66 $bannersOwned = $managerArticle->findAllBannersOwnedByPlayer($_SESSION['uuid']);
67 $idsBannersOwned = [];
68 foreach ($bannersOwned as $bannerOwned) {
69 $idsBannersOwned[] = $bannerOwned->getId();
70 }
71 $template = $this->getTwig()->load('player/shop.twig');
72 if (isset($_SESSION['basket'])) {
73 $numberArticlesInBasket = count($_SESSION['basket']);
74 } else {
75 $numberArticlesInBasket = 0;
76 }
77 echo $template->render(array(
78 'articles' => $articles,
79 'pfps' => $pfps,
80 'banners' => $banners,
81 'idsPfpsOwned' => $idsPfpsOwned,
82 'idsBannersOwned' => $idsBannersOwned,
83 'numberArticlesInBasket' => $numberArticlesInBasket
84 ));
85 }
86
96 public function showAll()
97 {
98 $managerArticle = new ArticleDAO($this->getPdo());
99 $articles = $managerArticle->findAll();
100 $template = $this->getTwig()->load('player/shop.twig');
101
102 echo $template->render(array('articles' => $articles));
103 }
104
113 public function showCheckout()
114 {
115 $articles = [];
116 foreach ($_SESSION['basket'] as $id) {
117 $managerArticle = new ArticleDAO($this->getPdo());
118 $article = $managerArticle->findById($id);
119 $articles[] = $article;
120 }
121
122 $template = $this->getTwig()->load('player/checkout.twig');
123 echo $template->render(array('articles' => $articles));
124 }
125
136 public function checkPaymentRequirement(?array $datas): ?bool
137 {
138 $cardNumber = preg_replace('/\s/', '', $datas['cardNumber']);
139
140 if (strlen($cardNumber) !== 16) {
141 MessageHandler::sendJsonCustomException(400, "Le numéro de la carte doit contenir 16 chiffres.");
142 return false;
143 }
144
145 if (!$this->checkLuhnValid($cardNumber)) {
146 MessageHandler::sendJsonCustomException(400, "Le numéro de carte n'est pas valide.");
147 return false;
148 }
149
150 if (strlen($datas['cvv']) !== 3) {
151 MessageHandler::sendJsonCustomException(400, "Le cryptogramme de sécurité doit contenir 3 chiffres.");
152 return false;
153 }
154
155
156 list($month, $year) = explode("/", $datas['expirationDate']);
157 $expirationDate = new DateTime();
158 $expirationDate->setDate(2000 + (int)$year, (int)$month, 1);
159 $now = new DateTime();
160 if ($expirationDate < $now) {
161 MessageHandler::sendJsonCustomException(400, "La date d'expiration de la carte est dépassée.");
162 return false;
163 }
164
165 echo MessageHandler::sendJsonMessage(200, "Paiement effectué avec succès.");
166 return true;
167 }
168
180 private function checkLuhnValid(?string $card): bool
181 {
182 $sum = 0;
183 $length = strlen($card);
184
185 for ($i = 0; $i < $length - 1; $i++) {
186 if ($i % 2 == 0) {
187 $digit = (int)$card[$i] * 2;
188 if ($digit >= 10) {
189 $digitStr = (string)$digit;
190 $digit = (int)$digitStr[0] + (int)$digitStr[1];
191 }
192 $sum += $digit;
193 } else {
194 $sum += (int)$card[$i];
195 }
196 }
197 $key = 10 - ($sum % 10) % 10;
198 return $key == $card[$length - 1];
199 }
200
211 public function showInvoice(int $invoiceId)
212 {
213 $managerArticle = new ArticleDAO($this->getPdo());
214 $managerPlayer = new PlayerDAO($this->getPdo());
215 $managerUser = new UserDAO($this->getPdo());
216 $managerInvoice = new InvoiceDAO($this->getPdo());
217
218 $articles = $managerArticle->findArticlesByInvoiceId($invoiceId);
219 $player = $managerPlayer->findWithDetailByUuid($_SESSION['uuid']);
220 $email = $managerUser->findById($player->getUserId())->getEmail();
221 $invoice = $managerInvoice->findById($invoiceId);
222
223 $template = $this->getTwig()->load('player/invoice.twig');
224 echo $template->render(array(
225 'invoice' => $invoice,
226 'username' => $player->getUsername(),
227 'email' => $email,
228 'articles' => $articles
229 ));
230 }
231
243 public function showSuccessPayment(array $articles, string $playerUuid, string $paymentType): void
244 {
245 $managerInvoice = new InvoiceDAO($this->getPdo());
246 $managerInvoice->createInvoiceWithArticles($playerUuid, $paymentType, $articles);
247
248 $managerPlayer = new PlayerDAO($this->getPdo());
249 $player = $managerPlayer->findByUuid($playerUuid);
250 $managerUser = new UserDAO($this->getPdo());
251 $user = $managerUser->findById($player->getUserId());
252
253 $mail = new Mailer(array($user->getEmail()), "ComusParty - Paiement effectué", "Votre paiement a bien été effectué. Vous pouvez consulter votre facture sur votre profil.");
254 $mail->send();
255
256 $template = $this->getTwig()->load('player/success-payment.twig');
257 echo $template->render();
258
259 unset($_SESSION['basket']);
260 header("Refresh: 5; url=/");
261 exit();
262 }
263}
Classe Mailer.
Definition Mailer.php:21
showCheckout()
Permet d'afficher la page de paiement.
showSuccessPayment(array $articles, string $playerUuid, string $paymentType)
Affiche la page de succès de paiement.
show()
Permet d'afficher tous les articles (avatars / bannières)
__construct(FilesystemLoader $loader, Environment $twig)
Constructeur de la classe ControllerShop.
showAll()
Permet d'afficher tous les articles.
checkLuhnValid(?string $card)
Exécute l'algorithme de Luhn sur le numéro de carte passé en paramètre.
showInvoice(int $invoiceId)
Affiche la facture générée grâce à l'ID passé en paramètre GET.
checkPaymentRequirement(?array $datas)
Vérifie si l'ensemble des données du formulaire de paiement, passées en paramètre via un tableau asso...
getTwig()
Retourne l'attribut twig, correspondant à l'environnement de Twig.
getPdo()
Retourne l'attribut PDO, correspondant à la connexion à la base de données.