Paypal IPN 集成问题

Posted

技术标签:

【中文标题】Paypal IPN 集成问题【英文标题】:Paypal IPN integration issues 【发布时间】:2012-04-08 18:41:15 【问题描述】:

我正在尝试在我的页面上处理来自贝宝的响应。我从贝宝文档本身找到了这段代码。当响应有效时,需要处理一些参数,如txn_idpayment_completed。我该怎么做? 代码如下

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

        //for proxy
        //WebProxy proxy = new WebProxy(new Uri("http://url:port#"));
        //req.Proxy = proxy;

        //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();

        if (strResponse == "VERIFIED")
        

            //UPDATE YOUR DATABASE

            //TextWriter txWriter = new StreamWriter(Server.MapPath("../uploads/") + Session["orderID"].ToString() + ".txt");
            //txWriter.WriteLine(strResponse);
            //txWriter.Close();

            //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")
        
            //UPDATE YOUR DATABASE

            //TextWriter txWriter = new StreamWriter(Server.MapPath("../uploads/") + Session["orderID"].ToString() + ".txt");
            //txWriter.WriteLine(strResponse);
            ////log for manual investigation
            //txWriter.Close();
        
        else
          //UPDATE YOUR DATABASE

            //TextWriter txWriter = new StreamWriter(Server.MapPath("../uploads/") + Session["orderID"].ToString() + ".txt");
            //txWriter.WriteLine("Invalid");
            ////log response/ipn data for manual investigation
            //txWriter.Close();
        
    

请给我一些意见。谢谢

【问题讨论】:

【参考方案1】:

在您得到响应并且您知道这是有效的之后,参数已经发布给您,您可以使用.Form 获取它们例如:

if (strResponse == "VERIFIED")

    // Now All informations are on
    HttpContext.Current.Request.Form;
    // for example you get the invoice like this
    HttpContext.Current.Request.Form["invoice"] 
    // or the payment_status like
    HttpContext.Current.Request.Form["payment_status"] 

else

  //log for manual investigation

【讨论】:

以上是关于Paypal IPN 集成问题的主要内容,如果未能解决你的问题,请参考以下文章

Paypal IPN 集成问题

PayPal 集成 (IPN) - 真的有多大可能?

与 IPN 集成的 PayPal Express Checkout

PayPal:IPN/REST/Webhooks 与 Java 程序的集成

PayPal IPN 集成(快速结帐 - Razor/C#)

联系表格 7 和 PayPal IPN 集成