I'm using IPN Simulator to send the requests:
https://developer.paypal.com/developer/ipnSimulator
When I receive the IPN, I make a POST to paypal with the same data, adding &cmd=_notify-validate at the end. Problem is I always receive "INVALID" response. Is it because of IPN simulator or am I posting a wrong request ?
This is my IPN Controller:
[HttpPost]
public string IPN()
{
bool useSandbox = true;
StringBuilder to_send = new StringBuilder();
foreach (string key in Request.Form.Keys)
{
if (to_send.ToString().Equals(""))
to_send.AppendFormat("{0}={1}", key, Request.Form[key]);
else
to_send.AppendFormat("&{0}={1}", key, Request.Form[key]);
}
string paypalUrl = useSandbox ? "https://www.sandbox.paypal.com/cgi-bin/webscr"
: "https://www.paypal.com/cgi-bin/webscr";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(paypalUrl);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
to_send.AppendFormat("&{0}={1}", "cmd", "_notify-validate");
string strRequest = to_send.ToString();
req.ContentLength = strRequest.Length;
string response = "";
using (StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII))
{
streamOut.Write(strRequest);
streamOut.Close();
using (StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream()))
{
response = streamIn.ReadToEnd();
}
}
}
Here's what I receive from paypal :
> payment_type: instant
payment_date: Sun Aug 09 2015 12:23:13 GMT+0300 (GTB Daylight Time)
payment_status: Completed
address_status: confirmed
payer_status: verified
first_name: John
last_name: Smith
payer_email: buyer#paypalsandbox.com
payer_id: TESTBUYERID01
address_name: John Smith
address_country: United States
address_country_code: US
address_zip: 95131
address_state: CA
address_city: San Jose
address_street: 123 any street
business: seller#paypalsandbox.com
receiver_email: seller#paypalsandbox.com
receiver_id: seller#paypalsandbox.com
residence_country: US
item_name: something
item_number: AK-1234
quantity: 1
shipping: 3.04
tax: 2.02
mc_currency: USD
mc_fee: 0.44
mc_gross: 12.34
mc_gross1: 12.34
txn_type: web_accept
txn_id: 363750782
notify_version: 2.1
custom: xyz123
invoice: abc1234
test_ipn: 1
verify_sign: AUxvCDK2PEEhNsHhVyUQ8Y-mDqfQARYxDdlEIxTy83GhdfASQp4iG0Rj
And here's what I'm posting back:
payment_type=instant&payment_date=Sun Aug 09 2015 12:23:13 GMT+0300 (GTB Daylight Time)&payment_status=Completed&address_status=confirmed&payer_status=verified&first_name=John&last_name=Smith&payer_email=buyer#paypalsandbox.com&payer_id=TESTBUYERID01&address_name=John Smith&address_country=United States&address_country_code=US&address_zip=95131&address_state=CA&address_city=San Jose&address_street=123 any street&business=seller#paypalsandbox.com&receiver_email=seller#paypalsandbox.com&receiver_id=seller#paypalsandbox.com&residence_country=US&item_name=something&item_number=AK-1234&quantity=1&shipping=3.04&tax=2.02&mc_currency=USD&mc_fee=0.44&mc_gross=12.34&mc_gross1=12.34&txn_type=web_accept&txn_id=363750782¬ify_version=2.1&custom=xyz123&invoice=abc1234&test_ipn=1&verify_sign=AUxvCDK2PEEhNsHhVyUQ8Y-mDqfQARYxDdlEIxTy83GhdfASQp4iG0Rj&cmd=_notify-validate
Edit 1:
I tried changing the encoding to UTF-8 as it says on paypal documentation, but it still doesn't work :
Ensure that you use the same character encoding for your response string as the encoding specified in the charset field of the original IPN message. When testing using the IPN Simulator, the character encoding will always be UTF-8.
byte[] byteArray = Encoding.UTF8.GetBytes(strRequest);
string response = "";
using (BinaryWriter streamOut = new BinaryWriter(req.GetRequestStream(), System.Text.Encoding.UTF8))
{
streamOut.Write(byteArray, 0, byteArray.Length);
streamOut.Close();
using (StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream()))
{
response = streamIn.ReadToEnd();
}
}
Is it possible that the variables in Request.Form are not in the order they have been sent ?
cmd=_notify-validate is required to be preceding the original variables not succeeding them, you may want make some adjustment in your to_send StringBuilder code block to something like this:
StringBuilder to_send = new StringBuilder();
to_send.Append("cmd=_notify-validate");
foreach (string key in Request.Form.Keys)
{
to_send.AppendFormat("&{0}={1}", key, Request.Form[key]);
}
string strRequest = to_send.ToString();
p.s Refer here for IPN trouble shooting tips , this would be quite helpful as most of the issues have been covered in there
I am trying to write automated tests for a REST API using apache HTTP client, we are using Facebook as an affiliate to log in.
I have used this question as a starting point:
apache HttpClient to access facebook
But it is using lots of deprecated methods.
I have switched all of these out but I am finding that it is not working.
to validate I have written a method to print out the body response and I am viewing that by making it into a HTML document. When I load that page it has the facebook error message of:
Cookies Required
Cookies are not enabled on your browser. Please enable cookies in your browser preferences to continue.
My code:
CookieStore cs = new BasicCookieStore();
HttpClientContext context = HttpClientContext.create();
HttpGet httpget = new HttpGet("http://www.facebook.com/login.php");
HttpResponse response= HttpClientBuilder.create().setDefaultCookieStore(cs).build().execute(httpget);
System.out.println("Login form get: " + response.getStatusLine());
HttpPost httpost = new HttpPost("https://www.facebook.com/login.php");
context.setCookieStore(cs);
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("email", "******"));
nvps.add(new BasicNameValuePair("pass", "*****"));
nvps.add(new BasicNameValuePair("lsd", "AVptst2v"));
httpost.setHeader("User-Agent", "Mozilla/5.0");
httpost.setHeader("Host", "www.facebook.com");
httpost.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
httpost.setHeader("Accept-Language", "en-US,en;q=0.5");
httpost.setHeader("Cookie", cs.toString());
httpost.setHeader("Connection", "keep-alive");
httpost.setHeader("Referer", "https://www.facebook.com/login");
httpost.setHeader("Content-Type", "application/x-www-form-urlencoded");
httpost.setEntity(new UrlEncodedFormEntity(nvps));
response = HttpClientBuilder.create().setDefaultCookieStore(cs).build().execute(httpost,context);
System.out.println("Login form post: " + response.getStatusLine());
System.out.println(printBodyOfResponse(response));
To answer my own question, I found that creating a context and passing that through with each request carried the session, I see lots of answers saying that cookie management is automatic, but for me the only solution was to pass the context throughout
BasicHttpContext context = new BasicHttpContext();
HttpGet get = new HttpGet("http://www.facebook.com/login.php");
HttpResponse response= HttpClientBuilder.create().build().execute(get,context);
System.out.println("Login form get: " + response.getStatusLine());
HttpPost httpost = new HttpPost("https://www.facebook.com/login.php?login_attempt=1");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("email",PropertiesUtil.loadSiteProperty("FB_email")));
nvps.add(new BasicNameValuePair("pass", PropertiesUtil.loadSiteProperty("FB_password")));
httpost.setHeader("User-Agent", "Mozilla/5.0");
httpost.setHeader("Host", "www.facebook.com");
httpost.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
httpost.setHeader("Accept-Language", "en-US,en;q=0.5");
httpost.setHeader("Connection", "keep-alive");
httpost.setHeader("Referer", "https://www.facebook.com/login");
httpost.setHeader("Content-Type", "application/x-www-form-urlencoded");
httpost.setEntity(new UrlEncodedFormEntity(nvps));
response = HttpClientBuilder.create().build().execute(httpost,context);
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;
Here are the log entries, the first is if Retry is set to > 0, the second is with no retry
2013-08-29 09:41:45,852 [33] DEBUG PayPal.PayPalResource [(null)] User-Agent:PayPalSDK/rest-sdk-dotnet 0.7.3 ;lang=DOTNET;v=4.0.30319.18052;bit=64;os=Windows 7 6.1.7601.65536;
2013-08-29 09:41:45,852 [33] DEBUG PayPal.PayPalResource [(null)] PayPal-Request-Id:a781e6bb-c0b0-4f06-abf8-1202dbc17a59
2013-08-29 09:41:45,920 [33] DEBUG PayPal.Manager.ConnectionManager [(null)] {"intent":"sale","payer":{"payment_method":"credit_card","funding_instruments":[{"credit_card":{"number":"4234567890123456","type":"visa","expire_month":5,"expire_year":2015,"cvv2":"123","first_name":"MIckey","last_name":"Keenan","billing_address":{"line1":"123 Main St","city":"Denver","country_code":"US","postal_code":"80210","state":"CO"},"payer_id":"205"}}]},"transactions":[{"amount":{"currency":"USD","total":"4.75"},"description":"WFS Lunch Order","item_list":{"items":[{"quantity":"1","name":"WFS Lunch Order","price":"4.75","currency":"USD"}]}}]}
2013-08-29 09:41:49,505 [33] ERROR PayPal.Manager.ConnectionManager [(null)] Error Response: {"name":"INTERNAL_SERVICE_ERROR","message":"An internal service error has occurred","information_link":"https://developer.paypal.com/webapps/developer/docs/api/#INTERNAL_SERVICE_ERROR","debug_id":"2520071cde1ca"}
2013-08-29 09:41:49,506 [33] INFO PayPal.Manager.ConnectionManager [(null)] Got InternalServerError status code from server
2013-08-29 09:41:49,507 [33] ERROR PayPal.Exception.PayPalException [(null)] Exception in HttpConnection Execute: Stream was not readable.
PayPal.Exception.PayPalException: Exception in HttpConnection Execute: Stream was not readable. ---> System.ArgumentException: Stream was not readable.
at System.IO.StreamReader..ctor(Stream stream, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize, Boolean leaveOpen)
at System.IO.StreamReader..ctor(Stream stream)
at PayPal.HttpConnection.Execute(String payLoad, HttpWebRequest httpRequest)
--- End of inner exception stack trace ---
Here is the second
2013-08-29 09:44:27,685 [16] DEBUG PayPal.PayPalResource [(null)] User-Agent:PayPalSDK/rest-sdk-dotnet 0.7.3 ;lang=DOTNET;v=4.0.30319.18052;bit=64;os=Windows 7 6.1.7601.65536;
2013-08-29 09:44:27,686 [16] DEBUG PayPal.PayPalResource [(null)] PayPal-Request-Id:1d5c68e5-4b4b-4876-8bf4-3b9f64d0afb6
2013-08-29 09:44:27,753 [16] DEBUG PayPal.Manager.ConnectionManager [(null)] {"intent":"sale","payer":{"payment_method":"credit_card","funding_instruments":[{"credit_card":{"number":"4234567890123456","type":"visa","expire_month":5,"expire_year":2015,"cvv2":"123","first_name":"MIckey","last_name":"Keenan","billing_address":{"line1":"123 Main St","city":"Denver","country_code":"US","postal_code":"80210","state":"CO"},"payer_id":"205"}}]},"transactions":[{"amount":{"currency":"USD","total":"4.75"},"description":"WFS Lunch Order","item_list":{"items":[{"quantity":"1","name":"WFS Lunch Order","price":"4.75","currency":"USD"}]}}]}
2013-08-29 09:44:28,847 [16] ERROR PayPal.Manager.ConnectionManager [(null)] Error Response: {"name":"INTERNAL_SERVICE_ERROR","message":"An internal service error has occurred","information_link":"https://developer.paypal.com/webapps/developer/docs/api/#INTERNAL_SERVICE_ERROR","debug_id":"465701e606a56"}
2013-08-29 09:44:28,848 [16] INFO PayPal.Manager.ConnectionManager [(null)] Got InternalServerError status code from server
2013-08-29 09:44:28,850 [16] ERROR PayPal.Exception.PayPalException [(null)] Exception in HttpConnection Execute
PayPal.Exception.PayPalException: Exception in HttpConnection Execute
We sometimes see issue with the Test Card 4111xxx in sandbox and are investigating the fix for the same. Hence use the other Test card in sandbox for your testing.
Reg the stream read error, will test the provided code and update back.
Issue has been fixed in core SDK. Fix is available in branch https://github.com/paypal/sdk-core-dotnet/tree/fix-log4net-stream. Please see https://github.com/paypal/rest-api-sdk-dotnet/issues/10 for more details.
After talking with PayPal support about this same issue: apparently even in the Sandbox, if a credit card is used too many times it will be blocked. They suggested trying other fake card numbers, and referred me to this site for generating them:
http://www.fakenamegenerator.com/
Using cards generated there has been working for me.
Every once in a while Paypal is returning a 500 error code to my .net applications ipn script. It is returning that code in the response to the verification request
Here's the error that is logged:
System.Net.WebException
The remote server returned an error: (500) Internal Server Error.
System.Web.HttpUnhandledException (0x80004005): Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.Net.WebException: The remote server returned an error: (500) Internal Server Error.
at System.Net.HttpWebRequest.GetResponse()
at ASP.ipn_aspx.Page_Load(Object sender, EventArgs e)
The code that Im using for the call is:
endPoint = "https://www.paypal.com/cgi-bin/webscr";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(endPoint);
//Set values for the request back
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
byte[] param = Request.BinaryRead(HttpContext.Current.Request.ContentLength);
string strRequest = Encoding.ASCII.GetString(param);
strRequest += "&cmd=_notify-validate";
req.ContentLength = strRequest.Length;
//Send the request to PayPal and get the response
StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
streamOut.Write(strRequest);
streamOut.Close();
StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
string strResponse = streamIn.ReadToEnd();
streamIn.Close();
This probably happens about once per day and PayPal doesn't retry the ipn message. The other 95% of the PayPal ipn transactions are working fine. Any ideas?
This is my code:
string accessToken = "##";
string accessTokenSecret = "##";
string consumerKey = "##";
string consumerSecret = "##";
string appToken = "##";
string realmId = "##"; //company id in quickbooks online
OAuthRequestValidator oauthValidator = new OAuthRequestValidator(accessToken, accessTokenSecret, consumerKey, consumerSecret);
ServiceContext context = new ServiceContext(oauthValidator, appToken, realmId, IntuitServicesType.QBO);
I am receiving: InvalidTokenException was unhandled by user code - {"Unauthorized"}
in the creating the new ServiceContext line. Not sure what the problem is.
Thanks for any help provided.
This error message:
InvalidTokenException was unhandled by user code - {"Unauthorized"}
Occurs when the OAuth tokens you're using are no longer valid.
I would double-check that:
You're using a valid set of OAuth tokens that you got from Intuit
The tokens are not expired (the Developer Playground tokens are very
short lived, longer-lived 6-month tokens are available if you set up
your own actual OAuth endpoint)
Here is Intuit's documentation for setting up your own OAuth endpoint:
http://docs.developer.intuit.com/0025_Intuit_Anywhere/0010_Getting_Started/0020_Connect/0010_From_Within_Your_App/Implement_OAuth_in_Your_App