public async Task <HttpResponseMessage> ReceiveNotificationTransacao(string notificationCode, string notificationType)
        {
            HttpResponseMessage response = new HttpResponseMessage();
            var    tsc       = new TaskCompletionSource <HttpResponseMessage>();
            string resultado = "false";

            try
            {
                if (notificationCode is null)
                {
                    throw new ArgumentNullException("notificationCode está nulo!");
                }
                if (notificationType is null)
                {
                    throw new ArgumentNullException("notificationType está nulo!");
                }

                // Grava os dados da noficação recebida:
                resultado = _pagSeguroApplication.NotificationTransacao(notificationCode, notificationType);

                TransacaoPagSeguro _transacaoPagSeguro = await GetTransacaoAPIPagSeguroByNotification(notificationCode.Trim(), EMail, Token);

                if (_transacaoPagSeguro.NotificationCode != null && _transacaoPagSeguro.Reference != null)
                {
                    resultado = _pagSeguroApplication.SaveDadosTransacaoPagSeguro(_transacaoPagSeguro);
                }

                response = Request.CreateResponse(HttpStatusCode.OK, resultado);

                tsc.SetResult(response);

                return(await tsc.Task);
            }
            catch (Exception ex)
            {
                response = Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);

                tsc.SetResult(response);

                return(await tsc.Task);
            }
        }
        public string UpdateRecebimentoPagSeguro(TransacaoPagSeguro transacaoPagSeguro)
        {
            string _msg = "";

            if (transacaoPagSeguro == null)
            {
                return("O Objeto TransacaoPagSeguro está nulo!");
            }

            RecebimentoRepository _recRepo = new RecebimentoRepository();

            int     _type      = (int)transacaoPagSeguro.Type;
            int     _status    = (int)transacaoPagSeguro.Status;
            int     _mtdType   = (int)transacaoPagSeguro.PaymentMethod.Type;
            int     _mtdcode   = transacaoPagSeguro.PaymentMethod.Code ?? 0;
            decimal _netAmount = (decimal)transacaoPagSeguro.NetAmount;

            //_msg = _recRepo.UpdateRecebimentoPagSeguro(transacaoPagSeguro.NotificationCode, transacaoPagSeguro.Reference, _type, _status,
            //   transacaoPagSeguro.Lasteventdate, _mtdType, _mtdcode, _netAmount);

            return(_msg);
        }
 public string SaveDadosTransacaoPagSeguro(TransacaoPagSeguro transacaoPagSeguro)
 {
     return(_pagSeguroRepository.SaveDadosTransacaoPagSeguro(transacaoPagSeguro));
 }
 public string UpdateRecebimentoPagSeguro(TransacaoPagSeguro transacaoPagSeguro)
 {
     return(_pagSeguroRepository.UpdateRecebimentoPagSeguro(transacaoPagSeguro));
 }
        public string SaveDadosTransacaoPagSeguro(TransacaoPagSeguro transacaoPagSeguro)
        {
            string _msg = "";

            if (transacaoPagSeguro != null)
            {
                AssinaturaAnuidadeRepository assinaturaAnuidadeRepository = new AssinaturaAnuidadeRepository();
                RecebimentoRepository        recebimentoRepository        = new RecebimentoRepository();

                var assinatura  = assinaturaAnuidadeRepository.GetAssinaturaAnuidadeByReference(transacaoPagSeguro.Reference);
                var recebimento = recebimentoRepository.GetRecebimentoByReference(transacaoPagSeguro.Reference);

                if (assinatura != null)
                {
                    if (recebimento == null)
                    {
                        //Ainda não há regitro
                        Recebimento rec = new Recebimento()
                        {
                            RecebimentoId        = 0,
                            AssinaturaAnuidadeId = assinatura.AssinaturaAnuidadeId,
                            NotificationCodePS   = transacaoPagSeguro.NotificationCode,

                            TypePS              = transacaoPagSeguro.Type,
                            StatusPS            = transacaoPagSeguro.Status,
                            TypePaymentMethodPS = transacaoPagSeguro.PaymentMethod.Type ?? 0,
                            CodePaymentMethodPS = transacaoPagSeguro.PaymentMethod.Code ?? 0,
                            NetAmountPS         = (decimal)transacaoPagSeguro.NetAmount,
                            GrossAmountPS       = (decimal)transacaoPagSeguro.GrossAmount,
                            DiscountAmountPS    = (decimal)transacaoPagSeguro.DiscountAmount,
                            FeeAmountPS         = (decimal)transacaoPagSeguro.FeeAmount,
                            ExtraAmountPS       = (decimal)transacaoPagSeguro.ExtraAmount,
                            DtVencimento        = assinatura.DtVencimentoPagamento,
                            StatusFBTC          = "1",
                            OrigemEmissaoTitulo = "1"
                        };

                        _msg = recebimentoRepository.Insert(rec, transacaoPagSeguro.Lasteventdate);
                        _msg = assinaturaAnuidadeRepository.SetInicioPagamentoPagSeguro(transacaoPagSeguro.Reference, true);
                    }
                    else
                    {
                        recebimento.StatusPS            = transacaoPagSeguro.Status;
                        recebimento.TypePaymentMethodPS = transacaoPagSeguro.PaymentMethod.Type ?? 0;
                        recebimento.CodePaymentMethodPS = transacaoPagSeguro.PaymentMethod.Code ?? 0;
                        recebimento.NetAmountPS         = (decimal)transacaoPagSeguro.NetAmount;
                        recebimento.GrossAmountPS       = (decimal)transacaoPagSeguro.GrossAmount;
                        recebimento.DiscountAmountPS    = (decimal)transacaoPagSeguro.DiscountAmount;
                        recebimento.FeeAmountPS         = (decimal)transacaoPagSeguro.FeeAmount;
                        recebimento.ExtraAmountPS       = (decimal)transacaoPagSeguro.ExtraAmount;
                        recebimento.DtVencimento        = assinatura.DtVencimentoPagamento;

                        _msg = recebimentoRepository.UpdateRecebimentoPagSeguro(recebimento.RecebimentoId, recebimento, transacaoPagSeguro.Lasteventdate);
                    }
                }
                else
                {
                    _msg = $"ATENÇÃO: Não foi encontrado registro para a referência {transacaoPagSeguro.Reference} e NotificationCode {transacaoPagSeguro.NotificationCode} informados pelo PagSeguro";
                }
            }
            else
            {
                _msg = $"ATENÇÃO: Objeto transacaoPagSeguro está nulo";
            }

            return(_msg);
        }
