Repository navigation
Expand file tree
/
Copy pathcheckout.php
More file actions
420 lines (328 loc) · 13.2 KB
/
Copy pathcheckout.php
File metadata and controls
420 lines (328 loc) · 13.2 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
<?php include 'headerwebsite.php'; ?>
<?php
include("apiCredential.php");
?>
<!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>Checkoutpage</title>
<style>
.container {
max-width: 700px;
position: relative;
top:34px;
/* border:2px solid red; */
box-shadow: -4px -2px 64px -17px #aaaaaa;
margin-bottom:70px;
padding:30px;
}
.container2{
position: relative;
top: 114px;
z-index: 3;
display:inline;
}
.lh-condensed
{ line-height: 1.25; }
</style>
</head>
<body>
<div class="container2">
<?php
/*
$curl = curl_init();
// Set the URL and other options
curl_setopt_array($curl, array(
CURLOPT_URL => "http://localhost/groceryWebsite/api/currentToken.php?key=avdfheuw23&id=",
CURLOPT_RETURNTRANSFER => true, // Return the response instead of outputting it
CURLOPT_ENCODING => "", // Accept any encoding
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET", // Change this to the HTTP method you need
));
// API endpoint URL
$url = "";
*/
function getMacAddress() {
$output = shell_exec('ifconfig eth0 | grep "ether"');
$mac_address = substr($output, strpos($output, " ") + 1, 17);
return $mac_address;
}
echo getMacAddress();
$ch = curl_init();
$keyApi=API_KEY;
$token=$_GET['token'];
if($token==null){
header("location:index.php");
return false;
}
$url = API_URL."carts.php?key=".$keyApi."&discount=0&code=''";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: Bearer $token"
));
$response = curl_exec($ch);
// Check for errors
if (curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
}
// Close the cURL session
curl_close($ch);
// Output the response
$response;
// Send the request and get the response
$response=json_decode($response,true);
?>
<div class="row">
<div class="col-lg-4 order-md-2 col-sm-12 mb-4">
<h4 class="d-flex justify-content-between align-items-center mb-3">
<span class="text-muted">Your cart</span>
<span class="badge badge-secondary badge-pill" id="numberofcart"><?php echo $response["totalRecord"]; ?></span>
</h4>
<ul class="list-group mb-3 sticky-top">
<div id="productOrder">
<?php
foreach( $response["data"] as $key ){
$image_path=$key["image"];
?>
<li class='list-group-item d-flex justify-content-between lh-condensed'>
<div>
<img src='<?=$image_path; ?>' width='60'>
<h2 class='text-muted priceProduct '> <?= $key["productName"];?></h2>
</div>
<span class='priceProduct'><h2> <?= $key["price"];?> Rs</h2></span>
<span class='productQty'> <?php echo $key["qty"];?> </span>
</li>
<?php
}
?>
</div>
<?php
?>
<li class="list-group-item d-flex justify-content-between">
<h1>Total Cart (Rs)</h1>
<h1 id="cartTotal"><?= $response["cartTotal"]["cartTotal"]; ?></h1>
</li>
<li class="list-group-item d-flex justify-content-between">
<h1>GST <span id="gstperc"></span> (RS)</h1>
<h1 id="gst"><?= $response["cartTotal"]["gst"];?></h1>
</li>
<li class="list-group-item d-flex justify-content-between">
<h1>Delivery Charge (RS)</h1>
<h1 id="deliveryCharge"><?= $response["cartTotal"]["deliveryCharge"];?></h1>
</li>
<li class="list-group-item d-flex justify-content-between">
<h1>Discount (RS)</h1>
<h1 id="discount"><?= $response["cartTotal"]["discount"];?></h1>
</li>
<li class="list-group-item d-flex justify-content-between">
<h1>Final Amount (RS)</h1>
<h1 id="finalAmount"><?= $response["cartTotal"]["totalAmount"];?></h1>
</li>
</ul>
<ul>
</ul>
</div>
<div class="container">
<div class="col-md-8 col-sm-12 order-md-1">
<h4 class="mb-3">Billing address</h4>
<form class="needs-validation" novalidate="">
<div class="row">
<div class="col-md-12 mb-3">
<label for="lastName">customer name</label>
<input type="text" class="form-control" id="customerName" placeholder="" value="" required="">
<div class="invalid-feedback"> Valid last name is required. </div>
</div>
</div>
<div class="mb-3">
<label for="email">Email <span class="text-muted">(Optional)</span></label>
<input type="email" class="form-control" id="email" placeholder="you@example.com">
<div class="invalid-feedback"> Please enter a valid email address for shipping updates. </div>
</div>
<div class="mb-3">
<label for="address">Address</label>
<input type="text" class="form-control" id="address" placeholder="1234 Main St" required="">
<div class="invalid-feedback"> Please enter your shipping address. </div>
</div>
<div class="mb-3">
<label for="address2">Mobile <span class="text-muted">(Optional)</span></label>
<input type="text" class="form-control" id="address2" placeholder="Apartment or suite">
</div>
<hr class="mb-4">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="same-address">
<label class="custom-control-label" for="same-address">Shipping address is the same as my billing address</label>
</div>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="save-info">
<label class="custom-control-label" for="save-info">Save this information for next time</label>
</div>
<hr class="mb-4">
<h4 class="mb-3">Payment</h4>
<div class="d-block my-3">
<div class="custom-control custom-radio">
<input id="paypal" name="paymentMethod" type="radio" class="custom-control-input" required="" checked>
<label class="custom-control-label" for="paypal">Cash On Delivery</label>
</div>
</div>
<hr class="mb-4">
<p id="errorMsg">
</p>
<button class="btn btn-block" id="backButton" type="button" onclick="backtoHome()">Back To Shopping</button>
<button class="btn btn-lg btn-block" id="placeOrder" onclick="PlaceOrder()" type="button">Place Order</button>
</form>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script>
<?php include "footer.php";?>
</body>
</html>
<script>
async function PlaceOrder(){
var discount= document.getElementById("discount").innerText;
var couponcode ="No Coupon Applied";
discount=parseInt(discount);
var samae_addrees =document.getElementById("same-address").checked;
var save_info= document.getElementById("save-info").checked;
var paymentmethod="cash";
var paymentStatus=1;
var orderStatus=1;
const APIKEY = "avdfheuw23";
jwtToken = localStorage.getItem("key");
const jwt = jwtToken;
const jwtData = jwt.split('.')[1]; // Get the data section of the JWT
const decodedJwtData = atob(jwtData); // Decode the base64-encoded data
const parsedJwtData = JSON.parse(decodedJwtData);
localStoreAddrees=parsedJwtData.address;
billingAddrees=document.getElementById("address").value;
console.log(billingAddrees);
if(billingAddrees ==""){
if (localStoreAddrees==""){
swal("Oops","please provide delivery address","error");
return false;
}
}else{
if(billingAddrees==localStoreAddrees){
saveAddrees=0;
address=billingAddrees;
}else {
if(samae_addrees==true){
address=billingAddrees;
}else{
if(localStoreAddrees!=""){
address=localStoreAddrees;
}else{
address=billingAddrees;
}
}
if(save_info==true){
saveAddrees=1;
}else{
saveAddrees=0;
}
}
}
var OrderObject={
"orderStatus":1,
"save": saveAddrees,
"discount":discount,
"deliveryAddress":address,
"couponCode":couponcode,
"type":"order_place",
"paymentStatus":1,
"paymentmethod":"cash",
"deliveryboyid":0
};
OrderObject=JSON.stringify(OrderObject);
console.log(parsedJwtData);
apiurl = API_PATH+"orders.php?key=" + APIKEY;
const orderResponse =await fetch(apiurl, {
method: "POST",
headers: {
'Authorization': `Bearer ${jwtToken}`
},
body:OrderObject
});
response=await orderResponse.text();
swal("Congrats","Order Placed Succesfully","success");
console.log(response);
localStorage.removeItem("carts");
localStorage.removeItem("cartTotal");
localStorage.removeItem("cartDiscount");
setTimeout(() => {
window.location.href="index.php";
},3000);
}
function backtoHome(){
window.location.href="index.php";
}
async function applyCoupon() {
CouponCode = document.getElementById("coupon").value;
if (CouponCode == "") {
alert("please provide coupon code");
} else {
CouponCode=CouponCode.toUpperCase();
getCartItem = localStorage.getItem("carts");
getCartItem = JSON.parse(getCartItem);
getCartTotal = localStorage.getItem("cartTotal");
getCartTotal = JSON.parse(getCartTotal);
cartData = {
"cartTotal": getCartTotal.cartTotal,
"couponCode": CouponCode,
"cart": {
"data": getCartItem,
"totalRecord": getCartTotal.totalItem
}
};
cartData = JSON.stringify(cartData);
console.log(cartData);
apiurl = API_PATH+"coupon.php?key=" + APIKEY;
const orderResponse = await fetch(apiurl, {
method: "POST",
body: cartData
});
response = await orderResponse.json();
console.log("Apply Coupon code");
console.log(response);
if(response.status=="success"){
swal("Congrats",response.msg,response.status);
}else{
swal("Oops!",response.msg,response.status);
}
document.getElementById("numberofcart").innerText = response.totalItem;
document.getElementById("cartTotal").innerText = response.cartTotal.cartTotal;
document.getElementById("gst").innerText = response.gst;
document.getElementById("deliveryCharge").innerText = response.deliveryCharge;
document.getElementById("finalAmount").innerText = response.totalAmount;
document.getElementById("discount").innerText = response.discount;
discount = response.discount;
code = response.couponCode;
var cartDiscount={
discount:discount,
code:code
}
localStorage.removeItem("cartTotal");
localStorage.setItem("cartDiscount",JSON.stringify(cartDiscount));
cartDetail(discount,code);
if (response.cartTotal.cartTotal < response.cartTotal.minOrder) {
alert("min");
errorMsg="";
errorMsg+="<div class='alert alert-danger alert-dismissible'>";
errorMsg+=" <button type='button' class='close' data-dismiss='alert'>×</button>";
errorMsg+=" <strong>Warning!</strong> <p> Minimum Amount To Place Is "+ response.cartTotal.minOrder+" Rs . </p>";
errorMsg+="</div>";
alert(errorMsg);
document.getElementById("errorMsg").innerHTML=errorMsg;
document.getElementById("placeOrder").disabled = true;
} else {
document.getElementById("placeOrder").disabled = false;
}
}
}
</script>