Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

11213=Malformed request: Content is not allowed in prolog.. #58

Open
almirrps opened this issue Jan 29, 2021 · 9 comments
Open

11213=Malformed request: Content is not allowed in prolog.. #58

almirrps opened this issue Jan 29, 2021 · 9 comments

Comments

@almirrps
Copy link

Boa tarde pessoal,

Estou utilizando a biblioteca em um projeto desenvolvido em .NET 3.1, mas no momento em que clico para redirecionar para o PagSeguro dá o seguinte erro:
HttpStatusCode: BadRequest
Mensagem completa
Uol.PagSeguro.PagSeguroServiceException: HttpStatusCode: BadRequest
at Uol.PagSeguro.PaymentService.RegisterCore(Credentials credentials, PaymentRequest payment)
at Uol.PagSeguro.PaymentService.Register(Credentials credentials, PaymentRequest payment)
at NopBrasil.Plugin.Payments.PagSeguro.Services.PagSeguroService.CreatePayment(PostProcessPaymentRequest postProcessPaymentRequest)
at NopBrasil.Plugin.Payments.PagSeguro.PagSeguroPaymentProcessor.PostProcessPayment(PostProcessPaymentRequest postProcessPaymentRequest)
11213=Malformed request: Content is not allowed in prolog..

Estou desconfiando que minha biblioteca esteja desatualizada, mas gostaria de uma confirmação. Alguém poderia me dar uma ajuda?

@darrudanoo
Copy link

@aljsilva conseguiu resolver? Estou com mesmo erro que apareceu "do nada" estava funcionando e simplesmente começou esse erro. O meu cenário é o mesmo que o seu. Core 3.1, Plugin do Pagseguro pro NopCommerce.

@almirrps
Copy link
Author

E eu achando que o erro era porque eu tinha cometido algum erro de programação (rsrsrsrs)... ...mais um grande motivo pro pessoal do PagSeguro dar uma atençãozinha pra nós ae.
O erro comigo persiste, já revirei os fontes e ainda nada! Aguardo retorno do pessoal ae ;-)

@darrudanoo
Copy link

Eu abri um chamado no Pagseguro. Abre você também pra tentar acelerar lá: https://app.pipefy.com/public/form/sBlh9Nq6
Como não dá pra saber quanto tempo vai levar, eu vou tentar mudar o plugin pra fazer o envio via JSON, pq acho que o problema é em como o XML está chegando lá.

@tijgrillo
Copy link

mesmo erro, vou abrir chamado também

@darrudanoo
Copy link

Eu consegui resolver, tive que mudar o envio, no PaymentService.cs o envio do post pro pagseguro é feito via XML no RegisterCore. Eu reescrevi o código fazendo o envio por form 'x-www-form-urlencoded' e deu certo.

@tijgrillo
Copy link

Muito bom, você consegue compartilhar o código com nós?

@leodemario
Copy link

Eu mudei de Xmlencoded para Formurlencoded e agora mudou o log para:
Mensagem curta
HttpStatusCode: BadRequest
Mensagem completa
Uol.PagSeguro.PagSeguroServiceException: HttpStatusCode: BadRequest
at Uol.PagSeguro.PaymentService.RegisterCore(Credentials credentials, PaymentRequest payment)
at Uol.PagSeguro.PaymentService.Register(Credentials credentials, PaymentRequest payment)
at Nop.Plugin.Payments.PagSeguro.Services.PagSeguroService.CreatePayment(PostProcessPaymentRequest postProcessPaymentRequest)
at Nop.Plugin.Payments.PagSeguro.PagSeguroPaymentProcessor.PostProcessPayment(PostProcessPaymentRequest postProcessPaymentRequest)
11004=Currency is required.
11024=Items invalid quantity.

@almirrps
Copy link
Author

almirrps commented Feb 22, 2021

Opa! Voltei! Creio que nesse caso são dois problemas a se resolver - na hora de transmitir as informações você não está informando a moeda (currency = Real ou Dollar) em que é feita a transação e o outro é problema na maneira como a quantidade do item comprado deve estar sendo informada. Checa essas duas informações, verifique se elas estão sendo informadas e como estão sendo informadas, deve resolver.

@darrudanoo
Copy link

@leodemario não é só isso, precisa mudar a forma de preencher o body da request.
@tijgrillo segue o que eu fiz:

Na PaymentService.cs eu mudei o RegisterCore para :

