private void ProcessReconcilation(object state)
        {
            var instance = CreateInstance();

            if (!instance.IsValid)
            {
                return;
            }

            string result = String.Empty;

            try
            {
                result = EgateAPI.egAuthRequest(instance.Id, EgateAPI.ProtocolId, CreateRequest(OperationType.TotalsReconcilation).Append(" 1 1").ToString()).GetString();
            }
            finally
            {
                ReleaseInstance(instance);
            }

            if (!IsValidResult(result))
            {
                OnError(instance);
            }

            reconcilation = ReconcilationState.Completed;
            OnMessage(String.Format("Результат сверки итогов : {0}", result), false);
        }
        protected override void DisposeManagedResources()
        {
            if (!IsValid)
            {
                return;
            }

            EgateAPI.egReleaseInstance(id);
            id = 0;
        }
        private void ProcessCancelTransaction(object state)
        {
            string status = EgateAPI.egGetOpStatus(current.Id, true).GetString();

            cancel = (status[0] == EgateAPI.StatusResult) ? CancelState.Completed : CancelState.None;
            OnMessage(String.Format("Транзакция отменена : {0}", cancel), false);

            if (cancel == CancelState.Completed)
            {
                SberBank.TransactionCancelled++;
            }
        }
        private void AcquireKey()
        {
            if (Ready)
            {
                return;
            }

            Instance instance = null;

            try
            {
                instance = CreateInstance();
                string result = EgateAPI.egAuthRequest(instance.Id, EgateAPI.ProtocolId, CreateRequest(OperationType.RequestWorkKey).ToString()).GetString();
                if (!IsValidResult(result))
                {
                    OnError(instance);
                    return;
                }

                OnMessage(String.Format("Ответ на запрос ключа : {0}", result), false);

                result = EgateAPI.egGetAuthResult(instance.Id).GetString();

                if (!IsValidResult(result))
                {
                    OnError(instance);
                    return;
                }

                AuthResponse response = null;

                try
                {
                    response = AuthResponse.Parse(result, null);
                }
                catch (Exception x)
                {
                    OnMessage(x.Message, true);
                }

                if ((response == null) || !AuthResponse.IsValidCode(response.GCSCode))
                {
                    return;
                }

                keyAcqiured = true;
                OnMessage(String.Format("Ключ получен успешно : {0}", response), false);
            }
            finally
            {
                ReleaseInstance(instance);
            }
        }
 static BankingDevice()
 {
     try
     {
         uint v = EgateAPI.egGetVersion();
         Version = String.Format("API v{0}.{1}.{2}.{3} ({4})", (int)(v / 1000000), (int)((v % 1000000) / 10000), (int)((v % 10000) / 100), v % 100,
                                 EgateAPI.egGetVersionDescription().GetString());
     }
     catch (Exception e)
     {
         Version = String.Concat("Не удалось определить версию", Environment.NewLine, e.Message);
     }
 }
        public bool BeginTransaction(int receiptId, int amount)
        {
            AcquireKey();
            if (!Ready)
            {
                OnMessage("Оборудование не готово", true);
                return(false);
            }

            if (state != EquipmentState.Idle)
            {
                OnMessage("Транзакция уже выполняется", true);
                return(false);
            }

            string request = CreateRequest(OperationType.Payment)
                             .Append(EgateAPI.ParameterSeparator)
                             .Append(amount)
                             .Append(EgateAPI.ParameterSeparator)
                             .Append(receiptId)
                             .ToString();

            OnMessage(String.Format("Транзакция начинается : {0}", request), false);

            current = CreateInstance();
            string result = EgateAPI.egAuthRequestAsync(current.Id, EgateAPI.ProtocolId, request).GetString();

            if (!result.StartsWith(EgateAPI.AsyncRequestStartedString))
            {
                OnError(current);
                return(false);
            }

            OnMessage(String.Format("Запрос отправлен : {0}", result), false);

            cancel = CancelState.None;
            state  = EquipmentState.Started;
            OnStateChanged(state, String.Empty);
            timer.Start();

            return(true);
        }
        private void ProcessTransaction(object sender, ElapsedEventArgs e)
        {
            string status = EgateAPI.egGetOpStatus(current.Id, false).GetString();

            if (status[0] == EgateAPI.StatusResult)
            {
                timer.Stop();
                OnMessage(String.Format("Запрос обработан : {0}", status.Substring(1)), false);

                AuthResponse response = null;
                try
                {
                    byte[] ba;
                    string result = EgateAPI.egGetAuthResult(current.Id).GetString(out ba);
                    if (IsValidResult(result))
                    {
                        lastTransactionResult = (cancel == CancelState.Completed) ? TransactionResult.Cancelled : TransactionResult.OK;
                    }
                    else
                    {
                        lastTransactionResult = TransactionResult.Error;
                        OnError(current);
                    }

                    response = AuthResponse.Parse(result, ba);
                }
                catch (Exception x)
                {
                    OnMessage(x.Message, true);
                }
                finally
                {
                    OnTransactionCompleted(lastTransactionResult, response);
                    state = EquipmentState.CardTaken;
                    OnStateChanged(state, status.Substring(1));
                }
            }
            else if (status[0] == EgateAPI.StatusBusy)
            {
                EquipmentState s = state;
                switch (status[1])
                {
                case EgateAPI.StatusWaitCard:
                    //if (_state == EquipmentState.Started)
                    s = EquipmentState.WaitingForCard;
                    break;

                case EgateAPI.StatusCardEntered:
                    //if (_state == EquipmentState.WaitingForCard)
                    s = EquipmentState.CardInserted;
                    break;

                case EgateAPI.StatusPinEntered:
                    if (state == EquipmentState.CardInserted)
                    {
                        s = EquipmentState.PinEntered;
                    }
                    break;

                case EgateAPI.StatusAuthCompleted:
                    if ((state != EquipmentState.Started) && (state != EquipmentState.WaitingForCard))
                    {
                        s = EquipmentState.AuthCompleted;
                    }
                    break;

                default:
                    s = state;
                    break;
                }

                if (state == s)
                {
                    return;
                }
                state = s;
                OnStateChanged(state, status.Substring(2));
            }
        }
        private void OnError(Instance instance)
        {
            int error = EgateAPI.egGetLastError(instance.Id);

            OnMessage(new StringBuilder().AppendFormat("Ошибка #{0}", error).AppendLineIfNotEmpty(EgateAPI.egGetErrorDescription(error).GetString()).ToString(), true);
        }
 public Instance()
 {
     id = EgateAPI.egInitInstance(Path.Combine(ApplicationServices.GetApplicationRootPath(), EgateAPI.EgateConfigPath));
 }