0
votes

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: [email protected]
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: [email protected]
receiver_email: [email protected]
receiver_id: [email protected]
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&[email protected]&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&[email protected]&[email protected]&[email protected]&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&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 ?

1

1 Answers

0
votes

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