| | |
| | | }, config); |
| | | |
| | | |
| | | |
| | | this.stepMultiplier = 1; |
| | | |
| | | this.cache = new window.jvbCache('cart', {TTL: 8.64e+7}); |
| | |
| | | |
| | | this.initElements(); |
| | | this.bindEvents(); |
| | | |
| | | this.popup = new window.jvbPopup({ |
| | | popup: this.checkout, |
| | | toggle: this.toggle, |
| | | name: 'Cart', |
| | | onOpen: this.maybeAddEmptyState.bind(this), |
| | | }); |
| | | this.init(); |
| | | |
| | | this.toggle.hidden = false; |
| | |
| | | } |
| | | } |
| | | handleClick(e) { |
| | | if (window.targetCheck(e, '.toggle-cart')) { |
| | | let toggle = window.targetCheck(e, '.toggle-cart'); |
| | | console.log('Toggle found. Toggling cart'); |
| | | this.toggleCart(); |
| | | } else if (window.targetCheck(e, 'button') && window.targetCheck(e, 'div.quantity')) { |
| | | if (window.targetCheck(e, 'button') && window.targetCheck(e, 'div.quantity')) { |
| | | let quantity = window.targetCheck(e, 'div.quantity'); |
| | | this.handleNumberClick(e, quantity); |
| | | }else if (window.targetCheck(e, '[data-add-to-cart]')) { |
| | |
| | | this.handleRemoveFromCart(remove); |
| | | } else if (window.targetCheck(e, '[data-clear-cart]')) { |
| | | this.clearCart(); |
| | | } else if (this.checkout.classList.contains('expanded') && |
| | | !this.checkout.contains(e.target) && |
| | | e.target !== this.toggle) { |
| | | this.closeCart(); |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | toggleCart() { |
| | | if (!this.checkout.classList.contains('expanded')) { |
| | | this.openCart(); |
| | | } else { |
| | | this.closeCart(); |
| | | } |
| | | } |
| | | openCart(message = 'Opened Cart') { |
| | | this.checkout.classList.add('expanded'); |
| | | this.toggle.title = 'Hide cart'; |
| | | this.toggle.ariaExpanded = true; |
| | | this.toggle.querySelector('span').textContent = 'Close Cart'; |
| | | this.a11y.announce(message); |
| | | this.maybeAddEmptyState(); |
| | | document.addEventListener('keydown', this.keyHandler); |
| | | } |
| | | maybeAddEmptyState() { |
| | | let empty = this.itemsList.querySelector('.empty'); |
| | | if(empty) { |
| | |
| | | this.checkoutPanel.title = 'Checkout'; |
| | | } |
| | | } |
| | | closeCart(message = 'Closed Cart') { |
| | | this.checkout.classList.remove('expanded'); |
| | | this.toggle.title = 'Show Cart'; |
| | | this.toggle.ariaExpanded = false; |
| | | |
| | | this.toggle.querySelector('span').textContent = ''; |
| | | this.a11y.announce(message); |
| | | document.removeEventListener('keydown', this.keyHandler); |
| | | } |
| | | handleEscape(e) { |
| | | if (e.key === 'Escape') { |
| | | this.closeCart('Closed Cart with escape key'); |
| | | this.stepMultiplier = 1; |
| | | } else if (e.ctrlKey && e.shiftKey) { |
| | | this.stepMultiplier = Math.max(parseInt(this.stepMultiplier) * 100, 1000); |
| | |
| | | if (squareConfig.isOpen !== '1') { |
| | | return; |
| | | } |
| | | const response = await fetch(this.config.api_url + 'checkout', { |
| | | |
| | | // Square Web Payments SDK handles EVERYTHING |
| | | // We just need to track the order for status updates |
| | | const response = await fetch(this.config.api_url + 'save-order', { |
| | | method: 'POST', |
| | | headers: { |
| | | 'Content-Type': 'application/json', |
| | | 'X-WP-Nonce': this.config.nonce |
| | | }, |
| | | body: JSON.stringify({ |
| | | source_id: token, |
| | | amount: orderData.total, |
| | | items: orderData.items, |
| | | order_id: token.orderId, // From Square SDK response |
| | | payment_id: token.paymentId, // From Square SDK response |
| | | customer: orderData.customer, |
| | | note: orderData.note, |
| | | currency: this.config.currency, |
| | | items: orderData.items, |
| | | action: 'jvb_integration_action', |
| | | service: 'square', |
| | | integration_action: 'process_order' |
| | | integration_action: 'save_order' |
| | | }) |
| | | }); |
| | | |
| | | const result = await response.json(); |
| | | |
| | | if (!response.ok) { |
| | | throw new Error(result.message || 'Payment failed'); |
| | | throw new Error(result.message || 'Failed to save order'); |
| | | } |
| | | |
| | | return result; |