-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpaymenthome.php
319 lines (273 loc) · 12.9 KB
/
paymenthome.php
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
<?php
session_start();
include 'config.php';
$user_id = $_SESSION['user_id'];
$select = mysqli_query($conn, "SELECT * FROM `tblpayment` WHERE userid = '$user_id'") or die('query failed');
if (mysqli_num_rows($select) > 0) {
$fetch = mysqli_fetch_assoc($select);
} else {
$fetch = null;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Link Credit Card</title>
<link rel="stylesheet" href="css/payment.css">
<link rel="icon" type="image/x-icon" href="css/images/logo.png">
</head>
<body>
<div class="container">
<br>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
<div class=" card-container">
<div class="front">
<div class="image">
<img src="css/paymentimg/chip.png" alt="">
<img src="css/paymentimg/unknown.png" alt="" id="cardimage">
</div>
<div class="card-number-box">
<?php
if (isset($fetch['cardnum']) && !empty($fetch['cardnum'])) {
$cardNumber = $fetch['cardnum'];
$splitCardNumber = implode('-', str_split($cardNumber, 4));
echo $splitCardNumber;
} else {
echo '4444-5555-2222-4444';
}
?>
</div>
<div class="flexbox">
<div class="box">
<span>Card Holder</span>
<div class="card-holder-name">
<?php
if (isset($fetch['cardholder']) && !empty($fetch['cardholder'])) {
echo $fetch['cardholder'];
} else {
echo 'FULL NAME';
}
?>
</div>
</div>
<div class="box">
<span>Expires</span>
<div class="expiration">
<span class="exp-month">
<?php
if (isset($fetch['monthexp']) && !empty($fetch['monthexp'])) {
echo $fetch['monthexp'];
} else {
echo 'MM';
}
?>
</span>
<span class="exp-year">
<?php
if (isset($fetch['yearexp']) && !empty($fetch['yearexp'])) {
echo $fetch['yearexp'];
} else {
echo 'YY';
}
?>
</span>
</div>
</div>
</div>
</div>
<div class="back">
<div class="stripe"></div>
<div class="box">
<span>
<?php
if (isset($fetch['cvv']) && !empty($fetch['cvv'])) {
echo $fetch['cvv'];
} else {
echo '1234';
}
?>
</span>
<div class="cvv-box"></div>
<img src="css/paymentimg/unknown.png" id="cvvimg" alt="">
</div>
</div>
</div>
<form action="" method="post" enctype="multipart/form-data">
<?php
if (isset($msg)) {
foreach ($msg as $msg) {
echo '<span class="error-msg">' . $msg . '</span>';
}
;
}
;
?>
<span class="note">Credit Card Status:
<?php echo isset($fetch['status']) ? $fetch['status'] : 'Unlinked'; ?>
</span>
<div class="radio-inputs">
<label class="radio">
<input type="radio" required name="radio" onclick="detectCardType('css/paymentimg/mastercard.png')"
<?php echo (isset($fetch['cardtype']) && $fetch['cardtype'] === 'Mastercard') ? 'checked' : ''; ?>
value="Mastercard">
<span class="name">MasterCard</span>
</label>
<label class="radio">
<input type="radio" required name="radio" onclick="detectCardType('css/paymentimg/visa.png')"
<?php echo (isset($fetch['cardtype']) && $fetch['cardtype'] === 'Visa') ? 'checked' : ''; ?>
value="Visa">
<span class="name">Visa</span>
</label>
<label class="radio">
<input type="radio" required name="radio" onclick="detectCardType('css/paymentimg/americanex.png')"
<?php echo (isset($fetch['cardtype']) && $fetch['cardtype'] === 'American Express') ? 'checked' : ''; ?>
value="American Express">
<span class="name">American Express</span>
</label>
</div>
<div class="inputBox">
<span>Card Number</span>
<input type="text" required maxlength="19" class="card-number-input" id="cardnumber" name="cardnumber"
placeholder="1234-5678-9012-3456"
value="<?php echo isset($fetch['cardnum']) ? $fetch['cardnum'] : ''; ?> "
oninput="formatCardNumberInput(this)">
</div>
<div class="inputBox">
<span>Card Holder</span>
<input type="text" required class="card-holder-input" placeholder="Ex. Juan Dela Cruz" name="cardname"
value="<?php echo isset($fetch['cardholder']) ? $fetch['cardholder'] : ''; ?>">
</div>
<div class="flexbox">
<div class="inputBox">
<span>Expiration MM</span>
<select name="expmonth" required id="" class="month-input">
<option value="month" selected disabled>month</option>
<?php
$selectedMonth = $fetch['monthexp']; // Assuming $fetch['monthexp'] contains the selected month value
for ($i = 1; $i <= 12; $i++) {
$formattedMonth = sprintf("%02d", $i); // Pad with leading zero if needed
$selected = ($selectedMonth == $formattedMonth) ? 'selected' : '';
echo "<option value=\"$formattedMonth\" $selected>$formattedMonth</option>";
}
?>
</select>
</div>
<div class="inputBox">
<span>Expiration YY</span>
<select name="expyear" required id="" class="year-input" value="<?php echo $fetch['yearexp']; ?>">
<?php
$selectedYear = $fetch['yearexp']; // Assuming $fetch['yearexp'] contains the selected year value
$currentYear = date("Y");
for ($i = $currentYear; $i <= $currentYear + 10; $i++) {
$selected = ($selectedYear == $i) ? 'selected' : '';
echo "<option value=\"$i\" $selected>$i</option>";
}
?>
</select>
</div>
<div class="inputBox">
<span>CVV</span>
<input type="number" maxlength="4" class="cvv-input" placeholder="1234" name="cvv"
value="<?php echo isset($fetch['cvv']) ? $fetch['cvv'] : ''; ?>">
</div>
</div>
<input type="submit" name="link" value="Link Card" class="submit-btn"
<?php echo ($fetch !== null && !empty($fetch)) ? 'hidden' : ''; ?>>
<br><br>
<a href="home.php" id="cardlink" class="back-btn"><i class="fas fa-arrow-left"></i> GO BACK</a>
</form>
</div>
<?php
if (isset($_POST['link'])) {
$cardNumber = $_POST['cardnumber'];
$cardHolder = mysqli_real_escape_string($conn, $_POST['cardname']);
$monthExp = $_POST['expmonth'];
$yearExp = $_POST['expyear'];
$cvv = $_POST['cvv'];
$cardType = mysqli_real_escape_string($conn, $_POST['radio']);
$user_id = $_SESSION['user_id'];
$pin = random_int(10000000, 99999999);
$pinAsString = strval($pin);
$cardNumber = $_POST["cardnumber"];
$cardNumberWithoutHyphen = str_replace("-", "", $cardNumber);
$selectedMonth = intval($_POST['expmonth']);
$selectedYear = intval($_POST['expyear']);
$currentYear = date("Y");
$currentMonth = date("m");
if ($selectedYear < $currentYear || ($selectedYear == $currentYear && $selectedMonth < $currentMonth)) {
$errors[] = "The selected card has already expired.";
}
if (empty($cardNumberWithoutHyphen) || !preg_match("/^\d{16}$/", $cardNumberWithoutHyphen)) {
$errors[] = "Please enter a valid 16-digit card number without hyphens";
}
$cardHolder = $_POST["cardname"];
if (empty($cardHolder) || !preg_match("/^[a-zA-Z\s]+$/", $cardHolder)) {
$errors[] = "Please enter a valid card holder name";
}
$cvv = $_POST["cvv"];
if (empty($cvv) || !preg_match("/^\d{3,4}$/", $cvv)) {
$errors[] = "Please enter a valid CVV (3 or 4 digits)";
}
if (!empty($errors)) {
foreach ($errors as $error) {
echo "<p style='background-color: red; color:white;'>$error</p>";
}
} else {
$update = mysqli_query($conn, "UPDATE `tblpayment` SET userid = '$user_id', pin='$pinAsString' WHERE cardnum = '$cardNumber' AND cardholder = '$cardHolder' AND monthexp = '$monthExp' AND yearexp = '$yearExp' AND cvv = '$cvv' AND cardtype = '$cardType'") or die('Update query failed');
if ($update !== false) {
if (mysqli_affected_rows($conn) > 0) {
echo "<p style='background-color: green; color:white;'>Card successfully linked!</p>";
} else {
echo "<p style='background-color: red; color:white;'>Failed to link card. Please try again.</p>";
}
} else {
echo "<p style='background-color: red; color:white;'>Failed to execute update query. Please try again.</p>";
}
}
}
?>
<script>
function formatCardNumberInput(input) {
var cardNumber = input.value.replace(/\D/g, '');
var formattedCardNumber = cardNumber.replace(/(\d{4})(?=\d)/g, '$1-');
input.value = formattedCardNumber;
}
window.onload = function() {
var cardNumberInput = document.getElementById('cardnumber');
formatCardNumberInput(cardNumberInput);
};
function detectCardType(imageSrc) {
var imgElement = document.getElementById('cardimage');
var cvvimage = document.getElementById('cvvimg');
imgElement.src = imageSrc;
cvvimage.src = imageSrc;
}
document.querySelector('.card-number-input').oninput = () => {
document.querySelector('.card-number-box').innerText = document.querySelector('.card-number-input').value;
}
document.querySelector('.card-holder-input').oninput = () => {
document.querySelector('.card-holder-name').innerText = document.querySelector('.card-holder-input').value;
}
document.querySelector('.month-input').oninput = () => {
document.querySelector('.exp-month').innerText = document.querySelector('.month-input').value;
}
document.querySelector('.year-input').oninput = () => {
document.querySelector('.exp-year').innerText = document.querySelector('.year-input').value;
}
document.querySelector('.cvv-input').onmouseenter = () => {
document.querySelector('.front').style.transform = 'perspective(1000px) rotateY(-180deg)';
document.querySelector('.back').style.transform = 'perspective(1000px) rotateY(0deg)';
}
document.querySelector('.cvv-input').onmouseleave = () => {
document.querySelector('.front').style.transform = 'perspective(1000px) rotateY(0deg)';
document.querySelector('.back').style.transform = 'perspective(1000px) rotateY(180deg)';
}
document.querySelector('.cvv-input').oninput = () => {
document.querySelector('.cvv-box').innerText = document.querySelector('.cvv-input').value;
}
</script>
</body>
</html>