使用ASP vNext实现Paypal IPN



我使用IPN模拟器发送请求:https://developer.paypal.com/developer/ipnSimulator

当我收到IPN时,我用相同的数据向paypal发送POST,并在末尾添加&cmd=_notify-validate。问题是我总是收到"无效"的回应。是因为IPN模拟器还是我发布了错误的请求?

这是我的IPN控制器:(HttpPost)公共字符串IPN(){

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

这是我从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: buyer@paypalsandbox.com
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: seller@paypalsandbox.com
receiver_email: seller@paypalsandbox.com
receiver_id: seller@paypalsandbox.com
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

我的回复是:

 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=buyer@paypalsandbox.com&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=seller@paypalsandbox.com&receiver_email=seller@paypalsandbox.com&receiver_id=seller@paypalsandbox.com&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

编辑1:

我尝试将编码更改为UTF-8,因为它在paypal文档上说,但它仍然不起作用:确保对响应字符串使用与原始IPN消息的字符集字段中指定的编码相同的字符编码。当使用IPN模拟器进行测试时,字符编码将始终为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();
                }
            }

是否有可能Request。表格不是按照发送的顺序吗?

cmd=_notify-validate需要在原始变量之前,而不是在它们之后,您可能需要在to_send StringBuilder代码块中进行一些调整,如下所示:

        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。请参考这里的IPN故障排除技巧,这将非常有帮助,因为大多数问题都已经涵盖在这里

相关内容

  • 没有找到相关文章

最新更新