diff --git a/api/merch/buy.php b/api/merch/buy.php index 89d3b77d65fa3f62507554502a01f20c2a1d04bc..20059286a0e2090aad9fc07010cbddb51e3de730 100644 --- a/api/merch/buy.php +++ b/api/merch/buy.php @@ -19,13 +19,22 @@ function buyItem($merchId, $userId, $email) ); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); $response = curl_exec($ch); - var_dump($response); if ($response === false) { echo 'Error: ' . curl_error($ch); } else { $data = json_decode($response, true); } + + if ($data["message"] === "success") { + $isSuccess = 1; + } else if ($data["message"] === "insufficient gems") { + $isSuccess = 0; + } else { + $isSuccess = -1; + } + + return $isSuccess; } @@ -34,6 +43,8 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['buyMerch'])) { $userId = $_POST['userId']; $email = $_POST['email']; - buyItem($merchId, $userId, $email); + $isSuccess = buyItem($merchId, $userId, $email); + + header('Location: ../../merchandise/confirmation/' . $merchId . '/?success=' . $isSuccess); } ?> \ No newline at end of file diff --git a/app/controllers/Merchandise.php b/app/controllers/Merchandise.php index ec0ea4a465c8348f4f2a4c1b4177a316fc36dd85..bbc1b0db159bed2955a017473ee6494f2655b4ec 100644 --- a/app/controllers/Merchandise.php +++ b/app/controllers/Merchandise.php @@ -68,4 +68,104 @@ class Merchandise extends Controller $this->view('merchandise/index', $data); $this->view('footer/index'); } + + public function confirmation($merchId = null) + { + $this->validateSession(); + $this->validateParamMerch($merchId); + + // Confirmation + if (isset($merchId) && !empty($merchId)) { + $data["pageTitle"] = "Test your knowledge!"; + $data["merch_id"] = intval($merchId); + $data["user_id"] = $_SESSION['user_id']; + + $query = $this->getQuery(); + $data["isSuccess"] = -1; + + if (isset($query["success"])) { + $data["isSuccess"] = $query["success"]; + } + + // user's gems + $baseUrl = 'http://soap:8080/service/gems'; + + $soapRequest = '<x:Envelope + xmlns:x="http://schemas.xmlsoap.org/soap/envelope/" + xmlns:ser="http://service.toco.org/"> + <x:Header/> + <x:Body> + <ser:getGems> + <user_id>' . $data["user_id"] . '</user_id> + </ser:getGems> + </x:Body> + </x:Envelope>'; + + $ch = curl_init($baseUrl); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, $soapRequest); + curl_setopt( + $ch, + CURLOPT_HTTPHEADER, + array( + 'Content-Type: text/xml', + 'SOAPAction: getGems', + 'X-api-key: toco_php' + ) + ); + + $response = curl_exec($ch); + + if (curl_errno($ch)) { + echo 'Curl error: ' . curl_error($ch); + } + + curl_close($ch); + $data['gems'] = $response; + + if (preg_match('/<return>(\d+)<\/return>/', $response, $matches)) { + $data['gems'] = (int) $matches[1]; + } else { + echo 'Error extracting numeric value from XML response.'; + } + + // merch + // $baseUrl = 'http://express:5000/merch/' . $merchId; + $baseUrl = 'http://192.168.0.11:5000/merch/' . $merchId; + + $ch = curl_init($baseUrl); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $response = curl_exec($ch); + $merch = json_decode($response, true); + + $data["merch"] = $merch['result']; + + $this->view('header/index', $data); + $this->view('navbar/index'); + $this->view('confirmation/index', $data); + $this->view('footer/index'); + } + } + + public function validateParamMerch($merchId) + { + if (isset($merchId) && !empty($merchId)) { + // $baseUrl = 'http://express:5000/merch/validate/' . $merchId; + $baseUrl = 'http://192.168.0.11:5000/merch/validate/' . $merchId; + $ch = curl_init($baseUrl); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $response = curl_exec($ch); + $valid = json_decode($response, true); + + $valid = $valid['result'] ?? []; + + if ($valid === true) { + return; + } + + $this->show404(); + exit(); + } + } } \ No newline at end of file diff --git a/app/views/confirmation/index.php b/app/views/confirmation/index.php new file mode 100644 index 0000000000000000000000000000000000000000..121586506c1663df64cdb639dba2c8f29c02fb33 --- /dev/null +++ b/app/views/confirmation/index.php @@ -0,0 +1,17 @@ +<div class="conf"> + <div class="container conf-container"> + <h1 class="font-bold text-xl text-blue-purple-gradient"> + <?= $data['isSuccess'] ?> + </h1> + <h1 class="font-bold text-xl text-blue-purple-gradient"> + <?= $data['gems'] ?> + </h1> + <img src="http://192.168.0.11:5000/image/?filename=<?= $data['merch']['image'] ?>" alt="merch-image" class="merch-image"> + <h1 class="font-bold text-xl text-blue-purple-gradient"> + <?= $data['merch']['name'] ?> + </h1> + <h1 class="font-bold text-xl text-blue-purple-gradient"> + <?= $data['merch']['price'] ?> + </h1> + </div> +</div> \ No newline at end of file diff --git a/app/views/header/index.php b/app/views/header/index.php index d56eb64b6b659ab2500b32cde36981597b8abf5d..7615531bf570e1acd3fb0e72e034a8281ddad95f 100644 --- a/app/views/header/index.php +++ b/app/views/header/index.php @@ -30,5 +30,6 @@ <link rel="stylesheet" href="/public/css/toast.css"> <link rel="stylesheet" href="/public/css/exercise.css"> <link rel="stylesheet" href="/public/css/question.css"> + <link rel="stylesheet" href="/public/css/merchandise.css"> </head> <body> \ No newline at end of file diff --git a/app/views/merchandise/index.php b/app/views/merchandise/index.php index e5cd614b6629ea4dc7c815e809a72fcc4975f0ce..bd47b5ab8d961014a1e2809a8c0f076248fb7201 100644 --- a/app/views/merchandise/index.php +++ b/app/views/merchandise/index.php @@ -1,40 +1,36 @@ -<?php -?> - <div class="merch"> <div class="container merch-container"> <h1 class="font-bold text-xl text-blue-purple-gradient"> Merchandise </h1> - <div> - <?= $data['gems'] ?> - </div> - <div> + <h2 class="gems font-bold"> + Your Balance: <?= $data['gems'] ?> gems + </h2> + <div class="merchandise-items"> <? foreach ($data['merch'] as $merch): ?> - <div> - <div> - <!-- <img src="http://express:5000/image/?filename=<?= $merch['image'] ?>" alt=""> --> - <img src="http://192.168.0.11:5000/image/?filename=<?= $merch['image'] ?>" alt=""> - </div> - <div> - <?= $merch['name'] ?> - </div> - <div> - <?= $merch['price'] ?> - </div> - <div> - <?= $merch['desc'] ?> - </div> - <div> - <form action="../../../api/merch/buy.php" method="post"> - <input type="hidden" name="merchId" value="<?= $merch['merchandise_id'] ?>"> - <input type="hidden" name="userId" value="<?= $data['user_id'] ?>"> - <input type="hidden" name="email" value="<?= $data['email'] ?>"> - <button type="submit" name="buyMerch">Buy</button> - </form> + <div class="merch-item" style="background-image: url('http://192.168.0.11:5000/image/?filename=<?= $merch['image'] ?>');"> + <div class="merch-image"></div> + <div class="merch-details"> + <div class="merch-name"> + <?= $merch['name'] ?> + </div> + <div class="merch-desc"> + <?= $merch['desc'] ?> + </div> + <div class="merch-price"> + <?= $merch['price'] ?> + </div> + <div> + <form action="../../../api/merch/buy.php" method="post"> + <input type="hidden" name="merchId" value="<?= $merch['merchandise_id'] ?>"> + <input type="hidden" name="userId" value="<?= $data['user_id'] ?>"> + <input type="hidden" name="email" value="<?= $data['email'] ?>"> + <button class="buy-button" type="submit" name="buyMerch">Buy</button> + </form> + </div> </div> </div> <? endforeach; ?> </div> </div> -</div> \ No newline at end of file +</div> diff --git a/public/css/merchandise.css b/public/css/merchandise.css new file mode 100644 index 0000000000000000000000000000000000000000..3e560331a7632615adb757012809e715c12d60be --- /dev/null +++ b/public/css/merchandise.css @@ -0,0 +1,96 @@ +.merch { + width: 100vw; + padding-top: 120px; + display: flex; + justify-content: center; + padding-bottom: 60px; + min-height: 100vh; +} + +.merch .merch-container { + display: flex; + flex-direction: column; + align-items: center; + gap: 50px; +} + +.merch .merch-container h1 { + text-align: center; +} + +.gems { + color: var(--orange); + font-size: 28px; +} + +.merchandise-items { + display: flex; + justify-content: center; + margin-bottom: 100px; + flex-wrap: wrap; + gap: 30px; +} + +.merch-item { + width: 300px; + height: 460px; + border-radius: 20px; + background: #FFFFFF; + box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.2); + justify-content: center; + cursor: pointer; + transition: all 0.3s ease 0s; + margin: 20px 0; + background-size: auto 300px; + background-position: top; + background-repeat: no-repeat; + flex-direction: column; +} + +.merch-item .merch-image { + height: 300px; +} + +.merch-item .merch-details { + text-align: center; +} + +.merch-item .merch-details .merch-name { + font-size: 24px; + font-weight: 700; +} + +.merch-item .merch-details .merch-price { + color: var(--orange); + font-weight: 600; + margin-top: 10px; +} + +.buy-button { + margin-top: 10px; + background: linear-gradient(45deg, #006CDB, #8535D4); + color: #FFFFFF; + padding: 8px 100px; + border-radius: 50px; + font-size: 16px; + letter-spacing: 1px; + cursor: pointer; + border: 0.5px solid #006CDB; + transition: all 0.3s ease 0s; +} + +.buy-button:hover { + transform: translateY(-4px); + opacity: 0.6; +} + +.buy-button:active { + transform: translateY(6px); +} + +@media (max-width: 767px) { + .merchandise-items { + flex-direction: column; + align-items: center; + } +} \ No newline at end of file diff --git a/public/css/question.css b/public/css/question.css index cf17af65b2a2c8d44e81239cf46cce33aeb0baf8..6da4ca7acaf76a8f2816680f292675ed31b9bfc6 100644 --- a/public/css/question.css +++ b/public/css/question.css @@ -94,20 +94,6 @@ transition: all 0.3s ease 0s; } -.submit-button { - margin-top: 90px; - margin-bottom: 40px; - background: linear-gradient(45deg, #006CDB, #8535D4); - color: #FFFFFF; - padding: 12px 100px; - border-radius: 50px; - font-size: 16px; - letter-spacing: 1px; - cursor: pointer; - border: 0.5px solid #006CDB; - transition: all 0.3s ease 0s; -} - .submit-button:hover { transform: translateY(-4px); opacity: 0.6;