135 lines
5.3 KiB
JavaScript
135 lines
5.3 KiB
JavaScript
|
|
async function submitOrderToServer(){
|
|
const totalHarga = hitungTotalHarga();
|
|
|
|
if (sessionStorage.getItem('order_id')) {
|
|
currentStep = 2;
|
|
showStep(currentStep);
|
|
return;
|
|
}
|
|
const biodataResult = JSON.parse(sessionStorage.getItem('checkout_biodata') || '{}');
|
|
const cartResult = JSON.parse(sessionStorage.getItem('cart') || '[]');
|
|
try {
|
|
const response = await fetch('/submit-checkout', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
|
|
},
|
|
body: JSON.stringify({ cartResult, biodataResult, totalHarga }),
|
|
})
|
|
|
|
const result = await response.json();
|
|
if (response.ok && result.status) {
|
|
// Hapus sessionStorage
|
|
|
|
sessionStorage.setItem('order_id', result.data.no_order);
|
|
sessionStorage.setItem('time_order', result.entry_at);
|
|
currentStep = 2;
|
|
showStep(currentStep)
|
|
document.getElementById('no_order_display').textContent = result.data.no_order
|
|
document.getElementById('no_order_price').textContent = result.data.total_harga ? 'Rp ' + parseInt(result.data.total_harga).toLocaleString('id-ID') : ''
|
|
$("#no_order_result").val(result.data.no_order)
|
|
}
|
|
} catch (error) {
|
|
console.error('message '+error);
|
|
alert('Terdapat kesalahan coba lagi nanti!')
|
|
}
|
|
}
|
|
|
|
function previewBuktiPembayaran() {
|
|
const input = document.getElementById('bukti_pembayaran');
|
|
const preview = document.getElementById('bukti_preview');
|
|
const previewText = document.getElementById('preview_text');
|
|
|
|
if (input.files && input.files[0]) {
|
|
const file = input.files[0];
|
|
const reader = new FileReader();
|
|
|
|
reader.onload = function (e) {
|
|
preview.src = e.target.result;
|
|
preview.classList.remove('d-none');
|
|
previewText.classList.add('d-none');
|
|
}
|
|
|
|
if (file.size <= 2 * 1024 * 1024) { // 2MB limit
|
|
reader.readAsDataURL(file);
|
|
} else {
|
|
alert("Ukuran file maksimal 2MB.");
|
|
input.value = '';
|
|
preview.classList.add('d-none');
|
|
previewText.classList.remove('d-none');
|
|
}
|
|
}
|
|
}
|
|
|
|
$("#checkoutFormFinal").on('submit', async function(e){
|
|
e.preventDefault()
|
|
const cara_pembayaran = document.getElementById('cara_pembayaran').value;
|
|
const bukti_pembayaran = document.querySelector('input[name="bukti_pembayaran"]').value;
|
|
|
|
if(cara_pembayaran === 'transfer' && !bukti_pembayaran){
|
|
Swal.fire({
|
|
title: 'Bukti pembayaran kosong!',
|
|
text: 'Silahkan upload bukti pembayaran jika metode yang dipakai transfer.',
|
|
icon: 'warning',
|
|
})
|
|
return;
|
|
}
|
|
|
|
// }
|
|
const form = this;
|
|
|
|
const formData = new FormData(form);
|
|
|
|
try {
|
|
const response = await fetch('/finish-checkout', {
|
|
method: 'POST',
|
|
body: formData,
|
|
headers: {
|
|
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content')
|
|
}
|
|
});
|
|
|
|
const result = await response.json();
|
|
|
|
if (result.status) {
|
|
// ✅ Hapus sessionStorage di client
|
|
sessionStorage.removeItem('cart');
|
|
sessionStorage.removeItem('checkout_biodata');
|
|
sessionStorage.removeItem('order_id');
|
|
sessionStorage.removeItem('time_order');
|
|
|
|
Swal.fire({
|
|
title: 'Pesanan Berhasil!',
|
|
text: 'Terima kasih, pesanan Anda sedang kami siapkan.',
|
|
icon: 'success',
|
|
confirmButtonText: 'Berhasil!',
|
|
confirmButtonColor: '#28a745'
|
|
}).then(() => {
|
|
window.location.href = "/success-page"; // kehalaman success
|
|
});
|
|
} else {
|
|
sessionStorage.removeItem('cart');
|
|
sessionStorage.removeItem('checkout_biodata');
|
|
sessionStorage.removeItem('order_id');
|
|
sessionStorage.removeItem('time_order');
|
|
Swal.fire({
|
|
title: 'Gagal!',
|
|
text: result.message,
|
|
icon: 'error',
|
|
confirmButtonText: 'Tutup!'
|
|
}).then(() => {
|
|
window.location.href = "/";
|
|
});
|
|
}
|
|
} catch (err) {
|
|
sessionStorage.removeItem('cart');
|
|
sessionStorage.removeItem('checkout_biodata');
|
|
sessionStorage.removeItem('order_id');
|
|
sessionStorage.removeItem('time_order');
|
|
console.error(err);
|
|
alert("Terjadi kesalahan saat mengirim data.");
|
|
}
|
|
})
|