internal static PaymentRequestResponse RegisterCore(Credentials credentials, PaymentRequest payment)
        {
            UriBuilder uriBuilder = new UriBuilder(PagSeguroConfiguration.PaymentUri);
            uriBuilder.Query = ServiceHelper.EncodeCredentialsAsQueryString(credentials);
            var request = new HttpRequestMessage(HttpMethod.Post, uriBuilder.Uri);
            PagSeguroTrace.Info(String.Format(CultureInfo.InvariantCulture, "PaymentService.Register({0}) - begin", payment));
            var nvc = new List<KeyValuePair<string, string>>();
            PaymentRequestSerializer.Write(nvc, payment);
            request.Content = new FormUrlEncodedContent(nvc);

            try
            {

                var client = new HttpClient();

                using (HttpResponseMessage response = client.SendAsync(request).Result)
                {
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        using (XmlReader reader = XmlReader.Create(response.Content.ReadAsStreamAsync().Result))
                        {
                            PaymentRequestResponse paymentResponse = new PaymentRequestResponse(PagSeguroConfiguration.PaymentRedirectUri);
                            PaymentRequestResponseSerializer.Read(reader, paymentResponse);
                            PagSeguroTrace.Info(String.Format(CultureInfo.InvariantCulture, "PaymentService.Register({0}) - end {1}", payment, paymentResponse.PaymentRedirectUri));
                            return paymentResponse;
                        }
                    }
                    else
                    {
                        PagSeguroServiceException pse = ServiceHelper.CreatePagSeguroServiceException(response);
                        PagSeguroTrace.Error(String.Format(CultureInfo.InvariantCulture, "PaymentService.Register({0}) - error {1}", payment, pse));
                        throw pse;
                    }
                }
            }
            catch (WebException exception)
            {
                PagSeguroServiceException pse = ServiceHelper.CreatePagSeguroServiceException((HttpWebResponse)exception.Response);
                PagSeguroTrace.Error(String.Format(CultureInfo.InvariantCulture, "PaymentService.Register({0}) - error {1}", payment, pse));
                throw pse;
            }
        }

E na PaymentRequestSerializer.cs eu criei esse método:

internal static void Write(List<KeyValuePair<string, string>> writer, PaymentRequest payment)
        {
            if (writer == null)
                throw new ArgumentNullException("writer");
            if (payment == null)
                throw new ArgumentNullException("payment");

            writer.Add(new KeyValuePair<string, string>("currency", payment.Currency));

            if (payment.Sender != null)
            {
                writer.Add(new KeyValuePair<string, string>("senderName", payment.Sender.Name));
                writer.Add(new KeyValuePair<string, string>("senderEmail", payment.Sender.Email));

                if (payment.Sender.Phone != null)
                {
                    writer.Add(new KeyValuePair<string, string>("senderPhone", payment.Sender.Phone.ToString()));
                }
            }

            if (payment.RedirectUri != null)
            {
                writer.Add(new KeyValuePair<string, string>("redirectURL", payment.RedirectUri.ToString()));
            }

            if (payment.Items.Count > 0)
            {

                for (int i = 0; i < payment.Items.Count; i++)
                {

                    writer.Add(new KeyValuePair<string, string>("itemId" + (i + 1), payment.Items[i].Id.ToString()));
                    writer.Add(new KeyValuePair<string, string>("itemDescription" + (i + 1), payment.Items[i].Description));
                    writer.Add(new KeyValuePair<string, string>("itemQuantity" + (i + 1), payment.Items[i].Quantity.ToString()));
                    writer.Add(new KeyValuePair<string, string>("itemAmount" + (i + 1), payment.Items[i].Amount.ToString(CultureInfo.InvariantCulture)));
                    writer.Add(new KeyValuePair<string, string>("itemWeight" + (i + 1), payment.Items[i].Weight.ToString()));

                }

            }
            if (payment.ExtraAmount.HasValue)
                writer.Add(new KeyValuePair<string, string>("extraAmount", payment.ExtraAmount.Value.ToString()));
            writer.Add(new KeyValuePair<string, string>("reference", payment.Reference));

            if (payment.Shipping != null)
            {
                if (payment.Shipping.ShippingType.HasValue)
                    writer.Add(new KeyValuePair<string, string>("shippingType", payment.Shipping.ShippingType.Value.ToString()));

                if (payment.Shipping.Cost.HasValue)
                    writer.Add(new KeyValuePair<string, string>("shippingCost", payment.Shipping.Cost.Value.ToString(CultureInfo.InvariantCulture)));

                if (payment.Shipping.Address != null)
                {
                    if (payment.Shipping.Address.Country != null)
                        writer.Add(new KeyValuePair<string, string>("shippingAddressCountry", payment.Shipping.Address.Country.ToString()));
                    if (payment.Shipping.Address.State != null)
                        writer.Add(new KeyValuePair<string, string>("shippingAddressState", payment.Shipping.Address.State));
                    if (payment.Shipping.Address.City != null)
                        writer.Add(new KeyValuePair<string, string>("shippingAddressCity", payment.Shipping.Address.City));
                    if (payment.Shipping.Address.District != null)
                        writer.Add(new KeyValuePair<string, string>("shippingAddressDistrict", payment.Shipping.Address.District));
                    if (payment.Shipping.Address.PostalCode != null)
                        writer.Add(new KeyValuePair<string, string>("shippingAddressPostalCode", payment.Shipping.Address.PostalCode));
                    if (payment.Shipping.Address.Street != null)
                        writer.Add(new KeyValuePair<string, string>("shippingAddressStreet", payment.Shipping.Address.Street));
                    if (payment.Shipping.Address.Number != null)
                        writer.Add(new KeyValuePair<string, string>("shippingAddressNumber", payment.Shipping.Address.Number));
                    if (payment.Shipping.Address.Complement != null)
                        writer.Add(new KeyValuePair<string, string>("shippingAddressComplement", payment.Shipping.Address.Complement));
                }
            }

            if(payment.MaxUses.HasValue)
            writer.Add(new KeyValuePair<string, string>("maxUses", payment.MaxUses.Value.ToString()));
            if (payment.MaxAge.HasValue)
                writer.Add(new KeyValuePair<string, string>("maxAge", payment.MaxAge.Value.ToString()));

        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants