PayPal IPN:未发送IPN,并且未验证握手

时间:2016-02-16 07:32:05

标签: c# asp.net paypal-ipn

我在asp.net编写的IIS服务器中使用IPN Samulator,c#但我遇到错误" IPN未发送,并且握手未经验证。"。我的托管服务器使用普通的http而不支持ssl。我的测试网址为http://realestate.owncircles.com/ipn.aspx

代码:

ServicePointManager.Expect100Continue = true;
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    // receive PayPal ipn data

    // extract ipn data into a string
    byte[] param = Request.BinaryRead(Request.ContentLength);
    string strRequest = Encoding.ASCII.GetString(param);

    // append PayPal verification code to end of string
    strRequest += "&cmd=_notify-validate";

    // create an HttpRequest channel to perform handshake with PayPal
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(System.Configuration.ConfigurationManager.AppSettings["paypalIPN"].ToString());
    req.Method = "POST";
    req.ContentType = "application/x-www-form-urlencoded";
    req.ContentLength = strRequest.Length;

    // send data back to PayPal to request verification
    StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), Encoding.ASCII);
    streamOut.Write(strRequest);
    streamOut.Close();

    // receive response from PayPal
    StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
    string strResponse = streamIn.ReadToEnd();
    streamIn.Close();

    // if PayPal response is successful / verified
    if (strResponse.Equals("VERIFIED"))
    {

    }

1 个答案:

答案 0 :(得分:2)

用这个

替换你的代码
//Post back to either sandbox or live 
    string strSandbox = "https://www.sandbox.paypal.com/cgi-bin/webscr";
    string strLive = "https://www.paypal.com/cgi-bin/webscr";
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strSandbox);


    //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 = strRequest + "&cmd=_notify-validate";
    req.ContentLength = strRequest.Length;


    //for proxy 
    //var proxy = New WebProxy(New System.Uri("http://url:port#")) 
    //req.Proxy = proxy 


    //Send the request to PayPal and get the response 
    StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), Encoding.ASCII);
    streamOut.Write(strRequest);
    streamOut.Close();
    StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
    string strResponse = streamIn.ReadToEnd();
    streamIn.Close();


    if (strResponse == "VERIFIED")
    {
        //check the payment_status is Completed 
        //check that txn_id has not been previously processed 
        //check that receiver_email is your Primary PayPal email 
        //check that payment_amount/payment_currency are correct 
        //process payment 
    }
    else if (strResponse == "INVALID")
    {
        //log for manual investigation 
    }
    else
    {
        //Response wasn't VERIFIED or INVALID, log for manual investigation 
    }
}
public vbIPNexample()
{
    Load += Page_Load;
}