【发布时间】:2015-11-01 09:10:30
【问题描述】:
我正在使用 IPN 模拟器发送请求: https://developer.paypal.com/developer/ipnSimulator
当我收到 IPN 时,我使用相同的数据向贝宝发送 POST,并在末尾添加 &cmd=_notify-validate。问题是我总是收到“无效”响应。是因为 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();
}
}
}
这是我从贝宝收到的:
> 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¬ify_version=2.1&custom=xyz123&invoice=abc1234&test_ipn=1&verify_sign=AUxvCDK2PEEhNsHhVyUQ8Y-mDqfQARYxDdlEIxTy83GhdfASQp4iG0Rj&cmd=_notify-validate
编辑1:
我尝试按照 paypal 文档中的说明将编码更改为 UTF-8,但仍然无法正常工作: 确保对响应字符串使用与原始 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.Form 中的变量不是按照它们发送的顺序?
【问题讨论】:
标签: paypal paypal-ipn paypal-sandbox asp.net-core asp.net-core-mvc