Beispiel #6
0
 public string UpdateRecebimentoPagSeguro(TransacaoPagSeguro transacaoPagSeguro)
 {
     return(_pagSeguroService.UpdateRecebimentoPagSeguro(transacaoPagSeguro));
 }
Beispiel #7
0
 public string SaveDadosTransacaoPagSeguro(TransacaoPagSeguro transacaoPagSeguro)
 {
     return(_pagSeguroService.SaveDadosTransacaoPagSeguro(transacaoPagSeguro));
 }
        /// <summary>
        /// Consulta ao PagSeguro por período.
        /// </summary>
        /// <param name="dtInicial"></param>
        /// <param name="dtFinal"></param>
        /// <param name="page"></param>
        /// <param name="maxPageResults"></param>
        /// <param name="eMail"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public async Task <TransactionSearchResult> GetSincronizarRecebimentosAPIPagSeguro(string dtInicial, string dtFinal, int page, int maxPageResults, string eMail, string token)
        {
            try
            {
                Uri _uri = new Uri($"{BaseUrl}transactions?initialDate={dtInicial}&finalDate={dtFinal}&page={page}&maxPageResults={maxPageResults}&email={eMail}&token={token}");

                HttpResponseMessage responseGet = await httpClient.GetAsync(_uri);

                string response = await responseGet.Content.ReadAsStringAsync();

                if (response.Equals("Unauthorized"))
                {
                    throw new Exception("ATENÇÃO: Ocorreu uma falha durante a autenticação do acesso ao serviço PagSeguro/transactions");
                }

                TransactionSearchResult _ts = new TransactionSearchResult();
                _ts.Transacoes = new List <TransacaoPagSeguro>();

                ErrorsPagSeguro errorsPagSeguro = new ErrorsPagSeguro();

                XDocument doc = XDocument.Parse(response);

                // Verificando se houve erro na requisição:
                foreach (var er in doc.Descendants("error"))
                {
                    errorsPagSeguro.NotificationConde = "";
                    errorsPagSeguro.DtNotificacaoErro = DateTime.Now;
                    errorsPagSeguro.Code    = (string)er.Element("code") ?? "";
                    errorsPagSeguro.Message = (string)er.Element("message") ?? "";
                }

                if (errorsPagSeguro.Message != null)
                {
                    throw new Exception($"ATENÇÃO: Ocorreu um erro ao tentar obter listagem de recebimentos junto ao PagSeguro. Código do Erro: {errorsPagSeguro.Code} - Mensagem: {errorsPagSeguro.Message} - dtInicial: {dtInicial} & dtFinal: {dtFinal}");
                }
                // Fim da verificação:

                foreach (var t in doc.Descendants("transactionSearchResult"))
                {
                    _ts.Date = (DateTime?)t.Element("date") ?? null;

                    foreach (var tr in doc.Descendants("transaction"))
                    {
                        TransacaoPagSeguro _tran = new TransacaoPagSeguro()
                        {
                            Date             = (string)tr.Element("date") ?? String.Empty,
                            Reference        = (string)tr.Element("reference") ?? String.Empty,
                            NotificationCode = (string)tr.Element("code") ?? String.Empty,
                            Type             = (int?)tr.Element("type") ?? null,
                            Status           = (int?)tr.Element("status") ?? null,
                            PaymentMethod    = new PaymentMethodPagSeguro()
                            {
                                Type = (int?)tr.Element("type") ?? null
                            },
                            GrossAmount    = (decimal?)tr.Element("grossAmount") ?? null,
                            DiscountAmount = (decimal?)tr.Element("discountAmount") ?? null,
                            FeeAmount      = (decimal?)tr.Element("feeAmount") ?? null,
                            NetAmount      = (decimal?)tr.Element("netAmount") ?? null,
                            ExtraAmount    = (decimal?)tr.Element("extraAmount") ?? null,
                            Lasteventdate  = (string)tr.Element("date") ?? String.Empty
                        };
                        _ts.Transacoes.Add(_tran);
                    }
                    _ts.ResultsInThisPage = (int?)t.Element("resultsInThisPage") ?? null;
                    _ts.CurrentPage       = (int?)t.Element("currentPage") ?? null;
                    _ts.TotalPages        = (int?)t.Element("totalPages") ?? null;
                }
                return(_ts);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Para consultar uma notificação de transação no PagSeguro, você deve fazer uma requisição à API de
        /// Consulta de Notificações, informando o código da notificação. Utilizar o protocolo HTTP e o
        /// método GET
        /// </summary>
        /// <param name="notificationCode"></param>
        /// <param name="eMail"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public async Task <TransacaoPagSeguro> GetTransacaoAPIPagSeguroByNotification(string notificationCode, string eMail, string token)
        {
            try
            {
                Uri _uri = new Uri($"{BaseUrl}transactions/notifications/{notificationCode}?email={eMail}&token={token}");

                HttpResponseMessage responseGet = await httpClient.GetAsync(_uri);

                string response = await responseGet.Content.ReadAsStringAsync();

                if (response.Equals("Unauthorized"))
                {
                    throw new Exception("ATENÇÃO: Ocorreu uma falha durante a autenticação do acesso ao serviço PagSeguro/transactions/notifications");
                }

                TransacaoPagSeguro _tr             = new TransacaoPagSeguro();
                ErrorsPagSeguro    errorsPagSeguro = new ErrorsPagSeguro();

                XDocument doc = XDocument.Parse(response);

                // Verificando se houve erro na requisição:
                foreach (var er in doc.Descendants("error"))
                {
                    errorsPagSeguro.NotificationConde = notificationCode;
                    errorsPagSeguro.DtNotificacaoErro = DateTime.Now;
                    errorsPagSeguro.Code    = (string)er.Element("code") ?? "";
                    errorsPagSeguro.Message = (string)er.Element("message") ?? "";
                }

                if (errorsPagSeguro.Message != null)
                {
                    throw new Exception($"ATENÇÃO: Ocorreu um erro ao processar ao consultar notificação junto ao PagSeguro. NotificationCode: {notificationCode}, Código do Erro: {errorsPagSeguro.Code} - Mensagem: {errorsPagSeguro.Message}");
                }
                // Fim da verificação:

                foreach (var tr in doc.Descendants("transaction"))
                {
                    TransacaoPagSeguro _tran = new TransacaoPagSeguro()
                    {
                        Date             = (string)tr.Element("date") ?? "",
                        Reference        = (string)tr.Element("reference") ?? "",
                        NotificationCode = (string)tr.Element("code") ?? "",
                        Type             = (int?)tr.Element("type") ?? null,
                        Status           = (int?)tr.Element("status") ?? null,
                        PaymentMethod    = new PaymentMethodPagSeguro()
                        {
                            Type = (int?)tr.Element("type") ?? null, Code = (int?)tr.Element("code") ?? null
                        },
                        GrossAmount    = (decimal?)tr.Element("grossAmount") ?? null,
                        DiscountAmount = (decimal?)tr.Element("discountAmount") ?? null,
                        NetAmount      = (decimal?)tr.Element("netAmount") ?? null,
                        ExtraAmount    = (decimal?)tr.Element("extraAmount") ?? null,
                        Lasteventdate  = (string)tr.Element("lastEventDate") ?? ""
                    };
                }
                return(_tr);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }