Integration going into loop

Asked

Viewed 366 times

0

I’m trying to integrate Pagseguro to my application, but I’m encountering huge difficulties, as there are numerous detailed information that I find ahead, leaving me more and more confused.

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

I’m using this code that the Gypsy made available on 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 below is?

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

For even this line quoted below, I bring the correct information. But when you get to the line above, the site is in a loop as if you were updating...

SenderDocument(Documents.GetDocumentByType("CPF"), musico.CPF);
payment.Sender.Documents.Add(senderCpf);
  • Note: I am learning Asp.net-MVC so I have difficulties in understanding some more technical terms. If you can make the answer as clear as possible I’d be grateful

2 answers

2


Console.WriteLine("");

It is a method used for applications of the type console (dirt, right? ). It serves to write something on the screen, show something to the user.

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

Hence, the application does not lock, it waits for an input via console.

The code within the action was made to run in console. You need to look for this kind of thing, copy the code and paste inside the action doesn’t seem like a way to make it work.

  • I’m new in the area, I’m still learning. The fact that Copy and paste the code came from this question followed by reply which seemed pretty obvious, because apparently it was a web site, so I adapted it to my system the way I knew how. Thanks for the reply, clarified my doubt.

  • if Console.Readkey() would expect an entry via console, as it would use to be via web?

  • 1

    That doesn’t exist! Besides, it’s completely unnecessary...

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 make their payment.

I didn’t write this test. It was the UOL team, and it shouldn’t be used for production code the way it is. The idea is that you can only see which address will come back within a test.

The correct, within a ActionResult, is to be used as follows:

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

var paymentRedirectUri = payment.Register(credentials);

return Redirect(paymentRedirectUri);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.