public bool VoidTransaction(string operationID, out string errorText)
        {
            errorText = null;

            IActiveXConnectAPI a = GetAPI();

            if (a == null)
            {
                errorText = "Failed to load API library...";
                return(false);
            }

            a.Void(operationID);

            if (!ProcessEventsUntil(a, "OnVoidResult", 100))
            {
                errorText = "Timeout waiting for OnVoidResult";
                return(false);
            }

            if (a.OperationResult != "OK")
            {
                errorText = a.Text;
                return(false);
            }

            return(true);
        }
        public bool CloseDocument(Document document)
        {
            Console.WriteLine("Closing document {0}", document.DocumentNr);

            IActiveXConnectAPI a = GetAPI();

            if (a == null)
            {
                return(false);
            }

            List <string> operationsID = new List <string>();

            if (document.Transactions != null)
            {
                foreach (var i in document.Transactions)
                {
                    operationsID.Add(i.OperationID);
                }
            }

            a.DocClosed(document.DocumentNr, operationsID.ToArray());
            Console.WriteLine("DocClosed method called, waiting for OnDocClosedResult event...");

            if (!ProcessEventsUntil(a, "OnDocClosedResult", 15))
            {
                Console.WriteLine("Timeout waiting for OnDocClosedResult");
                return(false);
            }

            string result = a.OperationResult;

            Console.WriteLine("OnDocClosedResult event received with OperationResult={0}", result);
            return(result == "OK");
        }
        public bool CloseDay(string operatorID, string operatorName)
        {
            IActiveXConnectAPI a = GetAPI();

            if (a == null)
            {
                return(false);
            }

            if (!UnlockDevice("999", "John", "", 0, ""))
            {
                Console.WriteLine("Failed to unlock device");
                return(false);
            }

            Console.WriteLine("CloseDay method called");
            a.CloseDay(operatorID, operatorName);

            Console.WriteLine("Waiting for OnCloseDayResult event...");
            if (!ProcessEventsUntil(a, "OnCloseDayResult", 130))
            {
                return(false);
            }

            string result = a.OperationResult;

            Console.WriteLine("OnCloseDayResult received with result={0}", result);
            return(result == "OK");
        }
        public bool UnlockDevice(string operatorID, string operatorName, string operation, long amount, string text)
        {
            IActiveXConnectAPI a = GetAPI();

            if (a == null)
            {
                return(false);
            }

            a.UnlockDevice(text, "EN", operatorID, operatorName, amount, "1.0", operation, 0);

            if (!ProcessEventsUntil(a, "OnUnlockDeviceResult", 15) || a.OperationResult != "OK")
            {
                return(false);
            }

            return(true);
        }
        public bool LockDevice(string text)
        {
            IActiveXConnectAPI a = GetAPI();

            if (a == null)
            {
                return(false);
            }

            a.LockDevice(text);

            if (!ProcessEventsUntil(a, "OnLockDeviceResult", 15) || a.OperationResult != "OK")
            {
                return(false);
            }

            return(true);
        }
        /// <summary>
        /// Gets COM object instance.
        /// If object is already created then return it. Else attempt to create it.
        /// </summary>
        /// <returns>API com object instance to be used. If COM object creation fails return null.</returns>
        IActiveXConnectAPI GetAPI()
        {
            if (_api == null)
            {
                _api = new AXC();
                _api.Initialize(new string[] { "ReceiptControlSymbolsNotSupported", "OnDisplayTextEventSupported", "OnMessageBoxEventSupported" });
                if (_api.OperationResult != "OK")
                {
                    _api.Dispose();
                    _api = null;
                    return(null);
                }

                return(_api);
            }
            else
            {
                return(_api);
            }
        }
        /// <summary>
        /// Processes events until event of expected type is received.
        /// </summary>
        /// <param name="api">COM object</param>
        /// <param name="expectedEventType">Event type to wait for. When this event is received method returns true</param>
        /// <param name="timeoutInSeconds">Timeout in seconds to wait for expected event</param>
        /// <returns>True if expected event was received before timeout. Otherwise returns false.</returns>
        private bool ProcessEventsUntil(IActiveXConnectAPI api, string expectedEventType, int timeoutInSeconds)
        {
            Stopwatch sw = Stopwatch.StartNew();

            api.PollEvent(200);

            string ev = api.EventType;

            while (ev != expectedEventType)
            {
                switch (ev)
                {
                case "OnPrompt":
                {
                    Console.WriteLine(api.PromptText);
                    string input = Console.ReadLine();

                    api.Input(input);
                    if (!ProcessEventsUntil(api, "OnInputResult", 15))
                    {
                        return(false);
                    }

                    if (api.OperationResult != "OK")
                    {
                        return(false);
                    }
                }
                break;

                case "OnPrint":
                {
                    api.GetNextReceipt();
                    while (!String.IsNullOrEmpty(api.ReceiptText))
                    {
                        Console.WriteLine("Printing received receipt:");
                        Console.WriteLine("ReceiptIsClientData: {0}", api.ReceiptIsClientData);
                        Console.WriteLine("ReceiptIsMerchantCopy: {0}", api.ReceiptIsMerchantCopy);
                        Console.WriteLine("Document nr: {0}", api.ReceiptDocumentNr);

                        Console.WriteLine("--------------------------");
                        Console.WriteLine(api.ReceiptText);
                        Console.WriteLine("--------------------------");

                        api.GetNextReceipt();
                    }
                }
                break;

                case "OnSelect":
                {
                    Console.WriteLine(api.Text);

                    string input = Console.ReadLine();
                    api.Input(input);
                    if (!ProcessEventsUntil(api, "OnInputResult", 15))
                    {
                        return(false);
                    }

                    if (api.OperationResult != "OK")
                    {
                        return(false);
                    }
                }
                break;

                case "OnDisplayText":
                {
                    Console.WriteLine("--------------");
                    Console.WriteLine(api.DisplayText);
                    Console.WriteLine("--------------");
                }
                break;

                case "OnMessageBox":
                {
                    Console.WriteLine("-----------------");
                    Console.WriteLine(api.MessageBoxText);
                    Console.WriteLine("-----------------");

                    string[] validButtons = null;
                    switch (api.MessageBoxType)
                    {
                    case "Ok":
                        validButtons = new[] { "Ok" };
                        break;

                    case "YesNo":
                        validButtons = new[] { "Yes", "No" };
                        break;

                    case "OkCancel":
                        validButtons = new[] { "Ok", "Cancel" };
                        break;

                    case "YesNoCancel":
                        validButtons = new[] { "Yes", "No", "Cancel" };
                        break;
                    }

                    Console.WriteLine("Please choose on of following buttons:");
                    for (int i = 0; i < validButtons.Length; i++)
                    {
                        Console.WriteLine("{0} {1}", i + 1, validButtons[i]);
                    }

                    int index;
                    if (!Int32.TryParse(Console.ReadLine(), out index))
                    {
                        Console.WriteLine("You must enter a number from the list above");
                        return(false);
                    }
                    index--;
                    if (index < 0 || index >= validButtons.Length)
                    {
                        Console.WriteLine("You must enter a number from the list above");
                        return(false);
                    }
                    else
                    {
                        api.Click(validButtons[index]);
                        ProcessEventsUntil(api, "OnClickResult", 15);
                    }
                }
                break;

                default:
                {
                    if (!String.IsNullOrEmpty(ev))
                    {
                        Console.WriteLine("Ignoring event: {0}", ev);
                    }
                }
                break;
                }

                if (sw.ElapsedMilliseconds > timeoutInSeconds * 1000)
                {
                    return(false);
                }

                api.PollEvent((int)(timeoutInSeconds * 1000 - sw.ElapsedMilliseconds));
                ev = api.EventType;
            }

            return(true);
        }
        public long Authorize(long amount, string documentNr, string currencyCode, out string errorText, out string operationID)
        {
            errorText   = null;
            operationID = null;

            IActiveXConnectAPI a = GetAPI();

            if (a == null)
            {
                errorText = "Failed to load API library";
                return(0);
            }

            Console.WriteLine("Waiting for OnCard event (insert card)...");
            while (true)
            {
                if (!ProcessEventsUntil(a, "OnCard", 30))
                {
                    errorText = "Timeout waiting for card to be inserted into device";
                    return(0);
                }

                if (ContainsFlag(a.Flags, "AllowAuthorize"))
                {
                    Console.WriteLine("CARD is payment card");
                    break;
                }
                else
                {
                    Console.WriteLine("This is not payment card, most probably it's loyalty/discount card, so calculate discount/loyalty points here... and wait for next CARD event");
                }
            }

            if (a.CurrencyCode != currencyCode)
            {
                errorText = String.Format("Currency '{0}' code is not supported", a.CurrencyCode);
                return(0);
            }

            string lastFourDigits = null;

            if (ContainsFlag(a.Flags, "ReqLastFourDigits"))
            {
                Console.WriteLine("Please enter last 4 digits of the card");
                lastFourDigits = Console.ReadLine();
            }

            a.Authorize(amount, 0, documentNr, lastFourDigits);
            Console.WriteLine("Authorize method called, waiting for OnAuthorizeResult event...");
            if (!ProcessEventsUntil(a, "OnAuthorizeResult", 100))
            {
                errorText = "Timeout waiting for OnAuthorizeResult";
                return(0);
            }

            if (a.OperationResult != "OK")
            {
                errorText = a.Text;
                return(0);
            }

            operationID = a.OperationID;

            if (a.AmountAuthorized == 0)
            {
                errorText = "Authorization declined";
            }

            return(a.AmountAuthorized);
        }