forked from abdulsamad707/groceryWebsite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckout.php
More file actions
371 lines (297 loc) · 12.8 KB
/
Copy pathcheckout.php
File metadata and controls
371 lines (297 loc) · 12.8 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
<?php include 'headerwebsite.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;
box-shadow: 4px 7px 9px #aaaaaa;
}
.lh-condensed
{ line-height: 1.25; }
</style>
</head>
<body>
<div class="container2">
<div class="row">
<div class="col-md-4 order-md-2 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">3</span>
</h4>
<ul class="list-group mb-3 sticky-top">
<div id="productOrder">
<li class="list-group-item d-flex justify-content-between lh-condensed">
<div>
<h2 class="text-muted">Brief description</h2>
</div>
<span><h2>120 Rs</h2></span>
<span><h2>12</h2></span>
</li>
<li class="list-group-item d-flex justify-content-between lh-condensed">
<div>
<h6 class="my-0">Second product</h6>
<h2 class="text-muted">Brief description</h2>
</div>
<span class="text-muted">$8</span>
</li>
<li class="list-group-item d-flex justify-content-between lh-condensed">
<div>
<h6 class="my-0">Third item</h6>
<h2 class="text-muted">Brief description</h2>
</div>
<span class="text-muted">$5</span>
</li>
</div>
<li class="list-group-item d-flex justify-content-between bg-light">
<div class="text-success">
<h6 class="my-0">Promo code</h6>
<h2 id="couponcode">EXAMPLECODE</h2>
</div>
</li>
<li class="list-group-item d-flex justify-content-between">
<h1>Total Cart (Rs)</h1>
<h1 id="cartTotal">$20</h1>
</li>
<li class="list-group-item d-flex justify-content-between">
<h1>GST (RS)</h1>
<h1 id="gst">$20</h1>
</li>
<li class="list-group-item d-flex justify-content-between">
<h1>Delivery Charge (RS)</h1>
<h1 id="deliveryCharge">$20</h1>
</li>
<li class="list-group-item d-flex justify-content-between">
<h1>Discount (RS)</h1>
<h1 id="discount">$20</h1>
</li>
<li class="list-group-item d-flex justify-content-between">
<h1>Final Amount (RS)</h1>
<h1 id="finalAmount">$20</h1>
</li>
</ul>
<ul>
</ul>
</div>
<div class="container">
<div class="col-md-8 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>
<div class="row">
<div class="col-md-6">
<input type="text" class="form-control" id="coupon" placeholder="Coupon Code">
</div>
<div class="col-md-6">
<button type="button" class="couponCodeApply" onclick="applyCoupon()">Apply Coupon </buuton>
</div>
</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-primary btn-block" id="backButton" type="button" onclick="backtoHome()">Back To Shopping</button>
<button class="btn btn-lg btn-block" id="placeOrder" onclick="PlaceOrder()" disabled=true 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(){
alert("hi");
var discount= document.getElementById("discount").innerText;
var couponcode= document.getElementById("couponcode").innerText;
discount=parseInt(discount);
alert(discount);
var samae_addrees =document.getElementById("same-address").checked;
var save_info= document.getElementById("save-info").checked;
alert(save_info);
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;
if(billingAddrees && localStoreAddrees==""){
alert("please provide delivery address");
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.json();
swal("Congrats","Order Placed Succesfully","success");
console.log(response);
localStorage.removeItem("carts");
localStorage.removeItem("cartTotal");
localStorage.removeItem("cartDiscount");
setTimeout(() => {
window.location.href="index.php";
},1000);
}
function backtoHome(){
window.location.href="index.php";
}
async function applyCoupon() {
CouponCode = document.getElementById("coupon").value;
if (CouponCode == "") {
alert("please provide coupon code");
} else {
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;
document.getElementById("couponcode").innerText = response.couponCode;
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-success alert-dismissible'>";
errorMsg+=" <button type='button' class='close' data-dismiss='alert'>×</button>";
errorMsg+=" <strong>Success!</strong> <p> Minimum Amount To Place Is"+ response.cartTotal.minOrder+".</p>";
errorMsg+="</div>";
alert(errorMsg);
document.getElementById("errorMsg").innerHTML=errorMsg;
document.getElementById("placeOrder").disabled = true;
} else {
document.getElementById("placeOrder").disabled = false;
}
}
}
</script>