INVALID paypal ipn response however [duplicate]

2019-09-06 21:03发布

问题:

This question is an exact duplicate of:

  • PayPal IPN Sandbox response always INVALID

I am using sandbox mode. I have a buy now button in sandbox mode linked to my sandbox business account which has ipn enabled with the url to my site. The ipn implementation is exactly the same as the sample code here: https://cms.paypal.com/cms_content/GB/en_GB/files/developer/IPN_ASP_NET_C.txt

I click the button and make a purchase using a sandbox personal account which is successful. it shows up as sent with code 200 in the business accounts ipn history but on the ipn page on my site the response is invalid.

Been at this for days now.. cant figure it out :(

回答1:

Here's what I'm using for https://ASPSecurityKit.net

    private void ProcessPayment(bool test)
    {
        try
        {
           string callbackResponse = null;
            string content = null;
            string callbackUrl = test ? "https://www.sandbox.paypal.com/cgi-bin/webscr"
            : "https://www.paypal.com/cgi-bin/webscr";

            var req = (HttpWebRequest) WebRequest.Create(callbackUrl);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            content = Encoding.ASCII.GetString(
                Request.BinaryRead(HttpContext.Request.ContentLength)
                );
            content += "&cmd=_notify-validate";
            req.ContentLength = content.Length;
                                                using (var streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII))
            {
                streamOut.Write(content);
            }
            using (var streamIn = new StreamReader(req.GetResponse().GetResponseStream()))
            {
            callbackResponse = streamIn.ReadToEnd();
            }


            if (callbackResponse.Equals("VERIFIED", StringComparison.OrdinalIgnoreCase))
            {
                // Now validate whether gross_amount is ok, receiver_email is your business acount mail id and so on.
            }
        }
        catch (Exception ex)
        {
            // Logger.Log(ex); // Uncomment this line if you have a logger
        }
    }

Note: I store all transactions in the database whether varified or invalid. That logic is ASPSecurityKit.net specific hence I have omitted that here.