【问题标题】:Keep getting INVALID response from PayPal不断收到来自 PayPal 的无效响应
【发布时间】:2016-11-23 01:13:41
【问题描述】:

我不断收到无效回复。这是我的代码:

    protected void Page_Load(object sender, EventArgs e)
    {
        ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
        ServicePointManager.Expect100Continue = true;
        mclog.Info("entered PayPalListener Page_Load");

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

        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;

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

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

        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
        }

我已经包含了日志输出,还引用了沙盒页面上显示的结果:

2016-07-19 16:16:54:870 W15 INFO MyPayPal.PayPal.PayPalListener.Page_Load.0 entered PayPalListener Page_Load 
2016-07-19 16:16:54:870 W15 INFO MyPayPal.PayPal.PayPalListener.Page_Load.0 strSandbox = [https://www.sandbox.paypal.com/cgi-bin/webscr] 
2016-07-19 16:16:54:870 W15 INFO MyPayPal.PayPal.PayPalListener.Page_Load.0 strRequest = [payment_type=instant&payment_date=Wed%20Jul%2020%202016%2008%3A08%3A21%20GMT+0930%20%28AUS%20Central%20Standard%20Time%29&payment_status=Completed&address_status=confirmed&payer_status=verified&first_name=John&last_name=Smith&payer_email=buyer@paypalsandbox.com&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&business=seller@paypalsandbox.com&receiver_email=seller@paypalsandbox.com&receiver_id=seller@paypalsandbox.com&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=919215415&notify_version=2.1&custom=xyz123&invoice=abc1234&test_ipn=1&verify_sign=AFcWxV21C7fd0v3bYYYRCpSSRl31Ak5zhWFvRQAir0SAm0lY6s-KEoQl&cmd=_notify-validate] 
2016-07-19 16:16:55:448 W15 INFO MyPayPal.PayPal.PayPalListener.Page_Load.0 Response was [INVALID] 

我在解码和编码中也尝试过 Encoding.UTF8,但是在接收和发送但不只用于接收时这样做会失败。发送 UTF8 时,握手完全失败。

【问题讨论】:

  • 在一次操作中编码整个参数字符串是不正确的。您确定必须分别对每个名称和值进行编码吗?
  • EJP:两种方法我都试过了,结果一样。

标签: c# paypal paypal-ipn paypal-sandbox


【解决方案1】:

问题是由 IPN 模拟器在 payment_date 字段中插入的“+”号引起的。就我而言,它是 2016 年 7 月 23 日星期六 08:52:03 GMT+0930(澳大利亚中部标准时间) 取出“+”即可解决问题。

从本质上讲,加号被去掉了:

string payment_date = Form.Request["payment_date"];

2016-07-22 16:09:33:751 W15 INFO MyPayPal.PayPal.PayPalListener.Page_Load.0 payment_date = [2016 年 7 月 23 日星期六 08:38:14 GMT 0930(澳大利亚中部标准时间)]

不带加号将参数返回给 PayPal 就足以使请求无效。

我将在这里和其他地方搜索以尝试找出一种方法来实现这一点,但是加号(可能是)减号似乎并没有出现在 MSDN 或 RFC DateTime 格式中,所以我如何检查它的原始存在是担心。

【讨论】:

  • 来自 PayPal:您在开发者门户中使用的 IPN 模拟器页面是一个文本框,可以进行编辑。该特定文件从您的桌面中提取日期和时间。默认情况下,日期和时间在 PDT payment_datepayment_date 由 PayPal 生成的时间/日期戳,格式如下:HH:MM:SS Mmm DD, YYYY PDT 长度:28 个字符 developer.paypal.com/docs/classic/ipn/integration-guide/… 对于直播,它将根据信息拉取在文档上。
猜你喜欢
  • 2012-01-08
  • 2012-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-28
  • 1970-01-01
  • 2015-07-16
  • 1970-01-01
相关资源
最近更新 更多