簡體   English   中英

PayPal IPN集成(快速結帳-Razor / C#)

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

我目前正在嘗試將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>
}

我完全不知道為什么它不能按預期工作。 我將不勝感激。

我不太會說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,一旦這樣做,我就感到很傻。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM