Pagseguro API Integration

0

I'm trying to integrate PagSeguro with my application, but I'm finding it very difficult because there are lots of non-detailed information that lies ahead of me, making me increasingly confused.

I have only 1 product that is sold per package, which in this case will be 3 different packages, to which the customer will choose only the packages to make the payment, so he would not need a cart.

I'm using this code that Gypsy has made available in one of the answers right here in the OS .

[HttpPost]
        public ActionResult NovoPedido(int? id)
        {
            const bool isSandbox = true;
             EnvironmentConfiguration.ChangeEnvironment(isSandbox);

            try
            {
                var credentials = PagSeguroConfiguration.Credentials(isSandbox);

                // Instanciar uma nova requisição de pagamento
                var payment = new PaymentRequest { Currency = Currency.Brl };

                Pacote pacote = db.Pacotes.Find(id);

                // Adicionar produtos
                payment.Items.Add(new Item(Convert.ToString(pacote.PacoteID), pacote.Titulo, 1, pacote.valor));

                int clienteId = Repositorios.Funcoes.GetUsuario().PessoaID;
                Pessoa pessoa= db.Pessoas.Find(clienteId);
                Cidade cidade = db.Cidades.Find(pessoa.CidadeID);
                Estado estado = db.Estados.Find(cidade.EstadoID);
                // Informações de entrega
                payment.Shipping = new Shipping
                {
                    ShippingType = ShippingType.NotSpecified,
                    Cost = 0.00m,
                    Address = new Address(
                        "BRA",
                        estado.Sigla,
                        estado.Nome,
                        cidade.Nome,
                        musico.CEP,
                        musico.Rua,
                        Convert.ToString(pessoa.NResidencia),
                        musico.Complemento
                        )
                };

                // Informações do remetente

                var nome = pessoa.Nome;
                var email = pessoa.Email;
                var celular = pessoa.Celular;
                celular = Regex.Replace(celular, "[^0-9]", "");
                int contador = celular.Count();
                string ddd = celular.Substring(0, 2);
                string numero = celular.Substring(2, (contador - 2));
                payment.Sender = new Sender(
                    nome,
                    email,
                    new Phone(ddd, numero)
                );

                // URL a redirecionar o usuário após pagamento
                payment.RedirectUri = new Uri("http://localhost:50534/Pagamento/Retorno");

                // Informações extras para identificar o pagamento.
                // Essas informações são livres para adicionar o que for necessário.

                var senderCpf = new SenderDocument(Documents.GetDocumentByType("CPF"), musico.CPF);
                payment.Sender.Documents.Add(senderCpf);

                var paymentRedirectUri = payment.Register(credentials);

            Console.WriteLine("URL do pagamento : " + paymentRedirectUri);
                Console.ReadKey();

            }
            catch (PagSeguroServiceException exception)
            {
                Console.WriteLine(exception.Message + "\n");

                foreach (var element in exception.Errors)
                {
                    Console.WriteLine(element + "\n");
                }
                Console.ReadKey();
            }
            return View();
        } //fim pedido

Does anyone know what this line is?

Console.WriteLine("URL do pagamento : " + paymentRedirectUri);

For until this line quoted below, I bring the correct information. But when it arrives in the line above, the site is in a loop as if it had been updating ...

SenderDocument(Documents.GetDocumentByType("CPF"), musico.CPF);
payment.Sender.Documents.Add(senderCpf);
    
asked by anonymous 05.11.2016 / 13:05

2 answers

2
Console.WriteLine("");

This is a method used for console type applications (sugestive, no?). It serves to write something on the screen, show something to the user.

The "problem" of your code has nothing to do with the above code, but with Console.ReadKey() , this method makes the application wait for a user input via console .

So the application does not hang, it waits for a console entry .

The code inside action was made to run in console . You need to look at this kind of thing, copy the code and paste it into action does not seem to be a way to make it work.

    
05.11.2016 / 15:22
2

This line:

Console.WriteLine("URL do pagamento : " + paymentRedirectUri);

It is a test to write in console the return of the URL that should be used to redirect the user to PagSeguro and to make its payment.

I did not write this test. It was the UOL team, and it should not be used for production code the way it is. The idea is that you can only see which address will return within a test.

The correct, within ActionResult , is to use as follows:

var senderCpf = new SenderDocument(Documents.GetDocumentByType("CPF"), musico.CPF);
payment.Sender.Documents.Add(senderCpf);

var paymentRedirectUri = payment.Register(credentials);

return Redirect(paymentRedirectUri);
    
08.11.2016 / 20:11