-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransfer.php
More file actions
329 lines (286 loc) · 12.1 KB
/
Copy pathtransfer.php
File metadata and controls
329 lines (286 loc) · 12.1 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?php
session_start();
include 'db_connection.php';
// If not logged in, redirect to login page
if (!isset($_SESSION['username'])) {
header("Location: index.html");
exit();
}
$current_username = $_SESSION['username']; // Current Login User Name
// Check and set the user ID
if (!isset($_SESSION['user_id'])) {
$stmt = $conn->prepare("SELECT id FROM users WHERE username = ?");
$stmt->bind_param("s", $current_username);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();
$stmt->close();
if ($user) {
$_SESSION['user_id'] = $user['id'];
} else {
die("User not found. Please log in again.");
}
}
// Get user ID from session
$user_id = $_SESSION['user_id'];
// Start timing
$start_time = microtime(true);
// Get the current user's savings card information
$stmt = $conn->prepare("SELECT debitcards.debitcard_id, debitcards.balance, cards.id as card_id
FROM debitcards
INNER JOIN cards ON debitcards.id = cards.id
INNER JOIN users ON cards.cardholder_id = users.id
WHERE users.id = ?");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();
$debit_cards = [];
while ($row = $result->fetch_assoc()) {
$debit_cards[] = $row;
}
$stmt->close();
$transfer_error = '';
$success_message = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$account_from = $_POST['account_from'] ?? '';
$account_to = $_POST['account_to'] ?? '';
$amount = $_POST['amount'] ?? 0;
if (empty($account_from) || empty($account_to) || $amount <= 0) {
$transfer_error = "All fields are required, and amount must be greater than 0.";
} elseif ($account_from === $account_to) { // Check if the card numbers are the same
$transfer_error = "Sender and recipient account cannot be the same.";
} else {
// Check if the payment card is locked
$stmt = $conn->prepare("SELECT debitcards.balance, cards.blocked
FROM debitcards
INNER JOIN cards ON debitcards.id = cards.id
WHERE debitcards.debitcard_id = ?");
$stmt->bind_param("s", $account_from);
$stmt->execute();
$result = $stmt->get_result();
$sender_card_data = $result->fetch_assoc();
$stmt->close();
if (!$sender_card_data) {
$transfer_error = "Sender account not found.";
} elseif ($sender_card_data['blocked'] == 1) { // Check Payment Card Status
$transfer_error = "Transfer failed. The sender's account is locked.";
} else {
// Check if the target account exists
$stmt = $conn->prepare("SELECT cards.cardholder_id, debitcards.id, debitcards.balance
FROM debitcards
INNER JOIN cards ON debitcards.id = cards.id
WHERE debitcards.debitcard_id = ?");
$stmt->bind_param("s", $account_to);
$stmt->execute();
$result = $stmt->get_result();
$recipient_card = $result->fetch_assoc();
$stmt->close();
if (!$recipient_card) {
$transfer_error = "Recipient account not found.";
}
else {
// Continue with the transfer logic
$sender_card = array_filter($debit_cards, fn($card) => $card['debitcard_id'] == $account_from);
$sender_card = reset($sender_card);
if (!$sender_card || $sender_card['balance'] < $amount) {
$transfer_error = "Insufficient balance.";
} else {
$conn->begin_transaction();
try {
// Updating of sponsor balances
$new_balance_sender = $sender_card['balance'] - $amount;
$stmt = $conn->prepare("UPDATE debitcards SET balance = ? WHERE debitcard_id = ?");
$stmt->bind_param("ds", $new_balance_sender, $account_from);
$stmt->execute();
// Updating recipient balances
$new_balance_recipient = $recipient_card['balance'] + $amount;
$stmt = $conn->prepare("UPDATE debitcards SET balance = ? WHERE debitcard_id = ?");
$stmt->bind_param("ds", $new_balance_recipient, $account_to);
$stmt->execute();
// Synchronize the initiator's savings card balance to the account table
$stmt = $conn->prepare("
UPDATE account
INNER JOIN cards ON account.user_id = cards.cardholder_id
INNER JOIN debitcards ON debitcards.id = cards.id
SET account.local_currency_balance = debitcards.balance
WHERE account.Account_type = 'card' AND debitcards.debitcard_id = ?
");
$stmt->bind_param("s", $account_from);
$stmt->execute();
// Synchronize the recipient's savings card balance to the account table
$stmt->bind_param("s", $account_to);
$stmt->execute();
// Adding Transfer Records
$stmt = $conn->prepare("INSERT INTO transfer (payer_id, payee_id) VALUES (?, ?)");
$stmt->bind_param("ii", $_SESSION['user_id'], $recipient_card['cardholder_id']);
$stmt->execute();
// Adding Transaction Records
$stmt = $conn->prepare("INSERT INTO transactions (transaction_type, card_id, amount) VALUES ('transfer', ?, ?)");
$stmt->bind_param("id", $sender_card['card_id'], $amount);
$stmt->execute();
$conn->commit();
$success_message = "Transfer successful.";
} catch (Exception $e) {
$conn->rollback();
$transfer_error = "Transfer failed. Please try again.";
}
}
}
}
}
}
// End timing
$end_time = microtime(true);
$execution_time = round(($end_time - $start_time) * 1000, 2); // Convert to milliseconds
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./css/dashboard.css" />
<link rel="icon" href="assets/logo.png" type="image/png">
<title>Send Transfer | GBC Internet Banking</title>
</head>
<body>
<header class="header">
<div class="header__content">
<div class="header__logo">
<a href="./dashboard.php"><img src="./assets/logo.png" alt="Bank Logo"></a>
</div>
<h1>Send Transfer</h1>
<div class="header__right">
Current User: <?php echo htmlspecialchars($_SESSION['username']); ?>
<button class="logout-button" style="margin-left: 10px;" onclick="window.location.href='logout.php'">Logout</button>
</div>
</div>
</header>
<main class="dashboard">
<div class="info-container">
<section class="account-summary">
<h2>Transfer Funds</h2>
<?php if ($transfer_error): ?>
<p style="color: red;"> <?php echo $transfer_error; ?> </p>
<?php endif; ?>
<?php if ($success_message): ?>
<p style="color: green;"> <?php echo $success_message; ?> </p>
<?php endif; ?>
<form action="" method="POST" class="transfer-form">
<div class="sub-accounts">
<div class="sub-account">
<label for="account_from">From</label>
<select id="account_from" name="account_from" required onchange="updateBalance()">
<option value="" disabled selected>Select Account</option>
<?php foreach ($debit_cards as $card): ?>
<option value="<?php echo $card['debitcard_id']; ?>" data-balance="<?php echo $card['balance']; ?>">
<?php echo $card['debitcard_id']; ?> - <?php echo number_format($card['balance'], 2); ?> HKD available
</option>
<?php endforeach; ?>
</select>
</div>
<div class="sub-account">
<label for="account_to">To Account</label>
<input type="text" id="account_to" name="account_to" placeholder="Recipient Account Number" required>
</div>
<div class="sub-account">
<label for="amount">Amount</label>
<input type="number" id="amount" name="amount" placeholder="Enter amount" step="0.01" min="0.01" required>
<p id="balance-info" style="color: green; margin-top: 5px;"></p>
</div>
</div>
<div class="submit-button-container">
<button type="submit" id="transferButton">Transfer</button>
</div>
</form>
</section>
<div class="back-link">
<p><a href="javascript:history.back()">Return to the previous page</a></p>
</div>
</div>
</main>
<!-- Footer -->
<footer class="footer">
<?php if ($execution_time): ?>
It took <?php echo $execution_time; ?> milliseconds to get data from the server.</p>
<?php endif; ?>
©2024 Global Banking Corporation Limited. All rights reserved.
</footer>
<script>
function updateBalance() {
const accountSelect = document.getElementById("account_from");
const selectedOption = accountSelect.options[accountSelect.selectedIndex];
const balance = selectedOption.getAttribute("data-balance");
const balanceInfo = document.getElementById("balance-info");
if (balance) {
balanceInfo.textContent = `${parseFloat(balance).toFixed(2)} HKD available`;
} else {
balanceInfo.textContent = "";
}
}
</script>
</body>
</html>
<style>
.dashboard {
display: flex;
justify-content: center;
align-items: flex-start;
min-height: 100vh;
padding-top: 50px;
}
.info-container {
width: 100%;
max-width: 800px;
padding: 20px;
background-color: white;
border-radius: 8px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
margin-top: 30px;
}
.transfer-form {
display: flex;
flex-direction: column;
gap: 15px;
}
.sub-account label {
display: block;
font-weight: bold;
margin-bottom: 5px;
font-size: 14px;
}
.sub-account input, .sub-account select {
width: 100%;
max-width: 400px;
padding: 8px;
font-size: 14px;
border: 1px solid #ccc;
border-radius: 5px;
margin-bottom: 10px;
}
.submit-button-container {
text-align: center;
margin-top: 20px;
}
.submit-button-container button {
padding: 10px 20px;
background-color: #4BA247;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
font-weight: bold;
transition: background-color 0.3s ease;
}
.submit-button-container button:hover {
background-color: #3D8C3A;
}
.back-link {
text-align: center;
margin-top: 20px;
}
.back-link a {
text-decoration: none;
color: #0064d2;
}
</style>