使用ASP vNext实现Paypal IPN

时间:2015-08-09 09:43:15

标签: paypal paypal-ipn paypal-sandbox asp.net-core asp.net-core-mvc

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

当我收到IPN时,我使用相同的数据对paypal进行POST,最后添加& cmd = _notify-validate。问题是我总是收到" INVALID"响应。是因为IPN模拟器还是我发布了错误的请求?

这是我的IPN控制器:         [HttpPost]         公共字符串IPN()         {                 bool useSandbox = true;

            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邮件的charset字段中指定的编码相同的字符编码。使用IPN Simulator进行测试时,字符编码将始终为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.Form中的变量是否可能不是按照它们发送的顺序排列的?

1 个答案:

答案 0 :(得分:0)

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.s请参阅此处IPN trouble shooting tips,这将非常有用,因为大部分问题已在此处讨论