简体   繁体   English

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

[英]PayPal IPN Integration (Express Checkout - Razor/C#)

I am currently trying to integrate PayPal with my site, and everything's gone well - up to the point where we do our IPN checking thing. 我目前正在尝试将PayPal与我的网站集成在一起,并且一切进展顺利-直到我们进行IPN检查为止。

It gets right down to the if/elseif/else block and that's where it ends. 它直接向下到if / elseif / else块,它在那里结束。 It outputs a message, that reads: 它输出一条消息,内容为:

"Invalid status. form_charset=UTF8" “无效状态。form_charset = UTF8”

Here is the code that I have. 这是我的代码。

@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>
}

I'm at a complete loss as to why this isn't working as expected; 我完全不知道为什么它不能按预期工作。 I would appreciate any help at all. 我将不胜感激。

I am not fluent in Razor, but here is how I do my asp.net webforms one. 我不太会说Razor,但是这是我做asp.net Webforms的方法。 I believe this was from another website(or most of it anyway), and I sadly don't remember where. 我相信这是来自另一个网站(或者大部分都是这样),可悲的是我不记得在哪里。 So credit is not mine for the basics of this code. 因此,对于此代码的基础知识并不是我的功劳。

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
}
}

The main difference seems to be that you use UTF8, and the code I have that works uses ASCII. 主要区别似乎是您使用UTF8,而我使用的代码使用ASCII。 I also added the 'ppDetails' part to give you an idea of how to handle the variables. 我还添加了“ ppDetails”部分,以使您了解如何处理变量。

Hope it helps. 希望能帮助到你。 Took me forever to figure out Paypal IPN, and once I did, I felt so silly. 永远让我弄清楚贝宝(Paypal)IPN,一旦这样做,我就感到很傻。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM