-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathA_payment.js
186 lines (122 loc) · 4.74 KB
/
A_payment.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
var totalPrice;
var totalDisc;
var finalTotal;
function priceLoad() {
console.log("in priceLoad")
let payBox = document.getElementById("payment-detail")
totalPrice = localStorage.getItem("total_Price")
totalDisc = localStorage.getItem("total_Discount")
// let roundedValue = (totalPrice - totalDisc + 1) - (totalPrice - totalDisc)
finalTotal = (totalPrice - totalDisc)>=399 ? totalPrice - totalDisc : totalPrice - totalDisc+39;
payBox.innerHTML = "";
let format = `<div id="bill-detail">Bill Datails</div>
<div id="pay-subtotal" class="final-pay-div">
<span class="bill-left-span">Subtotal</span>
<span class="bill-right-span">${totalPrice}</span>
</div>
<div id="pay-del-charge" class="final-pay-div">
<span class="bill-left-span">Delivery Charge</span>
<span class="bill-right-span">${(totalPrice - totalDisc)>399 ? '0' : '39'}</span>
</div>
<div id="pay-discount" class="final-pay-div">
<span class="bill-left-span">Discount</span>
<span class="bill-right-span">${totalDisc}</span>
</div>
<div id="pay-walllet" class="final-pay-div">
<span class="bill-left-span">Licious Wallet</span>
<span class="bill-right-span">0</span>
</div>
<div id="pay-rounded" class="final-pay-div">
<span class="bill-left-span">Rounded Value</span>
<span class="bill-right-span">0</span>
</div>
<hr>
<div id="pay-total" class="final-pay-div">
<span class="bill-left-span" style="font-size:20px ;font-weight: 700" >Total</span>
<span style="color:#D11243;font-size:20px ;font-weight: 700" class="bill-right-span">₹ ${finalTotal}</span>
</div>`
payBox.innerHTML = format;
}
priceLoad();
let payBtn = document.getElementById("pay-button");
payBtn.addEventListener("click", function () {
let cardDetail = JSON.parse(localStorage.getItem("card_Details"));
let _Name = document.getElementById("cardName").value;
let cvvNo = document.getElementById("cardCvv").value;
let cardMo = document.getElementById("careMonth").value;
let cardYear = document.getElementById("cardYear").value;
let cardNo = document.getElementById("user-card-no").value;
let user_info = JSON.parse(localStorage.getItem("current_user"));
let new_order = {};
new_order.user_id = user_info.id;
new_order.total_order = user_info.cart;
new_order.total_Amount = finalTotal;
;(async ()=>{
await fetch("https://63982e64044fa481d693d25f.mockapi.io/total_orders",{
method : "POST",
headers : {
"Content-Type" : "application/json",
},
body : JSON.stringify(new_order)
})
})();
if (cardDetail.name == _Name && cardDetail.card_no == cardNo && cardDetail.cvv_no == cvvNo && cardDetail.month == cardMo && cardDetail.year == cardYear){
deleteApiCart();
} else {
alert("Wrong Card Information")
}
})
function paybtnLoad() {
payBtn.innerHTML = ` Pay ₹${finalTotal}`
}
paybtnLoad()
// adding card detail to local storage
let cardDetails = {
name: "Team Licious",
card_no: 1234567812345678,
month: 12,
year: 2023,
cvv_no: 1234,
}
function addCardDetails() {
console.log("in addcardDetails")
localStorage.setItem("card_Details", JSON.stringify(cardDetails));
}
addCardDetails();
// deleting cart items from api
async function deleteApiCart() {
let currentUUser = JSON.parse(localStorage.getItem("current_user"));
currentUUser.cart = [];
let res = await fetch(`https://63982e64044fa481d693d25f.mockapi.io/users/${currentUUser.id}`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(currentUUser)
})
localStorage.setItem("current_user", JSON.stringify(currentUUser));
window.location.href = "congratulations.html";
}
// deleting cart items from local storage
// function deleteLSCart() {
// let currentUUser = JSON.parse(localStorage.getItem("current_user"));
// currentUUser.cart = [];
// }
// limiting no of input fields
// let _Name = document.getElementById("cardName");
// let cvvNo = document.getElementById("cardCvv");
// let cardMo = document.getElementById("careMonth");
// let cardYear = document.getElementById("cardYear");
// let cardNo = document.getElementById("user-card-no");
// cvvNo.oninput = () => {
// if(cvvNo.value.length > cvvNo.maxlength) {
// cvvNo.value = cvvNo.value.slice(0, cvvNo.maxlength)
// }
// }
// /