import { payments, devTools } from '../../../build/module'; import sandwiches from './sandwiches.json'; (window as any).devTools = devTools; const itemsDiv = document.querySelector('#items')!; const subTotalDiv = document.querySelector('#subtotal')!; const taxesDiv = document.querySelector('#taxes')!; const totalDiv = document.querySelector('#total')!; const paymentForm = document.querySelector('#payment')!; const overlayDiv = document.querySelector('.overlay')!; const logDiv = document.querySelector('.log')!; const payButton = document.querySelector('#pay')!; const selectedIndexes: number[] = []; const dollar = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', }); // Round to two decimal places. const round = (value: number) => { return Math.round(value * 100) / 100; }; // Calculate the sub total of selected items. const getSubTotal = () => { return round( selectedIndexes.reduce((acc, index) => { return acc + sandwiches[index].price; }, 0) ); }; // Set the tax rate to 10%. const tax = 0.1; const getItemTax = (item: typeof sandwiches[0]) => { let priceWithTax = 0; if (item.taxable !== false) { priceWithTax = item.price * tax; } return priceWithTax; }; const getTaxes = () => { return round( selectedIndexes.reduce((acc, index) => { const item = sandwiches[index]; return acc + getItemTax(item); }, 0) ); }; // Calculate the total including taxes. const getTotals = () => { return { subTotal: getSubTotal(), tax: getTaxes(), tip: 0, total: getSubTotal() + getTaxes(), }; }; // Render inital items. itemsDiv.innerHTML = ''; for (const [index, sandwich] of sandwiches.entries()) { itemsDiv.innerHTML += ` `; } // Render functions. const updateSelectedItems = () => { const items = itemsDiv.querySelectorAll('.item'); items.forEach((item, index) => { const isSelected = selectedIndexes.includes(index); item.setAttribute('data-selected', isSelected ? 'true' : 'false'); }); // Update the totals. const totals = getTotals(); subTotalDiv.textContent = dollar.format(totals.subTotal); taxesDiv.textContent = dollar.format(totals.tax); totalDiv.textContent = dollar.format(totals.total); }; const openPaymentModal = (isOpen: boolean) => { overlayDiv.style.display = isOpen ? 'block' : 'none'; if (!isOpen) { // clear selections. selectedIndexes.splice(0, selectedIndexes.length); updateSelectedItems(); } }; const logLine = (line: string) => { logDiv.setAttribute('style', 'display: block'); logDiv.textContent += `${line}\n`; logDiv.scrollTo({ top: logDiv.scrollHeight, behavior: 'smooth' }); }; // Event listeners. itemsDiv.addEventListener('click', (e) => { const target = e.target as HTMLElement; const item = target.closest('.item'); if (!item) return; const index = Number(item.getAttribute('data-index')); const isSelected = selectedIndexes.includes(index); if (index !== undefined) { if (isSelected) { selectedIndexes.splice(selectedIndexes.indexOf(index), 1); } else { selectedIndexes.push(index); } } updateSelectedItems(); }); overlayDiv.addEventListener('click', () => { openPaymentModal(false); }); // To simulate a charge response: Grab the requestId from the payments-charge payload shown // in the developer console when pressing "Charge". Then run: // devTools.sendMessage('payments-charge-response', { requestId: '', charge: { id: '123', status: 'success' } }) // devTools.sendMessage('payments-charge-response', { requestId: '', charge: { id: '123', status: 'failed' } }) // devTools.sendMessage('payments-charge-response', { requestId: '', charge: { id: '123', status: 'canceled' } }) // devTools.sendMessage('payments-charge-response', { requestId: '', charge: { id: '123', status: 'timeout' } }) // devTools.sendMessage('payments-charge-response', { requestId: '', charge: { id: '123', status: 'unknown' } }) paymentForm.addEventListener('submit', async (e) => { e.preventDefault(); try { openPaymentModal(true); const totals = getTotals(); const charge = await payments.charge({ // The order this charge should be associated with. // orderId: 'd0bcc3a3-5ac7-41a8-a408-7365a25a3bb0', // Staging order id // orderId: '891814fb-d6ba-4fed-9bb4-58d01ca08743', // Production order id. // totals are in cents, convert to an integer. subTotal: Math.floor(totals.subTotal * 100), tip: Math.floor(totals.tip * 100), tax: Math.floor(totals.tax * 100), total: Math.floor(totals.total * 100), // Items are separate from the order selections and do not support nested selections. These // are the items passed along with the charge, which will be shown in the payment processor // as the items the user is paying for. Charge items use a simpler structure than order selections // and do not support modifiers. items: selectedIndexes.map((index) => { const item = sandwiches[index]; const taxes = getItemTax(item); const total = item.price + taxes; return { name: item.name, // Note: Descriptions are limited to 40 characters by FreedomPay and will be cut off. description: item.description, sku: item.sku, quantity: 1, // totals are in cents, convert to an integer. price: Math.floor(item.price * 100), tax: Math.floor(taxes * 100), total: Math.floor(total * 100), }; }), }); logLine(`Charge response: ${JSON.stringify(charge)}`); if (charge.status !== 'pending') { openPaymentModal(false); } } catch (err: any) { logLine(`Charge error: ${err.message}`); openPaymentModal(false); } e.preventDefault(); }); // To simulate a terminal status update run: // devTools.sendMessage('payments-terminal-event', { terminal: { status: 'open' } }) // devTools.sendMessage('payments-terminal-event', { terminal: { status: 'closed' } }) // devTools.sendMessage('payments-terminal-event', { terminal: null }) payments.subscribe('terminal', (terminal) => { logLine(`Terminal event: ${JSON.stringify(terminal)}`); // Enable the pay button if the terminal is open. Here is where you might also show // a message to the user that the kiosk is not available. payButton.disabled = terminal?.status !== 'open'; });