let OVERSEAS_SHIPPING = false, COUNTRY = [], PROVINCE = [], CITY = [], SUBDISTRICT = [], CART_ITEMS = [], COURIER = [], ORIGIN = 455, TOTAL_WEIGHT = 0, TOTAL_AMOUNT = 0 $(document).ready(function(){ stepper = new Stepper($('.bs-stepper')[0]) getCartDetail() getCountry() getProvince() $('#is_overseas_hipping').change(function() { if(this.checked) { OVERSEAS_SHIPPING = true $('#country-option').css("display", "block") $('#province-option').css("display", "none") $('#city-option').css("display", "none") $('#subdistrict-option').css("display", "none") $('#city').prop("disabled", true); $('#subdistrict').prop("disabled", true); renderCountry() }else{ OVERSEAS_SHIPPING = false $('#country-option').css("display", "none") $('#province-option').css("display", "block") $('#city-option').css("display", "block") $('#subdistrict-option').css("display", "block") renderProvince() } }) //watch province option $('#province').change(function() { $('#city').prop("disabled", true); $('#subdistrict').prop("disabled", true); $('#city').prop('selectedIndex',0); $('#subdistrict').prop('selectedIndex',0); const selectedProvince = $('#province').find(":selected").val() console.log(selectedProvince) if (parseInt(selectedProvince) > 0) { getCity(selectedProvince) } else { $('#city').html(``); $('#city').prop("disabled", true); } }) //watch city option $('#city').change(function() { $('#subdistrict').prop("disabled", true); $('#subdistrict').prop('selectedIndex',0); const selectedCity = $('#city').find(":selected").val() console.log(selectedCity) if (parseInt(selectedCity) > 0) { getSubdistrict(selectedCity) } else { $('#subdistrict').html(``); $('#subdistrict').prop("disabled", true); } }) //watch county option $('#subdistrict').change(function() { const selectedSubdistrict = $('#subdistrict').find(":selected").val() console.log(selectedSubdistrict) }) $('#address-button').click(function(){ submitAddressForm() }) $('#ship-button').click(function(){ submitShippingForm() }) $('.address-update-button').click(function(){ stepper.to(1) $('#address-button').css("display", "block") $('#ship-button').css("display", "none") $('#pay-button').css("display", "none") }) $('.shipping-update-button').click(function(){ stepper.to(2) $('#address-button').css("display", "none") $('#ship-button').css("display", "block") $('#pay-button').css("display", "none") }) $('#pay-button').click(function(){ checkoutCart() }) }) function submitAddressForm(){ let $form = $('#address-form'), receiverName = $form.find( "input[name='receiver_name']" ).val(), fullAddress = $form.find( "textarea[name='full_address']" ).val(), postalCode = $form.find( "input[name='postal_code']" ).val(), phone = $form.find( "input[name='phone']" ).val(), country = $('#country-option').find(":selected").val(), province = $('#province-option').find(":selected").val(), city = $('#city-option').find(":selected").val(), subdistrict = $('#subdistrict-option').find(":selected").val() let fields = [receiverName, fullAddress, postalCode, phone] if(OVERSEAS_SHIPPING) fields.push(country) else fields.push(province, city, subdistrict) if(checkForm(fields)){ stepper.next() renderAddressBox() getCourier() $('#ship-button').css("display", "block") $('#address-button').css("display", "none") }else{ showError("Alamat", "Form Alamat Harus diisi") } } function submitShippingForm(){ let $form = $('#shipping-form'), shippingService = $form.find('input[name="shipping_method"]:checked').val(); console.log("SHIPPING SERVICE: ", shippingService) let fields = [shippingService] if(checkForm(fields)){ stepper.next() renderShippingBox() $('#ship-button').css("display", "none") $('#pay-button').css("display", "block") }else{ showError("Metode Pengiriman", "Anda harus memilih metode pengiriman") } } function checkoutCart(){ startLoadingButton('#pay-button') let $addressForm = $('#address-form'), $shippingForm = $('#shipping-form'), selectedCourier = $shippingForm.find('input[name="shipping_method"]:checked').val(); arr = selectedCourier.split("|"), service = arr[0], serviceType = arr[1], etd = arr[2], cost = arr[3], grandTotal = parseInt(cost) + parseInt(TOTAL_AMOUNT), request = { receiver_name: $addressForm.find("input[name='receiver_name']").val(), service: service, service_type: serviceType, service_price: cost, full_address: $addressForm.find("textarea[name='full_address']").val(), postal_code: $addressForm.find("input[name='postal_code']").val(), phone: $addressForm.find("input[name='phone']").val() } if(OVERSEAS_SHIPPING){ request.country = $("#country").find(":selected").text() request.country_id = $("#country").find(":selected").val() }else{ request.province = $("#province").find(":selected").text() request.province_id = $("#province").find(":selected").val() request.city = $("#city").find(":selected").text() request.city_id = $("#city").find(":selected").val() request.subdistrict = $("#subdistrict").find(":selected").text() request.subdistrict_id = $("#subdistrict").find(":selected").val() } $.ajax({ async: true, url: `${CART_API_URL}checkout`, type: 'POST', data: JSON.stringify(request), beforeSend: function (xhr) { xhr.setRequestHeader('Authorization', `Bearer ${SESSION}`); }, error: function(res) { const response = JSON.parse(res.responseText) let isRetry = retryRequest(response, window.location.href) if(isRetry) $.ajax(this) endLoadingButton('#pay-button', 'Bayar') }, success: function(res) { let data = res.data endLoadingButton('#pay-button', 'Bayar') openMidtrans(data) } }); } function checkForm(fields){ let isValid = true for(let field of fields){ if(!field){ isValid = false break } } return isValid } function getCartDetail(){ $.ajax({ async: true, url: `${CART_API_URL}detail`, type: 'GET', beforeSend: function (xhr) { xhr.setRequestHeader('Authorization', `Bearer ${SESSION}`); }, error: function(res) { const response = JSON.parse(res.responseText) let isRetry = retryRequest(response, window.location.href) if(isRetry) $.ajax(this) }, success: function(res) { CART = res.data if(CART){ CART_ITEMS = CART.items if(CART_ITEMS.length > 0){ TOTAL_AMOUNT = CART.total_amount renderCartItems() getTotalWeight() }else{ window.location.href = `${WEB_URL}carts` } }else{ window.location.href = `${WEB_URL}carts` } } }); } function getCountry(){ $.ajax({ async: true, url: `${VENDOR_API_URL}international-destination`, type: 'GET', error: function(res) { const response = JSON.parse(res.responseText) let isRetry = retryRequest(response, window.location.href) if(isRetry) $.ajax(this) }, success: function(res) { COUNTRY = res.data.rajaongkir.results } }); } function getProvince(){ $.ajax({ async: true, url: `${VENDOR_API_URL}province`, type: 'GET', error: function(res) { const response = JSON.parse(res.responseText) let isRetry = retryRequest(response, window.location.href) if(isRetry) $.ajax(this) }, success: function(res) { PROVINCE = res.data.rajaongkir.results renderProvince() } }); } function getCity(province) { $.ajax({ async: true, url: `${VENDOR_API_URL}city?province_id=${province}`, type: 'GET', error: function(res) { const response = JSON.parse(res.responseText) let isRetry = retryRequest(response, window.location.href) if(isRetry) $.ajax(this) }, success: function(res) { CITY = res.data.rajaongkir.results renderCity() } }) } function getSubdistrict(city) { $.ajax({ async: true, url: `${VENDOR_API_URL}subdistrict?city_id=${city}`, type: 'GET', error: function(res) { const response = JSON.parse(res.responseText) let isRetry = retryRequest(response, window.location.href) if(isRetry) $.ajax(this) }, success: function(res) { SUBDISTRICT = res.data.rajaongkir.results renderSubdistrict() } }) } function getCourier() { let origin = ORIGIN, destination = OVERSEAS_SHIPPING ? $('#country').find(":selected").val() : $('#subdistrict').find(":selected").val(), weight = TOTAL_WEIGHT if(OVERSEAS_SHIPPING){ $.ajax({ async: true, url: `${VENDOR_API_URL}international-shipping-cost?origin=${origin}&destination=${destination}&weight=${weight}&courier=pos`, type: 'GET', error: function(res) { const response = JSON.parse(res.responseText) let isRetry = retryRequest(response, window.location.href) if(isRetry) $.ajax(this) }, success: function(res) { COURIER = res.data.rajaongkir.results renderCourier() } }) }else{ $.ajax({ async: true, url: `${VENDOR_API_URL}shipping-cost?origin=${origin}&origin_type=subdistrict&destination=${destination}&destination_type=subdistrict&weight=${weight}`, type: 'GET', error: function(res) { const response = JSON.parse(res.responseText) let isRetry = retryRequest(response, window.location.href) if(isRetry) $.ajax(this) }, success: function(res) { COURIER = res.data.rajaongkir.results renderCourier() } }) } } function renderCartItems(){ console.log("RENDER ITEMS") let itemsHtml = `
${price}
Jumlah: ${item.quantity}