Stream was not Readable for PayPalRest API
This occurs in the Sandbox or production. In the sandbox if I enter in a
Visa of 4111111111111111 with a code, and exp date... When I hit the
payment.Creat(token) line, an exception is thrown, "Stream was not
readable". If I put in some other CC number like 4234567890123456 the
payment works fine. In production, if I use that 4234... I get the stream
was not readable error. I understand test cards do not always work, but I
expect back a Credit Card invalid response. The problem I have is that
some of my customers are getting the stream error.... If I put in a real
CC in production, it works fine.. but if i change 1 number I get the
invalid card error.... So sometimes it sends it back correctly, other
times it is the Stream error... I recently update the package to latest
and greatest API using Nuget, but does anyone else have any suggestions?
Here is my relevant code try { var token = GetAPIToken();
var creditCard = new CreditCard();
creditCard.number = req.CardNumber;
creditCard.expire_month = req.ExpMonth;
creditCard.expire_year = req.ExpYear;
creditCard.first_name = req.FirstName;
creditCard.last_name = req.LastName;
creditCard.type = req.CardType;
creditCard.payer_id = req.UserId.ToString();
creditCard.cvv2 = req.SecurityCode;
creditCard.billing_address = new Address()
{
line1 = req.Address1,
city = req.City,
state = req.State,
postal_code = req.ZipCode,
country_code = "US"
};
var payment = new Payment();
payment.intent = "sale";
payment.payer = new Payer();
payment.transactions = new List<Transaction>();
var t = new Transaction()
{
description = req.Description,
amount = new Amount()
{
total = req.TotalPayment.ToString("N2"),
currency = "USD",
},
};
t.item_list = new ItemList();
t.item_list.items = new List<Item>();
t.item_list.items.Add(new Item()
{
name = req.Description,
quantity = "1",
price = req.TotalPayment.ToString("N2"),
currency = "USD",
});
payment.transactions.Add(t);
payment.payer.funding_instruments = new
List<FundingInstrument>();
payment.payer.payment_method = "credit_card";
var fundingInstrument = new FundingInstrument();
fundingInstrument.credit_card = creditCard;
payment.payer.funding_instruments.Add(fundingInstrument);
var pay = payment.Create(token);
if (req.PaymentOption != PayOption.UsePayPal && pay.state ==
PayPalStateApproved
&& pay.transactions.Any() &&
pay.transactions[0].related_resources.Any()
&& pay.transactions[0].related_resources[0].sale != null
&&
!String.IsNullOrEmpty(pay.transactions[0].related_resources[0].sale.id))
{
resp.TransactionID =
pay.transactions[0].related_resources[0].sale.id;
}
else
{
resp.Status = Status.Error;
resp.Messages.Add(new Message { Code = "paypal", Text =
pay.state });
}
}
catch (Exception ex)
{
var err = CheckPayPalError(ex);
resp.Messages.Add(new Message { Code = "paypal", Text =
String.IsNullOrEmpty(err) ? ex.Message : err });
resp.Status = Status.Error;
}
return resp;
No comments:
Post a Comment