-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckoutold.php
More file actions
304 lines (249 loc) · 11.3 KB
/
checkoutold.php
File metadata and controls
304 lines (249 loc) · 11.3 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
<html lang="en">
<head>
<meta charset="UTF-8">
<?php
include "./includes/header.php"
?>
<script src="https://js.stripe.com/v3/"></script>
<link rel="stylesheet" href="./css/stripe.css">
</head>
<body>
<?php
include "./includes/nav.php"
?>
<div class="auth">
<img id="showcase" src="./images/hd/floorneedspolish.png"/>
<div class="checkout-container">
<br>
<br>
<div class="page-header">
<div class="data">
<h1 id="orderheading">Your Order</h1>
<h2 id="orderpricing">$0 SGD</h2>
</div>
<!-- hide the button if thcart is empty-->
</div>
<div class="cart">
<?php
require_once('vendor/autoload.php'); //composer autoload, for stripe
session_start();
$memberid = 1;
$intent = ""; //for stripe
setlocale(LC_MONETARY, 'en_US.UTF-8'); //for money_format to currency USD $
if (isset($_SESSION['memberid'])) {
$memberid = $_SESSION['memberid'];
}
else {
header("Location: ./login.php?rd=checkout"); //header wont work because bootstrap is bad bad
echo "<meta http-equiv='refresh' content='0;url=./login.php?rd=checkout'>";
return;
}
$config = parse_ini_file('../../private/db-config.ini');
$conn = new mysqli($config['servername'], $config['username'], $config['password'], "project1004");
if ($conn->connect_error) {
$errorMsg = "Connection failed: " . $conn->connect_error;
echo $errorMsg;
$success = false;
}
else {
$stmt = $conn->prepare("SELECT * FROM cart ca, car c where ca.member_id = ? AND c.id = ca.car_id");
//int id, varchar catID, varchar brand, varchar heading, float price, int stock, bool forRent, varchar model,text description,varchar bigImage,varchar logo, int isMain
$stmt->bind_param("i", $memberid);
$stmt->execute();
$result = $stmt->get_result();
$totalprice = 0;
//echo $result->num_rows;
if ($result->num_rows > 0) {
//$row = $result->fetch_assoc();
$d1 = 0;
while($row = $result->fetch_assoc()) {
$display = $row["media"];
$logo = $row["brand"] . ".png";
$itemname = strtoupper($row["brand"]) . " " . $row["model"];
$price = money_format('%.2n', $row["price"]);
$totalprice += $row["price"];
if ($d1 == 0) {
echo "<script>document.getElementById('showcase').src = './images/hd/$display';</script>";
$d1 = 1;
}
?>
<div class="cart-data">
<img class="display" src="./images/hd/<?php echo $display?>"/>
<img class="small-logo" src="./images/logos/<?php echo $logo?>"/>
<p class="item-name"><?php echo $itemname?></p>
<div class="item-data">
<p><?php echo $price?></p>
</div>
</div>
<?php
}
//stripe initialize, this secretkey is a test
\Stripe\Stripe::setApiKey('sk_test_51Hs3bsBSbRVapwZw2VcuX5ux9edv11xbDQRA2DRM8vnT5428GWDqOINBf6vjbc4PqS6pmTxwr90TtTSKMOOuwCHq00wh0BiG3k');
$intent = \Stripe\PaymentIntent::create([
'amount' => $totalprice*100, //payment in cents
'currency' => 'sgd',
]);
echo "<script>document.getElementById('orderpricing').innerHTML = '". money_format('%.2n', $totalprice) ."';</script>";
}
else {
//also hide checkout button
echo "<script>document.getElementById('orderheading').innerHTML = 'No items. Add more!';</script>";
}
if ($result->num_rows < 4) {
$extra = 5-$result->num_rows;
for ($i = 0; $i < $extra; $i++) {
?>
<div class="cart-data">
<a href="./">
<img class="display" src="./images/hd/background.jpg"/>
<img class="smaller-logo" src="./images/logos/plus.png"/>
<p class="item-name light-accent">Continue Shopping!</p>
<div class="item-data">
<p></p>
</div>
</a>
</div>
<?php
}
}
}
//;
?>
</div>
<form action="./purchaseHandler.php" method="post" id="payment-form">
<div class="form-row">
<div class="page-header">
<div class="data">
<h1 id="orderheading">Payment: Credit Card</h1>
</div>
</div>
<div id="card-element">
<!-- A Stripe Element will be inserted here. -->
</div>
<!-- Used to display form errors. -->
<div id="card-errors" role="alert"></div>
</div>
<button id="cobutton" class="cobutton" data-secret="<?php echo $intent->client_secret ?>">Checkout</button>
</form>
</div>
</div>
<!-- <div class="checkout-container">
<div class="checkout">
<p>Confirm your order</p>
<p>Total: $9,999,999 SGD</p>
<p>Select a payment method</p>
<div class="card-form"></div> place credit card info here
<button>Complete Payment</button> button will check out after confirming login password
<p>WE ACCEPT</p>
<p>icons of visa mc etc</p>
</div>
<div class="checkout-completed"> hidden until completed
<p>tick/cross icon</p>
<p>Payment Successful / Failed</p>
<p>Total: $9,999,999 SGD</p>
<p>You will receive an invoice via your email.</p>
<button>View Order Status</button> button will check out after confirming login password
</div>
</div> -->
<?php
include "./includes/footer.php";
?>
</body>
<script>
//for payments
var stripe = Stripe('pk_test_51Hs3bsBSbRVapwZwJaGdkddKhwOH1jPoAnBLj5w4vCFvMHebOWkLQg4I9d1QGCxrkW0lFTYKhmYLgjV84X8jYCOe00POi5NMh4');
var elements = stripe.elements();
var style = { //custom styling
base: {
color: '#32325d',
fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
fontSmoothing: 'antialiased',
fontSize: '16px',
'::placeholder': {
color: '#aab7c4'
}
},
invalid: {
color: '#fa755a',
iconColor: '#fa755a'
}
};
// Create an instance of the card Element.
var card = elements.create('card', {style: style});
// Add an instance of the card Element into the `card-element` <div>.
card.mount('#card-element');
// Handle real-time validation errors from the card Element.
card.on('change', function(event) {
var displayError = document.getElementById('card-errors');
if (event.error) {
displayError.textContent = event.error.message;
} else {
displayError.textContent = '';
}
});
// Handle form submission.
var form = document.getElementById('payment-form');
form.addEventListener('submit', function(event) {
event.preventDefault();
stripe.createToken(card).then(function(result) {
if (result.error) {
// Inform the user if there was an error.
var errorElement = document.getElementById('card-errors');
errorElement.textContent = result.error.message;
} else {
// Send the token to your server.
stripeTokenHandler(result.token);
}
});
});
// Submit the form with the token ID.
function stripeTokenHandler(token) { //dont even need token
// Insert the token ID into the form so it gets submitted to the server
var form = document.getElementById('payment-form');
// var hiddenInput = document.createElement('input');
// hiddenInput.setAttribute('type', 'hidden');
// hiddenInput.setAttribute('name', 'stripeToken');
// hiddenInput.setAttribute('value', token.id);
// form.appendChild(hiddenInput);
stripe.confirmCardPayment(document.getElementById('cobutton').dataset.secret, {
payment_method: {
card: card
}
}).then(function(result) {
if (result.error) {
// Show error
var hiddenInput = document.createElement('input');
hiddenInput.setAttribute('type', 'hidden');
hiddenInput.setAttribute('name', 'results');
hiddenInput.setAttribute('value', result.error.message);
form.appendChild(hiddenInput);
form.submit();
console.log(result.error.message);
}
else {
// The payment successs
if (result.paymentIntent.status === 'succeeded') {
var hiddenInput = document.createElement('input');
hiddenInput.setAttribute('type', 'hidden');
hiddenInput.setAttribute('name', 'results');
hiddenInput.setAttribute('value', 'success');
form.appendChild(hiddenInput);
var hiddenInput2 = document.createElement('input');
hiddenInput2.setAttribute('type', 'hidden');
hiddenInput2.setAttribute('name', 'chargeid');
hiddenInput2.setAttribute('value', result.paymentIntent.id);
form.appendChild(hiddenInput2);
form.submit();
}
}
})
}
var cartdatas = document.getElementsByClassName("cart-data")
var showcase = document.getElementById("showcase")
Array.from(cartdatas).forEach((element) => {
element.addEventListener('click', () => {
showcase.src = element.firstElementChild.src
})
})
</script>
</html>