var filestatus = [false,false, false]; var documentDetails; var tickImg = '/html/src/img/tick.svg'; var ConfirmView = '/ConfirmView.html'; var LoadingView = '/loading.html'; var IndexView = '/index.html' + window.location.search; $(document).ready(function(){ renderError(); confirmPhotosUploaded(); APIStuff(); }) function renderError() { if(typeof documentDetails !== "undefined") { if(documentDetails.error == true) { var errorMesssage = documentDetails.message $('.sub-text').text(errorMesssage + '. Please re-upload and try again.'); $('.sub-text').css("color", "yellow"); } } } function confirmPhotosUploaded (){ $("#licence_photo").change(function(){ var imgString = $('input[type=file]')[0].files[0].name; if(imgString != ''){ filestatus[0] = true; $('.btn-1').find('.btn-inner').find('img').attr('src', tickImg); checkFileStatus(); } else{ filestatus[0] = false; } }) $("#passport_photo").change(function(){ var imgString = $('input[type=file]')[1].files[0].name; if(imgString != ''){ filestatus[1] = true; $('.btn-2').find('.btn-inner').find('img').attr('src', tickImg); checkFileStatus(); } else{ filestatus[1] = false; } }) $("#person_photo").change(function(){ var imgString = $('input[type=file]')[2].files[0].name; if(imgString != ''){ filestatus[2] = true; $('.btn-3').find('.btn-inner').find('img').attr('src', tickImg); checkFileStatus(); } else { filestatus[2] = false; } }) } function checkFileStatus(){ if(filestatus[0] & filestatus[1] & filestatus[2] === true){ $('.scan-btn').css('background','#a9ff3c'); $('.styled-btn').prop('disabled', false); }else{ $('.scan-btn').css('background','#dddddd'); } } function buttonClick(){ $('.scan-btn').click(function(){ if(filestatus[0] & filestatus[1] && filestatus[2] === true){ APIStuff(); } setInterval(loadConfirm, 5000); }) } function checkResults(response){ if(response.error == true) { $('body').load(IndexView); } else { $('body').load(ConfirmView); } } function uploadFileToPresignedUrl(endpoint, file, callbacks) { try { var xhr = new XMLHttpRequest(); xhr.upload.addEventListener('progress', callbacks.onProgress); xhr.upload.addEventListener('error', callbacks.onFailure); xhr.upload.addEventListener('abort', callbacks.onFailure); xhr.onreadystatechange = function(e) { if (xhr.status === 200) { callbacks.onSuccess(); } }; xhr.open('PUT', endpoint, true); xhr.send(file); } catch (e) { callbacks.onFailure(e); alert(JSON.stringify(e)); } } function guid() { function s4() { return Math.floor((1 + Math.random()) * 0x10000) .toString(16) .substring(1); } return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4(); } /** * * @param {String} endpoint * @param {File} file */ function getPresignedAndUpload(endpoint, file) { var presignedUrl = {}; $.ajax({ type:'POST', url: endpoint, processData: false, headers: {"Access-Control-Allow-Origin": "*"}, contentType: false, crossDomain: true, dataType: "json", async: false, cache: false, data: JSON.stringify({ "image_name": file.name, "type": file.type }), success: function(response){ presignedUrl = response; }, error : function (e){ console.log('ERROR : ', e); } }); uploadFileToPresignedUrl(presignedUrl.IDCheck, file, { onSuccess: function() { }, onFailure: function(e) { }, onProgress: function(e) { } }); } function APIStuff(){ $(document).on('submit', '#theform', function(e){ e.preventDefault(); $('body').load(LoadingView); var bcid = "" if (window.location.search != "") { bcid = window.location.search.split("=")[1].split("&")[0]; } var form = document.getElementById("theform"); var form_data = new FormData($('#theform')[0]); var passport_photoFile = $('#passport_photo').get(0).files[0]; var licence_photoFile = $('#licence_photo').get(0).files[0]; var person_photoFile = $('#person_photo').get(0).files[0]; if(bcid != null) { var passport_photoFile_bcid = "passport_" + bcid + '.jpeg' var licence_photoFile_bcid = "licence_" + bcid + '.jpeg' var person_photoFile_bcid = "person_" + bcid + '.jpeg' form_data.append('passport_photoFile', passport_photoFile , passport_photoFile_bcid); form_data.append('licence_photoFile', licence_photoFile, licence_photoFile_bcid); form_data.append('person_photoFile', person_photoFile, person_photoFile_bcid); } else { var uuid = guid(); var passport_photoFile_uuid = "passport_" + uuid + '.jpeg' var licence_photoFile_uuid = "licence_" + uuid + '.jpeg' var person_photoFile_uuid = "person_" + uuid + '.jpeg' form_data.append('passport_photoFile', passport_photoFile , passport_photoFile_uuid); form_data.append('licence_photoFile', licence_photoFile, licence_photoFile_uuid); form_data.append('person_photoFile', person_photoFile, person_photoFile_uuid); } getPresignedAndUpload("api/presignedUrls", form_data.get("passport_photoFile")); getPresignedAndUpload("api/presignedUrls", form_data.get("licence_photoFile")); getPresignedAndUpload("api/presignedUrls", form_data.get("person_photoFile")); for (i = 0; i < form.elements.length; i++) { if (form.elements[i].type == 'file') { if (form.elements[i].value == '') { form.elements[i].parentNode.removeChild(form.elements[i]); } } } var json_object = { "bcid": bcid != undefined ? bcid : uuid, "person_photo": bcid != undefined ? person_photoFile_bcid : person_photoFile_uuid, "licence_photo": bcid != undefined ? licence_photoFile_bcid : licence_photoFile_uuid, "passport_photo": bcid != undefined ? passport_photoFile_bcid : passport_photoFile_uuid } $.ajax({ type:'POST', url:'api/ocr_voi', contentType: 'application/json; charset=utf-8', processData: false, contentType: false, crossDomain: true, dataType: "json", async: true, cache: false, data : JSON.stringify(json_object), success: function(response){ documentDetails = response; checkResults(documentDetails); }, error : function (e){ response = { "error": true, "message": "! Unable to conduct identity check" } documentDetails = response; checkResults(response); } // }); }); }); }