/// <summary>
        /// Performs a cancellation operation.
        /// </summary>
        /// <param name="sender">Cancellation button.</param>
        /// <param name="e">Click event arguments.</param>
        private void CancelTransaction(object sender, RoutedEventArgs e)
        {
            string atk = this.uxLvwTransactionList.SelectedItem.ToString();

            // Verifica se um ATK válido foi selecionado:
            if (string.IsNullOrEmpty(atk) == true)
            {
                this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { this.Log("Não é possivel cancelar um ATK vazio."); }).AsTask();
                return;
            }

            // Seleciona a transação a ser cancelada de acordo com o ATK:
            TransactionModel transaction = this.approvedTransactions.Where(t => t.AuthorizationTransactionKey == atk).First();

            // Cria a requisiçào de cancelamento:
            CancellationRequest request = CancellationRequest.CreateCancellationRequestByAcquirerTransactionKey(this.sak, atk, transaction.Amount, true);

            // Envia o cancelamento:
            PoiResponseBase response = this.authorizer.AuthorizationProvider.SendRequest(request);

            if (response is Rejection || this.WasDeclined(response.OriginalResponse as AcceptorCancellationResponse) == true)
            {
                // Cancelamento não autorizado:
                this.Log(this.GetDeclinedMessage(response.OriginalResponse as AcceptorCancellationResponse));
            }
            else
            {
                // Cancelamento autorizado.
                // Retira a transação da coleção de transação aprovadas:
                this.approvedTransactions.Remove(transaction);
                this.uxLvwTransactionList.Items.Remove(transaction.AuthorizationTransactionKey);
            }
        }
Exemple #2
0
        /// <summary>
        /// Reads the card password.
        /// Perfoms an authorization operation.
        /// </summary>
        /// <param name="card">Information about the card.</param>
        /// <param name="transaction">Information about the transaction.</param>
        /// <param name="authorizationMessage">Authorization message returned.</param>
        /// <returns></returns>
        public bool BuyThePizza(ICard card, ITransactionEntry transaction, out string authorizationMessage)
        {
            Pin pin;

            authorizationMessage = string.Empty;

            // Tries to read the card password:
            if (this.authorizer.ReadPassword(out pin, card, transaction.Amount) != ResponseStatus.Ok)
            {
                return(false);
            }

            // Tries to authorize the transaction:
            PoiResponseBase response = this.authorizer.Authorize(card, transaction, pin);

            // Verifies if there were any return:
            if (response == null)
            {
                return(false);
            }

            // Verifies authorization response:
            if (response.Rejected == false && (response as AuthorizationResponse).Approved == true)
            {
                // The transaction was approved:
                this.BoughtPizzas.Add(TransactionModel.Create(transaction, card, response as AuthorizationResponse));
                authorizationMessage = "Transação aprovada";
                return(true);
            }
            else
            {
                // The transaction was rejected or declined:
                if (response.Rejected == true && response is Rejection)
                {
                    // Transaction was rejected:
                    authorizationMessage = "Transação rejeitada";
                }
                else if (this.WasDeclined(response.OriginalResponse as AcceptorAuthorisationResponse) == true)
                {
                    // Transaction was declined:
                    authorizationMessage = this.GetDeclinedMessage(response.OriginalResponse as AcceptorAuthorisationResponse);
                }

                return(false);
            }
        }
        /// <summary>
        /// Perform an authorization process.
        /// </summary>
        /// <param name="sender">Send transaction button.</param>
        /// <param name="e">Click event arguments.</param>
        private void InitiateTransaction(object sender, RoutedEventArgs e)
        {
            // Limpa o log:
            this.uxLvwLog.Items.Clear();

            // Cria uma transação:
            // Tipo da transação inválido significa que o pinpad vai perguntar ao usuário o tipo da transação.
            TransactionType transactionType;
            Installment     installment = new Installment();

            if (this.uxCbxItemDebit.IsSelected == true)
            {
                transactionType = TransactionType.Debit;

                // É débito, então não possui parcelamento:
                installment.Number = 1;
                installment.Type   = InstallmentType.None;
            }
            else if (this.uxCbxItemCredit.IsSelected == true)
            {
                transactionType = TransactionType.Credit;

                // Cria o parcelamento:
                if (this.uxTbxInstallmentNumber.Text == string.Empty)
                {
                    installment.Number = 1;
                }
                else
                {
                    installment.Number = Int16.Parse(this.uxTbxInstallmentNumber.Text);
                }

                installment.Type = (this.uxTggInstallmentType.IsOn == true) ? InstallmentType.Issuer : InstallmentType.Merchant;
            }
            else
            {
                transactionType = TransactionType.Undefined;
            }

            // Pega o valor da transação
            decimal amount;

            decimal.TryParse(this.uxTbxAmount.Text, out amount);
            if (amount == 0)
            {
                this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { this.Log("Valor da transaçào inválido."); }).AsTask();
                return;
            }

            // Cria e configura a transação:
            TransactionEntry transaction = new TransactionEntry(transactionType, amount);

            transaction.Installment             = installment;
            transaction.InitiatorTransactionKey = this.uxTbxTransactionId.Text;
            transaction.CaptureTransaction      = true;
            ICard card;

            // Envia para o autorizador:
            PoiResponseBase poiResponse = null;

            try
            {
                poiResponse = this.authorizer.Authorize(transaction, out card);
            }
            catch (ExpiredCardException)
            {
                this.Log("Card Expired");
                this.authorizer.PromptForCardRemoval("CARTAO EXPIRADO");
                return;
            }

            if (poiResponse == null)
            {
                this.Log("Um erro ocorreu durante a transação.");
                return;
            }

            if (poiResponse == null)
            {
                this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { this.Log("Um erro ocorreu durante a transação."); }).AsTask();
                return;
            }

            // Verifica o retorno do autorizador:
            if (poiResponse.Rejected == false && this.WasDeclined(poiResponse.OriginalResponse as AcceptorAuthorisationResponse) == false)
            {
                // Transaction approved:
                this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { this.Log("Transação aprovada."); }).AsTask();

                // Cria uma instancia de transaçào aprovada:
                TransactionModel approvedTransaction = TransactionModel.Create(transaction, card, poiResponse as AuthorizationResponse);

                // Salva em uma collection:
                this.approvedTransactions.Add(approvedTransaction);

                // Adiciona o ATK (identificador unico da transação) ao log:
                this.uxLvwTransactionList.Items.Add(approvedTransaction.AuthorizationTransactionKey);
            }
            else if (poiResponse.Rejected == false && this.WasDeclined(poiResponse.OriginalResponse as AcceptorAuthorisationResponse) == true)
            {
                // Transaction declined:
                this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { this.Log("Transação declinada."); }).AsTask();
            }
            else if (poiResponse.Rejected == true && poiResponse is Rejection)
            {
                // Transaction rejected:
                this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { this.Log("Transação rejeitada."); }).AsTask();
            }
        }