order.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. $(document).ready(function() {
  2. const package_id = window.localStorage.getItem('budss_product_id');
  3. const qty = window.localStorage.getItem('budss_qty');
  4. const event_date = window.localStorage.getItem('budss_event_date');
  5. const event_id = window.localStorage.getItem('budss_event_time');
  6. const products = JSON.parse(window.localStorage.getItem('budss_products'));
  7. const paymentStatus = window.localStorage.getItem('paymentStarted');
  8. if (paymentStatus) {
  9. window.location.href = '/';
  10. }
  11. var events = null;
  12. var timer = 0;
  13. var remainingTime = 0;
  14. var sendObject = {};
  15. remainingTime = window.localStorage.getItem('remainingTime');
  16. timer = setInterval(function() {
  17. remainingTime--;
  18. window.localStorage.setItem('remainingTime', remainingTime);
  19. if (remainingTime < 60) {
  20. $('.cart_timer').css('color','#ff0000');
  21. }
  22. if (remainingTime === 0) {
  23. window.location.href='/#schedule';
  24. }
  25. const minutes = Math.floor(remainingTime / 60);
  26. const remainingSeconds = remainingTime % 60;
  27. $('.cart_timer').html(`${minutes}:${remainingSeconds.toString().padStart(2, '0')}`);
  28. },1000);
  29. $.getJSON('/api/getevents/?package_id=' + package_id + '&participants=' + qty, function(data) {
  30. events = data;
  31. const prod = events.find(function(item) {
  32. return item['id'] === parseInt(event_id) ? item : null;
  33. });
  34. var servicesName = '<div class="summary-title">Service Name</div>';
  35. servicesName += '<div class="summary-text">Dinner &amp; Cruise<br></div>';
  36. var servicesData = event_date + ' ' + prod['time'];
  37. var servicesQty = '<div class="summary-title">Amount</div>';
  38. servicesQty += '<div class="summary-text">'+qty+' seat(s)</div>';
  39. var price = 0;
  40. sendObject['cart'] = [];
  41. Object.keys(products).forEach(key => {
  42. let prodID = parseInt(key.replace('id_',''));
  43. let menu = prod['products'].find(function(item) {
  44. return item['id'] === prodID.toString() ? item : null;
  45. });
  46. sendObject['cart'].push({
  47. product_id: prodID,
  48. qty: products[key],
  49. discount: 0,
  50. event_id: parseInt(event_id)
  51. });
  52. price += parseInt(menu['price'])*parseInt(products[key]);
  53. servicesName += '<div class="summary-text">'+menu['label']+'<br></div>';
  54. servicesQty += '<div class="summary-text">'+products[key]+' meal(s)</div>';
  55. });
  56. $('.services-name').html(servicesName);
  57. $('.services-data').html(servicesData);
  58. $('.services-qty').html(servicesQty);
  59. $('.sum-price').html(formatCurrencyHUFWithDots(price));
  60. sendObject['total'] = price;
  61. sendObject['language'] = 'EN';
  62. sendObject['back_url'] = 'https://budapestsightseeing.hu/payment/';
  63. sendObject['uuid'] = window.localStorage.getItem('budss_uid');
  64. });
  65. $('#checkbox-13').on('click', function() {
  66. if ($('#checkbox-13').is(':checked')) {
  67. $('.company-fields').show();
  68. }
  69. else {
  70. $('.company-fields').hide();
  71. }
  72. });
  73. $('#payButton').on('click', function() {
  74. $('.w-form-fail').hide();
  75. if (isValidEmail($('#email').val()) && $('#email').val() === $('#email2').val()) {
  76. sendObject['customerEmail'] = $('#email').val();
  77. $('#email').removeClass('input-error');
  78. $('#email2').removeClass('input-error');
  79. }
  80. else {
  81. sendObject['customerEmail'] = '';
  82. $('#email').addClass('input-error');
  83. $('#email2').addClass('input-error');
  84. }
  85. if ($('#fname').val() === '') {
  86. $('#fname').addClass('input-error');
  87. }
  88. else {
  89. $('#fname').removeClass('input-error');
  90. }
  91. if ($('#lname').val() === '') {
  92. $('#lname').addClass('input-error');
  93. }
  94. else {
  95. $('#lname').removeClass('input-error');
  96. }
  97. if ($('#country').val() === '') {
  98. $('#country').addClass('input-error');
  99. }
  100. else {
  101. $('#country').removeClass('input-error');
  102. }
  103. if ($('#zip').val() === '') {
  104. $('#zip').addClass('input-error');
  105. }
  106. else {
  107. $('#zip').removeClass('input-error');
  108. }
  109. if ($('#city').val() === '') {
  110. $('#city').addClass('input-error');
  111. }
  112. else {
  113. $('#city').removeClass('input-error');
  114. }
  115. if ($('#street').val() === '') {
  116. $('#street').addClass('input-error');
  117. }
  118. else {
  119. $('#street').removeClass('input-error');
  120. }
  121. sendObject['phone'] = $('#phone').val();
  122. sendObject['invoice'] = {};
  123. sendObject['invoice'] = {
  124. name: $('#fname').val() + ' ' + $('#lname').val(),
  125. country: $('#country option:selected').attr('data-code'),
  126. country_id: $('#country').val(),
  127. zip: $('#zip').val(),
  128. city: $('#city').val(),
  129. address: $('#street').val() + ' ' + $('#housenumber').val(),
  130. vatnumber: $('#tax').val(),
  131. company: $('#company').val()
  132. };
  133. const checkedAccept = $('#Custom-Checkbox-ON-3').is(':checked');
  134. if (!checkedAccept) {
  135. $('#Custom-Checkbox-ON-3').parent('label').addClass('checkbox-error');
  136. }
  137. else {
  138. $('#Custom-Checkbox-ON-3').parent('label').removeClass('checkbox-error');
  139. }
  140. if (sendObject['customerEmail'] !== '' &&
  141. sendObject['phone'] !== '' &&
  142. sendObject['invoice']['name'] !== '' &&
  143. sendObject['invoice']['country'] !== '' &&
  144. sendObject['invoice']['zip'] !== '' &&
  145. sendObject['invoice']['city'] !== '' &&
  146. sendObject['invoice']['address'] !== '' && checkedAccept === true) {
  147. $('#email-form').hide();
  148. $('#paymentRedirect').show();
  149. $('html,body').scrollTop(0);
  150. $.post('/api/order/', { data: JSON.stringify(sendObject) }, function(resp) {
  151. const results = JSON.parse(resp);
  152. window.localStorage.setItem('paymentStarted', true);
  153. window.location.href=results.paymentUrl;
  154. });
  155. }
  156. else {
  157. $('.w-form-fail').show();
  158. }
  159. });
  160. function formatCurrencyHUFWithDots(amount) {
  161. if (typeof amount !== 'number') {
  162. throw new Error('A megadott értéknek számnak kell lennie.');
  163. }
  164. return amount.toString().replace(/\B(?=(\d{3})+(?!\d))/g, '.') + ' HUF';
  165. }
  166. function isValidEmail(email) {
  167. const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
  168. return emailRegex.test(email);
  169. }
  170. });