[英]ASP.net WebForms PayPal IPN Falsely marked Success
我正在努力解決我們網站購物車/付款系統的問題。 我們的網站是用 C# ASP.NET WebForms 編寫的
問題是來自 PayPal 的 IPN 響應似乎沒有運行。 檢查有關記錄上的 PayPal IPN 歷史記錄,PayPal 顯示來自我的站點的 200 成功響應。
我在我的網站上添加了一個日志記錄功能,因此任何對該頁面的點擊都會被記錄下來,並且我沒有看到我的通知頁面的點擊。
有誰知道我是否做錯了什么,或者他們是否更改了一些我需要審查的內容以使其更穩定?
以下是我的網站處理 PayPal 交易的方式:
使用發票編號、項目的一些文本和總費用,我填充命名值集合並將信息發布到 PayPal:
NameValueCollection nvc = new NameValueCollection();
nvc.Add("cmd", HttpUtility.HtmlAttributeEncode("_xclick"));
nvc.Add("business", HttpUtility.HtmlAttributeEncode("email@example.com"));
nvc.Add("currency_code", HttpUtility.HtmlAttributeEncode("USD"));
nvc.Add("item_name", HttpUtility.HtmlAttributeEncode(InvoiceText));
nvc.Add("no_shipping", HttpUtility.HtmlAttributeEncode("1"));
nvc.Add("notify_url", HttpUtility.HtmlAttributeEncode("https://example.com/ipn_PayPal.aspx"));
nvc.Add("invoice", InvoiceID);
nvc.Add("amount", HttpUtility.HtmlAttributeEncode(TotalCharge.ToString()));
nvc.Add("image_url", HttpUtility.HtmlAttributeEncode("https://example.com/Content/images/Logo.png"));
nvc.Add("return", HttpUtility.HtmlAttributeEncode("https://example.com/?PayPalThanks=" + InvoiceID));
nvc.Add("rm", HttpUtility.HtmlAttributeEncode("2"));
HttpHelper.RedirectAndPOST(this.Page, PPFee.PostPage, nvc);
作為參考,她是 HttpHelper 類:
public class HttpHelper
{
/// <summary>
/// This method prepares an Html form which holds all data in hidden field in the addetion to form submitting script.
/// </summary>
/// <param name="url">The destination Url to which the post and redirection will occur, the Url can be in the same App or ouside the App.</param>
/// <param name="data">A collection of data that will be posted to the destination Url.</param>
/// <returns>Returns a string representation of the Posting form.</returns>
/// <Author>Samer Abu Rabie</Author>
private static String PreparePOSTForm(string url, NameValueCollection data)
{
//Set a name for the form
string formID = "PostForm";
//Build the form using the specified data to be posted.
StringBuilder strForm = new StringBuilder();
strForm.Append("<form id=\"" + formID + "\" name=\"" + formID + "\" action=\"" + url + "\" method=\"POST\">");
foreach (string key in data)
{
strForm.Append("<input type=\"hidden\" name=\"" + key + "\" value=\"" + data[key] + "\">");
}
strForm.Append("</form>");
//Build the JavaScript which will do the Posting operation.
StringBuilder strScript = new StringBuilder();
strScript.Append("<script language='javascript'>");
strScript.Append("var v" + formID + " = document." + formID + ";");
strScript.Append("v" + formID + ".submit();");
strScript.Append("</script>");
//Return the form and the script concatenated. (The order is important, Form then JavaScript)
return strForm.ToString() + strScript.ToString();
}
public static void RedirectAndPOST(Page page, string destinationUrl, NameValueCollection data)
{
//Prepare the Posting form
string strForm = PreparePOSTForm(destinationUrl, data);
//Add a literal control the specified page holding the Post Form, this is to submit the Posting form with the request.
page.Controls.Add(new LiteralControl(strForm));
}
}
交易確實轉到 PayPal,人們可以正常付款,但在重定向回我的感謝頁面后,IPN 似乎從未發送過。
在我的 IPN 頁面上,我首先檢查發票,然后將其記錄到數據庫中:
int Invoice = Utils.ReturnInt(Request.Form["invoice"], -1);
if (Invoice == -1)
Invoice = Utils.ReturnInt(Request.QueryString["invoice"], 0);
LogInvHit(Invoice);
如果頁面在此中斷,我希望 PayPal 的 IPN 歷史記錄中出現錯誤,因此它要么越過這個,要么在假設成功 200 之前從未達到過
另外,作為參考,這就是我告訴 PayPal 是否成功的方式:
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strPayPalResponse);
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;
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();
據我估計,如果頁面被點擊,我至少應該有一個條目,但我沒有,所以我懷疑我的頁面實際上是錯誤的還是從 PayPal 獲得了 IPN 命中。
總之,我不認為 IPN 通知工作正常,但我假設我做錯了什么。 尋求一些幫助來解決我可以糾正的潛在問題
您誤解了 IPN 交付和驗證的工作原理。
當 PayPal 向您的偵聽器發布消息時,您的偵聽器將使用一些 HTTP 狀態代碼進行響應。 如果它以 2xx 狀態響應,則 IPN 被標記為成功傳送。 如果它沒有以 2xx 狀態響應,IPN 將重試並最終失敗,如果沒有收到 2xx 成功響應。
您突出顯示的代碼、頁面中斷的位置以及將 IPN 發回 PayPal 進行驗證的位置與上述內容完全無關(只要它不影響返回的 HTTP 狀態代碼)。
將收到的 IPN 消息發回 PayPal 進行驗證是為了您自己的目的,因此您知道該消息來自 PayPal 而不是其他某個實體。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.