示例#1
0
        public CheckOutPagSeguro getDadosParaCheckOutPagSeguro(int associadoId, string valor, string tipoEndereco, int anoInicio, int anoTermino, bool enderecoRequerido, bool isAnuidade)
        {
            CheckOutPagSeguro _check = new CheckOutPagSeguro();

            if (!valor.Equals("0.00"))
            {
                _check = _pagSeguroService.getDadosParaCheckOutPagSeguro(associadoId, tipoEndereco, anoInicio, anoTermino, enderecoRequerido, isAnuidade);

                DateTime _date = DateTime.Now;

                _check.Currency         = "BRL";
                _check.ItemId1          = "01";
                _check.ItemDescription1 = isAnuidade ? $"Assinatura Anuidade {anoInicio}" : "Assinatura de Evento";
                _check.ItemAmount1      = valor;
                _check.ItemQuantity1    = "1";
                _check.ItemWeight1      = "0";
                _check.Reference        = isAnuidade ? $"A{anoInicio}{_date.GetHashCode()}".Replace("-", "") : $"E{_date.Year}{_date.GetHashCode()}".Replace("-", "");

                _check.SenderName = Functions.AjustaTamanhoString(_check.SenderName, 50);
                //_check.SenderAreaCode = Functions.AjustaTamanhoString(_check.SenderAreaCode, 2);
                _check.SenderPhone = Functions.AjustaTamanhoString(_check.SenderPhone, 9);
                _check.SenderEmail = Functions.AjustaTamanhoString(_check.SenderEmail, 60);
                // _check.ShippingType = Functions.AjustaTamanhoString(_check.ShippingType, 3);
                //_check.ShippingAddressRequired = Functions.AjustaTamanhoString(_check.ShippingAddressRequired, 0);
                _check.ShippingAddressStreet     = Functions.AjustaTamanhoString(_check.ShippingAddressStreet, 80);
                _check.ShippingAddressNumber     = Functions.AjustaTamanhoString(_check.ShippingAddressNumber, 20);
                _check.ShippingAddressComplement = Functions.AjustaTamanhoString(_check.ShippingAddressComplement, 40);
                _check.ShippingAddressDistrict   = Functions.AjustaTamanhoString(_check.ShippingAddressDistrict, 60);
                //_check.ShippingAddressPostalCode = Functions.AjustaTamanhoString(_check.ShippingAddressPostalCode, 0);
                _check.ShippingAddressCity  = Functions.AjustaTamanhoString(_check.ShippingAddressCity, 60);
                _check.ShippingAddressState = Functions.AjustaTamanhoString(_check.ShippingAddressState, 2);
                //_check.ShippingAddressCountry = Functions.AjustaTamanhoString(_check.ShippingAddressCountry, 0);
            }
            return(_check);
        }
        public async Task <HttpResponseMessage> GetTokenCheckOut(int associadoId, string valor, string tipoEndereco, int anoInicio, int anoTermino, bool enderecoRequerido, bool isAnuidade)
        {
            HttpResponseMessage response = new HttpResponseMessage();
            var tsc = new TaskCompletionSource <HttpResponseMessage>();

            try
            {
                CheckOutPagSeguro _ck = _pagSeguroApplication.getDadosParaCheckOutPagSeguro(associadoId, valor, tipoEndereco, anoInicio, anoTermino, enderecoRequerido, isAnuidade);

                TokenCheckOutPagSeguro token = await GetTokenDinamicoAPIPagSeguro(EMail, Token, _ck);

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

                tsc.SetResult(response);

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

                tsc.SetResult(response);

                return(await tsc.Task);
            }
        }
        /// <summary>
        /// Para obter o código de checkout para uma venda junto ao PagSeguro. Essa função é para uso interno
        /// método GET
        /// </summary>
        /// <param name="notificationCode"></param>
        /// <param name="eMail"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public async Task <TokenCheckOutPagSeguro> GetTokenCheckOutPessoaId(int pessoaId, string valor, string tipoEndereco, int anoInicio, int anoTermino, bool enderecoRequerido, bool isAnuidade)
        {
            try
            {
                CheckOutPagSeguro _ck = _pagSeguroApplication.getDadosParaCheckOutPagSeguro(pessoaId, valor, tipoEndereco, anoInicio, anoTermino, enderecoRequerido, isAnuidade);

                TokenCheckOutPagSeguro token = await GetTokenDinamicoAPIPagSeguro(EMail, Token, _ck);

                return(token);
            }
            catch (Exception ex)
            {
                throw new Exception($"ATENÇÃO: Ocorreu um erro durante o acesso ao PagSeguro. Exception Type:{ex.GetType()}. Erro:{ex.Message}");
            }
        }
        public CheckOutPagSeguro getDadosParaCheckOutPagSeguro(int associadoId, string tipoEndereco, int anoInicio, int anoTermino, bool enderecoRequerido, bool isAnuidade)
        {
            CheckOutPagSeguro _check = new CheckOutPagSeguro();

            try
            {
                AssociadoRepository associadoRepository = new AssociadoRepository();

                Associado a = associadoRepository.GetAssociadoById(associadoId);

                if (a != null)
                {
                    string _codeArea = "";
                    string _telefone = "";
                    if (!string.IsNullOrWhiteSpace(a.NrCelular))
                    {
                        _codeArea = a.NrCelular.Substring(0, 2);
                        _telefone = a.NrCelular.Substring(2);
                    }

                    string _eMail = "";
                    _eMail = pS_IsDebug == "true" ? pS_CompradorTeste : a.EMail;

                    Endereco e = new Endereco();

                    if (a.EnderecosPessoa != null)
                    {
                        List <Endereco> lst = new List <Endereco>();

                        foreach (var end in a.EnderecosPessoa)
                        {
                            if (end.TipoEndereco == tipoEndereco)
                            {
                                e = end;
                            }
                        }
                    }

                    _check = new CheckOutPagSeguro()
                    {
                        Currency                  = "",
                        ItemId1                   = "",
                        ItemDescription1          = "",
                        ItemAmount1               = "0.00",
                        ItemQuantity1             = "",
                        ItemWeight1               = "",
                        Reference                 = "",
                        SenderName                = a.Nome,
                        SenderAreaCode            = _codeArea,
                        SenderPhone               = _telefone,
                        SenderEmail               = _eMail,
                        ShippingType              = "1",
                        ShippingAddressRequired   = "true",
                        ShippingAddressStreet     = e.Logradouro,
                        ShippingAddressNumber     = e.Numero,
                        ShippingAddressComplement = e.Complemento,
                        ShippingAddressDistrict   = e.Bairro,
                        ShippingAddressPostalCode = e.Cep,
                        ShippingAddressCity       = e.Cidade,
                        ShippingAddressState      = e.Estado,
                        ShippingAddressCountry    = "BRA"
                    };
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(_check);
        }
        /// <summary>
        /// Obtém o Token dinâmico no PagSeguro para cada pagamento da FBTC.
        /// </summary>
        /// <param name="token"></param>
        /// <param name="CheckOutPagSeguro"></param>
        /// <returns></returns>
        public async Task <TokenCheckOutPagSeguro> GetTokenDinamicoAPIPagSeguro(string email, string token, CheckOutPagSeguro c)
        {
            try
            {
                Uri _uri = new Uri($"{BaseUrl}checkout");

                var _parametros = new List <KeyValuePair <string, string> >();
                _parametros.Add(new KeyValuePair <string, string>("email", email));
                _parametros.Add(new KeyValuePair <string, string>("token", token));
                _parametros.Add(new KeyValuePair <string, string>("currency", c.Currency));
                _parametros.Add(new KeyValuePair <string, string>("itemId1", c.ItemId1));
                _parametros.Add(new KeyValuePair <string, string>("itemDescription1", c.ItemDescription1));
                _parametros.Add(new KeyValuePair <string, string>("itemAmount1", c.ItemAmount1.ToString()));
                _parametros.Add(new KeyValuePair <string, string>("itemQuantity1", c.ItemQuantity1));
                _parametros.Add(new KeyValuePair <string, string>("itemWeight1", c.ItemWeight1));
                _parametros.Add(new KeyValuePair <string, string>("reference", c.Reference));
                _parametros.Add(new KeyValuePair <string, string>("senderName", c.SenderName));
                _parametros.Add(new KeyValuePair <string, string>("senderAreaCode", c.SenderAreaCode));
                _parametros.Add(new KeyValuePair <string, string>("senderPhone", c.SenderPhone));
                _parametros.Add(new KeyValuePair <string, string>("senderEmail", c.SenderEmail));
                _parametros.Add(new KeyValuePair <string, string>("shippingType", c.ShippingType));
                _parametros.Add(new KeyValuePair <string, string>("shippingAddressRequired", c.ShippingAddressRequired));
                _parametros.Add(new KeyValuePair <string, string>("shippingAddressStreet", c.ShippingAddressStreet));
                _parametros.Add(new KeyValuePair <string, string>("shippingAddressNumber", c.ShippingAddressNumber));
                _parametros.Add(new KeyValuePair <string, string>("shippingAddressComplement", c.ShippingAddressComplement));
                _parametros.Add(new KeyValuePair <string, string>("shippingAddressDistrict", c.ShippingAddressDistrict));
                _parametros.Add(new KeyValuePair <string, string>("shippingAddressPostalCode", c.ShippingAddressPostalCode));
                _parametros.Add(new KeyValuePair <string, string>("shippingAddressCity", c.ShippingAddressCity));
                _parametros.Add(new KeyValuePair <string, string>("shippingAddressState", c.ShippingAddressState));
                _parametros.Add(new KeyValuePair <string, string>("shippingAddressCountry", c.ShippingAddressCountry));
                _parametros.Add(new KeyValuePair <string, string>("timeout", "25"));
                _parametros.Add(new KeyValuePair <string, string>("enableRecovery", "false"));

                var content = new System.Net.Http.FormUrlEncodedContent(_parametros);

                HttpResponseMessage responsePost = await httpClient.PostAsync(_uri, content);

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

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

                TokenCheckOutPagSeguro _ck             = new TokenCheckOutPagSeguro();
                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 token dinâmico junto ao PagSeguro. Reference: {c.Reference}, Código do Erro: {errorsPagSeguro.Code} - Mensagem: {errorsPagSeguro.Message}");
                }
                // Fim da verificação:

                if (doc != null)
                {
                    foreach (var c1 in doc.Descendants("checkout"))
                    {
                        _ck.Date = (DateTime?)c1.Element("date") ?? null;
                        _ck.Code = (String)c1.Element("code");
                    }
                    _ck.Reference = c.Reference;
                }

                return(_ck);
            }
            catch (Exception ex)
            {
                throw new Exception($"ATENÇÃO: Ocorreu um erro durante o acesso ao PagSeguro. Exception Type:{ex.GetType()}. Erro:{ex.Message}");
            }
        }