async function cryptoTransferController(micropayment, port, urlString, cb) {
    log("cryptoTransferController::", micropayment)
    // connect to micropayment-server and pass the request, and get a response
    i.validMemoBytes(micropayment.memo)

    let parsedData = await cryptoTransferTx(micropayment, port, urlString)
    let data = parsedData.data
    let tx = parsedData.tx
    let client = parsedData.client
    let mps = micropayment.paymentServer

    const socket = io(mps, {
        autoConnect: false
    })

    socket.on('connect', function () {
        log('socket.on connect count', count)
        count = count + 1
    })

    socket.on('reconnect', () => {
        log('Payment Server is available')
    })

    socket.on('connect_error', () => {
        log('Payment Server is NOT available')
        // we can implement a warning in the UI if needed
    })

    log('CRYPTOTRANSFER execute: cryptoTransferController')
    socket.binary(true).emit(CRYPTOTRANSFER, tx.data)

    socket.on(`${CRYPTOTRANSFER}_RESPONSE`, async function (res) {
        // unexpected or undefined response, handle any error here, likely due to network errors
        try {
            errorHandler(res)
            // cryptoTransfer succeeded, save into indexed-db
            indexedDBSave(data, port)
            // cryptoTransfer succeeded, call getTransactionReceipt to confirm
            let q = client.getTransactionReceipts(tx.id).prepare()
            socket.binary(true).emit(TRANSACTIONGETRECEIPT, q.data)
        } catch (e) {
            // here is a good place to prompt user that something went wrong
            // alertBanner

            log('What is the error?', e.message)

            //none of this for now.
            /*
            await alertBanner(
                `Your micropayment has failed: ${e.message}; <a href="${
                    window.location.href
                }">please try again</a>. For help, visit <a href="https://hashingsystems.com" target="_blank">hashingsystems.com</a>.`,
                false
            )
            if (port) {
                port.postMessage({
                    text: 'change-icon'
                })
            }
            showOnlyAbstract()
            */
        }
    })

    socket.on(`${TRANSACTIONGETRECEIPT}_RESPONSE`, async res => {
        log(`${TRANSACTIONGETRECEIPT}_RESPONSE`, res)
        if (res.receiptStatus === ResponseCodeEnum.SUCCESS) data.receipt = true
        indexedDBSave(data, port)
        socket.disconnect()
        if (data.receipt) {
            cb(null, res)
        } else {
            cb(res)
        }
    })

    // // socket.io builtin event
    // socket.on('error', function (e) {
    //     log('Socket connection error: ', e)
    // })

    socket.on('PAYMENTSERVERERROR', function (text) {
        log('Payment server connection error: ', text)
        cb(text)
    })

    // socket.on('error', function () {
    //     //Socket IO won't reconnect to a host that it has already tried, unless option specified
    //     socket = io.connect(host, {
    //         'force new connection': true
    //     });
    // });

    // socket.io builtin event
    socket.on('disconnect', function () {
        log('Socket disconnected')
    })

    // socket.io builtin event
    socket.on('disconnect', function () {
        log('Socket disconnected')
    })

    socket.open()
}
/**
 *
 * cryptoTransfer handles all cryptoTransfer calls to Hedera using socketio
 * @param {*} micropayment micropayment is the micropayment tag object (hedera-micropayment) parsed from publisher's website
 * @param {*} port port is chrome's long-lived connection
 * @param {string} urlString allows to keep track of the content that was read in indexed-db
 */
