question

mKonnekt avatar image
mKonnekt asked mKonnekt commented

Auto print orders directly

We are trying to print automatically an order or order receipt when order will be placed by REST APIs from web. We tried almost all the printer related jobs like :- StaticOrderPrintJob StaticReceiptPrintJob StaticOrderBasedPrintJob OrderPrintJob OrderBasedPrintJob

but in all cases it is redirecting us on clover printer app to print manually but we want to print our order or receipt automatically when order will be placed from our online web site. We are testing this on sandbox

So please let us know that 1. what we are missing to print automatically through android sdk or REST api ? 2. do we need to make any setting on merchant side ? 3. is there any other way for auto printing ?

code for print

  PrintJob pj = new StaticReceiptPrintJob.Builder().order(order).build();
                    pj.print(MainActivity.this, account, printer.get(i));

for getting printer

 private List<Printer> getPrinter() {
        List<String> id = Preferences.getInstance(this).readList(Preferences.ACTIVE_PRINTER_LIST, "");
        List<Printer> printList = new ArrayList<Printer>();
        if (id == null) {
            try {
                List<Printer> printers = printerConnector.getPrinters();
                if (printers != null && !printers.isEmpty()) {
                    printList.add(printers.get(0));
                    return printList;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        try {
            for (int i = 0; i < id.size(); i++) {
                printList.add(printerConnector.getPrinter(id.get(i)));
            }
            return printList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

for getting order

private class MyAsyncTask extends AsyncTask<String, Void, Order> {
        CallBackTask callback;

        public MyAsyncTask(CallBackTask callback) {
            this.callback = callback;
        }


        @Override
        protected final Order doInBackground(String... params) {
            String orderId = params[0];
            Cursor cursor = null;
            try {
                // Query the last order
                cursor = MainActivity.this.getContentResolver().query(OrderContract.Summaries.contentUriWithAccount(account), new String[]{OrderContract.Summaries.ID}, null, null, OrderContract.Summaries.LAST_MODIFIED + " DESC LIMIT 1");
                if (cursor != null && cursor.moveToFirst()) {
                    orderId = cursor.getString(cursor.getColumnIndex(OrderContract.Summaries.ID));
                }


                return orderConnector.getOrder(orderId);

            } catch (RemoteException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                e.printStackTrace();
            } catch (ServiceException e) {
                e.printStackTrace();
            } catch (BindingException e) {
                e.printStackTrace();
            } finally {
                if (cursor != null) {
                    cursor.close();
                }
            }
            return null;
        }

        @Override
        protected final void onPostExecute(Order order) {
            callback.setData(order);
        }
    }

Thanks mKonnekt

Orders
6 comments
10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Jeffrey Blattman avatar image Jeffrey Blattman ♦♦ commented ·

PrintJob.print()does not redirect the printer app, so not sure what you mean there. Please include the code you are using and the logcat around after you call print (use pastbin.com please). You should be using StaticOrderPrintJob.

0 Likes 0 ·
mKonnekt avatar image mKonnekt commented ·

Jeff Find below our code

PrintJob pj = new StaticReceiptPrintJob.Builder().order(order).build(); pj.print(MainActivity.this, account, printer.get(i));

0 Likes 0 ·
mKonnekt avatar image mKonnekt commented ·

for getting printer private List<printer> getPrinter() { List<string> id = Preferences.getInstance(this).readList(Preferences.ACTIVEPRINTERLIST, ""); List<printer> printList = new ArrayList<printer>(); if (id == null) { try { List<printer> printers = printerConnector.getPrinters(); if (printers != null && !printers.isEmpty()) { printList.add(printers.get(0)); return printList; } } catch (Exception e) { e.printStackTrace(); }

0 Likes 0 ·
mKonnekt avatar image mKonnekt commented ·

private class MyAsyncTask extends AsyncTask<string, void,="" order=""> { CallBackTask callback;

    public MyAsyncTask(CallBackTask callback) {
        this.callback = callback;
    }


    @Override
    protected final Order doInBackground(String... params) {
        String orderId = params[0];
        Cursor cursor = null;
0 Likes 0 ·
mKonnekt avatar image mKonnekt commented ·

try { // Query the last order cursor = MainActivity.this.getContentResolver().query(OrderContract.Summaries.contentUriWithAccount(account), new String[]{ OrderContract.Summaries.ID}, null, null, OrderContract.Summaries.LAST_MODIFIED + " DESC LIMIT 1"); if (cursor != null && cursor.moveToFirst()) { orderId = cursor.getString(cursor.getColumnIndex(OrderContract.Summaries.ID)); }

            return orderConnector.getOrder(orderId);
0 Likes 0 ·
Show more comments
Jeffrey Blattman avatar image
Jeffrey Blattman answered mKonnekt commented

You need to provide a payment ID or a payment when using a receipt print job. You are just passing the order. If you want to print an order (kitchen) receipt, like I said previously, use StaticOrderPrintJob.

I don't see anything in there about starting the printers activity.

You normally don't specify the printer when printing a PrintJob. The merchant configured the printers they want available and chooses one at print time (or auto-prints if only one is configured). The choice of printer is provided by the printers app (it's a dialog-themed activity allowing them to select the printer).

4 comments
10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

mKonnekt avatar image mKonnekt commented ·

Jeff

We are using StaticOrderPrintJob for printing the order but still we are unable to get the auto print (we have a screen where the printers are pre-configured for kitchen printer). Note that we are testing this on sandbox

PrintJob pj = new StaticOrderPrintJob.Builder().order(order).build(); new PrintJobsConnector(MainActivity.this).print(printer.get(i), pj);

0 Likes 0 ·
Jeffrey Blattman avatar image Jeffrey Blattman ♦♦ commented ·

It would help a lot to know what the error was.

0 Likes 0 ·
mkonnekt avatar image mkonnekt commented ·

Jeff We have encountered one more issue - when there are two printers - one for receipt and another for order. - we are getting an error. Two things on this

1) How can we decipher what error it is - if we see the printer - we are seeing only "Printer Error" 2) Are there any dev apps that we can run on our Sandbox machine to see the calls being made so that we can see where the error is coming from?

Is there any call that we can look into which will queue the print commands

Thanks

0 Likes 0 ·
Jeffrey Blattman avatar image Jeffrey Blattman ♦♦ commented ·

Standard Android debugging. You can connect a debugger to your app as long as it's debuggable (https://developer.android.com/guide/t...) , and you can use "adb logcat" to get application logs. The print queue will logs errors to logcat.

0 Likes 0 ·
mKonnekt avatar image
mKonnekt answered

code for print PrintJob pj = new StaticReceiptPrintJob.Builder().order(order).build(); pj.print(MainActivity.this, account, printer.get(i));

for getting printer private List<printer> getPrinter() { List<string> id = Preferences.getInstance(this).readList(Preferences.ACTIVEPRINTERLIST, ""); List<printer> printList = new ArrayList<printer>(); if (id == null) { try { List<printer> printers = printerConnector.getPrinters(); if (printers != null && !printers.isEmpty()) { printList.add(printers.get(0)); return printList; } } catch (Exception e) { e.printStackTrace(); } return null; }

    try {
        for (int i = 0; i < id.size(); i++) {
            printList.add(printerConnector.getPrinter(id.get(i)));
        }
        return printList;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;

}

for getting order private class MyAsyncTask extends AsyncTask<string, void,="" order=""> { CallBackTask callback;

    public MyAsyncTask(CallBackTask callback) {
        this.callback = callback;
    }


    @Override
    protected final Order doInBackground(String... params) {
        String orderId = params[0];
        Cursor cursor = null;
        try {
            // Query the last order
            cursor = MainActivity.this.getContentResolver().query(OrderContract.Summaries.contentUriWithAccount(account), new String[]{OrderContract.Summaries.ID}, null, null, OrderContract.Summaries.LAST_MODIFIED + " DESC LIMIT 1");
            if (cursor != null && cursor.moveToFirst()) {
                orderId = cursor.getString(cursor.getColumnIndex(OrderContract.Summaries.ID));
            }


            return orderConnector.getOrder(orderId);

        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (BindingException e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return null;
    }

    @Override
    protected final void onPostExecute(Order order) {
        callback.setData(order);
    }
}
10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Welcome to the
Clover Developer Community