c# - PayPal IPN 集成(快速结账 - Razor/C#)

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

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

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

“无效状态。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>
}

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

最佳答案

我对 Razor 不是很流利,但这是我使用 asp.net webforms 的方法。我相信这是来自另一个网站(或者大部分),遗憾的是我不记得在哪里。因此,此代码的基础知识不是我的功劳。

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,一旦我弄清楚了,我就觉得自己很傻。

关于c# - PayPal IPN 集成(快速结账 - Razor/C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7852848/

相关文章:

c# - 使用证书的相互 SSL 的 WCF 身份验证错误

javascript - 将 UTC 日期时间从服务器转换为网页上的本地日期时间

jquery - 更新 jQuery 中的 HiddenFor,然后将新值提交到 Controller

c# - 使用NEST C#在Elastic搜索中删除嵌套文档

c# - DataGrid.Items.Count 不按预期工作

c# - ASP.NET Core 和 ActionFilter

c# - 如何使用具有管理员权限的 rest api 测试/使用 linkedin 发布到公司页面?

asp.net - 如果提交不应推送到存储库的文件夹,.hgignore 文件是否有效?

asp.net - enableEventValidation 和 validateRequest 区别

asp.net-mvc-4 - 模型绑定(bind)器 ValueProvider 附加到现有值 + MVC 4