【问题标题】:PayPal IPN Integration (Express Checkout - Razor/C#)PayPal IPN 集成(快速结帐 - Razor/C#)
【发布时间】:2011-12-12 18:18:53
【问题描述】:

我目前正在尝试将 PayPal 与我的网站集成,并且一切顺利 - 直到我们进行 IPN 检查。

它一直到 if/elseif/else 块,这就是它结束的地方。它输出一条消息,内容如下:

“无效状态。form_charset=UTF8”

这是我的代码。

@using System.Collections.Generic
@using System.Text
@using System.Web
@using System.Web.UI
@using System.Web.UI.HtmlControls
@using System.Web.UI.WebControls
@using System.ComponentModel

@{
    Layout = "~/_SiteLayout.cshtml";
    Page.Title = "Checkout | SSSSS";

    string postUrl = "https://www.paypal.com/cgi-bin/webscr";
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(postUrl);

    //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 = System.Text.Encoding.UTF8.GetString(param);
    string ipnPost = strRequest;
    strRequest += "&cmd=_notify-validate";
    req.ContentLength = strRequest.Length;

    //for proxy
    //WebProxy proxy = new WebProxy(new Uri("http://url:port#"));
    //req.Proxy = proxy;

    //Send the request to PayPal and get the response
    StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), 
                             System.Text.Encoding.UTF8);
    streamOut.Write(strRequest);

    streamOut.Close();

    StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
    string strResponse = streamIn.ReadToEnd();
    streamIn.Close();

    /*/ logging ipn messages... be sure that you give write
    // permission to process executing this code
    string logPathDir = ResolveUrl("Messages");
    string logPath = string.Format("{0}\\{1}.txt", 
                     Server.MapPath(logPathDir), DateTime.Now.Ticks);
    File.WriteAllText(logPath, ipnPost);
    /*/

}
@if (strResponse == "VERIFIED")
{
    /*---------------- WILL DO OTHER CHECKS LATER    ------------------*/
    //check the payment_status is Completed
    <p>status is verified</p>
    //check that txn_id has not been previously processed
    //check that receiver_email is your Primary PayPal email
    //check that payment_amount/payment_currency are correct
    //process payment
}
else if (strResponse == "INVALID")
{
    //log for manual investigation
    <p>status is invalid.</p>

<p>@ipnPost</p>
}
else
{
    //log response/ipn data for manual investigation
    <p>status is invalid.</p>
<p>@ipnPost</p>
}

我完全不知道为什么这不能按预期工作;如果有任何帮助,我将不胜感激。

【问题讨论】:

  • 感谢@jason,使用上面的示例(我在其他地方找到了几乎相同的代码,但无法让它太有效,但刚刚遇到这个问题,现在我成功地收到了 IPN 通知所以只是想我会分享我的感谢:)

标签: c# asp.net razor paypal paypal-ipn


【解决方案1】:

我对 Razor 不是很流利,但这是我的 asp.net 网络表单之一。我相信这是来自另一个网站(或者大部分都是),遗憾的是我不记得在哪里。所以这段代码的基础知识不是我的功劳。

string strLive = "https://www.paypal.com/cgi-bin/webscr";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strLive); 
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();

NameValueCollection ppDetails = HttpUtility.ParseQueryString(strRequest);
if (strResponse == "VERIFIED"){
if (ppDetails["payment_status"] == "Completed"){
//yay, give them goodies
} else if (strResponse == "INVALID"){
//log IP and possibly block them from your web services if malicious
} else {
//log response/ipn data for manual investigation
}
}

主要区别似乎是您使用 UTF8,而我拥有的代码使用 ASCII。我还添加了“ppDetails”部分,让您了解如何处理变量。

希望对您有所帮助。我花了很长时间才弄明白 Paypal IPN,而一旦我弄明白了,我就觉得好傻。

【讨论】:

  • 我们都使用相同的代码哈哈。我的最初也是 ASCII - 但被告知将其更改为 UTF8,因为 ASCII 部分正在生成错误“form_charset = UTF8”(这与 ascii 的东西有关) - 但是,在更改为 UTF8 之后 - 它仍然无法正常工作跨度>
  • 感谢您添加用于处理变量的额外部分 - 非常感谢
  • @jason 你试过打印 strResponse 吗?而不是仅仅检查它是否是一组特定的单词
  • 谢谢,@JClaspill - 刚刚尝试过,使用“@strResponse”并输出“INVALID”
  • @jason 确保您使用 paypal 开发人员沙箱站点进行测试。我假设您是,但如果不在这里检查:developer.paypal.com 并将您的 strLive 网址更改为“sandbox.paypal.com/cgi-bin/webscr?”
猜你喜欢
  • 2018-04-09
  • 2018-03-25
  • 2013-10-14
  • 2021-07-23
  • 2017-12-01
  • 2014-03-01
  • 2017-09-11
  • 2013-07-28
  • 2014-12-07
相关资源
最近更新 更多