The below code is a sample application that connects to a PosBuddy terminal. The application sends an auth call followed by a sale.

Example implementation:

<script type='text/javascript' src='posbuddy_1.0.js'></script>

<script language="javascript" type="text/javascript">
    // Example implementation of PosBuddy in javascript
    // To use it, copy the code into a file with a .html extension and update the device specific variables

    var selected_items = [];
    var pb = new posBuddy(pb_writeLog);

    // Device specific variables
    // Update these with your own environment variables
    pb.setApplicationKey("6397dead-7d4b-47f7-a0a5-71e5d1705fc4");
    pb.setUsername("default");
    pb.setMerchantId("000000002451");
    pb.setAuthenticationKey("2e4c6acd-9a66-439e-8d02-b39588081559");
    pb.setBarcodeCallback(pb_barcodeCallback);
    pb.connect("123456", pb_statusCallback); //This number can be found on the POSBuddy app.

    // Initiate the auth call, result returned to pb_authCallback
    function startAuth() {
        pb.doAuth(pb_authCallback);
    }

    // Process result of auth. On success, start payment
    function pb_authCallback(obj) {
        if (typeof obj.errorBundle != 'undefined') {
            console.log("Auth error: " + obj.errorBundle.description + "\n" + obj.errorBundle.message);
        }
        else {
            
            pb.setAuthenticationKey(obj.authenticationKey);
            startPayment();
        }
    }
   
    function pb_barcodeCallback(barcode) {
         console.log("Received barcode: " + barcode);
    }
   
    // Function to display items and initiate the payment process
    function startPayment() {
        // display items
        selected_items.push({ "description": "Hamburger", "price": 5200, "count": 1 });
        let total_amount = 5200;
        pb.displayItems(total_amount, selected_items);

        selected_items.push({ "description": "Coffee", "price": 3000, "count": 1 });
        pb.displayItems(total_amount, selected_items);
        total_amount += 3000;

        // start payment after 3 seconds
        setTimeout(() => {
            pb.doSale(total_amount, null, pb_saleCallback);
        }, 3000);

    }

    // This function logs debug data received from posbuddy
    function pb_writeLog(logtype, message) {
        if (logtype != pb.transmitData && logtype != pb.receiveData) console.log("Log received:" + message);
    }

    // This function logs the changes to the websocet status and start payment after connection success
    function pb_statusCallback(status) {
        switch (status) {
            case pbStatus.connected:
                console.log("Received status: connected");
                startAuth();
                break;
            case pbStatus.disconnected:
                console.log("Received status: disconnected");
                break;
            case pbStatus.error:
                console.log("Received status: error");
                break;
        }
    }

    // This function is called when a payment is finished
    function pb_saleCallback(obj) {
        console.log("salecallback received: " + JSON.stringify(obj));
    }

</script>