From e2058345fe2fde6d12c12f6d03f34b7db86516ed Mon Sep 17 00:00:00 2001 From: bewe <93899302+bernarduswillson@users.noreply.github.com> Date: Fri, 17 Nov 2023 10:21:25 +0700 Subject: [PATCH] feat: add redeem voucher --- api/merch/redeem.php | 46 +++++++++++++++++++++++ app/controllers/Exercise.php | 2 +- app/controllers/Merchandise.php | 10 ++--- app/controllers/Transaction.php | 2 +- app/views/merchandise/index.php | 28 ++++++++++---- app/views/transaction/index.php | 14 +++++-- public/css/merchandise.css | 65 +++++++++++++++++++++++++++++++++ 7 files changed, 149 insertions(+), 18 deletions(-) create mode 100644 api/merch/redeem.php diff --git a/api/merch/redeem.php b/api/merch/redeem.php new file mode 100644 index 0000000..039c07f --- /dev/null +++ b/api/merch/redeem.php @@ -0,0 +1,46 @@ +<?php +function redeem($voucher, $userId) +{ + // rest buy merchandise + $ch = curl_init(); + // curl_setopt($ch, CURLOPT_URL, "http://express:5000/voucher/use/" . $voucher); + curl_setopt($ch, CURLOPT_URL, "http://172.20.10.2:5000/voucher/use/" . $voucher); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt( + $ch, + CURLOPT_POSTFIELDS, + json_encode( + array( + "user_id" => (int)$userId + ) + ) + ); + curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); + $response = curl_exec($ch); + + if ($response === false) { + echo 'Error: ' . curl_error($ch); + } else { + $data = json_decode($response, true); + } + + if ($data["message"] === "success") { + $isSuccess = 1; + } else { + $isSuccess = 0; + } + + return $isSuccess; +} + + +if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['redeemVoucher'])) { + $userId = $_POST['userId']; + $voucher = $_POST['voucher']; + + $isSuccess = redeem($voucher, $userId); + + header('Location: ../../merchandise'); +} +?> \ No newline at end of file diff --git a/app/controllers/Exercise.php b/app/controllers/Exercise.php index 0f93073..a842071 100644 --- a/app/controllers/Exercise.php +++ b/app/controllers/Exercise.php @@ -134,7 +134,7 @@ class Exercise extends Controller } // user's gems - $baseUrl = 'http://soap:8080/service/gems'; + $baseUrl = 'http://soap:8080/service'; $soapRequest = '<x:Envelope xmlns:x="http://schemas.xmlsoap.org/soap/envelope/" diff --git a/app/controllers/Merchandise.php b/app/controllers/Merchandise.php index b653e79..ff5f996 100644 --- a/app/controllers/Merchandise.php +++ b/app/controllers/Merchandise.php @@ -11,7 +11,7 @@ class Merchandise extends Controller $data["email"] = $_SESSION['email']; // user's gems - $baseUrl = 'http://soap:8080/service/gems'; + $baseUrl = 'http://soap:8080/service'; $soapRequest = '<x:Envelope xmlns:x="http://schemas.xmlsoap.org/soap/envelope/" @@ -54,7 +54,7 @@ class Merchandise extends Controller // merch // $baseUrl = 'http://express:5000/merch'; - $baseUrl = 'http://192.168.0.11:5000/merch'; + $baseUrl = 'http://172.20.10.2:5000/merch'; $ch = curl_init($baseUrl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); @@ -87,7 +87,7 @@ class Merchandise extends Controller } // user's gems - $baseUrl = 'http://soap:8080/service/gems'; + $baseUrl = 'http://soap:8080/service'; $soapRequest = '<x:Envelope xmlns:x="http://schemas.xmlsoap.org/soap/envelope/" @@ -131,7 +131,7 @@ class Merchandise extends Controller // merch // $baseUrl = 'http://express:5000/merch/' . $merchId; - $baseUrl = 'http://192.168.0.11:5000/merch/' . $merchId; + $baseUrl = 'http://172.20.10.2:5000/merch/' . $merchId; $ch = curl_init($baseUrl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); @@ -151,7 +151,7 @@ class Merchandise extends Controller { if (isset($merchId) && !empty($merchId)) { // $baseUrl = 'http://express:5000/merch/validate/' . $merchId; - $baseUrl = 'http://192.168.0.11:5000/merch/validate/' . $merchId; + $baseUrl = 'http://172.20.10.2:5000/merch/validate/' . $merchId; $ch = curl_init($baseUrl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); diff --git a/app/controllers/Transaction.php b/app/controllers/Transaction.php index a33f8fc..2abf854 100644 --- a/app/controllers/Transaction.php +++ b/app/controllers/Transaction.php @@ -10,7 +10,7 @@ class Transaction extends Controller $data["user_id"] = $_SESSION['user_id']; // user's transaction - $baseUrl = 'http://soap:8080/service/transaction'; + $baseUrl = 'http://soap:8080/service'; $soapRequest = '<x:Envelope xmlns:x="http://schemas.xmlsoap.org/soap/envelope/" diff --git a/app/views/merchandise/index.php b/app/views/merchandise/index.php index 91bf63d..49ae797 100644 --- a/app/views/merchandise/index.php +++ b/app/views/merchandise/index.php @@ -3,17 +3,31 @@ <h1 class="font-bold text-xl text-blue-purple-gradient"> Merchandise </h1> - <h2 class="gems"> - <img src="/public/icons/gems.svg" alt="gem" width="50px" class="gem-icon"> - <div> - <div class="font-reg"> - Your Balance: + <div class="merch-header"> + <div class="gems"> + <img src="/public/icons/gems.svg" alt="gem" width="50px" class="gem-icon"> + <div> + <div class="font-reg"> + Your Balance: + </div> + <div class="font-bold"> + <?= $data['gems'] ?> gems + </div> </div> + </div> + <div class="redeem"> <div class="font-bold"> - <?= $data['gems'] ?> gems + Reedem Voucher </div> + <form action="../../../api/merch/redeem.php" method="post"> + <div> + <input type="hidden" name="userId" value="<?= $data['user_id'] ?>"> + <input type="text" name="voucher" placeholder="Enter Voucher Code" class="redeem-input"> + <button class="redeem-button distinct-button" type="submit" name="redeemVoucher">Redeem</button> + </div> + </form> </div> - </h2> + </div> <div class="merchandise-items"> <? foreach ($data['merch'] as $merch): ?> <div class="merch-item" diff --git a/app/views/transaction/index.php b/app/views/transaction/index.php index 25ef30d..6495e0d 100644 --- a/app/views/transaction/index.php +++ b/app/views/transaction/index.php @@ -22,10 +22,16 @@ <?= $transaction['action'] ?> </h2> <? if (explode(' ', $transaction['action'])[0] == 'Buy'): ?> - <h3 class="font-bold amount-text-buy"> - - - <?= $transaction['amount'] ?> Gems - </h3> + <? if ($transaction['status'] == 'ACCEPTED'): ?> + <h3 class="font-bold amount-text-buy"> + - + <?= $transaction['amount'] ?> Gems + </h3> + <? else: ?> + <h3 class="font-bold amount-text-buy"> + Insufficient Gems + </h3> + <? endif; ?> <? else: ?> <h3 class="font-bold amount-text-get"> + diff --git a/public/css/merchandise.css b/public/css/merchandise.css index d1ce283..2eb3f0b 100644 --- a/public/css/merchandise.css +++ b/public/css/merchandise.css @@ -18,6 +18,12 @@ text-align: center; } +.merch-header { + display: flex; + justify-content: center; + gap: 50px; +} + .gems { color: var(--orange); font-size: 28px; @@ -32,6 +38,56 @@ text-align: center; } +.redeem { + color: var(--blue); + font-size: 28px; + width: 300px; + flex-grow: 1; + border-radius: 20px; + background: var(--white); + box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.2); + justify-content: center; + cursor: pointer; + transition: all 0.3s ease 0s; + padding: 20px 0; + text-align: center; + align-items: center; + display: flex; + flex-direction: column; +} + +.redeem-input { + width: 80%; + margin-top: 15px; + border-radius: 10px; + border: 1px solid #006CDB; + padding: 10px; + font-size: 16px; + text-align: center; +} + +.redeem-button { + margin-top: 20px; + color: #FFFFFF; + padding: 5px 30px; + border-radius: 50px; + font-size: 16px; + letter-spacing: 1px; + cursor: pointer; + border: 0.5px solid #006CDB; + transition: all 0.3s ease 0s; + +} + +.redeem-button:hover { + transform: translateY(-4px); + opacity: 0.6; +} + +.redeem-button:active { + transform: translateY(6px); +} + .merchandise-items { display: flex; justify-content: center; @@ -102,4 +158,13 @@ flex-direction: column; align-items: center; } + + .merch-header { + display: block; + margin: 0 2vw; + } + + .gems { + margin-bottom: 20px; + } } \ No newline at end of file -- GitLab