-
Notifications
You must be signed in to change notification settings - Fork 0
/
commerce.js
438 lines (399 loc) · 14.4 KB
/
commerce.js
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
var totalCost = document.getElementById('amounted');
//each element and their properties
var products = [
{
index: 1,
id: 'p1',
name: 'Samsung TV',
price: 500000
},
{
index: 2,
id: 'p2',
name: 'Pixel 4a',
price: 250000
},
{
index: 3,
id: 'p3',
name: 'PS 5',
price: 300000
},
{
index: 4,
id: 'p4',
name: 'MacBook Air',
price: 800000
},
{
index: 5,
id: 'p5',
name: 'Apple Watch',
price: 95000
},
{
index: 6,
id: 'p6',
name: 'Air Pods',
price: 75000
}
];
//Form Input Verification
//name verification
function NameVerification(){
let name = document.getElementById('elementname');
let nameerr = document.getElementById("nameError");
if(!name.value){
name.style.border = '4px solid red';
nameerr.innerHTML = 'Please enter your name';
}else {
name.style.border = '4px solid green';
nameerr.innerHTML = '';
}
}
//Email verification
function EmailVerification(){
let email = document.getElementById('email');
let eerr = document.getElementById('emailError')
if (!email.value){
email.style.border = '4px solid red';
eerr.innerHTML = 'Please enter your e-mail';
} else if (!email.value.includes('@')){
email.style.border = '4px solid red';
eerr.innerHTML = 'Please Enter correct HTML';
} else {
email.style.border = '4px solid green';
eerr.innerHTML = '';
}
}
//Phone number verification
function PNumberVerification(){
let num = document.getElementById('pnumber');
let numerr = document.getElementById('pError');
if (!num.value){
num.style.border = '4px solid red';
numerr.innerHTML = 'Please enter your phone number'
}else if (! /^[0-9]{11}$/.test(num.value)){
num.style.border = '4px solid red';
numerr.innerHTML = 'make sure you enter 11 digit number';
}else{
num.style.border = '4px solid green';
numerr.innerHTML = '';
}
}
// displaying element
function DisplayElem(elementid) {
let element = document.getElementById(elementid);
element.style.display = 'block';
}
// Not display Element
function NotDisplayElem(elementid) {
let element = document.getElementById(elementid);
element.style.display = 'none';
}
//Increse each body element
function IncreaseNum(elementid) {
let element = document.getElementById(elementid);
let coun = parseInt(element.innerHTML);
coun++;
element.innerHTML = coun;
//Handling the total amount and change in amount of product when clicking button
let pay1 = parseInt(document.getElementById('present1').innerHTML);
let pay2 = parseInt(document.getElementById('present2').innerHTML);
let pay3 = parseInt(document.getElementById('present3').innerHTML);
let pay4 = parseInt(document.getElementById('present4').innerHTML);
let pay5 = parseInt(document.getElementById('present5').innerHTML);
let pay6 = parseInt(document.getElementById('present6').innerHTML);
totalCost.innerHTML = (pay1 * products[0].price) + (pay2 * products[1].price) + (pay3 * products[2].price) + (pay4 * products[3].
price) + (pay5 * products[4].price) + (pay6 * products[5].price);
//console.log(totalCost.innerHTML);
let prc1 = document.getElementById('prc1');
prc1.innerHTML = parseInt(pay1 * products[0].price);
let prc2 = document.getElementById('prc2');
prc2.innerHTML = parseInt(pay2 * products[1].price);
let prc3 = document.getElementById('prc3');
prc3.innerHTML = parseInt(pay3 * products[2].price);
let prc4 = document.getElementById('prc4');
prc4.innerHTML = parseInt(pay4 * products[3].price);
let prc5 = document.getElementById('prc5');
prc5.innerHTML = parseInt(pay5 * products[4].price);
let prc6 = document.getElementById('prc6');
prc6.innerHTML = parseInt(pay6 * products[5].price);
let ttotal = document.getElementById('tamounnt');
ttotal.value = totalCost.innerHTML;
//console.log(ttotal.value);
}
// Decrease each body element
function DecreaseNum(elementname) {
let last = elementname.charAt(elementname.length - 1);
//console.log(last);
let element = document.getElementById(elementname);
let coun = parseInt(element.innerHTML);
if (coun >= 2) {
coun--;
element.innerHTML = coun;
}
else {
alert(`The quantity can't be less than 1 and it will be removed,So if you still want to buy it, add the item back in the Add to Cart Menu. Thank you! `);
let elel = 'pixel' + last;
let p = 'p' + last;
coun = 0;
element.innerHTML = coun;
// console.log(p);
// console.log(elel);
RemoveFromCart(p, elel);
}
//Handling the total amount and each element price when increment or decrement button is pressed.
let pay1 = parseInt(document.getElementById('present1').innerHTML);
let pay2 = parseInt(document.getElementById('present2').innerHTML);
let pay3 = parseInt(document.getElementById('present3').innerHTML);
let pay4 = parseInt(document.getElementById('present4').innerHTML);
let pay5 = parseInt(document.getElementById('present5').innerHTML);
let pay6 = parseInt(document.getElementById('present6').innerHTML);
totalCost.innerHTML = (pay1 * products[0].price) + (pay2 * products[1].price) + (pay3 * products[2].price) + (pay4 * products[3].
price) + (pay5 * products[4].price) + (pay6 * products[5].price);
//console.log(totalCost.innerHTML);
let prc1 = document.getElementById('prc1');
prc1.innerHTML = parseInt(pay1 * products[0].price);
let prc2 = document.getElementById('prc2');
prc2.innerHTML = parseInt(pay2 * products[1].price);
let prc3 = document.getElementById('prc3');
prc3.innerHTML = parseInt(pay3 * products[2].price);
let prc4 = document.getElementById('prc4');
prc4.innerHTML = parseInt(pay4 * products[3].price);
let prc5 = document.getElementById('prc5');
prc5.innerHTML = parseInt(pay5 * products[4].price);
let prc6 = document.getElementById('prc6');
prc6.innerHTML = parseInt(pay6 * products[5].price);
let ttotal = document.getElementById('tamounnt');
ttotal.value = totalCost.innerHTML;
console.log(ttotal.value);
}
//function on and of siaplay
function OnandOfPopPay(){
let pop = document.getElementById('pay');
if (pop.style.display === 'block'){
pop.style.display = 'none';
}
else {
pop.style.display = 'block';
}
}
//Displaying pop-up page
function displayPopPay() {
let pop = document.getElementById('pay');
pop.style.display = 'block';
}
//Nullifying the pop and reload the entire webpage
function ReloadSession() {
location.reload();
}
//Not displaying the pop up
function RemovePopPay() {
let pop = document.getElementById('pay');
pop.style.display = 'none';
}
//Display Summary
function DisplaySummary(refe){
let summary = document.getElementById('summary');
let refid = document.getElementById("refid");
refid.innerHTML = refe;
summary.style.display = 'block';
}
function RemoveFromCart(elementid, elementname) {
let Para = document.getElementById(elementid);
let counter = document.getElementById('spa');
let numcount = parseInt(counter.innerHTML);
let lastem = elementid.charAt(elementid.length - 1);
let newelem = 'present' + lastem;
if (Para.innerHTML === 'ADD TO CART') {
DisplayElem(elementname);
numcount += 1;
counter.innerHTML = numcount;
Para.innerHTML = "REMOVE FROM THE CART";
Para.style.backgroundColor = " #FF7A00";
let newelement = document.getElementById(newelem);
newelement.innerHTML = 1;
}
else {
NotDisplayElem(elementname);
if (count > 0) {
numcount -= 1;
counter.innerHTML = numcount;
Para.innerHTML = 'ADD TO CART';
Para.style.backgroundColor = " #FF7A00";
let newelement = document.getElementById(newelem);
newelement.innerHTML = 0;
} else {
numcount = 0;
counter.innerHTML = numcount;
Para.innerHTML = 'ADD TO CART';
Para.style.backgroundColor = " #FF7A00";
let newelement = document.getElementById(newelem);
newelement.innerHTML = 0;
}
}
//Handling the total amount and showing in the respective increment of element in each button
let pay1 = parseInt(document.getElementById('present1').innerHTML);
let pay2 = parseInt(document.getElementById('present2').innerHTML);
let pay3 = parseInt(document.getElementById('present3').innerHTML);
let pay4 = parseInt(document.getElementById('present4').innerHTML);
let pay5 = parseInt(document.getElementById('present5').innerHTML);
let pay6 = parseInt(document.getElementById('present6').innerHTML);
totalCost.innerHTML = (pay1 * products[0].price) + (pay2 * products[1].price) + (pay3 * products[2].price) + (pay4 * products[3].
price) + (pay5 * products[4].price) + (pay6 * products[5].price);
let prc1 = document.getElementById('prc1');
prc1.innerHTML = parseInt(pay1 * products[0].price);
let prc2 = document.getElementById('prc2');
prc2.innerHTML = parseInt(pay2 * products[1].price);
let prc3 = document.getElementById('prc3');
prc3.innerHTML = parseInt(pay3 * products[2].price);
let prc4 = document.getElementById('prc4');
prc4.innerHTML = parseInt(pay4 * products[3].price);
let prc5 = document.getElementById('prc5');
prc5.innerHTML = parseInt(pay5 * products[4].price);
let prc6 = document.getElementById('prc6');
prc6.innerHTML = parseInt(pay6 * products[5].price);
let ttotal = document.getElementById('tamounnt');
ttotal.value = totalCost.innerHTML;
}
//calling each element with its dom
let button1 = document.getElementById('p1');
let button2 = document.getElementById('p2');
let button3 = document.getElementById('p3');
let button4 = document.getElementById('p4');
let button5 = document.getElementById('p5');
let button6 = document.getElementById('p6');
button1.onclick = function () {
RemoveFromCart('p1', 'pixel1');
}
button2.onclick = function () {
RemoveFromCart('p2', 'pixel2');
}
button3.onclick = function () {
RemoveFromCart('p3', 'pixel3');
}
button4.onclick = function () {
RemoveFromCart('p4', 'pixel4');
}
button5.onclick = function () {
RemoveFromCart('p5', 'pixel5');
}
button6.onclick = function () {
RemoveFromCart('p6', 'pixel6');
}
//calling each increment with its dom
let btn1 = document.getElementById('btn-sale1');
let btn2 = document.getElementById('btn-sale2');
let btn3 = document.getElementById('btn-sale3');
let btn4 = document.getElementById('btn-sale4');
let btn5 = document.getElementById('btn-sale5');
let btn6 = document.getElementById('btn-sale6');
btn1.onclick = function () {
IncreaseNum('present1');
}
btn2.onclick = function () {
IncreaseNum('present2');
}
btn3.onclick = function () {
IncreaseNum('present3');
}
btn4.onclick = function () {
IncreaseNum('present4');
}
btn5.onclick = function () {
IncreaseNum('present5');
}
btn6.onclick = function () {
IncreaseNum('present6');
}
//calling each increment with its dom
let nbtn1 = document.getElementById('btn-sal1');
let nbtn2 = document.getElementById('btn-sal2');
let nbtn3 = document.getElementById('btn-sal3');
let nbtn4 = document.getElementById('btn-sal4');
let nbtn5 = document.getElementById('btn-sal5');
let nbtn6 = document.getElementById('btn-sal6');
nbtn1.onclick = function () {
DecreaseNum('present1');
}
nbtn2.onclick = function () {
DecreaseNum('present2');
}
nbtn3.onclick = function () {
DecreaseNum('present3');
}
nbtn4.onclick = function () {
DecreaseNum('present4');
}
nbtn5.onclick = function () {
DecreaseNum('present5');
}
nbtn6.onclick = function () {
DecreaseNum('present6');
}
//The receipt Catalogue Function
function PrintReceipt(){
let name = document.getElementById('elementname');
let nam = document.getElementById('sum-name');
nam.innerHTML = ' ' + name.value;
for (i = 1; i < 7; i++){
let eachElement = 'present' + i;
let elementid = document.getElementById(eachElement);
let elementVal = parseInt(elementid.innerHTML)
if (elementVal >= 1)
{
let nem = document.createElement("tr");
nem.innerHTML = `<td class="sum-tab-head">${products[i-1].name}</td><td class="sum-tab-head">${elementVal}</td>`
let parentElem = document.getElementById("sum-quantity");
parentElem.appendChild(nem);
}
}
}
//the submit and go the checkout
function Checkout(){
let name = document.getElementById('elementname');
let email = document.getElementById('email');
let number = document.getElementById('pnumber');
// let nam = document.getElementById('sum-name');
// nam.innerHTML = name.innerHTML;
let newnum = parseInt(document.getElementById('spa').innerHTML);
if (newnum <= 0){
NameVerification();
EmailVerification();
PNumberVerification();
alert("Ohh...You haven't select anything.Please SELECT what you want to buy from the Cart Menu!");
}
else{
if (!(name.value) || !(email.value) || !(number.value)){
NameVerification();
EmailVerification();
PNumberVerification();
}
else {
payWithPaystack();
}
}
}
//Paystack Module for payment //NB: this is just for testing.
const paymentForm = document.getElementById('paymentForm');
paymentForm.addEventListener("submit", payWithPaystack, false);
function payWithPaystack() {
let handler = PaystackPop.setup({
key: 'pk_test_d7550c1118c9fb6ecdd01e5ea47a7e1621ab54bc', // Replace with your public key
email: document.getElementById("email").value,
amount: document.getElementById('tamounnt').value * 100,
ref: ''+Math.floor((Math.random() * 1000000000) + 1), // generates a pseudo-unique reference. Please replace with a reference you generated. Or remove the line entirely so our API will generate one for you
// label: "Optional string that replaces customer email"
onClose: function(){
alert('Window closed.');
},
callback: function(response){
//response.reference
RemovePopPay();
PrintReceipt();
DisplaySummary(response.reference);
}
});
handler.openIframe();
}