-
Notifications
You must be signed in to change notification settings - Fork 0
/
tripletiseShop.js
196 lines (165 loc) · 7.37 KB
/
tripletiseShop.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
(function() {
const sidebarHtml = `
<div id="sidebar" class="sidebar">
<div class="sidebar-content">
<!-- Titelbalk met "Winkelwagen" en chevron-sluitknop -->
<div class="sidebar-header">
<h2>Winkelwagen</h2>
<button id="close-sidebar" class="close-button">
<span>›</span>
</button>
</div>
<!-- Winkelwagenitems lijst -->
<div id="cart-items">
<!-- Winkelwagen inhoud wordt hier dynamisch toegevoegd -->
</div>
<!-- NAW-gegevens formulier (verborgen bij start) -->
<div id="checkout-form" style="display: none;">
<h3>Aflevergegevens</h3>
<label>Naam:</label>
<input type="text" id="name" required>
<label>Adres:</label>
<input type="text" id="address" required>
<label>Woonplaats:</label>
<input type="text" id="city" required>
<button id="place-order-button">Bestellen en betalen</button>
</div>
<!-- Actieknoppen -->
<div class="sidebar-actions">
<button id="continue-shopping">Verder winkelen</button>
<button id="checkout-button">Ik ga bestellen</button>
</div>
</div>
</div>
`;
// Voeg de HTML aan de body van de klantpagina toe
document.body.insertAdjacentHTML('beforeend', sidebarHtml);
let cartItems = [];
const deliveryFee = 50;
function showSidebar() {
const sidebar = document.getElementById('sidebar');
sidebar.style.display = 'block';
setTimeout(() => {
sidebar.style.right = '0px';
}, 10);
}
function closeSidebar() {
const sidebar = document.getElementById('sidebar');
sidebar.style.right = '-800px';
setTimeout(() => {
sidebar.style.display = 'none';
}, 300);
}
document.getElementById('close-sidebar').addEventListener('click', closeSidebar);
document.getElementById('continue-shopping').addEventListener('click', closeSidebar);
// Toon het checkout-formulier
document.getElementById('checkout-button').addEventListener('click', () => {
document.getElementById('cart-items').style.display = 'none';
document.getElementById('checkout-form').style.display = 'block';
document.getElementById('checkout-button').style.display = 'none';
});
// NAW-gegevens invullen en doorgaan naar betaling met Mollie
document.getElementById('place-order-button').addEventListener('click', () => {
const name = document.getElementById('name').value;
const address = document.getElementById('address').value;
const city = document.getElementById('city').value;
if (!name || !address || !city) {
alert("Vul alstublieft alle velden in.");
return;
}
const totalPrice = cartItems.reduce((total, item) => total + item.price, 0) + deliveryFee;
console.log("Betaling starten met Mollie voor totaalbedrag:", totalPrice);
connectMollie(totalPrice, "Winkelwagen Aankoop", { name, address, city });
});
function updateCart() {
const cartItemsContainer = document.getElementById('cart-items');
cartItemsContainer.innerHTML = '';
let cartTotal = 0; // To calculate total items price
cartItems.forEach((item, index) => {
const cartItemElement = document.createElement('div');
cartItemElement.classList.add('cart-item');
const imageUrl = item.imageUrl || 'path/to/default-image.jpg';
cartItemElement.innerHTML = `
<img src="${imageUrl}" alt="${item.name}" />
<span class="product-name">${item.name}</span>
<span class="product-price">€ ${item.price.toFixed(2).replace('.', ',')}</span>
<button class="delete-item-button" style="border: none; background: transparent;" data-index="${index}">
<svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 -960 960 960" width="24px" fill="#5f6368">
<path d="M280-120q-33 0-56.5-23.5T200-200v-520h-40v-80h200v-40h240v40h200v80h-40v520q0 33-23.5 56.5T680-120H280Zm400-600H280v520h400v-520ZM360-280h80v-360h-80v360Zm160 0h80v-360h-80v360ZM280-720v520-520Z"/>
</svg>
</button>
`;
cartItemsContainer.appendChild(cartItemElement);
cartTotal += item.price; // Accumulate the total price of items
});
// Display delivery fee and total
const cartSummaryElement = document.createElement('div');
cartSummaryElement.classList.add('cart-summary');
const totalWithDelivery = cartTotal + deliveryFee;
cartSummaryElement.innerHTML = `
<div class="summary-item">
<span>Bezorgkosten:</span>
<span>€ ${deliveryFee.toFixed(2).replace('.', ',')}</span>
</div>
<div class="summary-item">
<strong>Totaal:</strong>
<strong>€${totalWithDelivery.toFixed(2).replace('.', ',')}</strong>
</div>
`;
cartItemsContainer.appendChild(cartSummaryElement);
// Add event listeners to the delete buttons
document.querySelectorAll('.delete-item-button').forEach(button => {
button.addEventListener('click', deleteCartItem);
});
}
function deleteCartItem(event) {
const itemIndex = event.target.getAttribute('data-index');
cartItems.splice(itemIndex, 1);
updateCart();
}
function connectMollie(amount, description) {
fetch("https://us-central1-vanwoerdenwonen-tripletise.cloudfunctions.net/mollieAuthRedirect", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ amount: amount, description: description })
})
.then(response => response.json())
.then(data => {
console.log("Received data:", data);
const paymentUrl = data.paymentUrl;
if (paymentUrl) {
window.location.href = paymentUrl;
} else {
console.error("No payment URL returned.");
}
})
.catch(error => {
console.error("Er ging iets mis:", error);
});
}
window.addEventListener('message', (event) => {
if (event.data.action === 'addToCart') {
const product = event.data.product;
cartItems.push(product);
updateCart();
}
if (event.data.action === 'showSidebar') {
showSidebar();
}
if (event.data.action === 'showCheckoutButton') {
showCheckoutButton();
}
});
document.getElementById('checkout-button').addEventListener('click', () => {
const totalPrice = cartItems.reduce((total, item) => total + item.price, 0);
console.log("Betaling starten met Mollie voor totaalbedrag:", totalPrice);
connectMollie(totalPrice, "Winkelwagen Aankoop");
});
document.getElementById('cart-button').addEventListener('click', (event) => {
event.preventDefault(); // Voorkom de standaard linkactie
showSidebar();
});
updateCart();
})();