private async Task ProcessAdjust(TransactionState transactionState, long newAmount) { using (transactionState.CreateAutoSaveContext()) { AdjustRequest adjustRequest = AdjustRequest.Create(transactionState.DocumentNumber, newAmount, transactionState.AuthorizationId); _pedEvents.Clear(); _pedHub.SendData(PedId, AsyncPosPacket.PrependLength(adjustRequest.Serialize(_apak, PedId))); transactionState.Receipts.Clear(); Stopwatch sw = new Stopwatch(); sw.Start(); while (sw.Elapsed.TotalMilliseconds < AuthorizationTimeout) { var eventData = await _pedEvents.WaitAnyAsync(new[] { AsyncPosEventTypes.AdjustResult.ToString(), AsyncPosEventTypes.PrintReceipt.ToString(), }, AuthorizationTimeout - (int)sw.Elapsed.TotalMilliseconds); if (eventData == null) continue; switch ((AsyncPosEventTypes)Enum.Parse(typeof(AsyncPosEventTypes), eventData.Id, true)) { case AsyncPosEventTypes.AdjustResult: { AdjustResponse result = AdjustResponse.Deserialize(eventData.Value); if (result.DocumentNumber != transactionState.DocumentNumber) break; if (!result.Approved) throw new AsyncPosException(Language.Get("cannot_confirm_partial_trans_error", transactionState.Language)); transactionState.Amount = newAmount; return; } case AsyncPosEventTypes.PrintReceipt: { PrintReceipt printReceipt = PrintReceipt.Deserialize(eventData.Value); if (printReceipt.DocumentNumber != transactionState.DocumentNumber) break; transactionState.Receipts.Add(new Receipt { Id = printReceipt.ReceiptId, Text = printReceipt.ReceiptText, Flags = printReceipt.Flags }); break; } } } throw new AsyncPosException(Language.Get("no_response", transactionState.Language)); } }
private async Task ProcessReversal(TransactionState transactionState) { using (transactionState.CreateAutoSaveContext()) { transactionState.State = TransactionStates.Reversing; transactionState.InformationText = Language.Get("reversed", transactionState.Language); _pedEvents.Clear(); DocClosedRequest docClosed = DocClosedRequest.Create(transactionState.DocumentNumber, null); _pedHub.SendData(PedId, AsyncPosPacket.PrependLength(docClosed.Serialize(_apak, PedId))); if (await _pedEvents.WaitOneAsync(AsyncPosEventTypes.Ack.ToString(), AckTimeout) == null) throw new AsyncPosException(Language.Get("no_response", transactionState.Language)); transactionState.State = TransactionStates.Reversed; Stopwatch sw = new Stopwatch(); sw.Start(); while (sw.Elapsed.TotalMilliseconds < ReversalReceiptTimeout) { var eventData = await _pedEvents.WaitOneAsync(AsyncPosEventTypes.PrintReceipt.ToString(), ReversalReceiptTimeout - (int)sw.Elapsed.TotalSeconds); if (eventData == null) continue; PrintReceipt printReceipt = PrintReceipt.Deserialize(eventData.Value); if (printReceipt.DocumentNumber != transactionState.DocumentNumber) continue; Receipt rcpt = new Receipt { Id = printReceipt.ReceiptId, Text = printReceipt.ReceiptText, Flags = printReceipt.Flags, IsReversalReceipt = true, }; transactionState.Receipts.Add(rcpt); if (printReceipt.Flags.IsFlagSet("LR")) break; } } }
private async Task ProcessConfirm(TransactionState transactionState) { using (transactionState.CreateAutoSaveContext()) { transactionState.State = TransactionStates.Confirming; DocClosedRequest docClosed = DocClosedRequest.Create(transactionState.DocumentNumber, transactionState.AuthorizationId); _pedEvents.Clear(); _pedHub.SendData(PedId, AsyncPosPacket.PrependLength(docClosed.Serialize(_apak, PedId))); var result = await _pedEvents.WaitOneAsync(AsyncPosEventTypes.Ack.ToString(), AckTimeout); if (result == null) throw new AsyncPosException(Language.Get("no_response", transactionState.Language)); transactionState.State = TransactionStates.Confirmed; } }