-
Notifications
You must be signed in to change notification settings - Fork 909
Expand file tree
/
Copy pathpaymentSummary.js
More file actions
95 lines (78 loc) · 2.58 KB
/
paymentSummary.js
File metadata and controls
95 lines (78 loc) · 2.58 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
import {cart} from '../../data/cart.js';
import {getProduct} from '../../data/products.js';
import {getDeliveryOption} from '../../data/deliveryOptions.js';
import {formatCurrency} from '../utils/money.js';
import {addOrder} from '../../data/orders.js';
export function renderPaymentSummary() {
let productPriceCents = 0;
let shippingPriceCents = 0;
cart.forEach((cartItem) => {
const product = getProduct(cartItem.productId);
productPriceCents += product.priceCents * cartItem.quantity;
const deliveryOption = getDeliveryOption(cartItem.deliveryOptionId);
shippingPriceCents += deliveryOption.priceCents;
});
const totalBeforeTaxCents = productPriceCents + shippingPriceCents;
const taxCents = totalBeforeTaxCents * 0.1;
const totalCents = totalBeforeTaxCents + taxCents;
const paymentSummaryHTML = `
<div class="payment-summary-title">
Order Summary
</div>
<div class="payment-summary-row">
<div>Items (3):</div>
<div class="payment-summary-money">
$${formatCurrency(productPriceCents)}
</div>
</div>
<div class="payment-summary-row">
<div>Shipping & handling:</div>
<div class="payment-summary-money">
$${formatCurrency(shippingPriceCents)}
</div>
</div>
<div class="payment-summary-row subtotal-row">
<div>Total before tax:</div>
<div class="payment-summary-money">
$${formatCurrency(totalBeforeTaxCents)}
</div>
</div>
<div class="payment-summary-row">
<div>Estimated tax (10%):</div>
<div class="payment-summary-money">
$${formatCurrency(taxCents)}
</div>
</div>
<div class="payment-summary-row total-row">
<div>Order total:</div>
<div class="payment-summary-money">
$${formatCurrency(totalCents)}
</div>
</div>
<button class="place-order-button button-primary
js-place-order">
Place your order
</button>
`;
document.querySelector('.js-payment-summary')
.innerHTML = paymentSummaryHTML;
document.querySelector('.js-place-order')
.addEventListener('click', async () => {
try {
const response = await fetch('https://supersimplebackend.dev/orders', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
cart: cart
})
});
const order = await response.json();
addOrder(order);
window.location.href = 'orders.html';
} catch (error) {
console.log('Unexpected error. Try again later.');
}
});
}