cryptoTransfer handles all cryptoTransfer calls to Hedera using socketio
@param {*} micropayment micropayment is the micropayment tag object (hedera-micropayment) parsed from publisher's website
@param {*} port port is chrome's long-lived connection
@param {string} urlString allows to keep track of the content that was read in indexed-db
cryptoTransfer handles all cryptoTransfer calls to Hedera using socketio @param { * } micropayment micropayment is the micropayment tag object ( hedera - micropayment ) parsed from publisher ' s website @param { * } port port is chrome ' s long - lived connection @param { string } urlString allows to keep track of the content that was read in indexed - db
async function cryptoTransferController(micropayment, port, urlString, cb) {
    log("cryptoTransferController::", micropayment)
    i.validMemoBytes(micropayment.memo)
    let parsedData = await cryptoTransferTx(micropayment, port, urlString)
    let data = parsedData.data
    let tx = parsedData.tx
    let client = parsedData.client
    let mps = micropayment.paymentServer
    const socket = io(mps, {
        autoConnect: false
    })
    socket.on('connect', function () {
        log('socket.on connect count', count)
        count = count + 1
    })
    socket.on('reconnect', () => {
        log('Payment Server is available')
    })
    socket.on('connect_error', () => {
        log('Payment Server is NOT available')
    })
    log('CRYPTOTRANSFER execute: cryptoTransferController')
    socket.binary(true).emit(CRYPTOTRANSFER, tx.data)
    socket.on(`${CRYPTOTRANSFER}_RESPONSE`, async function (res) {
        try {
            errorHandler(res)
            indexedDBSave(data, port)
            let q = client.getTransactionReceipts(tx.id).prepare()
            socket.binary(true).emit(TRANSACTIONGETRECEIPT, q.data)
        } catch (e) {
            log('What is the error?', e.message)
        }
    })
    socket.on(`${TRANSACTIONGETRECEIPT}_RESPONSE`, async res => {
        log(`${TRANSACTIONGETRECEIPT}_RESPONSE`, res)
        if (res.receiptStatus === ResponseCodeEnum.SUCCESS) data.receipt = true
        indexedDBSave(data, port)
        socket.disconnect()
        if (data.receipt) {
            cb(null, res)
        } else {
            cb(res)
        }
    })
    socket.on('PAYMENTSERVERERROR', function (text) {
        log('Payment server connection error: ', text)
        cb(text)
    })
    ;
    socket.on('disconnect', function () {
        log('Socket disconnected')
    })
    socket.on('disconnect', function () {
        log('Socket disconnected')
    })
    socket.open()
}
async function cryptoTransferController ( micropayment , port , urlString , cb ) { log ( "cryptoTransferController::" , micropayment ) i . validMemoBytes ( micropayment . memo ) let parsedData = await cryptoTransferTx ( micropayment , port , urlString ) let data = parsedData . data let tx = parsedData . tx let client = parsedData . client let mps = micropayment . paymentServer const socket = io ( mps , { autoConnect : false } ) socket . on ( 'connect' , function ( ) { log ( 'socket.on connect count' , count ) count = count + 1 } ) socket . on ( 'reconnect' , ( ) => { log ( 'Payment Server is available' ) } ) socket . on ( 'connect_error' , ( ) => { log ( 'Payment Server is NOT available' ) } ) log ( 'CRYPTOTRANSFER execute: cryptoTransferController' ) socket . binary ( true ) . emit ( CRYPTOTRANSFER , tx . data ) socket . on ( ` ${ CRYPTOTRANSFER } ` , async function ( res ) { try { errorHandler ( res ) indexedDBSave ( data , port ) let q = client . getTransactionReceipts ( tx . id ) . prepare ( ) socket . binary ( true ) . emit ( TRANSACTIONGETRECEIPT , q . data ) } catch ( e ) { log ( 'What is the error?' , e . message ) } } ) socket . on ( ` ${ TRANSACTIONGETRECEIPT } ` , async res => { log ( ` ${ TRANSACTIONGETRECEIPT } ` , res ) if ( res . receiptStatus === ResponseCodeEnum . SUCCESS ) data . receipt = true indexedDBSave ( data , port ) socket . disconnect ( ) if ( data . receipt ) { cb ( null , res ) } else { cb ( res ) } } ) socket . on ( 'PAYMENTSERVERERROR' , function ( text ) { log ( 'Payment server connection error: ' , text ) cb ( text ) } ) socket . on ( 'disconnect' , function ( ) { log ( 'Socket disconnected' ) } ) socket . on ( 'disconnect' , function ( ) { log ( 'Socket disconnected' ) } ) socket . open ( ) }
cryptoTransfer handles all cryptoTransfer calls to Hedera using socketio
 @param {*} micropayment micropayment is the micropayment tag object (hedera-micropayment) parsed from publisher's website
 @param {*} port port is chrome's long-lived connection
 @param {string} urlString allows to keep track of the content that was read in indexed-db
cryptoTransfer handles all cryptoTransfer calls to Hedera using socketio @param { * } micropayment micropayment is the micropayment tag object ( hedera - micropayment ) parsed from publisher ' s website @param { * } port port is chrome ' s long - lived connection @param { string } urlString allows to keep track of the content that was read in indexed - db
// connect to micropayment-server and pass the request, and get a response // we can implement a warning in the UI if needed // unexpected or undefined response, handle any error here, likely due to network errors // cryptoTransfer succeeded, save into indexed-db // cryptoTransfer succeeded, call getTransactionReceipt to confirm // here is a good place to prompt user that something went wrong // alertBanner //none of this for now. /*
            await alertBanner(
                `Your micropayment has failed: ${e.message}; <a href="${
                    window.location.href
                }">please try again</a>. For help, visit <a href="https://hashingsystems.com" target="_blank">hashingsystems.com</a>.`,
                false
            )
            if (port) {
                port.postMessage({
                    text: 'change-icon'
                })
            }
            showOnlyAbstract()
            */ // // socket.io builtin event // socket.on('error', function (e) { //     log('Socket connection error: ', e) // }) // socket.on('error', function () { //     //Socket IO won't reconnect to a host that it has already tried, unless option specified //     socket = io.connect(host, { //         'force new connection': true //     }); // }); // socket.io builtin event // socket.io builtin event