Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="amount"></param>
        /// <param name="paymentMethod"></param>
        /// <returns></returns>
        public async Task CreateTransaction(int amount, PagarMe.Mpos.PaymentMethod paymentMethod)
        {
            using (var port = new SerialPort(this.portName))
            {
                port.Open();

                using (var mpos = CreateMposInstance(port))
                {
                    await mpos.Initialize();

                    try
                    {
                        var result = await mpos.ProcessPayment(amount, this.GetEmvApplications(paymentMethod), paymentMethod)
                                     .HandleMposException(new TimeSpan(0, 0, CARD_READ_TIMEOUT), mpos);

                        Transaction transaction = new Transaction();
                        transaction.Amount   = amount;
                        transaction.CardHash = result.CardHash;

                        await transaction.SaveAsync();

                        int responseCode;
                        int.TryParse(transaction.AcquirerResponseCode, out responseCode);

                        await mpos.FinishTransaction(true, responseCode, transaction.Card.FirstDigits);

                        mpos.Display("TRANSACTION FINISHED");
                        await Task.Delay(2000);
                    }
                    catch (PinPadException ex1)
                    {
                        System.Diagnostics.Debug.WriteLine("PinPadException: {0}\n{1}", ex1.Message, ex1.StackTrace);
                        mpos.Display(ex1.Message);
                        await Task.Delay(2000);
                    }
                    catch (TimeoutException ex2)
                    {
                        System.Diagnostics.Debug.WriteLine("TimeoutException: {0}\n{1}", ex2.Message, ex2.StackTrace);
                        mpos.Display("CARD READ TIMEOUT");
                        await Task.Delay(2000);
                    }
                    catch (Exception ex3)
                    {
                        System.Diagnostics.Debug.WriteLine("Exception: {0}\n{1}", ex3.Message, ex3.StackTrace);
                        mpos.Display("AN ERROR OCURRED");
                        await Task.Delay(2000);
                    }
                    finally
                    {
                        await mpos.Close();
                    }
                }
            }
        }
Пример #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="paymentMethod"></param>
        /// <returns></returns>
        protected List <EmvApplication> GetEmvApplications(PagarMe.Mpos.PaymentMethod paymentMethod)
        {
            var list = new List <EmvApplication>();

            foreach (var brand in this.cardBrands)
            {
                list.Add(new EmvApplication(brand, paymentMethod));
            }

            return(list);
        }
Пример #3
0
        public async Task Pay(Int32 amount, Int32 paymentMethod)
        {
            PaymentMethod pm = paymentMethod == 2 ? PaymentMethod.Credit : PaymentMethod.Debit;

            var result = await mpos.ProcessPayment(amount, null, pm);

            Console.WriteLine("Pressione Enter");
            Console.ReadLine();
            if (result.Status == PaymentStatus.Accepted)
            {
                var transaction = new Transaction
                {
                    CardHash = result.CardHash,
                    Amount   = amount,
                };
                try
                {
                    await transaction.SaveAsync();
                }
                catch (PagarMe.PagarMeException ex)
                {
                    Console.WriteLine(ex.Error.Errors[0].Message);
                    await mpos.Close();

                    Console.WriteLine("FECHADO!");
                    Console.WriteLine("Fluxo de exceção acionado, transação não criada");
                    Console.ReadLine();
                }
                Console.WriteLine("TRANSACTION ID = " + transaction.Id);
                Console.WriteLine("Transaction ARC = " + transaction.AcquirerResponseCode + ", Id = " + transaction.Id);
                Console.WriteLine("ACQUIRER RESPONSE CODE = " + transaction.AcquirerResponseCode);
                int    responseCode = Int32.Parse(transaction.AcquirerResponseCode);
                object emvResponse  = transaction["card_emv_response"];
                var    response     = emvResponse?.ToString();

                if (response != null)
                {
                    await mpos.FinishTransaction(true, responseCode, response);
                }
            }
            else if (result.Status != PaymentStatus.Canceled)
            {
                await mpos.FinishTransaction(false, 0, null);
            }
            Console.WriteLine("Pressione Enter");
            Console.ReadLine();

            await mpos.Close();

            Console.WriteLine("CLOSED!");
        }