PayPal 即时付款通知 (IPN) ASP.NET C# 不起作用
本文关键字:ASP NET 不起作用 IPN 付款 通知 PayPal | 更新日期: 2023-09-27 18:34:37
我正在尝试让IPN为我的网站工作,但直到现在没有任何成功。
我已经红色了所有PayPals文档,并且我已经设置了使IPN工作所需的所有配置。
这是我到目前为止所做的:
1. 创建了一个开发帐户;
2. 将我的网站付款首选项更新为自动退货:开;
3. 在我的个人资料属性中开启了即时付款通知;
4. 创建了一个测试付款选项事件:
protected void btCheckout_Click(object sender, EventArgs e)
{
var queryString = System.Web.HttpUtility.ParseQueryString(string.Empty);
queryString["cmd"] = "_cart";
queryString["business"] = "xpto-facilitator@xpto.com";
queryString["upload"] = "1";
queryString["item_name_1"] = "My Cart Item 1";
queryString["quantity_1"] = "1";
queryString["amount_1"] = "100.00";
queryString["shopping_url"] = "http://xpto.com/Client/Checkout";
queryString["return"] = "http://xpto.com/Client/Checkout";
queryString["notify_url"] = "http://xpto.com/CheckoutResult.aspx";
Response.Redirect("https://www.sandbox.paypal.com/cgi-bin/webscr?" + queryString.ToString());
}
5. 创建了一个 IPN 页面:
protected void Page_Load(object sender, EventArgs e)
{
//Post back to either sandbox or live
string strSandbox = "https://www.sandbox.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;
//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")
{
//log for debugging purposes
LogManager.Error("CheckoutResult VERIFIED:" + strResponse);
}
else if (strResponse == "INVALID")
{
//log for debugging purposes
LogManager.Error("CheckoutResult INVALID: " + strResponse);
}
else
{
//log for debugging purposes
LogManager.Error("CheckoutResult something else: " + strResponse);
}
}
- 我设法继续结帐并成功支付了物品;
- 我的 IPN 页面没有执行,因为我没有记录任何内容;
- 使用即时付款通知 (IPN( 模拟器测试了我的处理程序,我总是收到以下错误:
由于 HTTP 错误,我们无法发送 IPN:401:未授权
我做错了什么?我错过了什么吗?
为
IPN 指定的notify_url
设置为需要登录的终结点。
queryString["notify_url"] = "http://xpto.com/CheckoutResult.aspx";
此 URL 必须设置为可以接受和处理来自 IPN 服务的 POST 请求的端点,而无需任何登录(我似乎找不到任何专门提到此要求的文档(。 这就是为什么您的侦听器收到的 IPN 必须经过验证的主要原因(您看起来这样做是正确的(,因为在验证之前,无法完全信任收到的请求是由PayPal发送的。