David
David

Reputation: 1058

Example code for IPN Listener is incorrect according to the docs. Is it?

Sample code provided by PayPal at this reference clearly contradicts all the code examples I can find. Specifically that it requires the "&cmd=_notify-validate" at the beginning and not at the end.

The following code passes the first test ie by just responding to the request in the IPN Simulator. Once the grunt code is added all I get is the "IPN was not sent, and the handshake was not verified. Please review your information." response.

I've commented the following code as much as seems appropriate:

        protected void Page_Load(object sender, EventArgs e)
        {
            mclog.Info("entered PayPalListener Page_Load");

            //  Commenting out the rest results in a successful handshake etc with the IPN Simulator

            string strSandbox = "https://www.sandbox.paypal.com/cgi-bin/webscr";
            // string strLive = "https://www.paypal.com/cgi-bin/webscr";
            mclog.Info(string.Format("strSandBox = [{0}]", strSandbox));


            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strSandbox);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            byte[] param = Request.BinaryRead(HttpContext.Current.Request.ContentLength);


            // The following string can have the cmd text at the beginning or end. Both cause the IPN to spit the dummy.
            string strRequest = "&cmd=_notify-validate&";
            strRequest += Encoding.ASCII.GetString(param);
            // "&cmd=_notify-validate";
            req.ContentLength = strRequest.Length;

            mclog.Info(string.Format("strRequest = [{0}]", strRequest));

            //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();
            mclog.Info(string.Format("strResponse = [{0}]", strResponse));



        }

The code appears correct so after much searching I assume there is something else I must do. There is mention of "enabling" IPN in my "profile" however there is no such option in the profiles that I can locate in my Developers Account.

I've been trying and searching for days and the frustration levels are of the scale. If you do have some suggestions then please be as detailed as possible because the PayPal documentation is very confusing and it seems much previous good advice is now out-of-date.

some logging results are:

2016-07-15 00:02:29:854 W15 INFO MyPayPal.PayPal.PayPalListener.Page_Load.0 entered PayPalListener Page_Load 
2016-07-15 00:02:29:854 W15 INFO MyPayPal.PayPal.PayPalListener.Page_Load.0 Received  
2016-07-15 00:02:29:870 W15 INFO MyPayPal.PayPal.PayPalListener.Page_Load.0 strSandBox = [https://www.sandbox.paypal.com/cgi-bin/webscr] 
2016-07-15 00:02:29:870 W15 INFO MyPayPal.PayPal.PayPalListener.Page_Load.0 strRequest = [payment_type=instant&payment_date=Fri%20Jul%2015%202016%2016%3A32%3A05%20GMT+0930%20%28Cen.%20Australia%20Standard%20Time%29&payment_status=Completed&address_status=confirmed&payer_status=verified&first_name=John&last_name=Smith&[email protected]&payer_id=TESTBUYERID01&address_name=John%20Smith&address_country=United%20States&address_country_code=US&address_zip=95131&address_state=CA&address_city=San%20Jose&address_street=123%20any%20street&[email protected]&[email protected]&[email protected]&residence_country=US&item_name1=something&item_number1=AK-1234&tax=2.02&mc_currency=USD&mc_fee=0.44&mc_gross=12.34&mc_gross_1=12.34&mc_handling=2.06&mc_handling1=1.67&mc_shipping=3.02&mc_shipping1=1.02&txn_type=cart&txn_id=636818821&notify_version=2.1&custom=xyz123&invoice=abc1234&test_ipn=1&verify_sign=AneSo.zKXXEtrcrzCxSU.y9TEUb1A9u2e-FoPLwAA6kE8qMzV0UNUYV5] 
2016-07-15 00:02:29:870 W15 INFO MyPayPal.PayPal.PayPalListener.Page_Load.0 strRequest = [payment_type=instant&payment_date=Fri%20Jul%2015%202016%2016%3A32%3A05%20GMT+0930%20%28Cen.%20Australia%20Standard%20Time%29&payment_status=Completed&address_status=confirmed&payer_status=verified&first_name=John&last_name=Smith&[email protected]&payer_id=TESTBUYERID01&address_name=John%20Smith&address_country=United%20States&address_country_code=US&address_zip=95131&address_state=CA&address_city=San%20Jose&address_street=123%20any%20street&[email protected]&[email protected]&[email protected]&residence_country=US&item_name1=something&item_number1=AK-1234&tax=2.02&mc_currency=USD&mc_fee=0.44&mc_gross=12.34&mc_gross_1=12.34&mc_handling=2.06&mc_handling1=1.67&mc_shipping=3.02&mc_shipping1=1.02&txn_type=cart&txn_id=636818821&notify_version=2.1&custom=xyz123&invoice=abc1234&test_ipn=1&verify_sign=AneSo.zKXXEtrcrzCxSU.y9TEUb1A9u2e-FoPLwAA6kE8qMzV0UNUYV5&cmd=_notify-validate] 
2016-07-15 00:45:09:012 W15 INFO MyPayPal.Global.createMCLog.0 ################################### 
2016-07-15 00:45:09:106 W15 INFO MyPayPal.Global.createMCLog.0 MyPayPal v1.0.0.0 Startup 
2016-07-15 00:45:12:700 W15 INFO MyPayPal.PayPal.PayPalListener.Page_Load.0 entered PayPalListener Page_Load 
2016-07-15 00:45:12:715 W15 INFO MyPayPal.PayPal.PayPalListener.Page_Load.0 strSandBox = [https://www.sandbox.paypal.com/cgi-bin/webscr] 
2016-07-15 00:45:12:778 W15 INFO MyPayPal.PayPal.PayPalListener.Page_Load.0 strRequest = [&cmd=_notify-validate&] 
2016-07-15 00:45:12:778 W15 INFO MyPayPal.PayPal.PayPalListener.Page_Load.0 strRequest = [&cmd=_notify-validate&payment_type=instant&payment_date=Fri%20Jul%2015%202016%2017%3A14%3A51%20GMT+0930%20%28Cen.%20Australia%20Standard%20Time%29&payment_status=Completed&address_status=confirmed&payer_status=verified&first_name=John&last_name=Smith&[email protected]&payer_id=TESTBUYERID01&address_name=John%20Smith&address_country=United%20States&address_country_code=US&address_zip=95131&address_state=CA&address_city=San%20Jose&address_street=123%20any%20street&[email protected]&[email protected]&[email protected]&residence_country=US&item_name1=something&item_number1=AK-1234&tax=2.02&mc_currency=USD&mc_fee=0.44&mc_gross=12.34&mc_gross_1=12.34&mc_handling=2.06&mc_handling1=1.67&mc_shipping=3.02&mc_shipping1=1.02&txn_type=cart&txn_id=952340474&notify_version=2.1&custom=xyz123&invoice=abc1234&test_ipn=1&verify_sign=AFcWxV21C7fd0v3bYYYRCpSSRl31Ag-J5Edcf2.K.Ii9C6RoBck3ltT5] 

Upvotes: 0

Views: 148

Answers (1)

user207421
user207421

Reputation: 311048

In my experience of running a Java servlet-based IPN listener for several years, the order of the parameters in the verification message doesn't make the slightest bit of difference, contrary to numerous statements in the IPN documentation.

Paypal provide Java code for sending the verification message from a servlet, and a Java servlet doesn't have access to the parameters in the order they were sent in the first place, which also goes to prove the point.

EDIT NB This is not valid:

strRequest = [&cmd=_notify-validate&payment_type=instant&payment_date=...

The POST parameters should not start with &. It's a separator, between the name-value pairs.

Upvotes: 1

Related Questions