-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckout.php
More file actions
260 lines (219 loc) · 10.5 KB
/
checkout.php
File metadata and controls
260 lines (219 loc) · 10.5 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
<!DOCTYPE html>
<html lang="en">
<head>
<?php
include "./includes/bootstrap-header.php"
?>
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
<link rel="stylesheet" href="./css/checkout.css">
<script src="https://js.stripe.com/v3/"></script>
<link rel="stylesheet" href="./css/stripe.css">
</head>
<body>
<?php
include "./includes/nav.php"
?>
<div class="login-dark" id="showcase" style="background: url(./images/hd/floorneedspolish.png) center / cover no-repeat;">
<form action="./purchaseHandler.php" method="post" id="payment-form" style="max-width: 640px;background: rgba(0,0,0,0.73);padding: 0;min-height: 500px;">
<div class="illustration"></div>
<h1 id="orderheading">Your Order</h1>
<h2 id="orderpricing">$0</h2>
<?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 > 99999999 ? 99999999 : $totalprice*100, //payment in cents, this cannot go over $999,999.99 as this is a test doesnt matter.
'currency' => 'sgd',
//'receipt_email' => "", //receipt emails in Stripe test mode will not send.
]);
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 = 4-$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 class="illustration"></div>
<div class="form-group">
<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>
<div class="form-group"><button class="btn btn-primary btn-block" id="cobutton" data-secret="<?php echo $intent->client_secret ?>">Checkout</button></div>
</form>
</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.style.background="url(" + element.firstElementChild.src + ")";
})
})
</script>
</html>